stax-datadog 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c75f53213c26a5e49fb6a4f284ffa0fd7b3099c4
4
- data.tar.gz: 0e1c05cd75862de9d4e32872b500d0624e251273
2
+ SHA256:
3
+ metadata.gz: 8e3a1010fda206a77ec3f8377d6ecdee06fa549834570220d46e05d9e9da7990
4
+ data.tar.gz: f6f72e74cab7b68554d69d20512185f12672430b6baa771e56b894bad95a4afc
5
5
  SHA512:
6
- metadata.gz: f1188f686cfb465f6b2bf67a242026711e2d4faa903db04cbf261217bdc0ba5170c07566b33e08c0fcb4b504205e3142f89e7cf034fda5048cd60b7654b5483a
7
- data.tar.gz: f33352ec51b8a1c8affec478215e4d75827c404714790e943e3acb6b110a1ee47ab7fa5cac6243c21d7467531fbda9593d7679edd3553ff7a7ad70c61cdfefc1
6
+ metadata.gz: c549c24dc00d3d285b11f1d90a1a1ce38528424a0edf9f7dec4bc630ee64528594640b92186fa5a142a4f45ce3c693a6cefe069c36005d1a743f38fda7e6be0b
7
+ data.tar.gz: 5a6d48da14ce7e21ab670aa57533b4c29c6c74bc7fda56237e0c62ab1a2503e9cf700ad7d86cc6bf9eb7c09358e2b9c5769b9f1f845844d2ed54e53b412a8d30
@@ -1,2 +1,3 @@
1
1
  require 'stax/datadog/api'
2
- require 'stax/datadog/cmd'
2
+ require 'stax/datadog/cmd'
3
+ Stax.add_command(:datadog, Stax::Datadog::Cmd)
@@ -6,9 +6,58 @@ module Stax
6
6
  class Api
7
7
  class << self
8
8
 
9
+ ## override to set these values in a different way, eg from SSM param store
10
+ def datadog_api_key
11
+ ENV['DATADOG_API_KEY']
12
+ end
13
+
14
+ def datadog_app_key
15
+ ENV['DATADOG_APP_KEY']
16
+ end
17
+
9
18
  def client
10
- # ensure_env('DATADOG_API_KEY', 'DATADOG_APP_KEY')
11
- @_client ||= Dogapi::Client.new(ENV['DATADOG_API_KEY'], ENV['DATADOG_APP_KEY'])
19
+ @_client ||= Dogapi::Client.new(datadog_api_key, datadog_app_key)
20
+ rescue RuntimeError => e
21
+ abort("Stax::Datadog: #{e.message}")
22
+ end
23
+
24
+ def lists
25
+ handle_response(client.get_all_dashboard_lists)['dashboard_lists']
26
+ end
27
+
28
+ ## find all lists with name or id
29
+ def find_lists(x)
30
+ lists.select do |l|
31
+ (l['name'] == x) || (l['id'] == x)
32
+ end
33
+ end
34
+
35
+ def list_exists?(name)
36
+ !find_lists(name).empty?
37
+ end
38
+
39
+ ## create list, unless there is already a list with this name
40
+ def create_or_get_list(name)
41
+ list = find_lists(name)&.first || handle_response(client.create_dashboard_list(name))
42
+ get_list(list['id'])
43
+ end
44
+
45
+ def get_list(id)
46
+ handle_response(client.get_dashboard_list(id))
47
+ end
48
+
49
+ def get_list_items(id)
50
+ handle_response(client.get_items_of_dashboard_list(id))
51
+ end
52
+
53
+ def add_to_list(id, dashboards)
54
+ resp = client.add_items_to_dashboard_list(id, Array(dashboards))
55
+ handle_response(resp)
56
+ end
57
+
58
+ def delete_from_list(id, dashboards)
59
+ resp = client.delete_items_from_dashboard_list(id, Array(dashboards))
60
+ handle_response(resp)
12
61
  end
13
62
 
14
63
  def dashboards
@@ -27,8 +76,11 @@ module Stax
27
76
  end
