mms-api 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e348b26040968b5237a1ecf092521d1b7a9fee4
4
- data.tar.gz: 315480e20f2e2df40e5d7371f711d51e24caae69
3
+ metadata.gz: 522bb6499771a5d35e3775dc983f503c17213531
4
+ data.tar.gz: c1a3b629f19493576070783dfa937d0611195169
5
5
  SHA512:
6
- metadata.gz: 9937df53420d73716e1146f6d33669e94cc96fd0bac80035696f219524188a79fb95bd5f0dffc32ca6c93d304064768b8c220859a737f48dda8c34eb1ccb76c1
7
- data.tar.gz: 53306305960f12f8d1716c2aec73baf36392cb59668ac8549ea561cbad03a0cc48672d975deffce68e7f3e2317f98be023d0829c8f8c90e3e2bf58bff3682b8f
6
+ metadata.gz: 0feb735e81520656658e134e5fccbd5a46af4e192862b4072aae6a431c4e0796e7d4bb1f688c962a661d0445da7bcc4f4e477cf9d99874aa57963215de1b30b2
7
+ data.tar.gz: 7bbbf7c6f5d75db491d175aa11f9b6a5774b3a862bf1400c37fac9e75b83a7491facd32651bb1d6e7326ba34b8d891298681259a9e9cc4a942b9e8f784f2ba0b
data/README.md CHANGED
@@ -14,14 +14,20 @@ The MMS Public API follows the principles of the REST architectural style to exp
14
14
 
15
15
  |Resource |Get All |Get One |Create |Update |Delete |
16
16
  |:--------------|:------:|:------:|:-----:|:-----:|:-----:|
17
- |Groups | + | + | | | |
18
- |Hosts | + | + | | | |
19
- |Clusters | + | + | | | |
20
- |Snapshots | + | + | | | |
21
- |Alerts | + | + | | | |
22
- |Restore Jobs | + | + | + | | |
23
- |Backup Configs | + | + | | | |
24
- |Metrics | + | + | | | |
17
+ |Groups | + | + | - | - | - |
18
+ |Hosts | + | + | + | + | + |
19
+ |Clusters | + | + | x | + | x |
20
+ |Snapshots | + | + | x | x | - |
21
+ |Alerts | + | + | x | - | x |
22
+ |Restore Jobs | + | + | + | x | x |
23
+ |Backup Configs | + | + | x | - | x |
24
+ |Metrics | + | + | x | x | x |
25
+
26
+ ```
27
+ + : supported by gem
28
+ - : not supported by gem
29
+ x : not supported by MMS
30
+ ```
25
31
 
26
32
  Library usage
27
33
  -------------
data/lib/mms.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  module MMS
2
- require 'rubygems' # For ruby < 1.9
2
+ require 'rubygems' # For ruby < 1.9
3
3
 
4
- require "uri"
5
- require "json"
6
- require "cgi"
7
- require "net/http"
4
+ require 'uri'
5
+ require 'json'
6
+ require 'cgi'
7
+ require 'net/http'
8
8
  require 'net/http/digest_auth'
9
9
  require 'terminal-table'
10
10
  require 'pathname'
@@ -1,7 +1,5 @@
1
1
  module MMS
2
-
3
2
  class Agent
4
-
5
3
  attr_accessor :client
6
4
 
7
5
  # @param [MMS::Client] client
@@ -10,7 +8,7 @@ module MMS
10
8
  end
11
9
 
12
10
  # @param [String] apiurl
13
- def set_apiurl(apiurl)
11
+ def apiurl(apiurl)
14
12
  @client.url = apiurl
15
13
  end
16
14
 
@@ -19,8 +17,8 @@ module MMS
19
17
  group_list = []
20
18
  client.get('/groups').each do |group|
21
19
  g = MMS::Resource::Group.new
22
- g.set_client(client)
23
- g.set_data(group)
20
+ g.client(client)
21
+ g.data(group)
24
22
 
25
23
  group_list.push g
26
24
  end
@@ -36,6 +34,72 @@ module MMS
36
34
  host_list
