-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdatadog-users-cli
executable file
·104 lines (82 loc) · 2.76 KB
/
datadog-users-cli
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env ruby
#
# Datadog Users CLI
# Keiran Sweet <Keiran.Sweet@sourcedgroup.com>
#
# This tool provides a way to create and enforce the configuration of a particular set of
# Datadog team users from the command line.
#
# It functions by defining a set of users and their level of access (role) in the configuration
# file under users: and then running the datadog-users-cli with the required arguements accordingly
#
# This tool uses the DatadogManagement class that provides a set of additional methods to make
# working with the Datadog API's for these administration tasks a little easier.
# It is effectively a wrapper around the Datadog Ruby SDK.
#
require 'date'
require 'rubygems'
require 'dogapi'
require 'json'
require 'fileutils'
require 'getoptlong'
require 'logger'
require 'yaml'
require_relative 'datadogmanagement'
datadogconfig = YAML.load_file('datadog-config.yaml')
logger = Logger.new(STDOUT)
# Parse the config block in the configuration file.
if datadogconfig.key?('config')
config = datadogconfig['config']
if config.key?('http_proxy')
#logger.info("Setting http_proxy from config file to #{config['http_proxy']}")
ENV['http_proxy'] = config['http_proxy']
end
if config.key?('https_proxy')
#logger.info("Setting https_proxy from config file to #{config['https_proxy']}")
ENV['https_proxy'] = config['https_proxy']
end
else
logger.fatal("The config block is not found in the configuration file")
exit 1
end
opts = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--team', '-t', GetoptLong::REQUIRED_ARGUMENT ],
[ '--class', '-c', GetoptLong::REQUIRED_ARGUMENT ]
)
def print_usage
puts "Datadog Backup CLI - Dump Datadog dashboards, screenboards and monitors to JSON backups"
puts "Usage: #{$0} --help - Display the help message"
puts "Usage: #{$0} --team all --class admin - Setup the admin users in all teams "
puts "Usage: #{$0} --team <name> --class admin - Setup the class of users in a specific team"
puts " (adm, st & ro)"
exit 1
end
if ( ARGV.count < 4 )
print_usage
end
opts.each do |opt, arg|
case opt
when '--help'
print_usage
when '--team'
if arg == 'all'
@teams = Hash.new
@teams = datadogconfig['teams']
else
@teams = Hash.new
@teams[arg] = datadogconfig['teams'][arg]
end
when '--class'
@role = arg
@users = datadogconfig['users'][arg]
end
end
@teams.keys.each do |team|
logger.info("=> Setting up users of role type \'#{@role}\' in Datadog team : \'#{team}\'")
datadog = DatadogManagement.new(
team,
datadogconfig['teams'][team]['apikey'],
datadogconfig['teams'][team]['appkey'])
datadog.configure_users(@users, @role )
end