class Podnix::API
Constants
- API_VERSION1
- HEADERS
- OPTIONS
- QUERY
- VERSION
Attributes
text[RW]
text is used to print stuff in the terminal (message, log, info, warn etc.)
Public Class Methods
new(options={})
click to toggle source
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 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.
# File lib/podnix/api.rb, line 72 def initialize(options={}) @options = OPTIONS.merge(options) @key = options[:key] @options[:query] = QUERY.merge(options) raise ArgumentError, "You must specify podnix_api_key in knife.rb or ENV['PODNIX_API_KEY']" if @key.empty? && !ENV['PODNIX_API_KEY'] end
Public Instance Methods
add_server(new_server)
click to toggle source
# 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)
click to toggle source
Yet to be tested
DELETE /nodes/:node_id
# 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()
click to toggle source
# 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()
click to toggle source
GET /servers
# 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)
click to toggle source
# 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()
click to toggle source
GET /servers
# 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()
click to toggle source
# File lib/podnix/api.rb, line 61 def last_response @last_response end
request(params,&block)
click to toggle source
# 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.message, 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)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
connection()
click to toggle source
Make a lazy connection.
# File lib/podnix/api.rb, line 156 def connection if ENV['PODNIX_API_KEY'] && @options[:query][:key].empty? QUERY[:key]="#{ENV['PODNIX_API_KEY']}" @options[:query] = @options[:query].merge(QUERY) end @options[:headers] = HEADERS.merge(@options[:headers]) #SSL certificate file paths #If ssl_ca_path and file specified shows error #Only file pass through #Excon.defaults[:ssl_ca_path] = "/etc/ssl/certs" #ENV['SSL_CERT_DIR'] = "/etc/ssl/certs" Excon.defaults[:ssl_ca_file] = File.expand_path(File.join(File.dirname(__FILE__), "..", "certs", "cacert.pem")) #ENV['SSL_CERT_FILE'] = File.expand_path(File.join(File.dirname(__FILE__), "..", "certs", "cacert.pem")) if !File.exist?(File.expand_path(File.join(File.dirname(__FILE__), "..", "certs", "cacert.pem"))) text.warn("Certificate file does not exist. SSL_VERIFY_PEER set as false") Excon.defaults[:ssl_verify_peer] = false #elsif !File.readable_real?(File.expand_path(File.join(File.dirname(__FILE__), "..", "certs", "test.pem"))) # text.warn("Certificate file is readable. SSL_VERIFY_PEER set as false") # Excon.defaults[:ssl_verify_peer] = false else text.info("Certificate found") Excon.defaults[:ssl_verify_peer] = true end text.info("HTTP Request Data:") text.msg("> HTTP #{@options[:scheme]}://#{@options[:host]}/#{@options[:query]}") @options.each do |key, value| text.msg("> #{key}: #{value}") end text.info("End HTTP Request Data.") @connection = Excon.new("#{@options[:scheme]}://#{@options[:host]}",@options) end