Class: Bosh::Cpi::RegistryClient

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/cpi/registry_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#endpointObject (readonly)

Returns the value of attribute endpoint.



8
9
10
# File 'lib/bosh/cpi/registry_client.rb', line 8

def endpoint
  @endpoint
end

#passwordObject (readonly)

Returns the value of attribute password.



10
11
12
# File 'lib/bosh/cpi/registry_client.rb', line 10

def password
  @password
end

#userObject (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

Parameters:

  • instance_id (String)

    EC2 instance id

Returns:

  • (Boolean)


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

Parameters:

  • instance_id (String)

    EC2 instance id

Returns:

  • (Hash)

    Agent settings



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.message}")
end

#update_settings(instance_id, settings) ⇒ Boolean

Update instance settings in the registry

Parameters:

  • instance_id (String)

    EC2 instance id

  • settings (Hash)

    New agent settings

Returns:

  • (Boolean)


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