Class: Megam::API
- Inherits:
-
Object
- Object
- Megam::API
- Defined in:
- lib/megam/api.rb,
lib/megam/api/logs.rb,
lib/megam/api/csars.rb,
lib/megam/api/login.rb,
lib/megam/api/nodes.rb,
lib/megam/api/errors.rb,
lib/megam/api/predefs.rb,
lib/megam/api/sshkeys.rb,
lib/megam/api/version.rb,
lib/megam/api/accounts.rb,
lib/megam/api/appdefns.rb,
lib/megam/api/requests.rb,
lib/megam/api/boltdefns.rb,
lib/megam/api/app_request.rb,
lib/megam/api/cloud_tools.rb,
lib/megam/api/bolt_request.rb,
lib/megam/api/marketplaces.rb,
lib/megam/api/predef_clouds.rb,
lib/megam/api/marketplace_addons.rb,
lib/megam/api/cloud_tool_settings.rb
Defined Under Namespace
Modules: Errors
Constant Summary collapse
- API_MEGAM_CO =
"api.megam.co".freeze
- API_VERSION2 =
"/v2".freeze
- X_Megam_DATE =
"X-Megam-DATE".freeze
- X_Megam_HMAC =
"X-Megam-HMAC".freeze
- X_Megam_OTTAI =
"X-Megam-OTTAI".freeze
- HEADERS =
{ 'Accept' => 'application/json', 'Accept-Encoding' => 'gzip', 'User-Agent' => "megam-api/#{Megam::API::VERSION}", 'X-Ruby-Version' => RUBY_VERSION, 'X-Ruby-Platform' => RUBY_PLATFORM }
- OPTIONS =
{ :headers => {}, :host => API_MEGAM_CO, :nonblock => false, :scheme => 'https' }
- VERSION =
"0.16"
Instance Attribute Summary collapse
-
#text ⇒ Object
text is used to print stuff in the terminal (message, log, info, warn etc.).
Instance Method Summary collapse
-
#delete_marketplaceapp(node_id) ⇒ Object
Yet to be tested DELETE /marketplacess/:node_id.
-
#delete_node(node_id) ⇒ Object
Yet to be tested DELETE /nodes/:node_id.
-
#get_accounts(email) ⇒ Object
GET /accounts Yet to be tested.
-
#get_addons(node_name) ⇒ Object
=begin.
- #get_appdefn(node_name, defn_id) ⇒ Object
-
#get_appreq(node_name) ⇒ Object
=begin.
- #get_boltdefn(boltdefns_id) ⇒ Object
- #get_boltreq(node_name) ⇒ Object
- #get_cloudtool(cloudtool_name) ⇒ Object
- #get_cloudtools ⇒ Object
- #get_cloudtoolsetting(id) ⇒ Object
- #get_cloudtoolsettings ⇒ Object
- #get_csar(id) ⇒ Object
-
#get_csars ⇒ Object
GET /csars.
-
#get_logs(node_id) ⇒ Object
PUT /logs/:node_id empty body posted to get the logs path having the node_id which is the key_name to fetch that particular log.
- #get_marketplaceapp(id) ⇒ Object
-
#get_marketplaceapps ⇒ Object
GET /nodes.
- #get_node(node_id) ⇒ Object
-
#get_nodes ⇒ Object
GET /nodes.
- #get_predef(predef_name) ⇒ Object
- #get_predefcloud(predefcloud_name) ⇒ Object
- #get_predefclouds ⇒ Object
- #get_predefs ⇒ Object
- #get_request(node_name) ⇒ Object
-
#get_requests ⇒ Object
GET /requests.
- #get_sshkey(sshkey_name) ⇒ Object
- #get_sshkeys ⇒ Object
-
#initialize(options = {}) ⇒ API
constructor
It is assumed that every API call will use an API_KEY/email.
- #last_response ⇒ Object
-
#post_accounts(new_account) ⇒ Object
The body content needs to be a json.
-
#post_addon(new_addon) ⇒ Object
=end.
- #post_appdefn(new_appdefn) ⇒ Object
-
#post_appreq(new_appreq) ⇒ Object
=end.
- #post_auth ⇒ Object
- #post_boltdefn(new_boltdefn) ⇒ Object
- #post_boltreq(new_boltreq) ⇒ Object
- #post_cloudtool(new_cloudtool) ⇒ Object
- #post_cloudtoolsetting(new_cloudtoolsetting) ⇒ Object
- #post_csar(new_csar) ⇒ Object
- #post_marketplaceapp(new_node) ⇒ Object
- #post_node(new_node) ⇒ Object
- #post_predef(new_predef) ⇒ Object
- #post_predefcloud(new_predefcloud) ⇒ Object
- #post_request(new_req) ⇒ Object
- #post_sshkey(new_sshkey) ⇒ Object
- #request(params, &block) ⇒ Object
- #update_appdefn(new_appdefn) ⇒ Object
- #update_boltdefn(new_boltdefn) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ API
It is assumed that every API call will use an API_KEY/email. This ensures validity of the person really the same guy on who he claims. 3 levels of options exits
- The global OPTIONS as available inside the API (OPTIONS)
- 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 be merged into a class variable @options
- Upon merge of the options, the api_key, email as available in the @options is deleted.
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/megam/api.rb', line 126 def initialize(={}) = OPTIONS.merge() if File.exist?("#{ENV['MEGAM_HOME']}/nilavu.yml") @common = YAML.load_file("#{ENV['MEGAM_HOME']}/nilavu.yml") #COMMON YML [:host] = "#{@common["api"]["host"]}" [:scheme] = "#{@common["api"]["scheme"]}" end @api_key = .delete(:api_key) || ENV['MEGAM_API_KEY'] @email = .delete(:email) raise ArgumentError, "You must specify [:email, :api_key]" if @email.nil? || @api_key.nil? end |
Instance Attribute Details
#text ⇒ Object
text is used to print stuff in the terminal (message, log, info, warn etc.)
86 87 88 |
# File 'lib/megam/api.rb', line 86 def text @text end |
Instance Method Details
#delete_marketplaceapp(node_id) ⇒ Object
Yet to be tested DELETE /marketplacess/:node_id
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/megam/api/csars.rb', line 38 def delete_marketplaceapp(node_id) = {:path => '/marketplaces/#{node_id}', :body => ""}.merge() request( :expects => 200, :method => :delete, :body => [:body] ) end |
#delete_node(node_id) ⇒ Object
Yet to be tested DELETE /nodes/:node_id
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/megam/api/nodes.rb', line 38 def delete_node(node_id) = {:path => '/nodes/#{node_id}', :body => ""}.merge() request( :expects => 200, :method => :delete, :body => [:body] ) end |
#get_accounts(email) ⇒ Object
GET /accounts Yet to be tested
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/megam/api/accounts.rb', line 5 def get_accounts(email) = {:path => "/accounts/#{email}", :body => ''}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_addons(node_name) ⇒ Object
=begin
5 6 7 8 9 10 11 12 13 |
# File 'lib/megam/api/marketplace_addons.rb', line 5 def get_addons(node_name) = {:path => "/marketplaceaddons/#{node_name}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_appdefn(node_name, defn_id) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/megam/api/appdefns.rb', line 4 def get_appdefn(node_name) = {:path => "/appdefns/#{node_name}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_appreq(node_name) ⇒ Object
=begin
5 6 7 8 9 10 11 12 13 |
# File 'lib/megam/api/app_request.rb', line 5 def get_appreq(node_name) = {:path => "/appreqs/#{node_name}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_boltdefn(boltdefns_id) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/megam/api/boltdefns.rb', line 5 def get_boltdefn(boltdefns_id) = {:path => "/boltdefns/#{boltdefns_id}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_boltreq(node_name) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/megam/api/bolt_request.rb', line 5 def get_boltreq(node_name) = {:path => "/boltreqs/#{node_name}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_cloudtool(cloudtool_name) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/megam/api/cloud_tools.rb', line 13 def get_cloudtool(cloudtool_name) = {:path => "/cloudtools/#{cloudtool_name}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_cloudtools ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/megam/api/cloud_tools.rb', line 3 def get_cloudtools = {:path => '/cloudtools',:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_cloudtoolsetting(id) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/megam/api/cloud_tool_settings.rb', line 13 def get_cloudtoolsetting(id) = {:path => "/cloudtoolsettings/#{id}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_cloudtoolsettings ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/megam/api/cloud_tool_settings.rb', line 3 def get_cloudtoolsettings = {:path => '/cloudtoolsettings',:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_csar(id) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/megam/api/csars.rb', line 15 def get_csar(id) = {:path => "/csars/#{id}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_csars ⇒ Object
GET /csars
5 6 7 8 9 10 11 12 13 |
# File 'lib/megam/api/csars.rb', line 5 def get_csars = {:path => '/csars',:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_logs(node_id) ⇒ Object
PUT /logs/:node_id empty body posted to get the logs path having the node_id which is the key_name to fetch that particular log
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/megam/api/logs.rb', line 6 def get_logs(node_id) = {:path => "/logs/#{node_id}", :body => OkJson.encode({:nothing =>"nothing in body"})}.merge() request( :expects => 200, :method => :post, :body => [:body] ) end |
#get_marketplaceapp(id) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/megam/api/marketplaces.rb', line 15 def get_marketplaceapp(id) = {:path => "/marketplaces/#{id}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_marketplaceapps ⇒ Object
GET /nodes
5 6 7 8 9 10 11 12 13 |
# File 'lib/megam/api/marketplaces.rb', line 5 def get_marketplaceapps = {:path => '/marketplaces',:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_node(node_id) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/megam/api/nodes.rb', line 15 def get_node(node_id) = {:path => "/nodes/#{node_id}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_nodes ⇒ Object
GET /nodes
5 6 7 8 9 10 11 12 13 |
# File 'lib/megam/api/nodes.rb', line 5 def get_nodes = {:path => '/nodes',:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_predef(predef_name) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/megam/api/predefs.rb', line 13 def get_predef(predef_name) = {:path => "/predefs/#{predef_name}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_predefcloud(predefcloud_name) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/megam/api/predef_clouds.rb', line 13 def get_predefcloud(predefcloud_name) = {:path => "/predefclouds/#{predefcloud_name}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_predefclouds ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/megam/api/predef_clouds.rb', line 3 def get_predefclouds = {:path => '/predefclouds',:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_predefs ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/megam/api/predefs.rb', line 3 def get_predefs = {:path => '/predefs',:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_request(node_name) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/megam/api/requests.rb', line 15 def get_request(node_name) = {:path => "/requests/#{node_name}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_requests ⇒ Object
GET /requests
5 6 7 8 9 10 11 12 13 |
# File 'lib/megam/api/requests.rb', line 5 def get_requests = {:path => '/requests',:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_sshkey(sshkey_name) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/megam/api/sshkeys.rb', line 13 def get_sshkey(sshkey_name) = {:path => "/sshkeys/#{sshkey_name}",:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#get_sshkeys ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/megam/api/sshkeys.rb', line 3 def get_sshkeys = {:path => '/sshkeys',:body => ""}.merge() request( :expects => 200, :method => :get, :body => [:body] ) end |
#last_response ⇒ Object
115 116 117 |
# File 'lib/megam/api.rb', line 115 def last_response @last_response end |
#post_accounts(new_account) ⇒ Object
The body content needs to be a json.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/megam/api/accounts.rb', line 18 def post_accounts(new_account) = {:path => '/accounts/content', :body => Megam::JSONCompat.to_json(new_account)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_addon(new_addon) ⇒ Object
=end
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/megam/api/marketplace_addons.rb', line 15 def post_addon(new_addon) = {:path => '/marketplaceaddons/content', :body => Megam::JSONCompat.to_json(new_addon)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_appdefn(new_appdefn) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/megam/api/appdefns.rb', line 24 def post_appdefn(new_appdefn) = {:path => '/appdefns/content', :body => Megam::JSONCompat.to_json(new_appdefn)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_appreq(new_appreq) ⇒ Object
=end
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/megam/api/app_request.rb', line 15 def post_appreq(new_appreq) = {:path => '/appreqs/content', :body => Megam::JSONCompat.to_json(new_appreq)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_auth ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/megam/api/login.rb', line 3 def post_auth = {:path => '/auth', :body => ""}.merge() request( :expects => 200, :method => :post, :body => [:body] ) end |
#post_boltdefn(new_boltdefn) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/megam/api/boltdefns.rb', line 15 def post_boltdefn(new_boltdefn) = {:path => '/boltdefns/content', :body => Megam::JSONCompat.to_json(new_boltdefn)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_boltreq(new_boltreq) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/megam/api/bolt_request.rb', line 15 def post_boltreq(new_boltreq) = {:path => '/boltreqs/content', :body => Megam::JSONCompat.to_json(new_boltreq)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_cloudtool(new_cloudtool) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/megam/api/cloud_tools.rb', line 23 def post_cloudtool(new_cloudtool) = {:path => '/cloudtools/content', :body => Megam::JSONCompat.to_json(new_cloudtool)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_cloudtoolsetting(new_cloudtoolsetting) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/megam/api/cloud_tool_settings.rb', line 23 def post_cloudtoolsetting(new_cloudtoolsetting) = {:path => '/cloudtoolsettings/content', :body => Megam::JSONCompat.to_json(new_cloudtoolsetting)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_csar(new_csar) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/megam/api/csars.rb', line 25 def post_csar(new_csar) = {:path => '/csars/content', :body => Megam::JSONCompat.to_json(new_node)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_marketplaceapp(new_node) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/megam/api/marketplaces.rb', line 25 def post_marketplaceapp(new_node) = {:path => '/marketplaces/content', :body => Megam::JSONCompat.to_json(new_node)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_node(new_node) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/megam/api/nodes.rb', line 25 def post_node(new_node) = {:path => '/nodes/content', :body => Megam::JSONCompat.to_json(new_node)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_predef(new_predef) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/megam/api/predefs.rb', line 23 def post_predef(new_predef) = {:path => '/predefs/content', :body => Megam::JSONCompat.to_json(new_predef)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_predefcloud(new_predefcloud) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/megam/api/predef_clouds.rb', line 23 def post_predefcloud(new_predefcloud) = {:path => '/predefclouds/content', :body => Megam::JSONCompat.to_json(new_predefcloud)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_request(new_req) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/megam/api/requests.rb', line 25 def post_request(new_req) = {:path => '/requests/content', :body => Megam::JSONCompat.to_json(new_req)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#post_sshkey(new_sshkey) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/megam/api/sshkeys.rb', line 23 def post_sshkey(new_sshkey) = {:path => '/sshkeys/content', :body => Megam::JSONCompat.to_json(new_sshkey)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#request(params, &block) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/megam/api.rb', line 138 def request(params,&block) just_color_debug("#{@options[:path]}") start = Time.now Megam::Log.debug("START") params.each do |pkey, pvalue| Megam::Log.debug("> #{pkey}: #{pvalue}") end begin response = connection.request(params, &block) rescue Excon::Errors::HTTPStatusError => error klass = case error.response.status when 401 then Megam::API::Errors:: when 403 then Megam::API::Errors::Forbidden when 404 then Megam::API::Errors::NotFound when 408 then Megam::API::Errors::Timeout when 422 then Megam::API::Errors::RequestFailed when 423 then Megam::API::Errors::Locked when /50./ then Megam::API::Errors::RequestFailed else Megam::API::Errors::ErrorWithResponse end reerror = klass.new(error., error.response) reerror.set_backtrace(error.backtrace) Megam::Log.debug("#{reerror.response.body}") reerror.response.body = Megam::JSONCompat.from_json(reerror.response.body.chomp) Megam::Log.debug("RESPONSE ERR: Ruby Object") Megam::Log.debug("#{reerror.response.body}") raise(reerror) end @last_response = response Megam::Log.debug("RESPONSE: HTTP Status and Header Data") Megam::Log.debug("> HTTP #{response.remote_ip} #{response.status}") response.headers.each do |header, value| Megam::Log.debug("> #{header}: #{value}") end Megam::Log.debug("End HTTP Status/Header Data.") if response.body && !response.body.empty? if response.headers['Content-Encoding'] == 'gzip' Megam::Log.debug("RESPONSE: Content-Encoding is gzip") response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read end Megam::Log.debug("RESPONSE: HTTP Body(JSON)") Megam::Log.debug("#{response.body}") begin unless response.headers[X_Megam_OTTAI] response.body = Megam::JSONCompat.from_json(response.body.chomp) Megam::Log.debug("RESPONSE: Ruby Object") else response.body = Megam::KoniPai.new.koni(response.body.chomp) Megam::Log.debug("RESPONSE: KoniPai Object ") end Megam::Log.debug("#{response.body}") rescue Exception => jsonerr Megam::Log.error(jsonerr) raise(jsonerr) end end Megam::Log.debug("END(#{(Time.now - start).to_s}s)") # reset (non-persistent) connection @connection.reset response end |
#update_appdefn(new_appdefn) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/megam/api/appdefns.rb', line 35 def update_appdefn(new_appdefn) = {:path => '/appdefns/update', :body => Megam::JSONCompat.to_json(new_appdefn)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |
#update_boltdefn(new_boltdefn) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/megam/api/boltdefns.rb', line 26 def update_boltdefn(new_boltdefn) = {:path => '/boltdefns/update', :body => Megam::JSONCompat.to_json(new_boltdefn)}.merge() request( :expects => 201, :method => :post, :body => [:body] ) end |