Class: Bosh::Cpi::RegistryClient
- Inherits:
-
Object
- Object
- Bosh::Cpi::RegistryClient
- Defined in:
- lib/bosh/cpi/registry_client.rb
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#delete_settings(instance_id) ⇒ Boolean
Delete instance settings from the registry.
-
#initialize(endpoint, user, password) ⇒ RegistryClient
constructor
A new instance of RegistryClient.
-
#read_settings(instance_id) ⇒ Hash
Read instance settings from the registry.
-
#update_settings(instance_id, settings) ⇒ Boolean
Update instance settings in the registry.
Constructor Details
#initialize(endpoint, user, password) ⇒ RegistryClient
Returns a new instance of RegistryClient.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/bosh/cpi/registry_client.rb', line 12 def initialize(endpoint, user, password) @endpoint = endpoint unless @endpoint =~ /^http:\/\// @endpoint = "http://#{@endpoint}" end @user = user @password = password auth = Base64.encode64("#{@user}:#{@password}").gsub("\n", '') @headers = { "Accept" => 'application/json', "Authorization" => "Basic #{auth}" } @client = HTTPClient.new end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
8 9 10 |
# File 'lib/bosh/cpi/registry_client.rb', line 8 def endpoint @endpoint end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
10 11 12 |
# File 'lib/bosh/cpi/registry_client.rb', line 10 def password @password end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
9 10 11 |
# File 'lib/bosh/cpi/registry_client.rb', line 9 def user @user end |
Instance Method Details
#delete_settings(instance_id) ⇒ Boolean
Delete instance settings from the registry
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/bosh/cpi/registry_client.rb', line 88 def delete_settings(instance_id) url = "#{@endpoint}/instances/#{instance_id}/settings" response = @client.delete(url, {:header => @headers}) unless [200, 404].include? response.status cloud_error("Cannot delete settings for '#{instance_id}', got HTTP #{response.status}") end true end |
#read_settings(instance_id) ⇒ Hash
Read instance settings from the registry
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 |
# File 'lib/bosh/cpi/registry_client.rb', line 58 def read_settings(instance_id) url = "#{@endpoint}/instances/#{instance_id}/settings" response = @client.get(url, {:header => @headers}) if response.status != 200 cloud_error("Cannot read settings for '#{instance_id}', got HTTP #{response.status}") end body = JSON.load(response.body) unless body.is_a?(Hash) cloud_error("Invalid registry response, Hash expected, got #{body.class}: #{body}") end settings = JSON.load(body["settings"]) unless settings.is_a?(Hash) cloud_error("Invalid settings format, Hash expected, got #{settings.class}: #{settings}") end settings rescue JSON::ParserError => e cloud_error("Cannot parse settings for '#{instance_id}': #{e.}") end |
#update_settings(instance_id, settings) ⇒ Boolean
Update instance settings in the registry
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bosh/cpi/registry_client.rb', line 37 def update_settings(instance_id, settings) unless settings.is_a?(Hash) raise ArgumentError, "Invalid settings format, Hash expected, #{settings.class} given" end payload = JSON.dump(settings) url = "#{@endpoint}/instances/#{instance_id}/settings" response = @client.put(url, {:body => payload, :header => @headers}) unless HTTP::Status.successful?(response.status) cloud_error("Cannot update settings for '#{instance_id}', got HTTP #{response.status}") end true end |