mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[openstack|image] Added image service, model and request
This commit is contained in:
parent
fc88380f4a
commit
c122d622c0
8 changed files with 318 additions and 0 deletions
|
@ -14,6 +14,7 @@ require 'fog/terremark'
|
|||
|
||||
require 'fog/compute'
|
||||
require 'fog/identity'
|
||||
require 'fog/image'
|
||||
require 'fog/cdn'
|
||||
require 'fog/dns'
|
||||
require 'fog/storage'
|
||||
|
|
24
lib/fog/image.rb
Normal file
24
lib/fog/image.rb
Normal file
|
@ -0,0 +1,24 @@
|
|||
module Fog
|
||||
module Image
|
||||
|
||||
def self.[](provider)
|
||||
self.new(:provider => provider)
|
||||
end
|
||||
|
||||
def self.new(attributes)
|
||||
attributes = attributes.dup # Prevent delete from having side effects
|
||||
case provider = attributes.delete(:provider).to_s.downcase.to_sym
|
||||
when :openstack
|
||||
require 'fog/openstack/image'
|
||||
Fog::Image::OpenStack.new(attributes)
|
||||
else
|
||||
raise ArgumentError.new("#{provider} has no identity service")
|
||||
end
|
||||
end
|
||||
|
||||
def self.providers
|
||||
Fog.services[:image]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
146
lib/fog/openstack/image.rb
Normal file
146
lib/fog/openstack/image.rb
Normal file
|
@ -0,0 +1,146 @@
|
|||
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'openstack'))
|
||||
require 'fog/openstack'
|
||||
|
||||
module Fog
|
||||
module Image
|
||||
class OpenStack < Fog::Service
|
||||
|
||||
requires :openstack_api_key, :openstack_username, :openstack_auth_url
|
||||
recognizes :openstack_auth_token, :openstack_management_url, :persistent,
|
||||
:openstack_compute_service_name, :openstack_tenant
|
||||
|
||||
model_path 'fog/openstack/models/image'
|
||||
|
||||
model :image
|
||||
collection :images
|
||||
|
||||
|
||||
request_path 'fog/openstack/requests/image'
|
||||
|
||||
request :list_public_images
|
||||
request :list_public_images_detailed
|
||||
request :get_image_by_id
|
||||
|
||||
|
||||
class Mock
|
||||
def self.data
|
||||
@data ||= Hash.new do |hash, key|
|
||||
hash[key] = {
|
||||
:users => {},
|
||||
:tenants => {}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def self.reset
|
||||
@data = nil
|
||||
end
|
||||
|
||||
def initialize(options={})
|
||||
require 'multi_json'
|
||||
@openstack_username = options[:openstack_username]
|
||||
end
|
||||
|
||||
def data
|
||||
self.class.data[@openstack_username]
|
||||
end
|
||||
|
||||
def reset_data
|
||||
self.class.data.delete(@openstack_username)
|
||||
end
|
||||
end
|
||||
|
||||
class Real
|
||||
|
||||
def initialize(options={})
|
||||
require 'multi_json'
|
||||
|
||||
@openstack_api_key = options[:openstack_api_key]
|
||||
@openstack_username = options[:openstack_username]
|
||||
@openstack_auth_uri = URI.parse(options[:openstack_auth_url])
|
||||
@openstack_auth_token = options[:openstack_auth_token]
|
||||
@openstack_management_url = options[:openstack_management_url]
|
||||
@openstack_must_reauthenticate = false
|
||||
@openstack_compute_service_name = options[:openstack_compute_service_name] || ['image']
|
||||
|
||||
@connection_options = options[:connection_options] || {}
|
||||
|
||||
authenticate
|
||||
|
||||
@persistent = options[:persistent] || false
|
||||
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
||||
end
|
||||
|
||||
def reload
|
||||
@connection.reset
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
private
|
||||
|
||||
def authenticate
|
||||
if @openstack_must_reauthenticate || @openstack_auth_token.nil?
|
||||
options = {
|
||||
:openstack_api_key => @openstack_api_key,
|
||||
:openstack_username => @openstack_username,
|
||||
:openstack_auth_uri => @openstack_auth_uri,
|
||||
:openstack_compute_service_name => @openstack_compute_service_name,
|
||||
:openstack_endpoint_type => 'adminURL'
|
||||
}
|
||||
|
||||
credentials = Fog::OpenStack.authenticate_v2(options, @connection_options)
|
||||
|
||||
@openstack_must_reauthenticate = false
|
||||
@auth_token = credentials[:token]
|
||||
@openstack_management_url = credentials[:server_management_url]
|
||||
uri = URI.parse(@openstack_management_url)
|
||||
else
|
||||
@auth_token = @openstack_auth_token
|
||||
uri = URI.parse(@openstack_management_url)
|
||||
end
|
||||
|
||||
@host = uri.host
|
||||
@path = uri.path
|
||||
@path.sub!(/\/$/, '')
|
||||
@port = uri.port
|
||||
@scheme = uri.scheme
|
||||
true
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
22
lib/fog/openstack/models/image/image.rb
Normal file
22
lib/fog/openstack/models/image/image.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Image
|
||||
class OpenStack
|
||||
|
||||
class Image < Fog::Model
|
||||
|
||||
identity :id
|
||||
|
||||
attribute :name
|
||||
attribute :size
|
||||
attribute :disk_format
|
||||
attribute :container_format
|
||||
attribute :id
|
||||
attribute :checksum
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
16
lib/fog/openstack/models/image/images.rb
Normal file
16
lib/fog/openstack/models/image/images.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/openstack/models/image/image'
|
||||
|
||||
module Fog
|
||||
module Image
|
||||
class OpenStack
|
||||
class Images < Fog::Collection
|
||||
model Fog::Image::OpenStack::Image
|
||||
|
||||
def all
|
||||
load(connection.list_public_images.body['images'])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
32
lib/fog/openstack/requests/image/get_image_by_id.rb
Normal file
32
lib/fog/openstack/requests/image/get_image_by_id.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
module Fog
|
||||
module Image
|
||||
class OpenStack
|
||||
class Real
|
||||
def get_image_by_id(image_id)
|
||||
request(
|
||||
:expects => [200, 204],
|
||||
:method => 'GET',
|
||||
:path => "images/#{image_id}"
|
||||
)
|
||||
end
|
||||
end # class Real
|
||||
|
||||
class Mock
|
||||
def get_image_by_id(image_id)
|
||||
response = Excon::Response.new
|
||||
response.status = [200, 204][rand(1)]
|
||||
response.body = {
|
||||
"images"=>[{
|
||||
"name"=>"mock-image-name",
|
||||
"size"=>25165824,
|
||||
"disk_format"=>"ami",
|
||||
"container_format"=>"ami",
|
||||
"id"=>"0e09fbd6-43c5-448a-83e9-0d3d05f9747e",
|
||||
"checksum"=>"2f81976cae15c16ef0010c51e3a6c163"}]
|
||||
}
|
||||
response
|
||||
end # def list_tenants
|
||||
end # class Mock
|
||||
end # class OpenStack
|
||||
end # module Identity
|
||||
end # module Fog
|
32
lib/fog/openstack/requests/image/list_public_images.rb
Normal file
32
lib/fog/openstack/requests/image/list_public_images.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
module Fog
|
||||
module Image
|
||||
class OpenStack
|
||||
class Real
|
||||
def list_public_images
|
||||
request(
|
||||
:expects => [200, 204],
|
||||
:method => 'GET',
|
||||
:path => 'images'
|
||||
)
|
||||
end
|
||||
end # class Real
|
||||
|
||||
class Mock
|
||||
def list_public_images
|
||||
response = Excon::Response.new
|
||||
response.status = [200, 204][rand(1)]
|
||||
response.body = {
|
||||
"images"=>[{
|
||||
"name"=>"mock-image-name",
|
||||
"size"=>25165824,
|
||||
"disk_format"=>"ami",
|
||||
"container_format"=>"ami",
|
||||
"id"=>"0e09fbd6-43c5-448a-83e9-0d3d05f9747e",
|
||||
"checksum"=>"2f81976cae15c16ef0010c51e3a6c163"}]
|
||||
}
|
||||
response
|
||||
end # def list_tenants
|
||||
end # class Mock
|
||||
end # class OpenStack
|
||||
end # module Identity
|
||||
end # module Fog
|
|
@ -0,0 +1,45 @@
|
|||
module Fog
|
||||
module Image
|
||||
class OpenStack
|
||||
class Real
|
||||
def list_public_images_detailed
|
||||
request(
|
||||
:expects => [200, 204],
|
||||
:method => 'GET',
|
||||
:path => 'images/detail'
|
||||
)
|
||||
end
|
||||
end # class Real
|
||||
|
||||
class Mock
|
||||
def list_public_images_detailed
|
||||
response = Excon::Response.new
|
||||
response.status = [200, 204][rand(1)]
|
||||
response.body = {"images"=>[{
|
||||
"name"=>"cirros-0.3.0-x86_64-blank",
|
||||
"size"=>25165824,
|
||||
"min_disk"=>0,
|
||||
"disk_format"=>"ami",
|
||||
"created_at"=>"2012-02-21T07:32:26",
|
||||
"container_format"=>"ami",
|
||||
"deleted_at"=>nil,
|
||||
"updated_at"=>"2012-02-21T07:32:29",
|
||||
"checksum"=>"2f81976cae15c16ef0010c51e3a6c163",
|
||||
"id"=>"0e09fbd6-43c5-448a-83e9-0d3d05f9747e",
|
||||
"deleted"=>false,
|
||||
"protected"=>false,
|
||||
"is_public"=>true,
|
||||
"status"=>"active",
|
||||
"min_ram"=>0,
|
||||
"owner"=>"ff528b20431645ebb5fa4b0a71ca002f",
|
||||
"properties"=>{
|
||||
"ramdisk_id"=>"b45aa128-cd36-4ad9-a026-1a1c2bfd8fdc",
|
||||
"kernel_id"=>"cd28951e-e1c2-4bc5-95d3-f0495abbcdc5"}
|
||||
}]
|
||||
}
|
||||
response
|
||||
end # def list_tenants
|
||||
end # class Mock
|
||||
end # class OpenStack
|
||||
end # module Identity
|
||||
end # module Fog
|
Loading…
Reference in a new issue