Class: Fog::Image::OpenStack::Real

Inherits:
Object
  • Object
show all
Includes:
OpenStack::Core
Defined in:
lib/fog/openstack/image.rb,
lib/fog/openstack/requests/image/get_image.rb,
lib/fog/openstack/requests/image/set_tenant.rb,
lib/fog/openstack/requests/image/create_image.rb,
lib/fog/openstack/requests/image/delete_image.rb,
lib/fog/openstack/requests/image/update_image.rb,
lib/fog/openstack/requests/image/get_image_by_id.rb,
lib/fog/openstack/requests/image/get_image_members.rb,
lib/fog/openstack/requests/image/get_shared_images.rb,
lib/fog/openstack/requests/image/list_public_images.rb,
lib/fog/openstack/requests/image/add_member_to_image.rb,
lib/fog/openstack/requests/image/update_image_members.rb,
lib/fog/openstack/requests/image/remove_member_from_image.rb,
lib/fog/openstack/requests/image/list_public_images_detailed.rb

Instance Attribute Summary

Attributes included from OpenStack::Core

#auth_token, #auth_token_expiration, #current_tenant, #current_user, #current_user_id, #openstack_domain_id, #openstack_domain_name, #openstack_project_domain, #openstack_project_domain_id, #openstack_user_domain, #openstack_user_domain_id

Instance Method Summary collapse

Methods included from OpenStack::Core

#credentials, #initialize_identity, #reload

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fog/openstack/image.rb', line 99

def initialize(options={})
  initialize_identity options

  @openstack_service_type           = options[:openstack_service_type] || ['image']
  @openstack_service_name           = options[:openstack_service_name]
  @openstack_endpoint_type          = options[:openstack_endpoint_type] || 'adminURL'

  @connection_options               = options[:connection_options] || {}

  authenticate

  unless @path.match(SUPPORTED_VERSIONS)
    @path = "/" + Fog::OpenStack.get_supported_version(SUPPORTED_VERSIONS,
                                                       @openstack_management_uri,
                                                       @auth_token,
                                                       @connection_options)
  end

  @persistent = options[:persistent] || false
  @connection = Fog::Core::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

Instance Method Details

#add_member_to_image(image_id, tenant_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/openstack/requests/image/add_member_to_image.rb', line 5

def add_member_to_image(image_id, tenant_id)
  request(
    :expects => [200, 204],
    :method  => 'PUT',
    :path    => "images/#{image_id}/members/#{tenant_id}"
  )
end

#create_image(attributes) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/openstack/requests/image/create_image.rb', line 5

def create_image(attributes)
  data = {
    'Content-Type'                  =>'application/octet-stream',
    'x-image-meta-name'             => attributes[:name],
    'x-image-meta-disk-format'      => attributes[:disk_format],
    'x-image-meta-container-format' => attributes[:container_format],
    'x-image-meta-size'             => attributes[:size],
    'x-image-meta-is-public'        => attributes[:is_public],
    'x-image-meta-min-ram'          => attributes[:min_ram],
    'x-image-meta-min-disk'         => attributes[:min_disk],
    'x-image-meta-checksum'         => attributes[:checksum],
    'x-image-meta-owner'            => attributes[:owner],
    'x-glance-api-copy-from'        => attributes[:copy_from]
  }.reject {|k,v| v.nil? }

  body = String.new
  if attributes[:location]
    body = File.open(attributes[:location], "rb")
    # Make sure the image file size is always present
    data['x-image-meta-size'] = File.size(body)
  end

  unless attributes[:properties].nil?
    attributes[:properties].each do |key,value|
      data["x-image-meta-property-#{key}"] = value
    end
  end

  request(
    :headers  => data,
    :body     => body,
    :expects  => 201,
    :method   => 'POST',
    :path     => "images"
  )
ensure
  body.close if body.respond_to?(:close)
end

#delete_image(image_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/openstack/requests/image/delete_image.rb', line 5

def delete_image(image_id)
  request(
    :expects  => 200,
    :method   => 'DELETE',
    :path     => "images/#{image_id}"
  )
end

#get_image(image_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/openstack/requests/image/get_image.rb', line 5

def get_image(image_id)
  request(
    :expects => [200, 204],
    :method  => 'HEAD',
    :path    => "images/#{image_id}"
  )
