megam_api 0.60 → 0.61
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +73 -23
- data/lib/megam/api.rb +127 -141
- data/lib/megam/api/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c3994fb43d32666338188b374d5029e3b5a3445
|
4
|
+
data.tar.gz: a68bfea9975a0d9054d17bccb3696ef0bdd45775
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1b639e61e7ae38b8dd0cf6070b561f9cb0e6291a9e687e64a59caf21ed1b6d9ba893bcf5cc38cf6f14d8be11b8734bb9bba5b0baa89a750bd8a53d608ad3c65
|
7
|
+
data.tar.gz: 44c5272ec3c87e63db416f3c480ab990b8b527c0fe679c2e2bdd67e1c1042c83c6ebf8c04346d1380789921899c2ca333afe3dec0457a0b289078e920d7b833f
|
data/README.md
CHANGED
@@ -1,37 +1,88 @@
|
|
1
|
-
Megam
|
1
|
+
Megam api for ruby
|
2
2
|
==================
|
3
3
|
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/megam_api.svg)](http://badge.fury.io/rb/megam_api)
|
5
5
|
|
6
|
-
Megam
|
6
|
+
Megam api is used to talk to console.megam.io
|
7
7
|
|
8
|
-
For more about the
|
8
|
+
For more about the REST API <http://docs.megam.io/v1.0/docs/assemblies>
|
9
9
|
|
10
|
-
[![Build Status](https://travis-ci.org/
|
10
|
+
[![Build Status](https://travis-ci.org/megamsys/megam_api.png)](https://travis-ci.org/megamsys/megam_api)
|
11
11
|
|
12
12
|
|
13
13
|
Usage
|
14
14
|
-----
|
15
15
|
|
16
|
-
|
16
|
+
```shell
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
megam = Megam::API.new(:headers => {:api_key => API_KEY, :email => EMAIL})
|
21
|
-
|
22
|
-
Now you can make requests to the api.
|
23
|
-
|
24
|
-
Requests
|
25
|
-
--------
|
18
|
+
gem install megam_api
|
26
19
|
|
27
|
-
|
20
|
+
```
|
28
21
|
|
22
|
+
* Let us show an account with email = "your_email_id"
|
29
23
|
|
30
|
-
|
31
|
-
|
32
|
-
Refer [documentation] (http://www.gomegam.com/docs)
|
24
|
+
```ruby
|
33
25
|
|
26
|
+
require 'megam_api'
|
34
27
|
|
28
|
+
require 'meggy/meg'
|
29
|
+
require 'megam/core/server_api'
|
30
|
+
require 'command_line_reporter'
|
31
|
+
|
32
|
+
class Meggy
|
33
|
+
class Meg
|
34
|
+
class AccountShow < Meg
|
35
|
+
include CommandLineReporter
|
36
|
+
|
37
|
+
banner "meg account show"
|
38
|
+
def run
|
39
|
+
begin
|
40
|
+
Megam::Config[:email] = Meggy::Config[:email]
|
41
|
+
Megam::Config[:api_key] = Meggy::Config[:api_key]
|
42
|
+
@excon_res = Megam::Account.show(Megam::Config[:email])
|
43
|
+
acct_res = @excon_res.data[:body]
|
44
|
+
report(acct_res)
|
45
|
+
rescue Megam::API::Errors::ErrorWithResponse => ewr
|
46
|
+
res = ewr.response.data[:body].some_msg
|
47
|
+
text.error(res[:msg])
|
48
|
+
text.msg("#{text.color("Retry Again", :white, :bold)}")
|
49
|
+
text.info(res[:links])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def report(acct_res)
|
54
|
+
table :border => true do
|
55
|
+
row :header => true, :color => 'green' do
|
56
|
+
column 'Account', :width => 15
|
57
|
+
column 'Information', :width => 32, :align => 'left'
|
58
|
+
end
|
59
|
+
row do
|
60
|
+
column 'email'
|
61
|
+
column acct_res.email
|
62
|
+
end
|
63
|
+
row do
|
64
|
+
column 'api_key'
|
65
|
+
column acct_res.api_key
|
66
|
+
end
|
67
|
+
row do
|
68
|
+
column 'authority'
|
69
|
+
column acct_res.authority
|
70
|
+
end
|
71
|
+
row do
|
72
|
+
column 'created_at'
|
73
|
+
column acct_res.created_at
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
* See [meggy](https://github.com/megamsys/meggy.git) where the files like `Meg` are there.
|
84
|
+
|
85
|
+
For more implementation details [see meggy](https://github.com/megamsys/meggy.git)
|
35
86
|
|
36
87
|
We are glad to help if you have questions, or request for new features..
|
37
88
|
|
@@ -39,16 +90,16 @@ We are glad to help if you have questions, or request for new features..
|
|
39
90
|
|
40
91
|
|
41
92
|
|
42
|
-
|
93
|
+
|
43
94
|
# License
|
44
95
|
|
45
96
|
| | |
|
46
97
|
|:---------------------|:-----------------------------------------|
|
47
|
-
| **Author:** | Kishorekumar Neelamegam (<nkishore@megam.
|
48
|
-
| | Raj Thilak (<rajthilak@megam.
|
49
|
-
| | Yeshwanth Kumar (<getyesh@megam.
|
98
|
+
| **Author:** | Kishorekumar Neelamegam (<nkishore@megam.io>)
|
99
|
+
| | Raj Thilak (<rajthilak@megam.io>)
|
100
|
+
| | Yeshwanth Kumar (<getyesh@megam.io>)
|
50
101
|
| | Subash Sethurajan (<subash.avc@gmail.com>)
|
51
|
-
| | Thomas Alrin (<
|
102
|
+
| | Thomas Alrin (<thomasalrin@megam.io>)
|
52
103
|
| **Copyright:** | Copyright (c) 2012-2014 Megam Systems.
|
53
104
|
| **License:** | Apache License, Version 2.0
|
54
105
|
|
@@ -63,4 +114,3 @@ distributed under the License is distributed on an "AS IS" BASIS,
|
|
63
114
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
64
115
|
See the License for the specific language governing permissions and
|
65
116
|
limitations under the License.
|
66
|
-
|
data/lib/megam/api.rb
CHANGED
@@ -1,110 +1,105 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
1
|
+
require 'base64'
|
2
|
+
require 'time'
|
3
|
+
require 'excon'
|
4
|
+
require 'uri'
|
5
|
+
require 'zlib'
|
6
6
|
require 'openssl'
|
7
|
-
require 'yaml' #COMMON YML
|
8
7
|
# open it up when needed. This will be needed when a new customer onboarded via pug.
|
9
|
-
require
|
10
|
-
|
11
|
-
__LIB_DIR__ = File.expand_path(File.join(File.dirname(__FILE__),
|
12
|
-
unless $LOAD_PATH.include?(__LIB_DIR__)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
require
|
17
|
-
require
|
18
|
-
require
|
19
|
-
require
|
20
|
-
require
|
21
|
-
require
|
22
|
-
require
|
23
|
-
require
|
24
|
-
require
|
25
|
-
require
|
26
|
-
require
|
27
|
-
require
|
28
|
-
require
|
29
|
-
require
|
30
|
-
require
|
31
|
-
require
|
32
|
-
require
|
33
|
-
require
|
34
|
-
require
|
35
|
-
require
|
36
|
-
require
|
37
|
-
require
|
38
|
-
require
|
39
|
-
|
40
|
-
require
|
41
|
-
|
42
|
-
require
|
43
|
-
require
|
44
|
-
require
|
45
|
-
require
|
46
|
-
require
|
47
|
-
require
|
48
|
-
require
|
49
|
-
require
|
50
|
-
require
|
51
|
-
require
|
52
|
-
require
|
53
|
-
require
|
54
|
-
require
|
55
|
-
require
|
56
|
-
require
|
57
|
-
require
|
58
|
-
require
|
59
|
-
require
|
60
|
-
require
|
61
|
-
require
|
62
|
-
require
|
63
|
-
require
|
64
|
-
require
|
65
|
-
require
|
66
|
-
require
|
67
|
-
require
|
68
|
-
require
|
69
|
-
require
|
70
|
-
require
|
71
|
-
require
|
72
|
-
require
|
73
|
-
require
|
74
|
-
require
|
75
|
-
require
|
76
|
-
|
77
|
-
require
|
78
|
-
|
79
|
-
|
80
|
-
require
|
81
|
-
require
|
82
|
-
require
|
83
|
-
require
|
84
|
-
require
|
85
|
-
require
|
86
|
-
require
|
87
|
-
require
|
88
|
-
require
|
89
|
-
require
|
90
|
-
require
|
91
|
-
require
|
92
|
-
require "megam/core/subscriptions_collection"
|
93
|
-
require "megam/core/subscriptions"
|
94
|
-
require "megam/core/promos"
|
8
|
+
require 'securerandom'
|
9
|
+
|
10
|
+
__LIB_DIR__ = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
11
|
+
$LOAD_PATH.unshift(__LIB_DIR__) unless $LOAD_PATH.include?(__LIB_DIR__)
|
12
|
+
|
13
|
+
require 'megam/api/errors'
|
14
|
+
require 'megam/api/version'
|
15
|
+
require 'megam/api/accounts'
|
16
|
+
require 'megam/api/requests'
|
17
|
+
require 'megam/api/cat_requests'
|
18
|
+
require 'megam/api/predef_clouds'
|
19
|
+
require 'megam/api/cloud_tool_settings'
|
20
|
+
require 'megam/api/sshkeys'
|
21
|
+
require 'megam/api/marketplaces'
|
22
|
+
require 'megam/api/marketplace_addons'
|
23
|
+
require 'megam/api/organizations'
|
24
|
+
require 'megam/api/domains'
|
25
|
+
require 'megam/api/csars'
|
26
|
+
require 'megam/api/assemblies'
|
27
|
+
require 'megam/api/assembly'
|
28
|
+
require 'megam/api/components'
|
29
|
+
require 'megam/api/event'
|
30
|
+
require 'megam/api/availableunits'
|
31
|
+
require 'megam/api/balances'
|
32
|
+
require 'megam/api/billinghistories'
|
33
|
+
require 'megam/api/billings'
|
34
|
+
require 'megam/api/credithistories'
|
35
|
+
require 'megam/api/discounts'
|
36
|
+
require 'megam/api/subscriptions'
|
37
|
+
require 'megam/api/promos'
|
38
|
+
|
39
|
+
require 'megam/core/server_api'
|
40
|
+
require 'megam/core/config'
|
41
|
+
require 'megam/core/stuff'
|
42
|
+
require 'megam/core/text'
|
43
|
+
require 'megam/core/log'
|
44
|
+
require 'megam/core/json_compat'
|
45
|
+
require 'megam/core/auth'
|
46
|
+
require 'megam/core/error'
|
47
|
+
require 'megam/core/account'
|
48
|
+
require 'megam/core/request'
|
49
|
+
require 'megam/core/request_collection'
|
50
|
+
require 'megam/core/predefcloud'
|
51
|
+
require 'megam/core/predefcloud_collection'
|
52
|
+
require 'megam/core/cloudtoolsetting'
|
53
|
+
require 'megam/core/cloudtoolsetting_collection'
|
54
|
+
require 'megam/core/sshkey'
|
55
|
+
require 'megam/core/sshkey_collection'
|
56
|
+
require 'megam/core/marketplace'
|
57
|
+
require 'megam/core/marketplace_collection'
|
58
|
+
require 'megam/core/marketplace_addon'
|
59
|
+
require 'megam/core/marketplace_addon_collection'
|
60
|
+
require 'megam/core/organizations'
|
61
|
+
require 'megam/core/organizations_collection'
|
62
|
+
require 'megam/core/domains'
|
63
|
+
require 'megam/core/assemblies'
|
64
|
+
require 'megam/core/assemblies_collection'
|
65
|
+
require 'megam/core/csar'
|
66
|
+
require 'megam/core/csar_collection'
|
67
|
+
require 'megam/core/konipai'
|
68
|
+
require 'megam/core/assembly'
|
69
|
+
require 'megam/core/assembly_collection'
|
70
|
+
require 'megam/core/components'
|
71
|
+
require 'megam/core/components_collection'
|
72
|
+
require 'megam/core/cat_requests'
|
73
|
+
require 'megam/core/cat_requests_collection'
|
74
|
+
require 'megam/core/event'
|
75
|
+
|
76
|
+
require 'megam/core/availableunits_collection'
|
77
|
+
require 'megam/core/availableunits'
|
78
|
+
require 'megam/core/balances_collection'
|
79
|
+
require 'megam/core/balances'
|
80
|
+
require 'megam/core/billinghistories_collection'
|
81
|
+
require 'megam/core/billinghistories'
|
82
|
+
require 'megam/core/billings_collection'
|
83
|
+
require 'megam/core/billings'
|
84
|
+
require 'megam/core/credithistories_collection'
|
85
|
+
require 'megam/core/credithistories'
|
86
|
+
require 'megam/core/discounts_collection'
|
87
|
+
require 'megam/core/discounts'
|
88
|
+
require 'megam/core/subscriptions_collection'
|
89
|
+
require 'megam/core/subscriptions'
|
90
|
+
require 'megam/core/promos'
|
95
91
|
|
96
92
|
module Megam
|
97
93
|
class API
|
98
|
-
|
99
|
-
#text is used to print stuff in the terminal (message, log, info, warn etc.)
|
94
|
+
# text is used to print stuff in the terminal (message, log, info, warn etc.)
|
100
95
|
attr_accessor :text
|
101
96
|
|
102
|
-
API_MEGAM_IO =
|
103
|
-
API_VERSION2 =
|
97
|
+
API_MEGAM_IO = 'api.megam.io'.freeze
|
98
|
+
API_VERSION2 = '/v2'.freeze
|
104
99
|
|
105
|
-
X_Megam_DATE =
|
106
|
-
X_Megam_HMAC =
|
107
|
-
X_Megam_OTTAI =
|
100
|
+
X_Megam_DATE = 'X-Megam-DATE'.freeze
|
101
|
+
X_Megam_HMAC = 'X-Megam-HMAC'.freeze
|
102
|
+
X_Megam_OTTAI = 'X-Megam-OTTAI'.freeze
|
108
103
|
|
109
104
|
HEADERS = {
|
110
105
|
'Accept' => 'application/json',
|
@@ -115,20 +110,17 @@ module Megam
|
|
115
110
|
}
|
116
111
|
|
117
112
|
OPTIONS = {
|
118
|
-
:
|
119
|
-
:
|
120
|
-
:
|
121
|
-
:
|
113
|
+
headers: {},
|
114
|
+
host: '127.0.0.1',
|
115
|
+
nonblock: false,
|
116
|
+
scheme: 'http'
|
122
117
|
}
|
123
118
|
|
124
|
-
|
125
119
|
def text
|
126
120
|
@text ||= Megam::Text.new(STDOUT, STDERR, STDIN, {})
|
127
121
|
end
|
128
122
|
|
129
|
-
|
130
|
-
@last_response
|
131
|
-
end
|
123
|
+
attr_reader :last_response
|
132
124
|
|
133
125
|
# It is assumed that every API call will use an API_KEY/email. This ensures validity of the person
|
134
126
|
# really the same guy on who he claims.
|
@@ -137,17 +129,17 @@ module Megam
|
|
137
129
|
# 2. The options as passed via the instantiation of API will override global options. The ones that are passed are :email and :api_key and will
|
138
130
|
# be merged into a class variable @options
|
139
131
|
# 3. Upon merge of the options, the api_key, email as available in the @options is deleted.
|
140
|
-
def initialize(options={})
|
132
|
+
def initialize(options = {})
|
141
133
|
@options = OPTIONS.merge(options)
|
142
134
|
@api_key = @options.delete(:api_key) || ENV['MEGAM_API_KEY']
|
143
135
|
@email = @options.delete(:email)
|
144
|
-
|
136
|
+
fail ArgumentError, 'You must specify [:email, :api_key]' if @email.nil? || @api_key.nil?
|
145
137
|
end
|
146
138
|
|
147
|
-
def request(params
|
139
|
+
def request(params, &block)
|
148
140
|
just_color_debug("#{@options[:path]}")
|
149
141
|
start = Time.now
|
150
|
-
Megam::Log.debug(
|
142
|
+
Megam::Log.debug('START')
|
151
143
|
params.each do |pkey, pvalue|
|
152
144
|
Megam::Log.debug("> #{pkey}: #{pvalue}")
|
153
145
|
end
|
@@ -170,35 +162,35 @@ module Megam
|
|
170
162
|
reerror.set_backtrace(error.backtrace)
|
171
163
|
Megam::Log.debug("#{reerror.response.body}")
|
172
164
|
reerror.response.body = Megam::JSONCompat.from_json(reerror.response.body.chomp)
|
173
|
-
Megam::Log.debug(
|
165
|
+
Megam::Log.debug('RESPONSE ERR: Ruby Object')
|
174
166
|
Megam::Log.debug("#{reerror.response.body}")
|
175
167
|
raise(reerror)
|
176
168
|
end
|
177
169
|
|
178
170
|
@last_response = response
|
179
|
-
Megam::Log.debug(
|
171
|
+
Megam::Log.debug('RESPONSE: HTTP Status and Header Data')
|
180
172
|
Megam::Log.debug("> HTTP #{response.remote_ip} #{response.status}")
|
181
173
|
|
182
174
|
response.headers.each do |header, value|
|
183
175
|
Megam::Log.debug("> #{header}: #{value}")
|
184
176
|
end
|
185
|
-
Megam::Log.debug(
|
177
|
+
Megam::Log.debug('End HTTP Status/Header Data.')
|
186
178
|
|
187
179
|
if response.body && !response.body.empty?
|
188
180
|
if response.headers['Content-Encoding'] == 'gzip'
|
189
|
-
Megam::Log.debug(
|
181
|
+
Megam::Log.debug('RESPONSE: Content-Encoding is gzip')
|
190
182
|
response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read
|
191
183
|
end
|
192
|
-
Megam::Log.debug(
|
184
|
+
Megam::Log.debug('RESPONSE: HTTP Body(JSON)')
|
193
185
|
Megam::Log.debug("#{response.body}")
|
194
186
|
|
195
187
|
begin
|
196
188
|
unless response.headers[X_Megam_OTTAI]
|
197
189
|
response.body = Megam::JSONCompat.from_json(response.body.chomp)
|
198
|
-
Megam::Log.debug(
|
190
|
+
Megam::Log.debug('RESPONSE: Ruby Object')
|
199
191
|
else
|
200
192
|
response.body = Megam::KoniPai.new.koni(response.body.chomp)
|
201
|
-
Megam::Log.debug(
|
193
|
+
Megam::Log.debug('RESPONSE: KoniPai Object ')
|
202
194
|
end
|
203
195
|
Megam::Log.debug("#{response.body}")
|
204
196
|
rescue Exception => jsonerr
|
@@ -206,7 +198,7 @@ module Megam
|
|
206
198
|
raise(jsonerr)
|
207
199
|
end
|
208
200
|
end
|
209
|
-
Megam::Log.debug("END(#{(Time.now - start)
|
201
|
+
Megam::Log.debug("END(#{(Time.now - start)}s)")
|
210
202
|
# reset (non-persistent) connection
|
211
203
|
@connection.reset
|
212
204
|
response
|
@@ -215,31 +207,27 @@ module Megam
|
|
215
207
|
private
|
216
208
|
|
217
209
|
def just_color_debug(path)
|
218
|
-
text.msg "--> #{text.color("(#{path})", :cyan
|
210
|
+
text.msg "--> #{text.color("(#{path})", :cyan, :bold)}" # Why " inside "
|
219
211
|
end
|
220
212
|
|
221
|
-
|
222
|
-
#Make a lazy connection.
|
213
|
+
# Make a lazy connection.
|
223
214
|
def connection
|
224
|
-
@options[:path] =API_VERSION2 + @options[:path]
|
215
|
+
@options[:path] = API_VERSION2 + @options[:path]
|
225
216
|
encoded_api_header = encode_header(@options)
|
226
|
-
@options[:headers] = HEADERS.merge(
|
227
|
-
|
228
|
-
X_Megam_DATE => encoded_api_header[:date],
|
229
|
-
}).merge(@options[:headers])
|
230
|
-
|
217
|
+
@options[:headers] = HEADERS.merge(X_Megam_HMAC => encoded_api_header[:hmac],
|
218
|
+
X_Megam_DATE => encoded_api_header[:date]).merge(@options[:headers])
|
231
219
|
|
232
|
-
Megam::Log.debug(
|
220
|
+
Megam::Log.debug('HTTP Request Data:')
|
233
221
|
Megam::Log.debug("> HTTP #{@options[:scheme]}://#{@options[:host]}")
|
234
222
|
@options.each do |key, value|
|
235
223
|
Megam::Log.debug("> #{key}: #{value}")
|
236
224
|
end
|
237
|
-
Megam::Log.debug(
|
238
|
-
if @options[:scheme] ==
|
239
|
-
|
225
|
+
Megam::Log.debug('End HTTP Request Data.')
|
226
|
+
if @options[:scheme] == 'https'
|
227
|
+
@connection = Excon.new("#{@options[:scheme]}://#{@options[:host]}", @options)
|
240
228
|
else
|
241
|
-
|
242
|
-
|
229
|
+
Excon.defaults[:ssl_verify_peer] = false
|
230
|
+
@connection = Excon.new("#{@options[:scheme]}://#{@options[:host]}:9000", @options)
|
243
231
|
end
|
244
232
|
@connection
|
245
233
|
end
|
@@ -252,21 +240,19 @@ module Megam
|
|
252
240
|
# :date
|
253
241
|
# (Refer https://github.com/indykish/megamplay.git/test/AuthenticateSpec.scala)
|
254
242
|
def encode_header(cmd_parms)
|
255
|
-
header_params ={}
|
243
|
+
header_params = {}
|
256
244
|
body_digest = OpenSSL::Digest::MD5.digest(cmd_parms[:body])
|
257
245
|
body_base64 = Base64.encode64(body_digest)
|
258
246
|
|
259
|
-
current_date = Time.now.strftime(
|
247
|
+
current_date = Time.now.strftime('%Y-%m-%d %H:%M')
|
260
248
|
|
261
|
-
data="#{current_date}"+"\n"+"#{cmd_parms[:path]}"+"\n"+"#{body_base64}"
|
249
|
+
data = "#{current_date}" + "\n" + "#{cmd_parms[:path]}" + "\n" + "#{body_base64}"
|
262
250
|
|
263
251
|
digest = OpenSSL::Digest.new('sha1')
|
264
252
|
movingFactor = data.rstrip!
|
265
253
|
hash = OpenSSL::HMAC.hexdigest(digest, @api_key, movingFactor)
|
266
|
-
final_hmac = @email+':' + hash
|
267
|
-
header_params = { :
|
254
|
+
final_hmac = @email + ':' + hash
|
255
|
+
header_params = { hmac: final_hmac, date: current_date }
|
268
256
|
end
|
269
|
-
|
270
257
|
end
|
271
|
-
|
272
258
|
end
|
data/lib/megam/api/version.rb
CHANGED