37
35
  end
38
36
 
37
+ # @param [String] groupid
38
+ # @param [String] hostname
39
+ # @param [Integer] port
40
+ # @option options [String] username Required if authMechanismName is MONGODB_CR. Otherwise illegal.
41
+ # @option options [String] password Required if authMechanismName is MONGODB_CR. Otherwise illegal.
42
+ # @option options [TrueClass, FalseClass] sslEnabled Must be true if the authMechanismName is MONGODB_X509. Default is false if omitted.
43
+ # @option options [TrueClass, FalseClass] logsEnabled Default is false if omitted.
44
+ # @option options [TrueClass, FalseClass] alertsEnabled Default is true if omitted.
45
+ # @option options [TrueClass, FalseClass] profilerEnabled Default is false if omitted.
46
+ # @option options [Integer] muninPort Default is 0 and Munin stats are not collected if omitted.
47
+ # @option options [String] authMechanismName Default is NONE if omitted. If set to MONGODB_CR then you must provide the username and password.
48
+ # @return [<MMS::Resource::Host>]
49
+ def host_create(groupid, hostname, port, options = {})
50
+ data = {}
51
+ data[:hostname] = hostname
52
+ data[:port] = port
53
+ data[:username] = options[:username] || nil
54
+ data[:password] = options[:password] || nil
55
+ data[:sslEnabled] = options[:sslEnabled] || false
56
+ data[:logsEnabled] = options[:logsEnabled] || false
57
+ data[:alertsEnabled] = options[:alertsEnabled] || true
58
+ data[:profilerEnabled] = options[:profilerEnabled] || false
59
+ data[:muninPort] = options[:muninPort] || 0
60
+ data[:authMechanismName] = options[:authMechanismName] || nil
61
+ ret_host = client.post("/groups/#{groupid}/hosts", data)
62
+ host = MMS::Resource::Host.new
63
+ host._from_hash(ret_host)
64
+ host
65
+ end
66
+
67
+ # @param [String] groupid
68
+ # @param [String] hostid
69
+ # @option options [String] username Required if authMechanismName is MONGODB_CR. Otherwise illegal.
70
+ # @option options [String] password Required if authMechanismName is MONGODB_CR. Otherwise illegal.
71
+ # @option options [TrueClass, FalseClass] sslEnabled Must be true if the authMechanismName is MONGODB_X509. Default is false if omitted.
72
+ # @option options [TrueClass, FalseClass] logsEnabled Default is false if omitted.
73
+ # @option options [TrueClass, FalseClass] alertsEnabled Default is true if omitted.
74
+ # @option options [TrueClass, FalseClass] profilerEnabled Default is false if omitted.
75
+ # @option options [Integer] muninPort Default is 0 and Munin stats are not collected if omitted.
76
+ # @option options [String] authMechanismName Default is NONE if omitted. If set to MONGODB_CR then you must provide the username and password.
77
+ # @return [<MMS::Resource::Host>]
78
+ def host_update(groupid, hostid, options = {})
79
+ data = {}
80
+ data[:username] = options[:username] if options.include?(:username)
81
+ data[:password] = options[:password] if options.include?(:password)
82
+ data[:sslEnabled] = options[:sslEnabled] if options.include?(:sslEnabled)
83
+ data[:logsEnabled] = options[:logsEnabled] if options.include?(:logsEnabled)
84
+ data[:alertsEnabled] = options[:alertsEnabled] if options.include?(:alertsEnabled)
85
+ data[:profilerEnabled] = options[:profilerEnabled] if options.include?(:profilerEnabled)
86
+ data[:muninPort] = options[:muninPort] if options.include?(:muninPort)
87
+ data[:authMechanismName] = options[:authMechanismName] if options.include?(:authMechanismName)
88
+ ret_host = client.patch("/groups/#{groupid}/hosts/#{hostid}", data)
89
+ host = MMS::Resource::Host.new
90
+ host._from_hash(ret_host)
91
+ host
92
+ end
93
+
94
+ # @param [String] groupid
95
+ # @param [String] hostid
96
+ # @return [TrueClass, FalseClass]
97
+ def host_delete(groupid, hostid)
98
+ client.delete("/groups/#{groupid}/hosts/#{hostid}")
99
+ host = client.delete("/groups/#{groupid}/hosts/#{hostid}")
100
+ host == {} ? true : false
101
+ end
102
+
39
103
  # @return [Array<MMS::Resource::Cluster>]
