Class: Openstack::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/openstack-client.rb,
lib/openstack-client/service_catalog.rb

Direct Known Subclasses

Keystone::V2_0::Client

Defined Under Namespace

Classes: Manager, Resource, ServiceCatalog

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keystone_or_options, options = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/openstack-client.rb', line 13

def initialize keystone_or_options, options={}
  require 'keystone/v2_0/client'
  if keystone_or_options.kind_of?(Keystone::V2_0::Client)
    keystone = keystone_or_options
    endpoint_type = options[:endpoint_type] || 'publicURL'
    self.management_url = keystone.service_catalog.url_for(service_type: 'image', endpoint_type: endpoint_type)
    self.auth_token     = keystone.service_catalog.token['id']
  else
    options = keystone_or_options
    self.management_url = options[:endpoint]
    self.auth_token     = options[:token]
  end

  raise 'endpoint or Keystone::V2_0::Client object was required.' unless self.management_url
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



9
10
11
# File 'lib/openstack-client.rb', line 9

def auth_token
  @auth_token
end

#management_urlObject

Returns the value of attribute management_url.



11
12
13
# File 'lib/openstack-client.rb', line 11

def management_url
  @management_url
end

Instance Method Details

#delete(url, headers = {}) ⇒ Object



41
42
43
# File 'lib/openstack-client.rb', line 41

def delete url, headers={}
  self.execute(method: :delete, url: url, headers: headers)
end

#execute(options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/openstack-client.rb', line 45

def execute options
  self.authenticate unless self.management_url

  options = options.dup

  options[:url] = self.management_url + options[:url]

  options[:headers] ||= {}
  options[:headers]['X-Auth-Token'] = self.auth_token if self.auth_token
  options[:headers]['User-Agent']   = 'ruby-openstack-client'
  options[:headers][:accept]        = :json

  if options[:payload]
    options[:headers][:content_type] = :json
    options[:payload] = options[:payload].to_json
  end

  response = RestClient::Request.execute(options)

  if [400, 401, 403, 404, 408, 409, 413, 500, 501].include?(response.code)
    raise "HTTP Error: #{response.code}"
  elsif [301, 302, 305].include?(response.code)
    options[:url] = response.headers[:location]
    return self.execute(options)
  end

  return [response, JSON.parse(response.to_s)]
end

#get(url, headers = {}) ⇒ Object



29
30
31
# File 'lib/openstack-client.rb', line 29

def get url, headers={}
  res = self.execute(method: :get, url: url, headers: headers)
end

#post(url, payload, headers = {}) ⇒ Object



33
34
35
# File 'lib/openstack-client.rb', line 33

def post url, payload, headers={}
  self.execute(method: :post, url: url, headers: headers, payload: payload)
end

#put(url, payload, headers = {}) ⇒ Object



37
38
39
# File 'lib/openstack-client.rb', line 37

def put url, payload, headers={}
  self.execute(method: :put, url: url, headers: headers, payload: payload)
end