wavefront-cli 2.18.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +7 -0
  4. data/.travis.yml +4 -5
  5. data/HISTORY.md +20 -1
  6. data/README.md +79 -29
  7. data/lib/wavefront-cli/base.rb +26 -2
  8. data/lib/wavefront-cli/commands/alert.rb +10 -10
  9. data/lib/wavefront-cli/commands/base.rb +15 -2
  10. data/lib/wavefront-cli/commands/cloudintegration.rb +3 -3
  11. data/lib/wavefront-cli/commands/config.rb +2 -1
  12. data/lib/wavefront-cli/commands/dashboard.rb +8 -6
  13. data/lib/wavefront-cli/commands/derivedmetric.rb +5 -5
  14. data/lib/wavefront-cli/commands/event.rb +3 -3
  15. data/lib/wavefront-cli/commands/integration.rb +5 -5
  16. data/lib/wavefront-cli/commands/link.rb +3 -3
  17. data/lib/wavefront-cli/commands/message.rb +2 -2
  18. data/lib/wavefront-cli/commands/metric.rb +1 -1
  19. data/lib/wavefront-cli/commands/notificant.rb +3 -3
  20. data/lib/wavefront-cli/commands/proxy.rb +3 -3
  21. data/lib/wavefront-cli/commands/query.rb +3 -3
  22. data/lib/wavefront-cli/commands/savedsearch.rb +3 -3
  23. data/lib/wavefront-cli/commands/settings.rb +22 -0
  24. data/lib/wavefront-cli/commands/source.rb +3 -3
  25. data/lib/wavefront-cli/commands/user.rb +4 -4
  26. data/lib/wavefront-cli/commands/usergroup.rb +5 -8
  27. data/lib/wavefront-cli/commands/webhook.rb +3 -3
  28. data/lib/wavefront-cli/commands/window.rb +3 -3
  29. data/lib/wavefront-cli/commands/write.rb +6 -4
  30. data/lib/wavefront-cli/config.rb +14 -0
  31. data/lib/wavefront-cli/controller.rb +2 -0
  32. data/lib/wavefront-cli/dashboard.rb +133 -14
  33. data/lib/wavefront-cli/display/base.rb +28 -7
  34. data/lib/wavefront-cli/display/dashboard.rb +32 -8
  35. data/lib/wavefront-cli/display/distribution.rb +4 -1
  36. data/lib/wavefront-cli/display/printer/long.rb +149 -140
  37. data/lib/wavefront-cli/display/printer/terse.rb +19 -42
  38. data/lib/wavefront-cli/display/query.rb +6 -0
  39. data/lib/wavefront-cli/display/settings.rb +21 -0
  40. data/lib/wavefront-cli/display/write.rb +12 -5
  41. data/lib/wavefront-cli/event.rb +1 -1
  42. data/lib/wavefront-cli/exception.rb +1 -0
  43. data/lib/wavefront-cli/settings.rb +37 -0
  44. data/lib/wavefront-cli/stdlib/array.rb +20 -0
  45. data/lib/wavefront-cli/stdlib/string.rb +8 -0
  46. data/lib/wavefront-cli/user.rb +1 -1
  47. data/lib/wavefront-cli/version.rb +1 -1
  48. data/lib/wavefront-cli/write.rb +310 -10
  49. data/spec/.rubocop.yml +3 -0
  50. data/spec/spec_helper.rb +81 -1
  51. data/spec/wavefront-cli/alert_spec.rb +1 -0
  52. data/spec/wavefront-cli/cloudintegration_spec.rb +1 -0
  53. data/spec/wavefront-cli/dashboard_spec.rb +47 -4
  54. data/spec/wavefront-cli/derivedmetric_spec.rb +1 -0
  55. data/spec/wavefront-cli/display/base_spec.rb +16 -9
  56. data/spec/wavefront-cli/display/printer/long_spec.rb +75 -106
  57. data/spec/wavefront-cli/display/printer/terse_spec.rb +33 -21
  58. data/spec/wavefront-cli/event_spec.rb +1 -0
  59. data/spec/wavefront-cli/externallink_spec.rb +1 -0
  60. data/spec/wavefront-cli/integration_spec.rb +1 -0
  61. data/spec/wavefront-cli/maintenancewindow_spec.rb +1 -0
  62. data/spec/wavefront-cli/proxy_spec.rb +2 -0
  63. data/spec/wavefront-cli/query_spec.rb +9 -0
  64. data/spec/wavefront-cli/resources/display/alert-human-long +24 -0
  65. data/spec/wavefront-cli/resources/display/alert-input.json +37 -0
  66. data/spec/wavefront-cli/resources/display/alerts-human-terse +9 -0
  67. data/spec/wavefront-cli/resources/display/alerts-input.json +1 -0
  68. data/spec/wavefront-cli/resources/display/user-human-long +19 -0
  69. data/spec/wavefront-cli/resources/display/user-human-long-no_sep +18 -0
  70. data/spec/wavefront-cli/resources/display/user-input.json +1 -0
  71. data/spec/wavefront-cli/resources/responses/README.md +2 -0
  72. data/spec/wavefront-cli/resources/responses/alert-list.json +1 -0
  73. data/spec/wavefront-cli/resources/responses/backups.json +1 -0
  74. data/spec/wavefront-cli/resources/responses/cloudintegration-list.json +1 -0
  75. data/spec/wavefront-cli/resources/responses/dashboard-list.json +1 -0
  76. data/spec/wavefront-cli/resources/responses/derivedmetric-list.json +1 -0
  77. data/spec/wavefront-cli/resources/responses/event-list.json +1 -0
  78. data/spec/wavefront-cli/resources/responses/integration-list.json +1 -0
  79. data/spec/wavefront-cli/resources/responses/link-list.json +1 -0
  80. data/spec/wavefront-cli/resources/responses/notificant-list.json +1 -0
  81. data/spec/wavefront-cli/resources/responses/proxy-list.json +1 -0
  82. data/spec/wavefront-cli/resources/responses/query-cpu.json +1 -0
  83. data/spec/wavefront-cli/resources/responses/savedsearch-list.json +1 -0
  84. data/spec/wavefront-cli/resources/responses/user-list.json +1 -0
  85. data/spec/wavefront-cli/resources/responses/usergroup-list.json +1 -0
  86. data/spec/wavefront-cli/resources/responses/webhook-list.json +1 -0
  87. data/spec/wavefront-cli/resources/responses/window-list.json +1 -0
  88. data/spec/wavefront-cli/savedsearch_spec.rb +1 -0
  89. data/spec/wavefront-cli/settings_spec.rb +19 -0
  90. data/spec/wavefront-cli/stdlib/array_spec.rb +20 -0
  91. data/spec/wavefront-cli/stdlib/string_spec.rb +13 -1
  92. data/spec/wavefront-cli/user_spec.rb +1 -0
  93. data/spec/wavefront-cli/usergroup_spec.rb +1 -0
  94. data/spec/wavefront-cli/webhook_spec.rb +1 -0
  95. data/spec/wavefront-cli/write_spec.rb +167 -0
  96. data/wavefront-cli.gemspec +3 -4
  97. metadata +65 -31
  98. data/.gitlab-ci.yml +0 -16
  99. data/lib/wavefront-cli/base_write.rb +0 -298
  100. data/lib/wavefront-cli/commands/report.rb +0 -37
  101. data/lib/wavefront-cli/display/printer/base.rb +0 -24
  102. data/lib/wavefront-cli/display/report.rb +0 -17
  103. data/lib/wavefront-cli/report.rb +0 -18
  104. data/spec/wavefront-cli/display/printer/base_spec.rb +0 -20
