1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #1307 from Keoven/update/openstack-module

[openstack] Bulk Update
This commit is contained in:
Nelvin Driz 2012-11-27 03:10:08 -08:00
commit b45468dab1
13 changed files with 292 additions and 77 deletions

View file

@ -72,44 +72,22 @@ module Fog
# Keystone Style Auth
def self.authenticate_v2(options, connection_options = {})
uri = options[:openstack_auth_uri]
connection = Fog::Connection.new(uri.to_s, false, connection_options)
@openstack_api_key = options[:openstack_api_key]
@openstack_username = options[:openstack_username]
@openstack_tenant = options[:openstack_tenant]
@openstack_auth_token = options[:openstack_auth_token]
@service_name = options[:openstack_service_name]
@identity_service_name = options[:openstack_identity_service_name]
@endpoint_type = options[:openstack_endpoint_type] || 'publicURL'
@openstack_region = options[:openstack_region]
uri = options[:openstack_auth_uri]
tenant_name = options[:openstack_tenant]
service_name = options[:openstack_service_name]
identity_service_name = options[:openstack_identity_service_name]
endpoint_type = (options[:openstack_endpoint_type] || 'publicURL').to_s
openstack_region = options[:openstack_region]
if @openstack_auth_token
req_body = {
'auth' => {
'token' => {
'id' => @openstack_auth_token
}
}
}
else
req_body = {
'auth' => {
'passwordCredentials' => {
'username' => @openstack_username.to_s,
'password' => @openstack_api_key.to_s
}
}
}
end
req_body['auth']['tenantName'] = @openstack_tenant if @openstack_tenant
body = retrieve_tokens_v2(connection, req_body, uri)
body = retrieve_tokens_v2(options, connection_options)
service = body['access']['serviceCatalog'].
detect {|s| service_name.include?(s['type']) }
svc = body['access']['serviceCatalog'].
detect{|x| @service_name.include?(x['type']) }
options[:unscoped_token] = body['access']['token']['id']
unless svc
unless @openstack_tenant
unless service
unless tenant_name
response = Fog::Connection.new(
"#{uri.scheme}://#{uri.host}:#{uri.port}/v2.0/tenants", false, connection_options).request({
:expects => [200, 204],
@ -124,53 +102,73 @@ module Fog
if body['tenants'].empty?
raise Errors::NotFound.new('No Tenant Found')
else
req_body['auth']['tenantName'] = body['tenants'].first['name']
options[:openstack_tenant] = body['tenants'].first['name']
end
end
body = retrieve_tokens_v2(connection, req_body, uri)
if body['access']['token']['tenant'].nil?
raise Errors::NotFound.new("Invalid Tenant '#{@openstack_tenant}'")
end
svc = body['access']['serviceCatalog'].
detect{|x| @service_name.include?(x['type']) }
body = retrieve_tokens_v2(options, connection_options)
service = body['access']['serviceCatalog'].
detect{|s| service_name.include?(s['type']) }
end
svc['endpoints'] = svc['endpoints'].select{ |x| x['region'] == @openstack_region } if @openstack_region
if svc['endpoints'].count > 1
regions = svc["endpoints"].map { |x| x['region'] }.uniq.join(',')
raise Errors::NotFound.new("Multiple regions available choose one of these '#{regions}'")
service['endpoints'] = service['endpoints'].select do |endpoint|
endpoint['region'] == openstack_region
end if openstack_region
if service['endpoints'].count > 1
regions = service["endpoints"].map{ |e| e['region'] }.uniq.join(',')
raise Errors::NotFound.new("Multiple regions available choose one of these '#{regions}'")
end
identity_svc = body['access']['serviceCatalog'].
detect{|x| @identity_service_name.include?(x['type']) } if @identity_service_name
identity_service = body['access']['serviceCatalog'].
detect{|x| identity_service_name.include?(x['type']) } if identity_service_name
tenant = body['access']['token']['tenant']
user = body['access']['user']
mgmt_url = svc['endpoints'].detect{|x| x[@endpoint_type]}[@endpoint_type]
identity_url = identity_svc['endpoints'].detect{|x| x['publicURL']}['publicURL'] if identity_svc
token = body['access']['token']['id']
expires = body['access']['token']['expires']
management_url = service['endpoints'].detect{|s| s[endpoint_type]}[endpoint_type]
identity_url = identity_service['endpoints'].detect{|s| s['publicURL']}['publicURL'] if identity_service
{
:user => user,
:tenant => tenant,
:token => token,
:expires => expires,
:server_management_url => mgmt_url,
:identity_public_endpoint => identity_url,
:current_user_id => body['access']['user']['id']
:server_management_url => management_url,
:token => body['access']['token']['id'],
:expires => body['access']['token']['expires'],
:current_user_id => body['access']['user']['id'],
:unscoped_token => options[:unscoped_token]
}
end
def self.retrieve_tokens_v2(connection, request_body, uri)
def self.retrieve_tokens_v2(options, connection_options = {})
api_key = options[:openstack_api_key].to_s
username = options[:openstack_username].to_s
tenant_name = options[:openstack_tenant].to_s
auth_token = options[:openstack_auth_token] || options[:unscoped_token]
uri = options[:openstack_auth_uri]
connection = Fog::Connection.new(uri.to_s, false, connection_options)
request_body = {:auth => Hash.new}
if auth_token
request_body[:auth][:token] = {
:id => auth_token
}
else
request_body[:auth][:passwordCredentials] = {
:username => username,
:password => api_key
}
end
request_body[:auth][:tenantName] = tenant_name if tenant_name
response = connection.request({
:expects => [200, 204],
:headers => {'Content-Type' => 'application/json'},
:body => Fog::JSON.encode(request_body),
:host => uri.host,
:method => 'POST',
:path => (uri.path and not uri.path.empty?) ? uri.path : 'v2.0'
:path => (uri.path and not uri.path.empty?) ? uri.path : 'v2.0'
})
Fog::JSON.decode(response.body)

View file

@ -69,6 +69,7 @@ module Fog
request :remove_fixed_ip
request :server_diagnostics
request :boot_from_snapshot
request :reset_server_state
# Server Extenstions
request :get_console_output
@ -125,6 +126,7 @@ module Fog
# Tenant
request :list_tenants
request :set_tenant
request :get_limits
# Volume
request :list_volumes
@ -249,9 +251,10 @@ module Fog
def initialize(options={})
@openstack_auth_token = options[:openstack_auth_token]
@auth_token = options[:openstack_auth_token]
@openstack_identity_public_endpoint = options[:openstack_identity_endpoint]
unless @openstack_auth_token
unless @auth_token
missing_credentials = Array.new
@openstack_api_key = options[:openstack_api_key]
@openstack_username = options[:openstack_username]
@ -334,14 +337,14 @@ module Fog
private
def authenticate
if @openstack_must_reauthenticate || @openstack_auth_token.nil?
if !@openstack_management_url || @openstack_must_reauthenticate
options = {
:openstack_api_key => @openstack_api_key,
:openstack_username => @openstack_username,
:openstack_auth_token => @openstack_auth_token,
:openstack_auth_uri => @openstack_auth_uri,
:openstack_region => @openstack_region,
:openstack_tenant => @openstack_tenant,
:openstack_api_key => @openstack_api_key,
:openstack_username => @openstack_username,
:openstack_auth_token => @auth_token,
:openstack_auth_uri => @openstack_auth_uri,
:openstack_region => @openstack_region,
:openstack_tenant => @openstack_tenant,
:openstack_service_name => @openstack_service_name,
:openstack_identity_service_name => @openstack_identity_service_name
}
@ -360,12 +363,9 @@ module Fog
@auth_token_expiration = credentials[:expires]
@openstack_management_url = credentials[:server_management_url]
@openstack_identity_public_endpoint = credentials[:identity_public_endpoint]
uri = URI.parse(@openstack_management_url)
else
@auth_token = @openstack_auth_token
uri = URI.parse(@openstack_management_url)
end
uri = URI.parse(@openstack_management_url)
@host = uri.host
@path, @tenant_id = uri.path.scan(/(\/.*)\/(.*)/).flatten

View file

@ -51,12 +51,16 @@ module Fog
request :get_role
request :list_roles
request :set_tenant
class Mock
attr_reader :auth_token
attr_reader :unscoped_token
attr_reader :auth_token_expiration
attr_reader :current_user
attr_reader :current_tenant
attr_reader :unscoped_token
def self.data
@users ||= {}
@ -114,6 +118,7 @@ module Fog
@current_user = self.data[:users].values.find do |u|
u['name'] == @openstack_username
end
@current_tenant_id = Fog::Mock.random_hex(32)
unless @current_user
@current_user_id = Fog::Mock.random_hex(32)
@ -151,6 +156,7 @@ module Fog
class Real
attr_reader :current_user
attr_reader :current_tenant
attr_reader :unscoped_token
def initialize(options={})
@openstack_auth_token = options[:openstack_auth_token]
@ -166,7 +172,7 @@ module Fog
end
@openstack_tenant = options[:openstack_tenant]
@openstack_auth_uri = URI.parse(options[:openstack_auth_url])
@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] || ['identity']
@ -236,7 +242,7 @@ module Fog
private
def authenticate
if @openstack_must_reauthenticate || @openstack_auth_token.nil?
if !@openstack_management_url || @openstack_must_reauthenticate
options = {
:openstack_api_key => @openstack_api_key,
:openstack_username => @openstack_username,
@ -256,6 +262,7 @@ module Fog
@auth_token = credentials[:token]
@openstack_management_url = credentials[:server_management_url]
@openstack_current_user_id = credentials[:current_user_id]
@unscoped_token = credentials[:unscoped_token]
uri = URI.parse(@openstack_management_url)
else
@auth_token = @openstack_auth_token

View file

@ -172,7 +172,7 @@ module Fog
private
def authenticate
if @openstack_must_reauthenticate || @openstack_auth_token.nil?
if !@openstack_management_url || @openstack_must_reauthenticate
options = {
:openstack_tenant => @openstack_tenant,
:openstack_api_key => @openstack_api_key,

View file

@ -17,7 +17,7 @@ module Fog
def save
requires :name, :description
data = connection.create_security_group(name, description)
merge_attributes(data.body['security_groups'])
merge_attributes(data.body['security_group'])
true
end

View file

@ -195,6 +195,11 @@ module Fog
connection.disassociate_address id, floating_ip
end
def reset_vm_state(vm_state)
requires :id
connection.reset_server_state id, vm_state
end
def min_count=(new_min_count)
@min_count = new_min_count
end

View file

@ -7,10 +7,9 @@ module Fog
class Users < Fog::Collection
model Fog::Identity::OpenStack::User
attribute :tenant
attribute :tenant_id
def all
tenant_id = tenant.nil? ? nil : tenant.id
load(connection.list_users(tenant_id).body['users'])
end

View file

@ -0,0 +1,97 @@
module Fog
module Compute
class OpenStack
# http://docs.openstack.org/api/openstack-compute/2/content/ProgramaticLimits.html
#
class Real
def get_limits
request(
:expects => 200,
:method => 'GET',
:path => '/limits.json'
)
end
end
class Mock
def get_limits
rate_limits = [
{ 'regex' => '.*',
'limit' => [
{ 'next-available' => '2012-11-22T16:13:44Z',
'unit' => 'MINUTE',
'verb' => 'POST',
'remaining' => 9,
'value' => 10 },
{ 'next-available' => '2012-11-23T00:46:14Z',
'unit' => 'MINUTE',
'verb' => 'PUT',
'remaining' => 10,
'value' => 10 },
{ 'next-available' => '2012-11-22T16:14:30Z',
'unit' => 'MINUTE',
'verb' => 'DELETE',
'remaining' => 99,
'value' => 100 } ],
'uri' => '*' },
{ 'regex' => '^/servers',
'limit' => [
{ 'next-available' => '2012-11-23T00:46:14Z',
'unit' => 'DAY',
'verb' => 'POST',
'remaining' => 50,
'value' => 50} ],
'uri'=>'*/servers' },
{ 'regex' => '.*changes-since.*',
'limit' => [
{ 'next-available' => '2012-11-23T00:46:14Z',
'unit' => 'MINUTE',
'verb' => 'GET',
'remaining' => 3,
'value' => 3 } ],
'uri' => '*changes-since*' }
]
absolute_limits = {
# Max
'maxServerMeta' => 128,
'maxTotalInstances' => 10,
'maxPersonality' => 5,
'maxImageMeta' => 128,
'maxPersonalitySize' => 10240,
'maxSecurityGroupRules' => 20,
'maxTotalKeypairs' => 100,
'maxTotalVolumes' => 10,
'maxSecurityGroups' => 10,
'maxTotalCores' => 20,
'maxTotalFloatingIps' => 10,
'maxTotalVolumeGigabytes' => 1000,
'maxTotalRAMSize' => 51200,
# Used
'totalVolumesUsed' => 0,
'totalCoresUsed' => -1,
'totalRAMUsed' => -2048,
'totalInstancesUsed' => -1,
'totalVolumeGigabytesUsed' => 0,
'totalSecurityGroupsUsed' => 0,
'totalKeyPairsUsed' => 0
}
Excon::Response.new(
:status => 200,
:body => {
'limits' => {
'rate' => rate_limits,
'absolute' => absolute_limits }
}
)
end
end
end
end
end

View file

@ -0,0 +1,24 @@
module Fog
module Compute
class OpenStack
class Real
def reset_server_state(server_id, status)
body = { 'os-resetState' => { 'state' => status } }
server_action(server_id, body, 202)
end
end
class Mock
def reset_server_state(server_id, status)
response = get_server_details(server_id)
response.body['server']['status'] = status.upcase
response
end
end
end
end
end

View file

@ -21,7 +21,7 @@ module Fog
class Mock
def create_role(name)
data = {
'id' => Fog::Mock.random_numbers(6).to_s,
'id' => Fog::Mock.random_base64(64),
'name' => name
}
self.data[:roles][data['id']] = data

View file

@ -0,0 +1,21 @@
module Fog
module Identity
class OpenStack
class Real
def set_tenant(tenant)
@openstack_must_reauthenticate = true
@openstack_tenant = tenant.to_s
authenticate
end
end
class Mock
def set_tenant(tenant)
true
end
end
end # class OpenStack
end # module Identity
end # module Fog

View file

@ -175,7 +175,7 @@ module Fog
private
def authenticate
if @openstack_must_reauthenticate || @openstack_auth_token.nil?
if !@openstack_management_url || @openstack_must_reauthenticate
options = {
:openstack_tenant => @openstack_tenant,
:openstack_api_key => @openstack_api_key,

View file

@ -0,0 +1,64 @@
Shindo.tests('Fog::Compute[:openstack] | limits requests', ['openstack']) do
@rate_limit_format = {
'regex' => String,
'uri' => String,
'limit' => Array
}
@rate_limit_usage_format = {
'next-available' => String,
'unit' => String,
'verb' => String,
'remaining' => Fixnum,
'value' => Fixnum
}
@absolute_limits_format = {
'maxServerMeta' => Fixnum,
'maxTotalInstances' => Fixnum,
'maxPersonality' => Fixnum,
'maxImageMeta' => Fixnum,
'maxPersonalitySize' => Fixnum,
'maxSecurityGroupRules' => Fixnum,
'maxTotalKeypairs' => Fixnum,
'maxTotalVolumes' => Fixnum,
'maxSecurityGroups' => Fixnum,
'maxTotalCores' => Fixnum,
'maxTotalFloatingIps' => Fixnum,
'maxTotalVolumeGigabytes' => Fixnum,
'maxTotalRAMSize' => Fixnum,
'totalVolumesUsed' => Fixnum,
'totalCoresUsed' => Fixnum,
'totalRAMUsed' => Fixnum,
'totalInstancesUsed' => Fixnum,
'totalVolumeGigabytesUsed' => Fixnum,
'totalSecurityGroupsUsed' => Fixnum,
'totalKeyPairsUsed' => Fixnum
}
@limits_format = {
'rate' => Array,
'absolute' => Hash
}
tests('success') do
tests('#get_limits') do
tests('format').formats(@limits_format) do
Fog::Compute[:openstack].get_limits.body['limits']
end
tests('rate limit format').formats(@rate_limit_format) do
Fog::Compute[:openstack].get_limits.body['limits']['rate'].first
end
tests('rate limit usage format').formats(@rate_limit_usage_format) do
Fog::Compute[:openstack].get_limits.body['limits']['rate'].first['limit'].first
end
tests('absolute limits format').formats(@absolute_limits_format) do
Fog::Compute[:openstack].get_limits.body['limits']['absolute']
end
end
end
end