Class: VagrantPlugins::Openstack::GlanceClient

Inherits:
Object
  • Object
show all
Includes:
Singleton, Domain, HttpUtils
Defined in:
lib/vagrant-openstack-illuin-provider/client/glance.rb

Instance Method Summary collapse

Methods included from HttpUtils

#delete, #get, #post

Methods included from HttpUtils::RequestLogger

#log_request, #log_response

Constructor Details

#initializeGlanceClient

Returns a new instance of GlanceClient.



15
16
17
18
# File 'lib/vagrant-openstack-illuin-provider/client/glance.rb', line 15

def initialize
  @logger = Log4r::Logger.new('vagrant_openstack::glance')
  @session = VagrantPlugins::Openstack.session
end

Instance Method Details

#get_all_images(env) ⇒ Object

Endpoint /images exists on both v1 and v2 API The attribute ‘visibility’ is used to detect if the call has been made on v1 or v2

In case of v2 we have all the needed information, but in case of v1 we don’t and we have to call /images/detail to get full details



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vagrant-openstack-illuin-provider/client/glance.rb', line 45

def get_all_images(env)
  images_json = get(env, "#{@session.endpoints[:image]}/images")
  images = JSON.parse(images_json)['images']

  return images if images.empty?

  is_v1 = false
  unless images[0].key? 'visibility'
    is_v1 = true
    images_json = get(env, "#{@session.endpoints[:image]}/images/detail")
    images = JSON.parse(images_json)['images']
  end

  images.map do |i|
    i['visibility'] = i['is_public'] ? 'public' : 'private' if is_v1
    Image.new(i['id'], i['name'], i['visibility'], i['size'], i['min_ram'], i['min_disk'])
  end
end

#get_api_version_list(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vagrant-openstack-illuin-provider/client/glance.rb', line 20

def get_api_version_list(env)
  json = RestUtils.get(env, @session.endpoints[:image],
                       'X-Auth-Token' => @session.token,
                       :accept => :json) do |response|
    log_response(response)
    case response.code
    when 200, 300
      response
    when 401
      fail Errors::AuthenticationFailed
    else
      fail Errors::VagrantOpenstackError, message: response.to_s
    end
  end
  JSON.parse(json)['versions']
end