@@ -8,15 +8,15 @@ class WavefrontCommandEvent < WavefrontCommandBase
8
8
  end
9
9
 
10
10
  def _commands
11
- ["list #{CMN} [-l] [-O fields] [-f format] [-s start] [-e end] " \
11
+ ["list #{CMN} [-l] [-O fields] [-s start] [-e end] " \
12
12
  '[-L limit] [-o cursor]',
13
- "describe #{CMN} [-f format] <id>",
13
+ "describe #{CMN} <id>",
14
14
  "create #{CMN} [-d description] [-s time] [-i | -e time] " \
15
15
  '[-S severity] [-T type] [-H host...] [-g tag...] [-N] <event>',
16
16
  "close #{CMN} [<id>]",
17
17
  "delete #{CMN} <id>",
18
18
  "update #{CMN} <key=value> <id>",
19
- "search #{CMN} [-f format] [-o offset] [-L limit] [-l] <condition>...",
19
+ "search #{CMN} [-o offset] [-L limit] [-l] <condition>...",
20
20
  "wrap #{CMN} [-C command] [-d description] [-S severity] [-T type] " \
21
21
  '[-H host...] [-g tag...] <event>',
22
22
  tag_commands,
@@ -8,8 +8,8 @@ class WavefrontCommandIntegration < WavefrontCommandBase
8
8
  end
9
9
 
10
10
  def _commands
11
- ["list #{CMN} [-al] [-O fields] [-f format] [-o offset] [-L limit]",
12
- "describe #{CMN} [-f format] <id>",
11
+ ["list #{CMN} [-al] [-O fields] [-o offset] [-L limit]",
12
+ "describe #{CMN} <id>",
13
13
  "install #{CMN} <id>",
14
14
  "uninstall #{CMN} <id>",
15
15
  "manifests #{CMN}",
@@ -17,9 +17,9 @@ class WavefrontCommandIntegration < WavefrontCommandBase
17
17
  "statuses #{CMN}",
18
18
  "alert install #{CMN} <id>",
19
19
  "alert uninstall #{CMN} <id>",
20
- "installed #{CMN} [-f format]",
21
- "manifests #{CMN} [-f format]",
22
- "search #{CMN} [-al] [-f format] [-o offset] [-L limit] <condition>..."]
20
+ "installed #{CMN}",
21
+ "manifests #{CMN}",
22
+ "search #{CMN} [-al] [-o offset] [-L limit] <condition>..."]
23
23
  end
24
24
 
25
25
  def _options
@@ -16,14 +16,14 @@ class WavefrontCommandLink < WavefrontCommandBase
16
16
  end
17
17
 
18
18
  def _commands
19
- ["list #{CMN} [-al] [-O fields] [-f format] [-o offset] [-L limit]",
20
- "describe #{CMN} [-f format] <id>",
19
+ ["list #{CMN} [-al] [-O fields] [-o offset] [-L limit]",
20
+ "describe #{CMN} <id>",
21
21
  "create #{CMN} [-m regex] [-s regex] [-p str=regex...] <name> " \
22
22
  '<description> <template>',
23
23
  "delete #{CMN} <id>",
24
24
  "import #{CMN} <file>",
25
25
  "update #{CMN} <key=value> <id>",
26
- "search #{CMN} [-al] [-f format] [-o offset] [-L limit] <condition>..."]
26
+ "search #{CMN} [-al] [-o offset] [-L limit] <condition>..."]
27
27
  end
28
28
 
29
29
  def _options
@@ -8,8 +8,8 @@ class WavefrontCommandMessage < WavefrontCommandBase
8
8
  end
9
9
 
10
10
  def _commands
11
- ["list #{CMN} [-al] [-O fields] [-f format] [-o offset] [-L limit]",
12
- "mark #{CMN} [-f format] <id>"]
11
+ ["list #{CMN} [-al] [-O fields] [-o offset] [-L limit]",
12
+ "mark #{CMN} <id>"]
13
13
  end
14
14
 
15
15
  def _options
@@ -8,7 +8,7 @@ class WavefrontCommandMetric < WavefrontCommandBase
8
8
  end
9
9
 
10
10
  def _commands
11
- ["describe #{CMN} [-f format] [-o offset] [-g glob...] <metric>"]
11
+ ["describe #{CMN} [-o offset] [-g glob...] <metric>"]
12
12
  end
13
13
 
14
14
  def _options
@@ -8,13 +8,13 @@ class WavefrontCommandNotificant < WavefrontCommandBase
8
8
  end
9
9
 
10
10
  def _commands
11
- ["list #{CMN} [-al] [-O fields] [-f format] [-o offset] [-L limit]",
12
- "describe #{CMN} [-f format] <id>",
11
+ ["list #{CMN} [-al] [-O fields] [-o offset] [-L limit]",
12
+ "describe #{CMN} <id>",
13
13
  "import #{CMN} <file>",
14
14
  "delete #{CMN} <id>",
15
15
  "test #{CMN} <id>",
16
16
  "update #{CMN} <key=value> <id>",
17
- "search #{CMN} [-al] [-f format] [-o offset] [-L limit] <condition>..."]
17
+ "search #{CMN} [-al] [-o offset] [-L limit] <condition>..."]
18
18
  end
19
19
 
20
20
  def _options
@@ -8,12 +8,12 @@ class WavefrontCommandProxy < WavefrontCommandBase
8
8
  end
9
9
 
10
10
  def _commands
11
- ["list #{CMN} [-al] [-O fields] [-f format] [-o offset] [-L limit]",
12
- "describe #{CMN} [-f format] <id>",
11
+ ["list #{CMN} [-al] [-O fields] [-o offset] [-L limit]",
12
+ "describe #{CMN} <id>",
13
13
  "delete #{CMN} <id>",
14
14
  "undelete #{CMN} <id>",
15
15
  "rename #{CMN} <id> <name>",
16
- "search #{CMN} [-al] [-f format] [-o offset] [-L limit] <condition>...",
16
+ "search #{CMN} [-al] [-o offset] [-L limit] <condition>...",
17
17
  "versions #{CMN}"]
18
18
  end
19
19
 
@@ -9,11 +9,11 @@ class WavefrontCommandQuery < WavefrontCommandBase
9
9
 
10
10
  def _commands
11
11
  ['aliases [-DV] [-c file] [-P profile]',
12
- "#{CMN} [-g granularity] [-s time] [-e time] [-f format] " \
12
+ "#{CMN} [-g granularity] [-s time] [-e time] " \
13
13
  '[-WikvO] [-S mode] [-N name] [-p points] [-F options] <query>',
14
- "raw #{CMN} [-H host] [-s time] [-e time] [-f format] " \
14
+ "raw #{CMN} [-H host] [-s time] [-e time] " \
15
15
  '[-F options] <metric>',
16
- "run #{CMN} [-g granularity] [-s time] [-e time] [-f format] " \
16
+ "run #{CMN} [-g granularity] [-s time] [-e time] " \
17
17
  '[-F options] [-WkivO] [-S mode] [-N name] [-p points] <alias>']
18
18
  end
19
19
 
@@ -16,11 +16,11 @@ class WavefrontCommandSavedsearch < WavefrontCommandBase
16
16
  end
17
17
 
18
18
  def _commands
19
- ["list #{CMN} [-al] [-O fields] [-f format] [-o offset] [-L limit]",
20
- "describe #{CMN} [-f format] <id>",
19
+ ["list #{CMN} [-al] [-O fields] [-o offset] [-L limit]",
20
+ "describe #{CMN} <id>",
21
21
  "delete #{CMN} <id>",
22
22
  "import #{CMN} <file>",
23
- "search #{CMN} [-al] [-f format] [-o offset] [-L limit] <condition>..."]
23
+ "search #{CMN} [-al] [-o offset] [-L limit] <condition>..."]
24
24
  end
25
25
 
26
26
  def _options
@@ -0,0 +1,22 @@
1
+ require_relative 'base'
2
+
3
+ # Define the settings command.
4
+ #
5
+ class WavefrontCommandSettings < WavefrontCommandBase
6
+ def description
7
+ 'view and manage system preferences'
8
+ end
9
+
10
+ def _commands
11
+ ["list permissions #{CMN} [-l] [-O fields]",
12
+ "show preferences #{CMN} [-l] [-O fields]",
13
+ "update #{CMN} <key=value>...",
14
+ "default usergroups #{CMN} [-l] [-O fields]"]
15
+ end
16
+
17
+ def _options
18
+ [common_options,
19
+ '-l, --long list derived metrics in detail',
20
+ '-O, --fields=F1,F2,... only show given fields']
21
+ end
22
+ end
@@ -8,12 +8,12 @@ class WavefrontCommandSource < WavefrontCommandBase
8
8
  end
9
9
 
10
10
  def _commands
11
- ["list #{CMN} [-l] [-O fields] [-f format] [-o cursor] [-L limit] [-a]",
12
- "describe #{CMN} [-f format] <id>",
11
+ ["list #{CMN} [-l] [-O fields] [-o cursor] [-L limit] [-a]",
12
+ "describe #{CMN} <id>",
13
13
  "description set #{CMN} <id> <description>",
14
14
  "description clear #{CMN} <id>",
15
15
  "clear #{CMN} <id>",
16
- "search #{CMN} [-f format] [-o offset] [-L limit] [-l] <condition>...",
16
+ "search #{CMN} [-o offset] [-L limit] [-l] <condition>...",
17
17
  tag_commands]
18
18
  end
19
19
 
@@ -13,9 +13,9 @@ class WavefrontCommandUser < WavefrontCommandBase
13
13
  #
14
14
  def _commands
15
15
  ["list #{CMN} [-l] [-O fields]",
16
- "describe #{CMN} [-f format] <id>",
17
- "create #{CMN} [-e] [-m permission...] [-g group...] [-f format] <id>",
18
- "invite #{CMN} [-m permission...] [-g group...] [-f format] <id>",
16
+ "describe #{CMN} <id>",
17
+ "create #{CMN} [-e] [-m permission...] [-g group...] <id>",
18
+ "invite #{CMN} [-m permission...] [-g group...] <id>",
19
19
  "update #{CMN} <key=value> <id>",
20
20
  "delete #{CMN} <user>...",
21
21
  "import #{CMN} <file>",
@@ -24,7 +24,7 @@ class WavefrontCommandUser < WavefrontCommandBase
24
24
  "leave #{CMN} <id> <group>...",
25
25
  "grant #{CMN} <privilege> to <id>",
26
26
  "revoke #{CMN} <privilege> from <id>",
27
- "search #{CMN} [-al] [-f format] [-o offset] [-L limit] <condition>..."]
27
+ "search #{CMN} [-al] [-o offset] [-L limit] <condition>..."]
28
28
  end
29
29
 
30
30
  def _options
@@ -16,8 +16,8 @@ class WavefrontCommandUsergroup < WavefrontCommandBase
16
16
  end
17
17
 
18
18
  def _commands
19
- ["list #{CMN} [-al] [-O fields] [-f format] [-o offset] [-L limit]",
20
- "describe #{CMN} [-f format] <id>",
19
+ ["list #{CMN} [-al] [-O fields] [-o offset] [-L limit]",
20
+ "describe #{CMN} <id>",
21
21
  "create #{CMN} [-p permission...] <name>",
22
22
  "delete #{CMN} <id>",
23
23
  "import #{CMN} <file>",
@@ -28,7 +28,7 @@ class WavefrontCommandUsergroup < WavefrontCommandBase
28
28
  "remove user #{CMN} <id> <user>...",
29
29
  "grant #{CMN} <permission> to <id>",
30
30
  "revoke #{CMN} <permission> from <id>",
31
- "search #{CMN} [-al] [-f format] [-o offset] [-L limit] <condition>..."]
31
+ "search #{CMN} [-al] [-o offset] [-L limit] <condition>..."]
32
32
  end
33
33
 
34
34
  def _options
@@ -43,10 +43,7 @@ class WavefrontCommandUsergroup < WavefrontCommandBase
43
43
  end
44
44
 
45
45
  def postscript
46
- 'Valid permissions at the time of writing are: agent_management, ' \
47
- 'alerts_management, dashboard_management, embedded_charts, ' \
48
- 'events_management, external_links_management, host_tag_management, ' \
49
- 'metrics_management, and user_management. Check the API docs for ' \
50
- 'an up-to-date list.'.fold(TW, 0)
46
+ "'wf settings list permissions' will give you a list of all " \
47
+ 'currently supported permissions.'.fold(TW, 0)
51
48
  end
52
49
  end
@@ -8,12 +8,12 @@ class WavefrontCommandWebhook < WavefrontCommandBase
8
8
  end
9
9
 
10
10
  def _commands
11
- ["list #{CMN} [-al] [-O fields] [-f format] [-o offset] [-L limit]",
12
- "describe #{CMN} [-f format] <id>",
11
+ ["list #{CMN} [-al] [-O fields] [-o offset] [-L limit]",
12
+ "describe #{CMN} <id>",
13
13
  "delete #{CMN} <id>",
14
14
  "import #{CMN} <file>",
15
15
  "update #{CMN} <key=value> <id>",
16
- "search #{CMN} [-al] [-f format] [-o offset] [-L limit] <condition>..."]
16
+ "search #{CMN} [-al] [-o offset] [-L limit] <condition>..."]
17
17
  end
18
18
 
19
19
  def _options
@@ -16,8 +16,8 @@ class WavefrontCommandWindow < WavefrontCommandBase
16
16
  end
17
17
 
18
18
  def _commands
19
- ["list #{CMN} [-al] [-O fields] [-f format] [-o offset] [-L limit]",
20
- "describe #{CMN} [-f format] <id>",
19
+ ["list #{CMN} [-al] [-O fields] [-o offset] [-L limit]",
20
+ "describe #{CMN} <id>",
21
21
  "create #{CMN} -d reason [-s time] [-e time] " \
22
22
  '[-A alert_tag...] [-T host_tag...] [-H host...] <title>',
23
23
  "close #{CMN} <id>",
@@ -25,7 +25,7 @@ class WavefrontCommandWindow < WavefrontCommandBase
25
25
  "delete #{CMN} <id>",
26
26
  "import #{CMN} <file>",
27
27
  "update #{CMN} <key=value> <id>",
28
- "search #{CMN} [-al] [-f format] [-o offset] [-L limit] <condition>...",
28
+ "search #{CMN} [-al] [-o offset] [-L limit] <condition>...",
29
29
  "ongoing #{CMN}",
30
30
  "pending #{CMN} [<hours>]"]
31
31
  end
@@ -10,10 +10,10 @@ class WavefrontCommandWrite < WavefrontCommandBase
10
10
  def _commands
11
11
  ['point [-DnViq] [-c file] [-P profile] [-E proxy] [-t time] ' \
12
12
  '[-p port] [-H host] [-T tag...] [-u method] [-S socket] <metric> ' \
13
- '<value>',
13
+ '[--] <value>',
14
14
  'distribution [-DnViq] [-c file] [-P profile] [-E proxy] [-H host] ' \
15
15
  '[-p port] [-T tag...] [-u method] [-S socket] [-I interval] ' \
16
- '<metric> <val>...',
16
+ '<metric> [--] <val>...',
17
17
  'file [-DnViq] [-c file] [-P profile] [-E proxy] [-H host] ' \
18
18
  '[-p port] [-F infileformat] [-m metric] [-T tag...] [-I interval] ' \
19
19
  '[-u method] [-S socket] <file>']
@@ -43,7 +43,9 @@ class WavefrontCommandWrite < WavefrontCommandBase
43
43
  "with the '-F' option. Use 't' for timestamp, 'm' for metric " \
44
44
  "name, 'v' for value, 's' for source, 'd' for a comma-separated " \
45
45
  "distribution, and 'T' for tags. Put 'T' last. Currently " \
46
- "supported transport methods are 'socket' (the default) " \
47
- "and 'http'.".cmd_fold(TW, 0)
46
+ "supported transport methods are 'socket' (write to a proxy: the " \
47
+ "default); 'api' (write directly to Wavefront); 'http' (write to " \
48
+ "a proxy over HTTP); and 'unix' (write to a local Unix socket)."
49
+ .cmd_fold(TW, 0)
48
50
  end
49
51
  end
@@ -53,6 +53,20 @@ module WavefrontCli
53
53
  puts IO.read(config_file)
54
54
  end
55
55
 
56
+ def do_about
57
+ require 'wavefront-sdk/defs/version'
58
+ require_relative 'display/base'
59
+
60
+ info = { 'wf version': WF_CLI_VERSION,
61
+ 'wf path': CMD.realpath.to_s,
62
+ 'SDK version': WF_SDK_VERSION,
63
+ 'SDK location': WF_SDK_LOCATION.to_s,
64
+ 'Ruby version': RUBY_VERSION,
65
+ 'Ruby platform': Gem::Platform.local.os.capitalize }
66
+
67
+ WavefrontDisplay::Base.new(info).long_output
68
+ end
69
+
56
70
  def base_config
57
71
  return read_config if config_file.exist?
58
72
 
@@ -136,6 +136,8 @@ class WavefrontCliController
136
136
  abort e.message
137
137
  rescue WavefrontCli::Exception::UnsupportedNoop
138
138
  abort 'Multiple API call operations cannot be performed as no-ops.'
139
+ rescue WavefrontCli::Exception::UserGroupNotFound => e
140
+ abort "Cannot find user group '#{e.message}'."
139
141
  rescue Wavefront::Exception::UnsupportedWriter => e
140
142
  abort "Unsupported writer '#{e.message}'."
141
143
  rescue StandardError => e
@@ -43,33 +43,152 @@ module WavefrontCli
43
43
  resp.tap { |r| r.response.items = queries }
44
44
  end
45
45
 
46
+ def do_favs
47
+ require 'wavefront-sdk/search'
48
+ wfs = Wavefront::Search.new(mk_creds, mk_opts)
49
+ query = conds_to_query(['favorite=true'])
50
+ wfs.search(:dashboard, query, limit: :all, sort_field: :id)
51
+ end
52
+
46
53
  def do_fav
47
54
  wf.favorite(options[:'<id>'])
55
+ do_favs
48
56
  end
49
57
 
50
58
  def do_unfav
51
59
  wf.unfavorite(options[:'<id>'])
60
+ do_favs
61
+ end
62
+
63
+ def do_acls
64
+ wf.acls([options[:'<id>']])
65
+ end
66
+
67
+ def do_acl_clear
68
+ wf.acl_set(options[:'<id>'], [], [id: everyone_id, name: 'Everyone'])
69
+ do_acls
70
+ end
71
+
72
+ def do_acl_grant
73
+ acl_action(:grant_to)
74
+ end
75
+
76
+ def do_acl_revoke
77
+ acl_action(:revoke_from)
78
+ end
79
+
80
+ # Based on command-line options, return an array describing the
81
+ # users or groups (entities) which will be granted or revoked a
82
+ # privilege.
83
+ # @return [Array] [type_of_entity, [Hash]...]
84
+ #
85
+ def acl_entities
86
+ acl_type = options[:modify] ? :modify : :view
87
+
88
+ if options[:user]
89
+ [:users, user_lists(acl_type, options[:'<name>'])]
90
+ else
91
+ [:groups, group_lists(acl_type, options[:'<name>'])]
92
+ end
52
93
  end
53
94
 
54
- # @param obj [Object] the thing to search
55
- # @param key [String, Symbol] the key to search for
56
- # @param aggr [Array] values of matched keys
57
- # @return [Array]
95
+ # Make a list of users to be given to the SDK ACL methods. Users
96
+ # are defined as a Hash, with keys :id and :name.
97
+ # @param acl_type [Symbol] :view or :modify
98
+ # @param users [Array] user names
99
+ # @return [Array[Hash]]
58
100
  #
59
- def extract_values(obj, key, aggr = [])
60
- if obj.is_a?(Hash)
61
- obj.each_pair do |k, v|
62
- if k == key && !v.to_s.empty?
63
- aggr.<< v
64
- else
65
- extract_values(v, key, aggr)
101
+ def user_lists(acl_type, users)
102
+ { view: [], modify: [] }.tap do |l|
103
+ l[acl_type] = users.map { |u| { id: u, name: u } }
104
+ end
105
+ end
106
+
107
+ # Generate arrays ready for passing to the SDK acl methods
108
+ # @return see #user_lists, but name and id are not the same.
109
+ #
110
+ def group_lists(acl_type, groups)
111
+ { view: [], modify: [] }.tap do |l|
112
+ l[acl_type] = groups.each_with_object([]) do |g, a|
113
+ name = group_name(g)
114
+
115
+ if name.nil?
116
+ puts "Cannot find group with id '#{g}'."
117
+ next
66
118
  end
119
+
120
+ a.<< ({ id: g, name: name })
67
121
  end
68
- elsif obj.is_a?(Array)
69
- obj.each { |e| extract_values(e, key, aggr) }
70
122
  end
123
+ end
71
124
 
72
- aggr
125
+ private
126
+
127
+ # When given an ACL action (grant or revoke), call the right
128
+ # method with the right arguments.
129
+ # @param action [Symbol] :grant_to or :revoke_from
130
+ # @return [Wavefront::Response]
131
+ #
132
+ def acl_action(action)
133
+ entity_type, entities = acl_entities
134
+
135
+ resp = send(format('%s_%s', action, entity_type),
136
+ options[:'<id>'],
137
+ entities)
138
+
139
+ print_status(resp.status)
140
+ do_acls
141
+ end
142
+
143
+ # The #grant_to_ and #revoke_from_ methods are called by
144
+ # #acl_action, and speak to the SDK. They all return a
145
+ # Wavefront::Response object.
146
+ #
147
+ def grant_to_users(id, lists)
148
+ wf.acl_add(id, lists[:view], lists[:modify])
149
+ end
150
+
151
+ def revoke_from_users(id, lists)
152
+ wf.acl_delete(id, lists[:view], lists[:modify])
153
+ end
154
+
155
+ def grant_to_groups(id, lists)
156
+ wf.acl_add(id, lists[:view], lists[:modify])
157
+ end
158
+
159
+ def revoke_from_groups(id, lists)
160
+ wf.acl_delete(id, lists[:view], lists[:modify])
161
+ end
162
+
163
+ def print_status(status)
164
+ puts status.message unless status.message.empty?
165
+ rescue NoMethodError
166
+ nil
167
+ end
168
+
169
+ # Get the name of a user group, given the ID.
170
+ # @param group_id [String] UUID of a group
171
+ # @return [String, Nil] name of group, nil if it does not exist
172
+ #
173
+ def group_name(group_id)
174
+ require 'wavefront-sdk/usergroup'
175
+ wfs = Wavefront::UserGroup.new(mk_creds, mk_opts)
176
+ wfs.describe(group_id).response.name
177
+ rescue RuntimeError
178
+ nil
179
+ end
180
+
181
+ # @return [String] UUID of 'Everyone' group
182
+ # @raise WavefrontCli::Exception::UserGroupNotFound if group
183
+ # does not exist. This is caught in the controller.
184
+ #
185
+ def everyone_id
186
+ require 'wavefront-sdk/search'
187
+ wfs = Wavefront::Search.new(mk_creds, mk_opts)
188
+ query = conds_to_query(['name=Everyone'])
189
+ wfs.search(:usergroup, query).response.items.first.id
190
+ rescue RuntimeError
191
+ raise WavefrontCli::Exception::UserGroupNotFound, 'Everyone'
73
192
  end
74
193
  end
75
194
  end