Class: Bosh::Registry::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/registry/client.rb

Overview

Represents Bosh Registry Client. It performs CRUD operations against the OpenStack Registry.

Settings example: settings = {

"vm" => {
  "name" => server_name
},
"agent_id" => agent_id,
"networks" => network_spec,
"disks" => {
  "system" => "/dev/vda",
  "ephemeral" => "/dev/vdb",
  "persistent" => {"volume_id" => device_name}
}

}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, user, password) ⇒ Client

Returns a new instance of Client.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bosh/registry/client.rb', line 30

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.



26
27
28
# File 'lib/bosh/registry/client.rb', line 26

def endpoint
  @endpoint
end

#passwordObject (readonly)

Returns the value of attribute password.



28
29
30
# File 'lib/bosh/registry/client.rb', line 28

def password
  @password
end

#userObject (readonly)

Returns the value of attribute user.



27
28
29
# File 'lib/bosh/registry/client.rb', line 27

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)


106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bosh/registry/client.rb', line 106

def delete_settings(instance_id)
  url = "#{@endpoint}/instances/#{instance_id}/settings"

  response = @client.delete(url, {:header => @headers})

  if response.status != 200
    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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bosh/registry/client.rb', line 76

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 = Yajl::Parser.parse(response.body)

  unless body.is_a?(Hash)
    cloud_error("Invalid registry response, Hash expected, got #{body.class}: #{body}")
  end

  settings = Yajl::Parser.parse(body["settings"])

  unless settings.is_a?(Hash)
    cloud_error("Invalid settings format, Hash expected, got #{settings.class}: #{settings}")
  end

  settings
rescue Yajl::ParseError => 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)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bosh/registry/client.rb', line 55

def update_settings(instance_id, settings)
  unless settings.is_a?(Hash)
    raise ArgumentError, "Invalid settings format, Hash expected, #{settings.class} given"
  end

  payload = Yajl::Encoder.encode(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