Class: Podnix::API
- Inherits:
-
Object
- Object
- Podnix::API
- Defined in:
- lib/podnix/api.rb,
lib/podnix/api/errors.rb,
lib/podnix/api/images.rb,
lib/podnix/api/models.rb,
lib/podnix/api/servers.rb,
lib/podnix/api/version.rb
Defined Under Namespace
Modules: Errors
Constant Summary collapse
- HEADERS =
{ 'Accept' => 'application/json', 'Accept-Encoding' => 'gzip', 'User-Agent' => "podnix/#{Podnix::API::VERSION}", 'X-Ruby-Version' => RUBY_VERSION, 'X-Ruby-Platform' => RUBY_PLATFORM }
- QUERY =
{ }
- OPTIONS =
{ :headers => {}, :query => {}, :host => 'api.podnix.com', :nonblock => false, :scheme => 'https' }
- API_VERSION1 =
""
- VERSION =
"0.3.0"
Instance Attribute Summary collapse
-
#text ⇒ Object
text is used to print stuff in the terminal (message, log, info, warn etc.).
Instance Method Summary collapse
- #add_server(new_server) ⇒ Object
-
#delete_server(server_id) ⇒ Object
Yet to be tested DELETE /nodes/:node_id.
- #get_images ⇒ Object
-
#get_models ⇒ Object
GET /servers.
- #get_server(query) ⇒ Object
-
#get_servers ⇒ Object
GET /servers.
-
#initialize(options = {}) ⇒ API
constructor
It is assumed that every API call will use an API_KEY/email.
- #last_response ⇒ Object
- #request(params, &block) ⇒ Object
- #start_server(server_id) ⇒ Object
- #stop_server(server_id) ⇒ 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.
72 73 74 75 76 77 |
# File 'lib/podnix/api.rb', line 72 def initialize(={}) @options = OPTIONS.merge() @key = [:key] @options[:query] = QUERY.merge() raise ArgumentError, "You must specify podnix_api_key in knife.rb or ENV['PODNIX_API_KEY']" if @key.empty? && !ENV['PODNIX_API_KEY'] end |
Instance Attribute Details
#text ⇒ Object
text is used to print stuff in the terminal (message, log, info, warn etc.)
34 35 36 |
# File 'lib/podnix/api.rb', line 34 def text @text end |
Instance Method Details
#add_server(new_server) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/podnix/api/servers.rb', line 25 def add_server(new_server) #https://api.podnix.com/servers/add?&name=test&model=1&image=2&password=Secret123&ssd=1&storage=10&key=123-znsbKicQwKl4tZHQOXo3Olwls8BOrR3O @options = { :path => "/servers/add",:body => ""}.merge(@options) @options[:query] = @options[:query].merge(new_server) request( :expects => 200, :method => :post, :body => @options[:body] ) end |
#delete_server(server_id) ⇒ Object
Yet to be tested DELETE /nodes/:node_id
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/podnix/api/servers.rb', line 51 def delete_server(server_id) @options = {:path => "/servers/delete", :body => ""}.merge(@options) @options[:query]=@options[:query].merge(server_id) request( :expects => 200, :method => :delete, :body => @options[:body] ) end |
#get_images ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/podnix/api/images.rb', line 5 def get_images @options = {:path => '/images/list',:body => ""}.merge(@options) request( :expects => 200, :method => :get, :body => @options[:body] ) end |
#get_models ⇒ Object
GET /servers
5 6 7 8 9 10 11 12 13 |
# File 'lib/podnix/api/models.rb', line 5 def get_models @options = {:path => '/models/list',:body => ""}.merge(@options) request( :expects => 200, :method => :get, :body => @options[:body] ) end |
#get_server(query) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/podnix/api/servers.rb', line 15 def get_server(query) @options = {:path => "/servers/list",:body => ""}.merge(@options) @options[:query]=@options[:query].merge(query) request( :expects => 200, :method => :get, :body => @options[:body] ) end |
#get_servers ⇒ Object
GET /servers
5 6 7 8 9 10 11 12 13 |
# File 'lib/podnix/api/servers.rb', line 5 def get_servers @options = {:path => '/servers/list',:body => ""}.merge(@options) request( :expects => 200, :method => :get, :body => @options[:body] ) end |
#last_response ⇒ Object
61 62 63 |
# File 'lib/podnix/api.rb', line 61 def last_response @last_response end |
#request(params, &block) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/podnix/api.rb', line 79 def request(params,&block) start = Time.now text.msg "#{text.color("START", :cyan, :bold)}" params.each do |pkey, pvalue| text.msg("> #{pkey}: #{pvalue}") end begin response = connection.request(params, &block) rescue Excon::Errors::HTTPStatusError => error klass = case error.response.status when 401 then Podnix::API::Errors::Unauthorized when 403 then Podnix::API::Errors::Forbidden when 404 then Podnix::API::Errors::NotFound when 408 then Podnix::API::Errors::Timeout when 422 then Podnix::API::Errors::RequestFailed when 423 then Podnix::API::Errors::Locked when /50./ then Podnix::API::Errors::RequestFailed else Podnix::API::Errors::ErrorWithResponse end reerror = klass.new(error., error.response) reerror.set_backtrace(error.backtrace) text.msg "#{text.color("#{reerror.response.body}", :white)}" begin response.body = MultiJson.load(reerror.response.body.chomp) rescue end text.msg("#{text.color("RESPONSE ERR: Ruby Object", :magenta, :bold)}") text.msg "#{text.color("#{reerror.response.body}", :white, :bold)}" raise(reerror) end @last_response = response text.msg("#{text.color("RESPONSE: HTTP Status and Header Data", :magenta, :bold)}") text.msg("> HTTP #{response.remote_ip} #{response.status}") response.headers.each do |header, value| text.msg("> #{header}: #{value}") end text.info("End HTTP Status/Header Data.") if response.body && !response.body.empty? if response.headers['Content-Encoding'] == 'gzip' response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read end text.msg("#{text.color("RESPONSE: HTTP Body(JSON)", :magenta, :bold)}") text.msg "#{text.color("#{response.body}", :white)}" begin begin response.body = MultiJson.load(response.body.chomp) rescue end text.msg("#{text.color("RESPONSE: Ruby Object", :magenta, :bold)}") text.msg "#{text.color("#{response.body}", :white, :bold)}" rescue Exception => jsonerr text.error(jsonerr) raise(jsonerr) # exception = Podnix::JSONCompat.from_json(response_body) # msg = "HTTP Request Returned #{response.code} #{response.message}: " # msg << (exception["error"].respond_to?(:join) ? exception["error"].join(", ") : exception["error"].to_s) # text.error(msg) end end text.msg "#{text.color("END(#{(Time.now - start).to_s}s)", :blue, :bold)}" # reset (non-persistent) connection @connection.reset response end |
#start_server(server_id) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/podnix/api/servers.rb', line 37 def start_server(server_id) @options = {:path => "/servers/start", :body => ""}.merge(@options) @options[:query]=@options[:query].merge(server_id) request( :expects => 200, :method => :post, :body => @options[:body] ) end |
#stop_server(server_id) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/podnix/api/servers.rb', line 62 def stop_server(server_id) @options = {:path => "/servers/stop?id=#{server_id}", :body => ""}.merge(@options) @options[:query]=@options[:query].merge(server_id) request( :expects => 200, :method => :post, :body => @options[:body] ) end |