40
104
  def clusters
41
105
  cluster_list = []
@@ -45,13 +109,25 @@ module MMS
45
109
  cluster_list
46
110
  end
47
111
 
112
+ # @param [String] groupid
113
+ # @param [String] clusterid
114
+ # @param [String] name
115
+ # @return [<MMS::Resource::Cluster>]
116
+ def cluster_update(groupid, clusterid, name)
117
+ data = { clusterName: name }
118
+ ret_cluster = client.patch("/groups/#{groupid}/clusters/#{clusterid}", data)
119
+ cluster = MMS::Resource::Cluster.new
120
+ cluster._from_hash(ret_cluster)
121
+ cluster
122
+ end
123
+
48
124
  # @return [Array<MMS::Resource::Snapshot>]
49
125
  def snapshots
50
126
  snapshot_list = []
51
127
  clusters.each do |cluster|
52
128
  snapshot_list.concat cluster.snapshots
53
129
  end
54
- snapshot_list.sort_by { |snapshot| snapshot.created_date }.reverse
130
+ snapshot_list.sort_by(&:created_date).reverse
55
131
  end
56
132
 
57
133
  # @return [Array<MMS::Resource::Alert>]
@@ -60,7 +136,7 @@ module MMS
60
136
  groups.each do |group|
61
137
  alert_list.concat group.alerts
62
138
  end
63
- alert_list.sort_by { |alert| alert.created }.reverse
139
+ alert_list.sort_by(&:created).reverse
64
140
  end
65
141
 
66
142
  # @return [Array<MMS::Resource::RestoreJob>]
@@ -69,7 +145,7 @@ module MMS
69
145
  clusters.each do |cluster|
70
146
  restorejob_list.concat cluster.restorejobs
71
147
  end
72
- restorejob_list.sort_by { |job| job.created }.reverse
148
+ restorejob_list.sort_by(&:created).reverse
73
149
  end
74
150
 
75
151
  # @param [String] type_value
@@ -79,8 +155,9 @@ module MMS
79
155
  def restorejob_create(type_value, group_id, cluster_id)
80
156
  if type_value.length == 24
81
157
  find_group(group_id).cluster(cluster_id).snapshot(type_value).create_restorejob
82
- elsif datetime = (type_value == 'now' ? DateTime.now : DateTime.parse(type_value))
83
- raise('Invalid datetime. Correct `YYYY-MM-RRTH:m:sZ`') if datetime.nil?
158
+ else
159
+ datetime = (type_value == 'now' ? DateTime.now : DateTime.parse(type_value))
160
+ fail('Invalid datetime. Correct `YYYY-MM-RRTH:m:sZ`') if datetime.nil?
84
161
  datetime_string = [[datetime.year, datetime.month, datetime.day].join('-'), 'T', [datetime.hour, datetime.minute, datetime.second].join(':'), 'Z'].join
85
162
  find_group(group_id).cluster(cluster_id).create_restorejob(datetime_string)
86
163
  end
@@ -110,6 +187,5 @@ module MMS
110
187
  def find_group(id)
111
188
  MMS::Resource::Group.find(@client, id)
112
189
  end
113
-
114
190
  end
115
191
  end
@@ -1,15 +1,14 @@
1
1
  require 'singleton'
2
2
 
3
3
  module MMS
4
-
5
4
  class Cache
6
-
7
5
  include Singleton
8
6
 
9
7
  attr_accessor :storage
8
+ attr_reader :storage
10
9
 
11
10
  def initialize
