class ScalrAPI

Public Class Methods

new(url, key_id, key_secret) click to toggle source
# File lib/kitchen/driver/ScalrAPI.rb, line 9
def initialize(url, key_id, key_secret)

        @api_url = url
        @api_key_id = key_id
        @api_key_secret = key_secret

end

Public Instance Methods

create(url, data) click to toggle source

Create item in API

# File lib/kitchen/driver/ScalrAPI.rb, line 109
def create(url, data)
        response = self.request('POST', url, data)
        return response['data']
end
delete(url) click to toggle source

Delete item from API

# File lib/kitchen/driver/ScalrAPI.rb, line 115
def delete(url)
        response = self.request('DELETE', url)
        return true   
end
edit(url, data) click to toggle source

Edit items in API via PATCH

# File lib/kitchen/driver/ScalrAPI.rb, line 127
def edit(url, data)
        response = self.request('PATCH', url, data)
        return response['data']
end
fetch(url) click to toggle source

Fetch a single item from API

# File lib/kitchen/driver/ScalrAPI.rb, line 103
def fetch(url)
        response = self.request('GET', url)
        return response['data']
end
list(url) click to toggle source

List items from API

# File lib/kitchen/driver/ScalrAPI.rb, line 89
def list(url)
        data = []
        
        while url != nil do
                response = self.request('GET', url)
        
                data.concat response['data']
                url = response['pagination']['next']
        end
        
        return data
end
post(url, data) click to toggle source

Edit items in API

# File lib/kitchen/driver/ScalrAPI.rb, line 121
def post(url, data)
        response = self.request('POST', url, data)
        return response['data']
end
request(method, url, body='') click to toggle source
# File lib/kitchen/driver/ScalrAPI.rb, line 17
def request(method, url, body='')

        #JSON encode body if set
        if body != ''
                body = body.to_json
        end
        
        #Split URL into components
        parts = URI.parse(@api_url + url)
        
        path = parts.path
        host = parts.host
        port = parts.port

        query = ''
        if parts.query != nil
                #Convert querystring into an array
                q = parts.query.split('&')
                
                #Sort the querystring array
                q.sort!
                
                #Convert querystring array back to string
                query = q.join('&')
        end
        
        #Create ISO 8601 date/time string
        time = Time.now.utc.iso8601 + '+00:00'
        
        #Collection of request data for generating signature
        request = [
                method,
                time,
                path,
                query,
                body
        ]
        
        #Calculate signature based on request data
        signature = 'V1-HMAC-SHA256 ' + Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @api_key_secret, request.join("\n"))).strip()
        
        #HTTP request headers
        headers = {
                'X-Scalr-Key-Id' => @api_key_id,
                'X-Scalr-Signature' => signature,
                'X-Scalr-Date' => time,
                'X-Scalr-Debug' => '1'
        }
        
        if body != ""
                headers['Content-Type'] = 'application/json'
        end
        begin
                response = ::RestClient::Request.execute(
                        :method => method, 
                        :url => @api_url + url,
                        :headers => headers,
                        :payload  => body
                )
        rescue RestClient::ExceptionWithResponse => e
                puts "Scalr server returned an error: "
                puts e.response
                raise e
        end
        if response == ""
                response = "{}"
        end
        return JSON.parse(response)

end