-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen-datadog-dashboard
executable file
·89 lines (77 loc) · 2.03 KB
/
open-datadog-dashboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env ruby
require 'json'
require 'net/http'
require 'optparse'
def domain
if @custom_domain.nil? || @custom_domain.empty?
"app.datadoghq.com"
else
@custom_domain
end
end
def alfred_error(title, subtitle)
alfred_results = [{
"title" => title,
"subtitle" => subtitle,
"valid" => false
}]
alfred_results = { "items" => alfred_results }.to_json
end
def datadog_request(path)
uri = URI("https://#{domain}/#{path}?api_key=#{@api_key}&application_key=#{@app_key}")
response = nil
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |https|
request = Net::HTTP::Get.new(uri)
response = https.request(request)
end
case response.code.to_i
when 200..399
JSON.parse(response.body)
else
raise "DD request failed (#{response.code}: #{response.body})"
end
end
def request_dashes
datadog_request("api/v1/dash")["dashes"]
end
def request_screenboards
datadog_request("api/v1/screen")["screenboards"]
end
def get_dashes_as_alfred_results
data = request_dashes + request_screenboards
data.map do |dash|
dash_id = dash['resource'].sub('/api/v1/', '')
{
"uid" => dash_id,
"title" => dash['title'],
"subtitle" => "#{dash['description']}",
"arg" => "https://#{domain}/#{dash_id}",
"autocomplete" => dash['title']
}
end
rescue StandardError => e
puts alfred_error("Failed to fetch dashes", e.to_s)
exit 1
end
def validate_required_ivar(varname)
value = instance_variable_get("@#{varname}")
if !value || value.empty?
puts alfred_error("No #{varname.tr('_-', ' ')} provided", "Please run :auth-dd to fix this")
exit 1
end
end
OptionParser.new do |opts|
opts.on("--custom_domain=DOMAIN") do |domain|
@custom_domain = domain
end
opts.on("--app_key=APP_KEY") do |app_key|
@app_key = app_key
end
opts.on("--api_key=API_KEY") do |api_key|
@api_key = api_key
end
end.parse!
validate_required_ivar("app_key")
validate_required_ivar("api_key")
alfred_results = { "items" => get_dashes_as_alfred_results }.to_json
puts alfred_results