mms-api 0.1.0 → 0.1.1

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: 91354b2007ab1bf12006b201e465bbc64c7c4e73
4
- data.tar.gz: 0c1fdc6932a86acdc3f3f41061f2dcb8782bd612
3
+ metadata.gz: 0f46973360a5bc90648f5db16ed68f2a715df24f
4
+ data.tar.gz: 87a3cdbe52ea77dd5b1778d8df9318a2b34734e6
5
5
  SHA512:
6
- metadata.gz: 78d3bde92f63163a64454077f6edee97d82619458c3329f9ba5fa6f84eb2414a04452b46be57f3d0d8fabb9a9862fe2cc67ff48919c90433641b9bd621660479
7
- data.tar.gz: e0210ba968b2d67ba035700b952b67445c896ab9b849ba1e1d9899369f18286b57c8d11aa45301de47d02a8cd6eb95815bb17b7e35819092c2f4712be82ff640
6
+ metadata.gz: 34e98f586c2a03de4ea3260a11f22fba361e93407de77f740f0f12b5c2a79144befb8ed1343ccff7143f3fed09238fd048c4415d7915a69115bb14ca4be823b1
7
+ data.tar.gz: 2cbf2343574237b3814aae43a47815591d0b5cf773da2555ceb08271b4836cb50442c437b1a38b9a5af6f76b1589b6be88e73d7c92bc4abfb61eea1a7f26b2e5
data/README.md CHANGED
@@ -12,35 +12,91 @@ API coverage
12
12
  ------------
