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

Inherits:
Object
  • Object
show all
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 collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fog/openstack/image.rb', line 94

def initialize(options={})
  require 'multi_json'

  @openstack_auth_token = options[:openstack_auth_token]

  unless @openstack_auth_token
    missing_credentials = Array.new
    @openstack_api_key  = options[:openstack_api_key]
    @openstack_username = options[:openstack_username]

    missing_credentials << :openstack_api_key  unless @openstack_api_key
    missing_credentials << :openstack_username unless @openstack_username
    raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?
  end

  @openstack_tenant               = options[:openstack_tenant]
  @openstack_auth_uri             = URI.parse(options[:openstack_auth_url])
  @openstack_management_url       = options[:openstack_management_url]
  @openstack_must_reauthenticate  = false
  @openstack_service_type         = options[:openstack_service_type] || ['image']
  @openstack_service_name         = options[:openstack_service_name]
  @openstack_endpoint_type        = options[:openstack_endpoint_type] || 'adminURL'
  @openstack_region               = options[:openstack_region]

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

  @current_user = options[:current_user]
  @current_tenant = options[:current_tenant]

  authenticate

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

Instance Attribute Details

#current_tenantObject (readonly)

Returns the value of attribute current_tenant.



92
93
94
# File 'lib/fog/openstack/image.rb', line 92

def current_tenant
  @current_tenant
end

#current_userObject (readonly)

Returns the value of attribute current_user.



91
92
93
# File 'lib/fog/openstack/image.rb', line 91

def current_user
  @current_user
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



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
43
# File 'lib/fog/openstack/requests/image/create_image.rb', line 6

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

#credentialsObject



129
130
131
132
133
134
135
136
# File 'lib/fog/openstack/image.rb', line 129

def credentials
  { :provider                 => 'openstack',
    :openstack_auth_url       => @openstack_auth_uri.to_s,
    :openstack_auth_token     => @auth_token,
    :openstack_management_url => @openstack_management_url,
    :current_user             => @current_user,
    :current_tenant           => @current_tenant }
end

#delete_image(image_id) ⇒ Object



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

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_imagesObject



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

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

#list_public_images_detailed(attribute = nil, query = nil) ⇒ Object



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

def list_public_images_detailed(attribute=nil, query=nil)

  if attribute
    path = "images/detail?#{attribute}=#{URI::encode(query)}"
  else
    path = 'images/detail'
  end

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

#reloadObject



138
139
140
# File 'lib/fog/openstack/image.rb', line 138

def reload
  @connection.reset
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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/fog/openstack/image.rb', line 142

def request(params)
  begin
    response = @connection.request(params.merge({
      :headers  => {
        'Content-Type' => 'application/json',
        'X-Auth-Token' => @auth_token
      }.merge!(params[:headers] || {}),
      :host     => @host,
      :path     => "#{@path}/#{params[:path]}"#,
      # Causes errors for some requests like tenants?limit=1
      # :query    => ('ignore_awful_caching' << Time.now.to_i.to_s)
    }))
  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 = MultiJson.decode(response.body)
  end
  response
end

#set_tenant(tenant) ⇒ Object



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

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

#update_image(attributes) ⇒ Object



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
# File 'lib/fog/openstack/requests/image/update_image.rb', line 6

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]
  }

  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    => MultiJson.encode(data),
    :expects => [200, 202],
    :method  => 'PUT',
    :path    => "images/#{image_id}/members"
  )
end