Class: Cloudcost::ApiConnection
- Inherits:
-
Object
- Object
- Cloudcost::ApiConnection
- Defined in:
- lib/cloudcost/api_connection.rb
Overview
Connecting to and accessing the cloudscale.ch API
Constant Summary collapse
- API_URL =
"https://api.cloudscale.ch"
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
Instance Method Summary collapse
- #get_resource(resource, options = {}) ⇒ Object
- #get_servers(options = {}) ⇒ Object
- #get_volumes(options = {}) ⇒ Object
-
#initialize(api_token, options = {}) ⇒ ApiConnection
constructor
A new instance of ApiConnection.
- #set_server_tags(uuid, tags) ⇒ Object
Constructor Details
#initialize(api_token, options = {}) ⇒ ApiConnection
Returns a new instance of ApiConnection.
13 14 15 16 17 |
# File 'lib/cloudcost/api_connection.rb', line 13 def initialize(api_token, = {}) @api_url = [:api_url] || API_URL @api_token = api_token @connection = new_connection end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
11 12 13 |
# File 'lib/cloudcost/api_connection.rb', line 11 def connection @connection end |
Instance Method Details
#get_resource(resource, options = {}) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/cloudcost/api_connection.rb', line 19 def get_resource(resource, = {}) path = "v1/#{resource}" path += "?tag:#{[:tag]}" if [:tag] response = @connection.get(path: path, expects: [200]) JSON.parse(response.body, symbolize_names: true) end |
#get_servers(options = {}) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/cloudcost/api_connection.rb', line 26 def get_servers( = {}) servers = get_resource("servers", ) servers = servers.reject { |server| server[:tags].key?([:missing_tag].to_sym) } if [:missing_tag] servers = servers.select { |server| /#{[:name]}/.match? server[:name] } if [:name] servers end |
#get_volumes(options = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cloudcost/api_connection.rb', line 42 def get_volumes( = {}) volumes = get_resource("volumes", ) volumes = volumes.reject { |volume| volume[:tags].key?([:missing_tag].to_sym) } if [:missing_tag] volumes = volumes.select { |volume| /#{[:name]}/.match? volume[:name] } if [:name] volumes = volumes.select { |volume| /#{[:type]}/.match? volume[:type] } if [:type] unless [:attached].nil? volumes = volumes.select do |volume| (volume[:servers].size.positive?) == [:attached] end end volumes end |
#set_server_tags(uuid, tags) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/cloudcost/api_connection.rb', line 33 def (uuid, ) @connection.patch( path: "v1/servers/#{uuid}", body: { tags: }.to_json, headers: { "Content-Type": "application/json" }, expects: [204] ) end |