13
13
  The MMS Public API follows the principles of the REST architectural style to expose a number of internal resources which enable programmatic access to [MMS’s features](http://mms.mongodb.com/help/reference/api/). Current implementation support only a few of API features.
14
14
 
15
- |Resource|Get All|Get One|Create|Update|Delete|
16
- |:---|:---:|:---:|:---:|:---:|:---:|
17
- |Groups| + | + | | | | |
18
- |Hosts| + | + | | | | |
19
- |Clusters| + | + | | | | |
20
- |Snapshots| + | + | | | | |
21
- |Alerts| + | + | | | | |
22
- |Restore Jobs| + | + | + | | | |
23
-
24
- Config
25
- ------
15
+ |Resource |Get All |Get One |Create |Update |Delete |
16
+ |:------------|:------:|:------:|:-----:|:-----:|:-----:|
17
+ |Groups | + | + | | | |
18
+ |Hosts | + | + | | | |
19
+ |Clusters | + | + | | | |
20
+ |Snapshots | + | + | | | |
21
+ |Alerts | + | + | | | |
22
+ |Restore Jobs | + | + | + | | |
23
+
24
+ Library usage
25
+ -------------
26
+
27
+ Source code itself is well-documented so when writing code it should auto-complete and hint in all supported usages.
28
+
29
+
30
+ ### Client
31
+ Most important part of the api is client. In order to make any request you need to instantiate client with correct params.
32
+
26
33
  ```ruby
27
- config = MMS::Config.new
34
+ client = MMS::Client.new('username', 'api_key')
28
35
  ```
29
36
 
30
- Client
31
- ------
37
+ This client is used by all other classes connecting to api no matter if it's Resource or helper class like Agent.
38
+
39
+
40
+ ### Agent
41
+ Agent is simple wrapper class for listing all accessible resources.
42
+
32
43
  ```ruby
33
- client = MMS::Client.new(config.username, config.apikey)
44
+ client = MMS::Client.new('username', 'api_key')
45
+ agent = MMS::Agent.new(client)
46
+
47
+ agent.alerts.each do |alert|
48
+ alert.ack('now')
49
+ end
34
50
  ```
35
51
 
36
- Agent
37
- -----
52
+ List of resource-listing agent methods:
53
+ - groups
54
+ - hosts
55
+ - clusters
56
+ - snapshots
57
+ - alerts
58
+ - restorejobs
59
+
60
+ ### Resources
61
+
62
+ You can find lists of resource by using agent as pointed above, or by various resource methods.
63
+ Each resource have a find method loading certain resource with provided id (plus corresponding parent ids), e.g.
38
64
  ```ruby
39
- agent = MMS::Agent.new(client)
65
+ client = new MMS::Client.new('username', 'api_key')
66
+ host = MMS::Resource::Host.find(client, 'group_id', 'host_id')
67
+ ```
68
+
69
+ Additionally some resources have additional instance methods to retrieve sub-resources, e.g.
70
+ ```ruby
71
+ client = new MMS::Client.new('username', 'api_key')
72
+ group = MMS::Resource::Group.find(client, 'group_id')
73
+ hosts = group.hosts
74
+ ```
75
+
76
+ Cli usage
77
+ ---------
78
+
79
+ There is a built-in cli with several commands retrieving api resource lists.
80
+
81
+ ### Configuration
82
+
83
+ Cli uses configuration with all values set to default ones.
84
+ Config itself has `config_file` property which merges itself with params from the file.
85
+ By default `config_file` points to home directory, but it can be changed to points to any file.
86
+
87
+ ```
88
+ username=sysadmin@example.tld
89
+ apikey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
90
+ apiurl=https://mms.mydomain.tld/api/public/v1.0
91
+ default_group_id=your-group-id
92
+ default_cluster_id=your-cluster-id
40
93
  ```
41
94
 
42
- Cli
43
- ---
95
+ Additionally some options can be modified using cli options.
96
+
97
+ ### Available commands
98
+
99
+
44
100
  ```bash
45
101
  $ mms-api --help
46
102
  Usage:
@@ -71,13 +127,3 @@ Options:
71
127
  -j, --json Print JSON output (default: false)
72
128
  -l, --limit <integer> Limit for result items
73
129
  ```
74
-
75
- `mms-api` reads default configuration from your home directory `~/.mms-api`. Example configuration:
76
-
77
- ```
78
- username=sysadmin@example.tld
79
- apikey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
80
- # apiurl=https://mms.mydomain.tld/api/public/v1.0
81
- default_group_id=your-group-id
82
- default_cluster_id=your-cluster-id
83
- ```
@@ -16,8 +16,7 @@ module MMS
16
16
  def set_data(data)
17
17
  @data = data
18
18
  from_hash(data)
19
- cache_key = "Class::#{self.class.name}:#{@id}"
20
- MMS::Cache.instance.set(cache_key, data)
19
+ MMS::Cache.instance.set(cache_key(@id), data)
21
20
  end
22
21
 
23
22
  # @param [Hash] data
@@ -61,11 +60,15 @@ module MMS
61
60
  raise("`#{__method__}` is not implemented for `#{self.class.name}`")
62
61
  end
63
62
 
63
+ def invalidate_cache
64
+ MMS::Cache.instance.delete(cache_key(@id))
65
+ end
66
+
64
67
  # @param [MMS::Client] client
65
68
  # @param arguments...
66
69
  # @return self
67
70
  def self.find(client, *arguments)
68
- cache_key = "Class::#{self.name}:#{arguments.last()}"
71
+ cache_key = self.cache_key(arguments.last())
69
72
  data = MMS::Cache.instance.get(cache_key)
70
73
  unless data
71
74
  data = self._find(client, *arguments)
@@ -76,5 +79,16 @@ module MMS
76
79
  resource.set_data(data)
77
80
  resource
78
81
  end
82
+
83
+ private
84
+
85
+ def cache_key(id)
86
+ "Class::#{self.class.name}:#{id}"
87
+ end
88
+
89
+ def self.cache_key(id)
90
+ "Class::#{self.name}:#{id}"
91
+ end
92
+
79
93
  end
80
94
  end
@@ -45,12 +45,8 @@ module MMS
45
45
  if @restorejobs.empty?
46
46
  @client.get('/groups/' + group.id + '/clusters/' + @id + '/restoreJobs?pageNum=' + page.to_s + '&itemsPerPage=' + limit.to_s).each do |job|
47
47
 
48
- if job['snapshotId'].nil? and job['clusterId'].nil?
49
- raise MMS::ResourceError.new("RestoreJob `#{job['id']}` with status `#{job['statusName']}` has no `clusterId` and no `snapshotId`.", self)
50
- elsif job['clusterId'].nil?
51
- snapshot = group.find_snapshot(job['snapshotId'])
52
- job['clusterId'] = snapshot.cluster.id unless snapshot.nil?
53
- end
48
+ job['clusterId'] = @id
49
+ job['groupId'] = group.id
54
50
 
55
51
  j = MMS::Resource::RestoreJob.new
56
52
  j.set_client(@client)
@@ -66,42 +62,23 @@ module MMS
66
62
  # @return [Array<MMS::Resource::RestoreJob>]
67
63
  def create_restorejob(point_in_time = nil)
68
64
  data = {
69
- 'timestamp' => {
70
- 'date' => point_in_time,
71
- 'increment' => 0
72
- }
65
+ 'timestamp' => {
66
+ 'date' => point_in_time,
67
+ 'increment' => 0
68
+ }
73
69
  }
74
- jobs = @client.post('/groups/' + group.id + '/clusters/' + @id + '/restoreJobs', data)
70
+ job_data_list = @client.post('/groups/' + group.id + '/clusters/' + @id + '/restoreJobs', data)
75
71
 
76
- if jobs.nil?
72
+ if job_data_list.nil?
77
73
  raise MMS::ResourceError.new("Cannot create job from snapshot `#{self.id}`", self)
78
74
  end
79
75
 
80
- job_list = []
81
- # work around due to bug in MMS API; cannot read restoreJob using provided info.
82
- # The config-server RestoreJob and Snapshot has no own ClusterId to be accessed.
83
- tries = 5
84
- while tries > 0
85
- begin
86
- restore_jobs = restorejobs
87
- tries = 0
88
- rescue Exception => e
89
- tries-=1;
90
- raise MMS::ResourceError.new(e.message, self) if tries < 1
91
-
92
- STDERR.puts e.message
93
- STDERR.puts 'Sleeping for 5 seconds. Trying again...'
94
- sleep(5)
95
- end
96
- end
97
-
98
- jobs.each do |job|
99
- _list = restore_jobs.select { |restorejob| restorejob.id == job['id'] }
100
- _list.each do |restorejob|
101
- job_list.push restorejob
102
- end
76
+ job_data_list.map do |job_data|
77
+ j = MMS::Resource::RestoreJob.new
78
+ j.set_client(@client)
79
+ j.set_data(job_data)
80
+ j
103
81
  end
104
- job_list
105
82
  end
106
83
 
107
84
  def table_row
@@ -74,15 +74,15 @@ module MMS
74
74
  end
75
75
 
76
76
  # @param [String] id
77
- # @return [MMS::Resource::Snapshot]
77
+ # @return [MMS::Resource::Snapshot, NilClass]
78
78
  def find_snapshot(id)
79
79
  snapshot = nil
80
80
  clusters.each do |cluster|
81
81
  begin
82
82
  snapshot = cluster.snapshot(id)
83
- rescue => e
84
- # STDERR.puts 'cannot load snapshotId for cluster if config-server is the source!'
85
- # STDERR.puts 'not supported in current MMS API version'
83
+ break unless snapshot.nil?
84
+ rescue MMS::ApiError => e
85
+ # Snapshot is not available on this cluster. Skip it!
86
86
  end
87
87
  end
88
88
  snapshot
@@ -20,38 +20,17 @@ module MMS
20
20
  attr_accessor :delivery_status_name
21
21
  attr_accessor :delivery_url
22
22
 
23
+ attr_accessor :cluster_id
24
+ attr_accessor :group_id
25
+
23
26
  # @return [MMS::Resource::Cluster]
24
27
  def cluster
25
- begin
26
- cluster = MMS::Resource::Cluster.find(@client, @data['groupId'], @data['clusterId'])
27
- rescue
28
- # Workaround
29
- # time to time the mms-api return data without "clusterId" defined
30
- # creation of empty clluster instance is a good solution here.
31
- cluster = MMS::Resource::Cluster.new
32
- cluster.set_client(@client)
33
- cluster.set_data({'groupId' => @data['groupId']})
34
- end
35
- cluster
28
+ MMS::Resource::Cluster.find(@client, @group_id, @cluster_id)
36
29
  end
37
30
 
38
- # @return [TrueClass, FalseClass]
39
- def has_cluster
40
- # cluster definition for config-server cannot be loaded
41
- # as there is no clusterId for this type of group.
42
- # there is snapshotId for RestoreJob but seems to be stored
43
- # internally in MMS API. Not accessible by public API.
44
- snapshot != nil
45
- end
46
-
47
- # @return [MMS::Resource::Snapshot]
31
+ # @return [MMS::Resource::Snapshot, NilClass]
48
32
  def snapshot
49
- # snapshot details for config-server cannot be loaded
50
- # as there is no clusterId. See also has_cluster()
51
- if @snapshot.nil?
52
- @snapshot = cluster.group.find_snapshot(@snapshot_id)
53
- end
54
- @snapshot
33
+ @snapshot ||= cluster.group.find_snapshot(@snapshot_id)
55
34
  end
56
35
 
57
36
  def table_row
@@ -61,12 +40,12 @@ module MMS
61
40
 
62
41
  def table_section
63
42
  [
64
- table_row,
65
- [@id, "#{cluster.name} (#{cluster.id})", {:value => '', :colspan => 5}],
66
- ['', cluster.group.name, {:value => '', :colspan => 5}],
67
- [{:value => 'download url:', :colspan => 7}],
68
- [{:value => @delivery_url || '(waiting for link)', :colspan => 7}],
69
- :separator
43
+ table_row,
44
+ [@id, "#{cluster.name} (#{cluster.id})", {:value => '', :colspan => 5}],
45
+ ['', cluster.group.name, {:value => '', :colspan => 5}],
46
+ [{:value => 'download url:', :colspan => 7}],
47
+ [{:value => @delivery_url || '(waiting for link)', :colspan => 7}],
48
+ :separator
70
49
  ]
71
50
  end
72
51
 
@@ -74,17 +53,22 @@ module MMS
74
53
  ['Timestamp / RestoreId', 'SnapshotId / Cluster / Group', 'Name (created)', 'Status', 'Point in time', 'Delivery', 'Restore status']
75
54
  end
76
55
 
77
- # @return [Hash, NilClass]
78
- def self.find_recursively(client, group_id, cluster_id, id)
79
- cluster = MMS::Resource::Cluster.find(client, group_id, cluster_id)
80
- # config server has no cluster but owns RestoreJob and Snapshot
81
- restore_jobs = cluster.restorejobs
82
- job = restore_jobs.select { |restorejob| restorejob.id == id }
83
- job.first.data unless job.nil? and job.empty?
56
+ def self._find(client, group_id, cluster_id, id)
57
+ begin
58
+ client.get('/groups/' + group_id + '/clusters/' + cluster_id + '/restoreJobs/' + id)
59
+ rescue MMS::ApiError => e
60
+ # workaround for https://jira.mongodb.org/browse/DOCS-5017
61
+ self._find_from_list(client, group_id, cluster_id, id)
62
+ end
84
63
  end
85
64
 
86
- def self._find(client, group_id, cluster_id, id)
87
- client.get('/groups/' + group_id + '/clusters/' + cluster_id + '/restoreJobs/' + id)
65
+ def self._find_from_list(client, group_id, cluster_id, id)
66
+ cluster = MMS::Resource::Cluster.find(client, group_id, cluster_id)
67
+
68
+ job = cluster.restorejobs.find { |restorejob| restorejob.id == id }
69
+ raise("Cannot find RestoreJob id `#{id}`") if job.nil?
70
+
71
+ job.data
88
72
  end
89
73
 
90
74
  private
@@ -99,6 +83,8 @@ module MMS
99
83
  @delivery_status_name = data['delivery']['statusName'] unless data['delivery'].nil?
100
84
  @delivery_url = data['delivery']['url'] unless data['delivery'].nil?
101
85
  @name = DateTime.parse(@created).strftime("%Y-%m-%d %H:%M:%S")
86
+ @cluster_id = data['clusterId']
87
+ @group_id = data['groupId']
102
88
  end
103
89
 
104
90
  def _to_hash
@@ -11,6 +11,7 @@ module MMS
11
11
  attr_accessor :created_increment
12
12
  attr_accessor :expires
13
13
  attr_accessor :parts
14
+ attr_accessor :is_possibly_inconsistent
14
15
 
15
16
  # @return [TrueClass, FalseClass]
16
17
  def is_cluster
@@ -59,27 +60,22 @@ module MMS
59
60
  # @return [Array<MMS::Resource::RestoreJob>]
60
61
  def create_restorejob
61
62
  data = {:snapshotId => @id}
62
- jobs = @client.post '/groups/' + cluster.group.id + '/clusters/' + cluster.id + '/restoreJobs', data
63
+ job_data_list = @client.post '/groups/' + cluster.group.id + '/clusters/' + cluster.id + '/restoreJobs', data
63
64
 
64
- if jobs.nil?
65
+ if job_data_list.nil?
65
66
  raise MMS::ResourceError.new("Cannot create job from snapshot `#{self.id}`", self)
66
67
  end
67
68
 
68
- job_list = []
69
- # work around due to bug in MMS API; cannot read restoreJob using provided info.
70
- # The config-server RestoreJob and Snapshot has no own ClusterId to be accessed.
71
- restore_jobs = cluster.restorejobs
72
- jobs.each do |job|
73
- _list = restore_jobs.select { |restorejob| restorejob.id == job['id'] }
74
- _list.each do |restorejob|
75
- job_list.push restorejob
76
- end
69
+ job_data_list.map do |job_data|
70
+ j = MMS::Resource::RestoreJob.new
71
+ j.set_client(@client)
72
+ j.set_data(job_data)
73
+ j
77
74
  end
78
- job_list
79
75
  end
80
76
 
81
77
  def table_row
82
- [cluster.group.name, cluster.name, @id, @complete, @created_increment, @name, @expires]
78
+ [cluster.group.name, cluster.name, @id, @complete, @created_increment, @name, @expires, @is_possibly_inconsistent]
83
79
  end
84
80
 
85
81
  def table_section
@@ -97,7 +93,7 @@ module MMS
97
93
  end
98
94
 
99
95
  def self.table_header
100
- ['Group', 'Cluster', 'SnapshotId', 'Complete', 'Created increment', 'Name (created date)', 'Expires']
96
+ ['Group', 'Cluster', 'SnapshotId', 'Complete', 'Created increment', 'Name (created date)', 'Expires', 'Inconsistent']
101
97
  end
102
98
 
103
99
  # @param [MMS::Client] client
@@ -117,6 +113,7 @@ module MMS
117
113
  @created_increment = data['created'].nil? ? nil : data['created']['increment']
118
114
  @expires = data['expires']
119
115
  @parts = data['parts']
116
+ @is_possibly_inconsistent = data['isPossiblyInconsistent']
120
117
  @name = @created_date.nil? ? @id : DateTime.parse(@created_date).strftime("%Y-%m-%d %H:%M:%S")
121
118
  end
122
119
 
@@ -1,3 +1,3 @@
1
1
  module MMS
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mms-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cargo Media
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-02-20 00:00:00.000000000 Z
13
+ date: 2015-03-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: net-http-digest_auth
@@ -142,9 +142,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  version: '0'
143
143
  requirements: []
144
144
  rubyforge_project:
145
- rubygems_version: 2.4.5
145
+ rubygems_version: 2.4.6
146
146
  signing_key:
147
147
  specification_version: 4
148
148
  summary: MongoDB MMS API client
149
149
  test_files: []
150
- has_rdoc: