Class: ScalrAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/driver/ScalrAPI.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, key_id, key_secret) ⇒ ScalrAPI

Returns a new instance of ScalrAPI.



9
10
11
12
13
14
15
# 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

Instance Method Details

#create(url, data) ⇒ Object

Create item in API



109
110
111
112
# File 'lib/kitchen/driver/ScalrAPI.rb', line 109

def create(url, data)
	response = self.request('POST', url, data)
	return response['data']
end

#delete(url) ⇒ Object

Delete item from API



115
116
117
118
# File 'lib/kitchen/driver/ScalrAPI.rb', line 115

def delete(url)
	response = self.request('DELETE', url)
	return true	
end

#edit(url, data) ⇒ Object

Edit items in API via PATCH



127
128
129
130
# File 'lib/kitchen/driver/ScalrAPI.rb', line 127

def edit(url, data)
	response = self.request('PATCH', url, data)
	return response['data']
end

#fetch(url) ⇒ Object

Fetch a single item from API



103
104
105
106
# File 'lib/kitchen/driver/ScalrAPI.rb', line 103

def fetch(url)
	response = self.request('GET', url)
	return response['data']
end

#list(url) ⇒ Object

List items from API



89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object

Edit items in API



121
122
123
124
# 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 = '') ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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