end

#get_image_by_id(image_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/openstack/requests/image/get_image_by_id.rb', line 5

def get_image_by_id(image_id)
  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => "images/#{image_id}"
  )
end

#get_image_members(image_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/openstack/requests/image/get_image_members.rb', line 5

def get_image_members(image_id)
  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => "images/#{image_id}/members"
  )
end

#get_shared_images(tenant_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/openstack/requests/image/get_shared_images.rb', line 5

def get_shared_images(tenant_id)
  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => "shared-images/#{tenant_id}"
  )
end

#list_public_images(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/image/list_public_images.rb', line 5

def list_public_images(options = {})
  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => 'images',
    :query   => options
  )
end

#list_public_images_detailed(options = {}, query_deprecated = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fog/openstack/requests/image/list_public_images_detailed.rb', line 5

def list_public_images_detailed(options = {}, query_deprecated = nil)
  if options.is_a?(Hash)
    query = options
  elsif options
    Fog::Logger.deprecation("Calling OpenStack[:glance].list_public_images_detailed(attribute, query) format"\
                            " is deprecated, call .list_public_images_detailed(attribute => query) instead")
    query = { options => query_deprecated }
  else
    query = {}
  end

  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => 'images/detail',
    :query   => query
  )
end

#remove_member_from_image(image_id, member_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/openstack/requests/image/remove_member_from_image.rb', line 5

def remove_member_from_image(image_id, member_id)
  request(
    :expects => [200, 204],
    :method  => 'DELETE',
    :path    => "images/#{image_id}/members/#{member_id}"
  )
end

#request(params) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fog/openstack/image.rb', line 121

def request(params)
  begin
    response = @connection.request(params.merge({
      :headers  => {
        'Content-Type' => 'application/json',
        'X-Auth-Token' => @auth_token
      }.merge!(params[:headers] || {}),
      :path     => "#{@path}/#{params[:path]}"#,
    }))
  rescue Excon::Errors::Unauthorized => error
    if error.response.body != 'Bad username or password' # token expiration
      @openstack_must_reauthenticate = true
      authenticate
      retry
    else # bad credentials
      raise error
    end
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
    when Excon::Errors::NotFound
      Fog::Compute::OpenStack::NotFound.slurp(error)
    else
      error
    end
  end
  unless response.body.empty?
    response.body = Fog::JSON.decode(response.body)
  end
  response
end

#set_tenant(tenant) ⇒ Object



5
6
7
8
9
# File 'lib/fog/openstack/requests/image/set_tenant.rb', line 5

def set_tenant(tenant)
  @openstack_must_reauthenticate = true
  @openstack_tenant = tenant.to_s
  authenticate
end

#update_image(attributes) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/openstack/requests/image/update_image.rb', line 5

def update_image(attributes)
  data = {
    'x-image-meta-name'             => attributes[:name],
    'x-image-meta-disk-format'      => attributes[:disk_format],
    'x-image-meta-container-format' => attributes[:container_format],
    'x-image-meta-size'             => attributes[:size],
    'x-image-meta-is-public'        => attributes[:is_public],
    'x-image-meta-min-ram'          => attributes[:min_ram],
    'x-image-meta-min-disk'         => attributes[:min_disk],
    'x-image-meta-checksum'         => attributes[:checksum],
    'x-image-meta-owner'            => attributes[:owner]
  }.reject {|k,v| v.nil? }

  unless attributes[:properties].nil?
    attributes[:properties].each do |key,value|
      data["x-image-meta-property-#{key}"] = value
    end
  end

  request(
    :headers  => data,
    :expects  => 200,
    :method   => 'PUT',
    :path     => "images/#{attributes[:id]}"
  )
end

#update_image_members(image_id, members) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/openstack/requests/image/update_image_members.rb', line 5

def update_image_members(image_id, members)
  # Sample members
  # [
  #   {'member_id' => 'tenant1', 'can_share' => true  },
  #   {'member_id' => 'tenant2', 'can_share' => false }
  # ]
  data = { 'memberships' => members }

  request(
    :body    => Fog::JSON.encode(data),
    :expects => [204],
    :method  => 'PUT',
    :path    => "images/#{image_id}/members"
  )
end