mms-api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 41146da71becc079697b0e03e08bbad39088d209
4
+ data.tar.gz: cda31bca1e5833e7292818155c367033989617b5
5
+ SHA512:
6
+ metadata.gz: 14c8fe7367ccf6b628fac96b1a734ccb24d6308482afb3451fce71acb4d487903a00e148c87755115c4d7e6144e34c48e66c701305079e36fca7dc6f7e588c6c
7
+ data.tar.gz: 25c3aead9b5d93199285b51edfc34135b7f243c7a0748e8024a077ec51a0bd8358e287946d3ec4a766934b5cd5ca41af25015e41c015ee21efe360d80a5c36d9
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Cargo Media
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ mms-api
2
+ =======
3
+ Minimalistic [MMS API](http://mms.mongodb.com/) agent for ruby
data/bin/mms-api ADDED
@@ -0,0 +1,116 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
4
+
5
+ require 'mms'
6
+ require 'optparse'
7
+ require 'terminal-table'
8
+ require 'pp'
9
+
10
+ name = 'mms-api'
11
+ version = MMS::VERSION
12
+
13
+ actions_available = ["groups", "clusters", "snapshots", "restorejobs"]
14
+
15
+ options = {}
16
+ optparse = OptionParser.new do |opts|
17
+ opts.banner = "Usage: #{name} [options] action"
18
+
19
+ opts.on("-u", "--username <string>", "MMS user") do |u|
20
+ options[:username] = u
21
+ end
22
+
23
+ opts.on("-k", "--apikey <string>", "MMS api-key") do |k|
24
+ options[:apikey] = k
25
+ end
26
+
27
+ options[:limit] = 5
28
+ opts.on("-l", "--limit <string>", "Limit for result lines") do |l|
29
+ options[:limit] = l.to_i
30
+ end
31
+
32
+ opts.on("-v", "--version", "Version") do |v|
33
+ puts "#{name} v#{version}"
34
+ exit
35
+ end
36
+
37
+ opts.on("-h", "--help", "Show this help") do |h|
38
+ puts opts
39
+ exit
40
+ end
41
+ end
42
+
43
+ begin
44
+ optparse.parse!
45
+ if not options[:username]
46
+ puts "Missing options: MMS username [-u <string>]"
47
+ exit
48
+ end
49
+ if not options[:apikey]
50
+ puts "Missing options: MMS api-key [-k <string>]"
51
+ exit
52
+ end
53
+ rescue OptionParser::InvalidOption, OptionParser::MissingArgument
54
+ puts $!.to_s
55
+ puts optparse
56
+ exit
57
+ end
58
+
59
+ begin
60
+ action = ARGV.first.downcase
61
+ raise("Unknown action #{action.upcase}") unless actions_available.include? (action)
62
+ rescue => e
63
+ puts "Error: #{e.message}"
64
+ puts "Available actions: #{(actions_available.join ', ').upcase}"
65
+ puts optparse
66
+ exit 1
67
+ end
68
+
69
+ begin
70
+ agent = MMS::Agent.new(options[:username], options[:apikey])
71
+ results = agent.send(action)
72
+
73
+ rows = []
74
+ case action
75
+ when 'groups'
76
+ heading = ['Name', 'Active Agents', 'Replicas count', 'Shards count', 'Last Active Agent']
77
+ results.each do |group|
78
+ rows << [group['name'], group['activeAgentCount'], group['replicaSetCount'], group['shardCount'], group['lastActiveAgent']]
79
+ end
80
+ when 'clusters'
81
+ heading = ['Name', 'Shard name', 'Replica name', 'Type', 'Last heartbeat']
82
+ results.each do |cluster|
83
+ rows << [cluster['clusterName'], cluster['shardName'], cluster['replicaSetName'], cluster['typeName'], cluster['lastHeartbeat']]
84
+ end
85
+ when 'snapshots'
86
+ heading = ['SnapshotId', 'Complete', 'Created increment', 'Created date', 'Expires']
87
+ results_sorted = results.sort_by { |k| k['created']['date'] }.reverse
88
+ results_sorted.first(options[:limit]).each do |snapshot|
89
+ rows << [snapshot['id'], snapshot['complete'], snapshot['created']['increment'], snapshot['created']['date'], snapshot['expires']]
90
+ rows << :separator
91
+ part_count = 0
92
+ snapshot['parts'].each do |part|
93
+ file_size_mb = part['fileSizeBytes'].to_i / (1024*1024)
94
+ rows << ['', "part #{part_count}", part['typeName'], part['replicaSetName'], "#{file_size_mb} MB"]
95
+ part_count += 1
96
+ end
97
+ rows << :separator
98
+ end
99
+ when 'restorejobs'
100
+ heading = ['RestoreId', 'SnapshotId', 'Created', 'Status', 'Point in time', 'Delivery', 'Restore status']
101
+ results_sorted = results.sort_by { |k| k['created'] }.reverse
102
+ results_sorted.first(options[:limit]).each do |restore|
103
+ rows << [restore['id'], restore['snapshotId'], restore['created'], restore['statusName'], restore['pointInTime'], restore['delivery']['methodName'], restore['delivery']['statusName']]
104
+ rows << [{:value => 'download url:', :colspan => 7}]
105
+ rows << [{:value => restore['delivery']['url'], :colspan => 7}]
106
+ rows << :separator
107
+ end
108
+ end
109
+
110
+ table = Terminal::Table.new :title => action.upcase, :headings => (heading.nil? ? [] : heading), :rows => rows
111
+
112
+ puts table
113
+
114
+ rescue => e
115
+ exit 1
116
+ end
data/lib/mms.rb ADDED
@@ -0,0 +1,7 @@
1
+ module MMS
2
+ require 'rubygems' # For ruby < 1.9
3
+
4
+ require 'mms/agent'
5
+ require 'mms/helper'
6
+ require 'mms/version'
7
+ end
data/lib/mms/agent.rb ADDED
@@ -0,0 +1,88 @@
1
+ module MMS
2
+
3
+ class Agent
4
+
5
+ attr_accessor :api_protocol
6
+ attr_accessor :api_host
7
+ attr_accessor :api_port
8
+ attr_accessor :api_path
9
+ attr_accessor :api_version
10
+
11
+ attr_accessor :username
12
+ attr_accessor :apikey
13
+
14
+ def initialize(username = nil, apikey = nil)
15
+ @api_protocol = 'https'
16
+ @api_host = 'mms.mongodb.com'
17
+ @api_port = '443'
18
+ @api_path = '/api/public'
19
+ @api_version = 'v1.0'
20
+
21
+ @username = username
22
+ @apikey = apikey
23
+ end
24
+
25
+ def get_url
26
+ [@api_protocol, '://', @api_host, ':', @api_port, @api_path, '/', @api_version].join.to_s
27
+ end
28
+
29
+ def groups
30
+ MMS::Helper.get get_url + '/groups', @username, @apikey
31
+ end
32
+
33
+ def clusters(group_list = [])
34
+ if group_list.empty?
35
+ groups.each do |group|
36
+ group_list.push group['id']
37
+ end
38
+ end
39
+
40
+ results = []
41
+ group_list.each do |group|
42
+ output = MMS::Helper.get get_url + '/groups/' + group + '/clusters', @username, @apikey
43
+ results = results + output
44
+ end
45
+
46
+ results
47
+ end
48
+
49
+ def snapshots(cluster_list = [])
50
+ if cluster_list.empty?
51
+ clusters.each do |cluster|
52
+ cluster_list.push({
53
+ :id => cluster['id'],
54
+ :group_id => cluster['groupId']
55
+ })
56
+ end
57
+ end
58
+
59
+ results = []
60
+ cluster_list.each do |cluster|
61
+ output = MMS::Helper.get get_url + '/groups/' + cluster[:group_id] + '/clusters/' + cluster[:id] + '/snapshots' , @username, @apikey
62
+ results = results + output
63
+ end
64
+
65
+ results
66
+ end
67
+
68
+ def restorejobs(cluster_list = [])
69
+ if cluster_list.empty?
70
+ clusters.each do |cluster|
71
+ cluster_list.push({
72
+ :id => cluster['id'],
73
+ :group_id => cluster['groupId']
74
+ })
75
+ end
76
+ end
77
+
78
+ results = []
79
+ cluster_list.each do |cluster|
80
+ output = MMS::Helper.get get_url + '/groups/' + cluster[:group_id] + '/clusters/' + cluster[:id] + '/restoreJobs' , @username, @apikey
81
+ results = results + output
82
+ end
83
+
84
+ results
85
+ end
86
+
87
+ end
88
+ end
data/lib/mms/helper.rb ADDED
@@ -0,0 +1,39 @@
1
+ require "net/http"
2
+ require "uri"
3
+ require 'net/http/digest_auth'
4
+ require 'json'
5
+
6
+ module MMS
7
+
8
+ class Helper
9
+
10
+ def self.get(path, username, password)
11
+
12
+ digest_auth = Net::HTTP::DigestAuth.new
13
+ digest_auth.next_nonce
14
+
15
+ uri = URI.parse path
16
+ uri.user= CGI.escape(username)
17
+ uri.password= CGI.escape(password)
18
+
19
+ http = Net::HTTP.new(uri.host, uri.port)
20
+ http.use_ssl = true
21
+
22
+ req = Net::HTTP::Get.new uri.request_uri
23
+ res = http.request req
24
+
25
+ auth = digest_auth.auth_header uri, res['WWW-Authenticate'], 'GET'
26
+ req = Net::HTTP::Get.new uri.request_uri
27
+ req.add_field 'Authorization', auth
28
+
29
+ response = http.request(req)
30
+ response_json = JSON.parse(response.body)
31
+
32
+ if response_json['results'].nil?
33
+ fail("Error during API request!")
34
+ end
35
+
36
+ response_json['results']
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module MMS
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mms-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cargo Media
8
+ - kris-lab
9
+ - tomaszdurka
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-08-25 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: net-http-digest_auth
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.4'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '1.4'
29
+ - !ruby/object:Gem::Dependency
30
+ name: terminal-table
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: 1.4.5
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: 1.4.5
43
+ - !ruby/object:Gem::Dependency
44
+ name: rake
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rspec
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '2.0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '2.0'
71
+ description: Agent to collect data for MMS API
72
+ email: hello@cargomedia.ch
73
+ executables:
74
+ - mms-api
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - LICENSE
79
+ - README.md
80
+ - bin/mms-api
81
+ - lib/mms.rb
82
+ - lib/mms/agent.rb
83
+ - lib/mms/helper.rb
84
+ - lib/mms/version.rb
85
+ homepage: https://github.com/cargomedia/mms
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: MongoDB MMS API client
109
+ test_files: []