12
- @storage = Hash.new {|hash, key| hash[key] = nil }
11
+ @storage = Hash.new { |hash, key| hash[key] = nil }
13
12
  end
14
13
 
15
14
  # @param [String] key
@@ -32,9 +31,5 @@ module MMS
32
31
  def clear
33
32
  initialize
34
33
  end
35
-
36
- def storage
37
- @storage
38
- end
39
34
  end
40
35
  end
@@ -2,11 +2,8 @@ require 'clamp'
2
2
  require 'parseconfig'
3
3
 
4
4
  module MMS
5
-
6
5
  class CLI
7
-
8
6
  class MMS::CLI::Command < Clamp::Command
9
-
10
7
  attr_accessor :app_name
11
8
  attr_accessor :config
12
9
 
@@ -25,7 +22,7 @@ module MMS
25
22
  @config.apiurl = u
26
23
  end
27
24
 
28
- option ['-v', '--version'], :flag, 'Version' do |v|
25
+ option ['-v', '--version'], :flag, 'Version' do |_v|
29
26
  puts "mms-api v#{MMS::VERSION}"
30
27
  exit
31
28
  end
@@ -43,28 +40,28 @@ module MMS
43
40
  parse_user_home_config
44
41
  end
45
42
 
46
- option ['-i', '--ignore'], :flag, 'Ignore flag of --group-id and -cluster-id', :default => false
43
+ option ['-i', '--ignore'], :flag, 'Ignore flag of --group-id and -cluster-id', default: false
47
44
 
48
- option ['-j', '--json'], :flag, 'Print JSON output', :default => false
45
+ option ['-j', '--json'], :flag, 'Print JSON output', default: false
49
46
 
50
47
  option ['-l', '--limit'], '<integer>', 'Limit for result items' do |l|
51
48
  @config.limit = Integer(l)
52
49
  end
53
50
 
54
- def initialize(invocation_path, context = {}, parent_attribute_values = {})
51
+ def initialize(_invocation_path, _context = {}, _parent_attribute_values = {})
55
52
  @config ||= MMS::Config.new
56
53
  end
57
54
 
58
55
  def parse_user_home_config
59
- raise(MMS::ConfigError.new('Config file path is not set!')) if @config.config_path.nil?
56
+ fail(MMS::ConfigError.new('Config file path is not set!')) if @config.config_path.nil?
60
57
  config_file = Pathname.new(@config.config_path)
61
- raise(MMS::ConfigError.new("Config file `#{config_file}` does not exist")) unless config_file.exist?
58
+ fail(MMS::ConfigError.new("Config file `#{config_file}` does not exist")) unless config_file.exist?
62
59
 
63
60
  config = ParseConfig.new(config_file)
64
61
  config.params.map do |key, value|
65
62
  begin
66
63
  @config.send("#{key}=", value)
67
- rescue Exception => e
64
+ rescue Exception => _e
68
65
  raise MMS::ConfigError.new("Config option `#{key}` from file `#{config_file}` is not allowed!")
69
66
  end
70
67
  end
@@ -92,13 +89,13 @@ module MMS
92
89
  end
93
90
 
94
91
  # @param [String] heading
95
- # @param [Array<MMS::Resource>]
92
+ # @param [Array<MMS::Resource>] resource_list
96
93
  def print(heading, resource_list)
97
94
  json? ? print_json(resource_list) : print_human(heading, resource_list)
98
95
  end
99
96
 
100
97
  # @param [String] heading
101
- # @param [Array<MMS::Resource>]
98
+ # @param [Array<MMS::Resource>] resource_list
102
99
  def print_human(heading, resource_list)
103
100
  rows = []
104
101
 
@@ -106,12 +103,12 @@ module MMS
106
103
  rows += resource.table_section
107
104
  end
108
105
 
109
- puts Terminal::Table.new :headings => (heading.nil? ? [] : heading), :rows => rows
106
+ puts Terminal::Table.new headings: (heading.nil? ? [] : heading), rows: rows
110
107
 
111
108
  print_tips unless ignore?
112
109
  end
113
110
 
