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 +4 -4
- data/README.md +14 -8
- data/lib/mms.rb +5 -5
- data/lib/mms/agent.rb +87 -11
- data/lib/mms/cache.rb +2 -7
- data/lib/mms/cli.rb +36 -74
- data/lib/mms/client.rb +22 -13
- data/lib/mms/config.rb +15 -44
- data/lib/mms/errors.rb +2 -10
- data/lib/mms/resource.rb +20 -23
- data/lib/mms/resource/alert.rb +3 -7
- data/lib/mms/resource/backup_config.rb +0 -3
- data/lib/mms/resource/cluster.rb +9 -15
- data/lib/mms/resource/group.rb +13 -15
- data/lib/mms/resource/host.rb +7 -12
- data/lib/mms/resource/metric.rb +5 -7
- data/lib/mms/resource/restore_job.rb +7 -9
- data/lib/mms/resource/snapshot.rb +10 -13
- data/lib/mms/resource/snapshot_schedule.rb +9 -12
- data/lib/mms/version.rb +1 -1
- metadata +30 -2
data/lib/mms/client.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
module MMS
|
2
|
-
|
3
2
|
class Client
|
4
|
-
|
5
3
|
attr_accessor :username
|
6
4
|
attr_accessor :apikey
|
7
5
|
attr_accessor :url
|
@@ -28,6 +26,19 @@ module MMS
|
|
28
26
|
_request(Net::HTTP::Post, @url + path, @username, @apikey, data)
|
29
27
|
end
|
30
28
|
|
29
|
+
# @param [String] path
|
30
|
+
# @return [Hash]
|
31
|
+
def delete(path)
|
32
|
+
_request(Net::HTTP::Delete, @url + path, @username, @apikey, nil)
|
33
|
+
end
|
34
|
+
|
35
|
+
# @param [String] path
|
36
|
+
# @param [Hash] data
|
37
|
+
# @return [Hash]
|
38
|
+
def patch(path, data)
|
39
|
+
_request(Net::HTTP::Patch, @url + path, @username, @apikey, data)
|
40
|
+
end
|
41
|
+
|
31
42
|
private
|
32
43
|
|
33
44
|
# @param [Net::HTTPRequest] http_method
|
@@ -37,22 +48,21 @@ module MMS
|
|
37
48
|
# @param [Hash] data
|
38
49
|
# @return [Hash]
|
39
50
|
def _request(http_method, path, username, password, data = nil)
|
40
|
-
|
41
51
|
digest_auth = Net::HTTP::DigestAuth.new
|
42
52
|
digest_auth.next_nonce
|
43
53
|
|
44
54
|
uri = URI.parse path
|
45
|
-
uri.user= CGI.escape(username)
|
46
|
-
uri.password= CGI.escape(password)
|
55
|
+
uri.user = CGI.escape(username)
|
56
|
+
uri.password = CGI.escape(password)
|
47
57
|
|
48
58
|
http = Net::HTTP.new uri.host, uri.port
|
49
59
|
http.use_ssl = (uri.scheme == 'https')
|
50
60
|
|
51
|
-
req = http_method.new(uri.request_uri,
|
61
|
+
req = http_method.new(uri.request_uri, 'Content-Type' => 'application/json')
|
52
62
|
res = http.request req
|
53
63
|
|
54
|
-
|
55
|
-
req = http_method.new(uri.request_uri,
|
64
|
+
fail 'Invalid method' unless http_method.is_a?(Class) && http_method < Net::HTTPRequest
|
65
|
+
req = http_method.new(uri.request_uri, 'Content-Type' => 'application/json')
|
56
66
|
method_name = http_method.name.split('::').last.upcase
|
57
67
|
auth = digest_auth.auth_header(uri, res['WWW-Authenticate'], method_name)
|
58
68
|
req.add_field 'Authorization', auth
|
@@ -61,16 +71,15 @@ module MMS
|
|
61
71
|
response = http.request req
|
62
72
|
response_json = JSON.parse response.body
|
63
73
|
|
64
|
-
unless response.code == 200
|
74
|
+
unless response.code == 200 || response_json['error'].nil?
|
65
75
|
msg = "http 'get' error for url `#{url}`"
|
66
76
|
msg = response_json['detail'] unless response_json['detail'].nil?
|
67
77
|
|
68
|
-
|
69
|
-
|
78
|
+
fail MMS::AuthError.new(msg, req, response) if response.code == '401'
|
79
|
+
fail MMS::ApiError.new(msg, req, response)
|
70
80
|
end
|
71
81
|
|
72
|
-
(response_json.nil?
|
82
|
+
(response_json.nil? || response_json['results'].nil?) ? response_json : response_json['results']
|
73
83
|
end
|
74
|
-
|
75
84
|
end
|
76
85
|
end
|
data/lib/mms/config.rb
CHANGED
@@ -1,57 +1,28 @@
|
|
1
1
|
module MMS
|
2
|
-
|
3
2
|
class Config
|
4
|
-
|
5
3
|
default = {
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
api_protocol: proc {
|
19
|
-
'https'
|
20
|
-
},
|
21
|
-
api_host: proc {
|
22
|
-
'mms.mongodb.com'
|
23
|
-
},
|
24
|
-
api_port: proc {
|
25
|
-
'443'
|
26
|
-
},
|
27
|
-
api_path: proc {
|
28
|
-
'/api/public'
|
29
|
-
},
|
30
|
-
api_version: proc {
|
31
|
-
'v1.0'
|
32
|
-
},
|
33
|
-
default_group_id: proc {
|
34
|
-
nil
|
35
|
-
},
|
36
|
-
default_cluster_id: proc {
|
37
|
-
nil
|
38
|
-
},
|
39
|
-
config_path: proc {
|
40
|
-
Dir.home + '/.mms-api'
|
41
|
-
}
|
4
|
+
username: proc { nil },
|
5
|
+
apikey: proc { nil },
|
6
|
+
apiurl: proc { [api_protocol, '://', api_host, ':', api_port, api_path, '/', api_version].join.to_s },
|
7
|
+
limit: proc { 10 },
|
8
|
+
api_protocol: proc { 'https' },
|
9
|
+
api_host: proc { 'mms.mongodb.com' },
|
10
|
+
api_port: proc { '443' },
|
11
|
+
api_path: proc { '/api/public' },
|
12
|
+
api_version: proc { 'v1.0' },
|
13
|
+
default_group_id: proc { nil },
|
14
|
+
default_cluster_id: proc { nil },
|
15
|
+
config_path: proc { Dir.home + '/.mms-api' }
|
42
16
|
}
|
43
17
|
|
44
18
|
default.each do |key, value|
|
45
19
|
define_method(key) do
|
46
|
-
if default[key].equal?(value)
|
47
|
-
default[key] = instance_eval(&value)
|
48
|
-
end
|
20
|
+
default[key] = instance_eval(&value) if default[key].equal?(value)
|
49
21
|
default[key]
|
50
22
|
end
|
51
|
-
define_method("#{key}=") do |
|
52
|
-
default[key] =
|
23
|
+
define_method("#{key}=") do |val|
|
24
|
+
default[key] = val
|
53
25
|
end
|
54
26
|
end
|
55
|
-
|
56
27
|
end
|
57
28
|
end
|
data/lib/mms/errors.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
module MMS
|
2
|
-
|
3
|
-
class RuntimeError < StandardError;
|
4
|
-
|
2
|
+
class RuntimeError < StandardError
|
5
3
|
# @param [String] message
|
6
4
|
# @param [Net::HTTPRequest] request
|
7
5
|
# @param [Net::HTTPResponse] response
|
@@ -14,11 +12,9 @@ module MMS
|
|
14
12
|
|
15
13
|
attr_reader :request
|
16
14
|
attr_reader :response
|
17
|
-
|
18
15
|
end
|
19
16
|
|
20
17
|
class ResourceError < StandardError
|
21
|
-
|
22
18
|
# @param [String] message
|
23
19
|
# @param [MMS::Resource] resource
|
24
20
|
def initialize(message, resource)
|
@@ -28,19 +24,15 @@ module MMS
|
|
28
24
|
end
|
29
25
|
|
30
26
|
attr_reader :resource
|
31
|
-
|
32
27
|
end
|
33
28
|
|
34
29
|
class ApiError < RuntimeError
|
35
|
-
|
36
|
-
def initialize(message, request, response)
|
30
|
+
def initialize(_message, request, response)
|
37
31
|
super("API Response error! Code: #{response.code}, body: #{response.body}", request, response)
|
38
32
|
end
|
39
|
-
|
40
33
|
end
|
41
34
|
|
42
35
|
class AuthError < RuntimeError; end
|
43
36
|
|
44
37
|
class ConfigError < StandardError; end
|
45
|
-
|
46
38
|
end
|
data/lib/mms/resource.rb
CHANGED
@@ -1,19 +1,18 @@
|
|
1
1
|
module MMS
|
2
|
-
|
3
2
|
class Resource
|
4
|
-
|
5
3
|
attr_accessor :id
|
6
4
|
attr_accessor :data
|
7
5
|
|
8
6
|
attr_accessor :client
|
7
|
+
attr_writer :client
|
9
8
|
|
10
9
|
# @param [MMS::Client] client
|
11
|
-
def
|
10
|
+
def client(client)
|
12
11
|
@client = client
|
13
12
|
end
|
14
13
|
|
15
14
|
# @param [Hash] data
|
16
|
-
def
|
15
|
+
def data(data)
|
17
16
|
@data = data
|
18
17
|
from_hash(data)
|
19
18
|
MMS::Cache.instance.set(cache_key(@id), data)
|
@@ -33,31 +32,31 @@ module MMS
|
|
33
32
|
|
34
33
|
# @return [Array<String>]
|
35
34
|
def table_row
|
36
|
-
|
35
|
+
fail("`#{__method__}` is not implemented for `#{self.class.name}`")
|
37
36
|
end
|
38
37
|
|
39
38
|
# @return [Array]
|
40
39
|
def table_section
|
41
|
-
|
40
|
+
fail("`#{__method__}` is not implemented for `#{self.class.name}`")
|
42
41
|
end
|
43
42
|
|
44
43
|
# @return [Array<String>]
|
45
44
|
def self.table_header
|
46
|
-
|
45
|
+
fail("`#{__method__}` is not implemented for `#{self.class.name}`")
|
47
46
|
end
|
48
47
|
|
49
|
-
def _load(
|
50
|
-
|
48
|
+
def _load(_id)
|
49
|
+
fail("`#{__method__}` is not implemented for `#{self.class.name}`")
|
51
50
|
end
|
52
51
|
|
53
|
-
# @param [Hash]
|
54
|
-
def _from_hash(
|
55
|
-
|
52
|
+
# @param [Hash] _data
|
53
|
+
def _from_hash(_data)
|
54
|
+
fail("`#{__method__}` is not implemented for `#{self.class.name}`")
|
56
55
|
end
|
57
56
|
|
58
57
|
# @return [Hash]
|
59
58
|
def _to_hash
|
60
|
-
|
59
|
+
fail("`#{__method__}` is not implemented for `#{self.class.name}`")
|
61
60
|
end
|
62
61
|
|
63
62
|
def invalidate_cache
|
@@ -65,18 +64,17 @@ module MMS
|
|
65
64
|
end
|
66
65
|
|
67
66
|
# @param [MMS::Client] client
|
68
|
-
# @param arguments
|
67
|
+
# @param [Hash] arguments
|
69
68
|
# @return self
|
70
69
|
def self.find(client, *arguments)
|
71
|
-
cache_key = self.cache_key(arguments.last
|
70
|
+
cache_key = self.cache_key(arguments.last)
|
72
71
|
data = MMS::Cache.instance.get(cache_key)
|
73
|
-
unless data
|
74
|
-
data = self._find(client, *arguments)
|
75
|
-
end
|
76
72
|
|
77
|
-
|
78
|
-
|
79
|
-
resource
|
73
|
+
data = _find(client, *arguments) unless data
|
74
|
+
|
75
|
+
resource = new
|
76
|
+
resource.client(client)
|
77
|
+
resource.data(data)
|
80
78
|
resource
|
81
79
|
end
|
82
80
|
|
@@ -87,8 +85,7 @@ module MMS
|
|
87
85
|
end
|
88
86
|
|
89
87
|
def self.cache_key(id)
|
90
|
-
"Class::#{
|
88
|
+
"Class::#{name}:#{id}"
|
91
89
|
end
|
92
|
-
|
93
90
|
end
|
94
91
|
end
|
data/lib/mms/resource/alert.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
module MMS
|
2
|
-
|
3
2
|
class Resource::Alert < Resource
|
4
|
-
|
5
3
|
attr_accessor :name
|
6
4
|
|
7
5
|
attr_accessor :type_name
|
@@ -19,14 +17,13 @@ module MMS
|
|
19
17
|
MMS::Resource::Group.find(@client, @data['groupId'])
|
20
18
|
end
|
21
19
|
|
22
|
-
|
23
20
|
# @param [Time, Integer] time
|
24
21
|
# @param [String] description
|
25
22
|
# @return [TrueClass, FalseClass]
|
26
23
|
def ack(time, description)
|
27
24
|
data = {
|
28
|
-
|
29
|
-
|
25
|
+
acknowledgedUntil: time.to_i,
|
26
|
+
acknowledgementComment: description
|
30
27
|
}
|
31
28
|
alert = @client.post '/groups/' + group.id + '/alerts/' + @id, data
|
32
29
|
!alert.nil?
|
@@ -39,7 +36,7 @@ module MMS
|
|
39
36
|
def table_section
|
40
37
|
rows = []
|
41
38
|
rows << table_row
|
42
|
-
rows << [{:
|
39
|
+
rows << [{ value: "AlertId: #{@id} GroupId: #{group.id}", colspan: 9, alignment: :left }]
|
43
40
|
rows << :separator
|
44
41
|
rows
|
45
42
|
end
|
@@ -75,6 +72,5 @@ module MMS
|
|
75
72
|
def _to_hash
|
76
73
|
@data
|
77
74
|
end
|
78
|
-
|
79
75
|
end
|
80
76
|
end
|
data/lib/mms/resource/cluster.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
module MMS
|
2
|
-
|
3
2
|
class Resource::Cluster < Resource
|
4
|
-
|
5
3
|
attr_accessor :name
|
6
4
|
attr_accessor :shard_name
|
7
5
|
attr_accessor :replicaset_name
|
@@ -24,12 +22,12 @@ module MMS
|
|
24
22
|
MMS::Resource::Snapshot.find(@client, group.id, @id, nil, id)
|
25
23
|
end
|
26
24
|
|
27
|
-
def snapshots(page = 1, limit =
|
25
|
+
def snapshots(page = 1, limit = 100)
|
28
26
|
if @snapshots.empty?
|
29
27
|
@client.get('/groups/' + group.id + '/clusters/' + @id + '/snapshots?pageNum=' + page.to_s + '&itemsPerPage=' + limit.to_s).each do |snapshot|
|
30
28
|
s = MMS::Resource::Snapshot.new
|
31
|
-
s.
|
32
|
-
s.
|
29
|
+
s.client(@client)
|
30
|
+
s.data(snapshot)
|
33
31
|
|
34
32
|
@snapshots.push s
|
35
33
|
end
|
@@ -41,16 +39,15 @@ module MMS
|
|
41
39
|
MMS::Resource::SnapshotSchedule.find(@client, group.id, @id)
|
42
40
|
end
|
43
41
|
|
44
|
-
def restorejobs(page = 1, limit =
|
42
|
+
def restorejobs(page = 1, limit = 100)
|
45
43
|
if @restorejobs.empty?
|
46
44
|
@client.get('/groups/' + group.id + '/clusters/' + @id + '/restoreJobs?pageNum=' + page.to_s + '&itemsPerPage=' + limit.to_s).each do |job|
|
47
|
-
|
48
45
|
job['clusterId'] = @id
|
49
46
|
job['groupId'] = group.id
|
50
47
|
|
51
48
|
j = MMS::Resource::RestoreJob.new
|
52
|
-
j.
|
53
|
-
j.
|
49
|
+
j.client(@client)
|
50
|
+
j.data(job)
|
54
51
|
|
55
52
|
@restorejobs.push j
|
56
53
|
end
|
@@ -70,13 +67,13 @@ module MMS
|
|
70
67
|
job_data_list = @client.post('/groups/' + group.id + '/clusters/' + @id + '/restoreJobs', data)
|
71
68
|
|
72
69
|
if job_data_list.nil?
|
73
|
-
|
70
|
+
fail MMS::ResourceError.new("Cannot create job from snapshot `#{id}`", self)
|
74
71
|
end
|
75
72
|
|
76
73
|
job_data_list.map do |job_data|
|
77
74
|
j = MMS::Resource::RestoreJob.new
|
78
|
-
j.
|
79
|
-
j.
|
75
|
+
j.client(@client)
|
76
|
+
j.data(job_data)
|
80
77
|
j
|
81
78
|
end
|
82
79
|
end
|
@@ -97,8 +94,6 @@ module MMS
|
|
97
94
|
client.get('/groups/' + group_id + '/clusters/' + id)
|
98
95
|
end
|
99
96
|
|
100
|
-
private
|
101
|
-
|
102
97
|
def _from_hash(data)
|
103
98
|
@name = data['clusterName']
|
104
99
|
@shard_name = data['shardName']
|
@@ -110,6 +105,5 @@ module MMS
|
|
110
105
|
def _to_hash
|
111
106
|
@data
|
112
107
|
end
|
113
|
-
|
114
108
|
end
|
115
109
|
end
|
data/lib/mms/resource/group.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
module MMS
|
2
|
-
|
3
2
|
class Resource::Group < Resource
|
4
|
-
|
5
3
|
attr_reader :name
|
6
4
|
attr_reader :active_agent_count
|
7
5
|
attr_reader :replicaset_count
|
@@ -18,12 +16,12 @@ module MMS
|
|
18
16
|
# @param [Integer] page
|
19
17
|
# @param [Integer] limit
|
20
18
|
# @return [Array<MMS::Resource::Host>]
|
21
|
-
def hosts(page = 1, limit =
|
19
|
+
def hosts(page = 1, limit = 100)
|
22
20
|
host_list = []
|
23
21
|
@client.get('/groups/' + @id + '/hosts?pageNum=' + page.to_s + '&itemsPerPage=' + limit.to_s).each do |host|
|
24
22
|
h = MMS::Resource::Host.new
|
25
|
-
h.
|
26
|
-
h.
|
23
|
+
h.client(@client)
|
24
|
+
h.data(host)
|
27
25
|
|
28
26
|
host_list.push h
|
29
27
|
end
|
@@ -34,12 +32,12 @@ module MMS
|
|
34
32
|
# @param [Integer] limit
|
35
33
|
# @param [String] status
|
36
34
|
# @return [Array<MMS::Resource::Alert>]
|
37
|
-
def alerts(page = 1, limit =
|
35
|
+
def alerts(page = 1, limit = 100, status = 'OPEN')
|
38
36
|
alert_list = []
|
39
37
|
@client.get('/groups/' + @id + '/alerts?status=' + status + '&pageNum=' + page.to_s + '&itemsPerPage=' + limit.to_s).each do |alert|
|
40
38
|
a = MMS::Resource::Alert.new
|
41
|
-
a.
|
42
|
-
a.
|
39
|
+
a.client(@client)
|
40
|
+
a.data(alert)
|
43
41
|
|
44
42
|
alert_list.push a
|
45
43
|
end
|
@@ -55,12 +53,12 @@ module MMS
|
|
55
53
|
# @param [Integer] page
|
56
54
|
# @param [Integer] limit
|
57
55
|
# @return [Array<MMS::Resource::Cluster>]
|
58
|
-
def clusters(page = 1, limit =
|
56
|
+
def clusters(page = 1, limit = 100)
|
59
57
|
if @clusters.empty?
|
60
58
|
@client.get('/groups/' + @id + '/clusters?pageNum=' + page.to_s + '&itemsPerPage=' + limit.to_s).each do |cluster|
|
61
59
|
c = MMS::Resource::Cluster.new
|
62
|
-
c.
|
63
|
-
c.
|
60
|
+
c.client(@client)
|
61
|
+
c.data(cluster)
|
64
62
|
|
65
63
|
@clusters.push c
|
66
64
|
end
|
@@ -78,8 +76,8 @@ module MMS
|
|
78
76
|
if @backup_configs.empty?
|
79
77
|
@client.get('/groups/' + @id + '/backupConfigs').each do |backup_config|
|
80
78
|
bc = MMS::Resource::BackupConfig.new
|
81
|
-
bc.
|
82
|
-
bc.
|
79
|
+
bc.client(@client)
|
80
|
+
bc.data(backup_config)
|
83
81
|
|
84
82
|
@backup_configs.push bc
|
85
83
|
end
|
@@ -95,7 +93,7 @@ module MMS
|
|
95
93
|
begin
|
96
94
|
snapshot = cluster.snapshot(id)
|
97
95
|
break unless snapshot.nil?
|
98
|
-
rescue MMS::ApiError =>
|
96
|
+
rescue MMS::ApiError => _e
|
99
97
|
# Snapshot is not available on this cluster. Skip it!
|
100
98
|
end
|
101
99
|
end
|
@@ -104,7 +102,7 @@ module MMS
|
|
104
102
|
begin
|
105
103
|
snapshot = host.snapshot(id)
|
106
104
|
break unless snapshot.nil?
|
107
|
-
rescue MMS::ApiError =>
|
105
|
+
rescue MMS::ApiError => _e
|
108
106
|
# Snapshot is not available on this host. Skip it!
|
109
107
|
end
|
110
108
|
end
|