Class: Clc::Client
- Inherits:
-
Object
- Object
- Clc::Client
- Defined in:
- lib/clc/client.rb
Instance Attribute Summary collapse
-
#account ⇒ Object
readonly
Returns the value of attribute account.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #create_group(params) ⇒ Object
- #create_ip_address(server_id, params) ⇒ Object
-
#create_server(params) ⇒ Object
TODO: Takes a lot of time.
- #delete_ip_address(server_id, ip_string) ⇒ Object
- #delete_server(id) ⇒ Object
- #follow(link) ⇒ Object
-
#initialize(params = {}) ⇒ Client
constructor
A new instance of Client.
- #list_datacenters ⇒ Object
- #list_groups(datacenter_id) ⇒ Object
- #list_ip_addresses(server_id) ⇒ Object
- #list_servers(datacenter_id = 'ca1') ⇒ Object
- #list_templates(datacenter_id) ⇒ Object
- #power_off_server(id) ⇒ Object
- #power_on_server(id) ⇒ Object
-
#reboot_server(id) ⇒ Object
TODO: Reboot is slower.
-
#reset_server(id) ⇒ Object
TODO: Reset is quicker.
- #show_datacenter(id, group_links = true) ⇒ Object
- #show_group(id, params = {}) ⇒ Object
- #show_operation(id) ⇒ Object
- #show_server(id, uuid = false) ⇒ Object
- #wait_for(operation_id, timeout = 1200) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/clc/client.rb', line 10 def initialize(params = {}) @connection = Faraday.new(:url => params[:endpoint] || 'https://api.ctl.io') do |builder| builder.use Clc::CloudExceptions::Handler builder.request :json builder.response :json builder.adapter Faraday.default_adapter end setup_logging(@connection.builder, params[:verbosity]) if params[:verbosity] response = @connection.post( '/v2/authentication/login', 'username' => params[:username] || ENV['CLC_USERNAME'], 'password' => params[:password] || ENV['CLC_PASSWORD'] ) @connection. :Bearer, response.body.fetch('bearerToken') @account = response.body.fetch('accountAlias') end |
Instance Attribute Details
#account ⇒ Object (readonly)
Returns the value of attribute account.
7 8 9 |
# File 'lib/clc/client.rb', line 7 def account @account end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/clc/client.rb', line 8 def connection @connection end |
Instance Method Details
#create_group(params) ⇒ Object
139 140 141 |
# File 'lib/clc/client.rb', line 139 def create_group(params) connection.post("/v2/groups/#{account}", params).body end |
#create_ip_address(server_id, params) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/clc/client.rb', line 97 def create_ip_address(server_id, params) body = connection.post( "/v2/servers/#{account}/#{server_id}/publicIPAddresses", params ).body async_response('links' => [body]) end |
#create_server(params) ⇒ Object
TODO: Takes a lot of time
56 57 58 59 |
# File 'lib/clc/client.rb', line 56 def create_server(params) body = connection.post("/v2/servers/#{account}", params).body async_response(body) end |
#delete_ip_address(server_id, ip_string) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/clc/client.rb', line 106 def delete_ip_address(server_id, ip_string) url = "/v2/servers/#{account}/#{server_id}/publicIPAddresses/#{ip_string}" body = connection.delete(url).body async_response('links' => [body]) end |
#delete_server(id) ⇒ Object
61 62 63 64 |
# File 'lib/clc/client.rb', line 61 def delete_server(id) body = connection.delete("v2/servers/#{account}/#{id}").body async_response(body) end |
#follow(link) ⇒ Object
143 144 145 |
# File 'lib/clc/client.rb', line 143 def follow(link) connection.get(link['href']).body end |
#list_datacenters ⇒ Object
31 32 33 |
# File 'lib/clc/client.rb', line 31 def list_datacenters connection.get("v2/datacenters/#{@account}").body end |
#list_groups(datacenter_id) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/clc/client.rb', line 131 def list_groups(datacenter_id) datacenter = show_datacenter(datacenter_id, true) root_group_link = datacenter['links'].detect { |link| link['rel'] == 'group' } flatten_groups(show_group(root_group_link['id'])) end |
#list_ip_addresses(server_id) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/clc/client.rb', line 113 def list_ip_addresses(server_id) server = show_server(server_id) ip_links = server['links'].select do |link| link['rel'] == 'publicIPAddress' end ip_links.map { |link| follow(link).merge('id' => link['id']) } end |
#list_servers(datacenter_id = 'ca1') ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/clc/client.rb', line 39 def list_servers(datacenter_id = 'ca1') datacenter = show_datacenter(datacenter_id) group_links = datacenter['links'].select { |l| l['rel'] == 'group' } groups = group_links.map do |link| group = connection.get("v2/groups/#{@account}/#{link['id']}?serverDetail=detailed").body flatten_groups(group) end.flatten groups.map { |group| group['servers'] }.flatten.compact end |
#list_templates(datacenter_id) ⇒ Object
92 93 94 95 |
# File 'lib/clc/client.rb', line 92 def list_templates(datacenter_id) url = "/v2/datacenters/#{account}/#{datacenter_id}/deploymentCapabilities" connection.get(url).body.fetch('templates') end |
#power_off_server(id) ⇒ Object
86 87 88 89 90 |
# File 'lib/clc/client.rb', line 86 def power_off_server(id) response = connection.post("/v2/operations/#{account}/servers/powerOff", [id]) body = response.body.first async_response(body) end |
#power_on_server(id) ⇒ Object
80 81 82 83 84 |
# File 'lib/clc/client.rb', line 80 def power_on_server(id) response = connection.post("/v2/operations/#{account}/servers/powerOn", [id]) body = response.body.first async_response(body) end |
#reboot_server(id) ⇒ Object
TODO: Reboot is slower. Looks like OS-level reboot
74 75 76 77 78 |
# File 'lib/clc/client.rb', line 74 def reboot_server(id) response = connection.post("/v2/operations/#{account}/servers/reboot", [id]) body = response.body.first async_response(body) end |
#reset_server(id) ⇒ Object
TODO: Reset is quicker. Probably ‘hard-reset’
67 68 69 70 71 |
# File 'lib/clc/client.rb', line 67 def reset_server(id) response = connection.post("/v2/operations/#{account}/servers/reset", [id]) body = response.body.first async_response(body) end |
#show_datacenter(id, group_links = true) ⇒ Object
35 36 37 |
# File 'lib/clc/client.rb', line 35 def show_datacenter(id, group_links = true) connection.get("v2/datacenters/#{@account}/#{id}?groupLinks=#{group_links}").body end |
#show_group(id, params = {}) ⇒ Object
127 128 129 |
# File 'lib/clc/client.rb', line 127 def show_group(id, params = {}) connection.get("v2/groups/#{account}/#{id}", params).body end |
#show_operation(id) ⇒ Object
123 124 125 |
# File 'lib/clc/client.rb', line 123 def show_operation(id) connection.get("v2/operations/#{account}/status/#{id}").body end |
#show_server(id, uuid = false) ⇒ Object
51 52 53 |
# File 'lib/clc/client.rb', line 51 def show_server(id, uuid = false) connection.get("/v2/servers/#{@account}/#{id}?uuid=#{uuid}").body end |
#wait_for(operation_id, timeout = 1200) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/clc/client.rb', line 147 def wait_for(operation_id, timeout = 1200) expire_at = Time.now + timeout loop do operation = show_operation(operation_id) status = operation['status'] yield status if block_given? case status when 'succeeded' then return true when 'failed', 'unknown' then raise 'Operation Failed' # reason? when 'executing', 'resumed', 'notStarted' raise 'Operation takes too much time to complete' if Time.now > expire_at next sleep(2) else raise "Operation status unknown: #{status}" end end end |