114
- # @param [Array<MMS::Resource>]
111
+ # @param [Array<MMS::Resource>] resource_list
115
112
  def print_json(resource_list)
116
113
  rows = []
117
114
 
@@ -126,127 +123,100 @@ module MMS
126
123
  puts 'Default group: ' + @config.default_group_id unless @config.default_group_id.nil?
127
124
  puts 'Default cluster: ' + @config.default_cluster_id unless @config.default_cluster_id.nil?
128
125
 
129
- if !@config.default_group_id.nil? or !@config.default_cluster_id.nil?
126
+ if !@config.default_group_id.nil? || !@config.default_cluster_id.nil?
130
127
  puts "Add flag --ignore or update --default-group-id, --default-cluster-id or update your `#{@config.config_path}` to see all resources"
131
128
  end
132
129
  end
133
130
 
134
-
135
131
  # @param [Array] arguments
136
132
  def run(arguments)
137
- begin
138
- parse_user_home_config
139
- super
140
- rescue Clamp::HelpWanted => e
141
- raise(help)
142
- rescue Clamp::UsageError => e
143
- raise([e.message, help].join("\n"))
144
- rescue MMS::AuthError => e
145
- raise('Authorisation problem. Please check you credential!')
146
- rescue MMS::ResourceError => e
147
- raise(["Resource #{e.resource.class.name} problem:", e.message].join("\n"))
148
- end
133
+ parse_user_home_config
134
+ super
135
+ rescue Clamp::HelpWanted => _e
136
+ raise(help)
137
+ rescue Clamp::UsageError => e
138
+ raise([e.message, help].join("\n"))
139
+ rescue MMS::AuthError => _e
140
+ raise('Authorisation problem. Please check you credential!')
141
+ rescue MMS::ResourceError => e
142
+ raise(["Resource #{e.resource.class.name} problem:", e.message].join("\n"))
149
143
  end
150
144
  end
151
145
 
152
146
  class MMS::CLI::Command::Groups < MMS::CLI::Command
153
-
154
147
  subcommand 'list', 'Group list' do
155
-
156
148
  def execute
157
149
  print(MMS::Resource::Group.table_header, groups)
158
150
  end
159
151
  end
160
-
161
152
  end
162
153
 
163
154
  class MMS::CLI::Command::Hosts < MMS::CLI::Command
164
-
165
155
  subcommand 'list', 'Host list' do
166
-
167
156
  def execute
168
- host_list = groups.collect! { |group| group.hosts }.flatten
157
+ host_list = groups.collect!(&:hosts).flatten
169
158
  print(MMS::Resource::Host.table_header, host_list)
170
159
  end
171
160
  end
172
-
173
161
  end
174
162
 
175
163
  class MMS::CLI::Command::Clusters < MMS::CLI::Command
176
-
177
164
  subcommand 'list', 'Cluster list' do
178
-
179
165
  def execute
180
166
  print(MMS::Resource::Cluster.table_header, clusters)
181
167
  end
182
168
  end
183
169
 
184
170
  subcommand 'snapshot-schedule', 'Cluster snapshot schedule config' do
185
-
186
171
  def execute
187
- snapshot_schedule_list = clusters.collect! { |cluster| cluster.snapshot_schedule }.flatten
172
+ snapshot_schedule_list = clusters.collect!(&:snapshot_schedule).flatten
188
173
  print(MMS::Resource::SnapshotSchedule.table_header, snapshot_schedule_list)
189
174
  end
190
175
  end
191
-
192
176
  end
193
177
 
194
-
195
178
  class MMS::CLI::Command::Alerts < MMS::CLI::Command
196
-
197
179
  subcommand 'list', 'Alerts list' do
198
-
199
180
  def execute
200
- alert_list = groups.collect! { |group| group.alerts }.flatten
181
+ alert_list = groups.collect!(&:alerts).flatten
201
182
  print(MMS::Resource::Alert.table_header, alert_list)
202
183
  end
203
-
204
184
  end
205
185
 
206
186
  subcommand 'ack', 'Acknowledge alert' do