28
77
 
29
78
  def handle_response(resp)
30
- unless resp[0].match(/^2\d\d$/)
79
+ if resp[0].match(/^2\d\d$/)
80
+ resp[1]
81
+ else
31
82
  warn(resp[1].fetch('errors', 'datadog error'))
83
+ nil
32
84
  end
33
85
  end
34
86
 
@@ -36,7 +88,7 @@ module Stax
36
88
  if dashboard_exists?(args[0])
37
89
  warn("Dashboard #{args[0]} already exists")
38
90
  else
39
- handle_response(client.create_dashboard(*args))
91
+ handle_response(client.create_dashboard(*args)).dig('dash', 'id')
40
92
  end
41
93
  end
42
94
 
@@ -61,4 +113,4 @@ module Stax
61
113
  end
62
114
 
63
115
  end
64
- end
116
+ end
@@ -3,6 +3,11 @@ module Stax
3
3
  class Cmd < Base
4
4
 
5
5
  no_commands do
6
+ ## if this is defined, dashboard will be added to this list
7
+ def list_name
8
+ app_name
9
+ end
10
+
6
11
  def dashboard_name
7
12
  app_name + '-' + branch_name
8
13
  end
@@ -20,18 +25,34 @@ module Stax
20
25
  end
21
26
  end
22
27
 
28
+ desc 'list', 'dashboard list'
29
+ def list
30
+ Datadog::Api.find_lists(list_name).each do |list|
31
+ debug("Datadog list #{list['name']} (#{list['id']})")
32
+ print_table Datadog::Api.get_list_items(list['id']).fetch('dashboards', []).map { |d|
33
+ [d['title'], d['id'], d['modified']]
34
+ }
35
+ end
36
+ end
37
+
23
38
  desc 'create', 'create dashboard'
24
39
  def create
25
- Datadog::Api.create_dashboard(
40
+ debug("Creating datadog dashboard #{dashboard_name}")
41
+ id = Datadog::Api.create_dashboard(
26
42
  dashboard_name,
27
43
  dashboard_description,
28
44
  graph_definitions,
29
45
  template_variables,
30
46
  )
47
+
48
+ ## add it to list (create as needed)
49
+ list = Datadog::Api.create_or_get_list(list_name)
50
+ Datadog::Api.add_to_list(list['id'], [{type: :custom_timeboard, id: id}])
31
51
  end
32
52
 
33
53
  desc 'update', 'update dashboard'
34
54
  def update
55
+ debug("Updating datadog dashboard #{dashboard_name}")
35
56
  Datadog::Api.update_dashboard(
36
57
  dashboard_name,
37
58
  dashboard_description,
@@ -43,10 +64,11 @@ module Stax
43
64
  desc 'delete [TITLE/ID]', 'delete timeboards'
44
65
  def delete(title_or_id = nil)
45
66
  if yes?("Delete dashboards with name #{dashboard_name}?", :yellow)
67
+ debug("Deleting datadog dashboard #{dashboard_name}")
46
68
  Datadog::Api.delete_dashboards(dashboard_name)
47
69
  end
48
70
  end
49
71
 
50
72
  end
51
73
  end
52
- end
74
+ end
@@ -1,5 +1,5 @@
1
1
  module Stax
2
2
  module Datadog
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
- end
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stax-datadog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Lister
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-22 00:00:00.000000000 Z
11
+ date: 2020-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -93,7 +93,7 @@ homepage: https://github.com/rlister/stax-datadog
93
93
  licenses:
94
94
  - MIT
95
95
  metadata: {}
96
- post_install_message:
96
+ post_install_message:
97
97
  rdoc_options: []
98
98
  require_paths:
99
99
  - lib
@@ -108,9 +108,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  requirements: []
111
- rubyforge_project:
112
- rubygems_version: 2.6.13
113
- signing_key:
111
+ rubygems_version: 3.0.3
112
+ signing_key:
114
113
  specification_version: 4
115
114
  summary: Control datadog dashboards with stax.
116
115
  test_files: []