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/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.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fog/openstack/image.rb', line 85

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_name         = options[:openstack_service_name] || ['image']

  @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.



83
84
85
# File 'lib/fog/openstack/image.rb', line 83

def current_tenant
  @current_tenant
end

#current_userObject (readonly)

Returns the value of attribute current_user.



82
83
84
# File 'lib/fog/openstack/image.rb', line 82

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-owner" => attributes[:owner],
      "x-glance-api-copy-from" => attributes[:copy_from]
  }

  body = String.new
  if attributes[:location]
    file = File.open(attributes[:location], "rb")
    body = file
  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



117
118
119
120
121
122
123
124
# File 'lib/fog/openstack/image.rb', line 117

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



126
127
128
# File 'lib/fog/openstack/image.rb', line 126

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/fog/openstack/image.rb', line 130

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
# 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-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