207
-
208
- parameter '[alert-id]', 'Alert ID', :default => 'all'
209
- parameter '[group-id]', 'Group ID', :default => '--default-group-id'
210
- parameter '[timestamp]', 'Postpone to timestamp', :default => 'forever'
187
+ parameter '[alert-id]', 'Alert ID', default: 'all'
188
+ parameter '[group-id]', 'Group ID', default: '--default-group-id'
189
+ parameter '[timestamp]', 'Postpone to timestamp', default: 'forever'
211
190
 
212
191
  def execute
213
192
  g_id = group_id == '--default-group-id' ? @config.default_group_id : group_id
214
193
  agent.alert_ack(alert_id, timestamp, g_id)
215
194
  puts 'Done.'
216
195
  end
217
-
218
196
  end
219
-
220
197
  end
221
198
 
222
199
  class MMS::CLI::Command::Snapshots < MMS::CLI::Command
223
-
224
200
  subcommand 'list', 'Snapshot list' do
225
-
226
201
  def execute
227
- snapshot_list = clusters.collect! { |cluster| cluster.snapshots }.flatten.sort_by { |snapshot| snapshot.created_date }.reverse
202
+ snapshot_list = clusters.collect!(&:snapshots).flatten.sort_by(&:created_date).reverse
228
203
  print(MMS::Resource::Snapshot.table_header, snapshot_list)
229
204
  end
230
205
  end
231
-
232
206
  end
233
207
 
234
208
  class MMS::CLI::Command::RestoreJobs < MMS::CLI::Command
235
-
236
209
  subcommand 'list', 'Restorejob list' do
237
-
238
210
  def execute
239
- restorejob_list = clusters.collect! { |cluster| cluster.restorejobs }.flatten.sort_by { |job| job.created }.reverse
211
+ restorejob_list = clusters.collect!(&:restorejobs).flatten.sort_by(&:created).reverse
240
212
  print(MMS::Resource::RestoreJob.table_header, restorejob_list)
241
213
  end
242
-
243
214
  end
244
215
 
245
216
  subcommand 'create', 'Restorejob create' do
246
-
247
- parameter '[snapshot-source]', 'Restore from source. Options: now | timestamp | snapshot-id', :default => 'now'
248
- parameter '[group-id]', 'Group ID', :default => '--default-group-id'
249
- parameter '[cluster-id]', 'Cluster ID', :default => '--default-cluster-id'
217
+ parameter '[snapshot-source]', 'Restore from source. Options: now | timestamp | snapshot-id', default: 'now'
218
+ parameter '[group-id]', 'Group ID', default: '--default-group-id'
219
+ parameter '[cluster-id]', 'Cluster ID', default: '--default-cluster-id'
250
220
 
251
221
  def execute
252
222
  g_id = group_id == '--default-group-id' ? @config.default_group_id : group_id
@@ -256,19 +226,14 @@ module MMS
256
226
 
257
227
  puts 'Done.'
258
228
  end
259
-
260
229
  end
261
-
262
230
  end
263
231
 
264
232
  class MMS::CLI::CommandManager < MMS::CLI::Command
265
-
266
233
  def run(arguments)
267
- begin
268
- super
269
- rescue Exception => e
270
- abort(e.message.empty? ? 'Unknown error/Interrupt' : e.message)
271
- end
234
+ super
235
+ rescue Exception => e
236
+ abort(e.message.empty? ? 'Unknown error/Interrupt' : e.message)
272
237
  end
273
238
 
274
239
  subcommand 'groups', 'Groups ', MMS::CLI::Command::Groups
@@ -277,9 +242,6 @@ module MMS
277
242
  subcommand 'alerts', 'Alerts', MMS::CLI::Command::Alerts
278
243
  subcommand 'snapshots', 'Snapshots', MMS::CLI::Command::Snapshots
279
244
  subcommand 'restorejobs', 'Restorejobs', MMS::CLI::Command::RestoreJobs
280
-
281
245
  end
282
-
283
246
  end
284
-
285
247
  end