From: marios <mar...@redhat.com> https://issues.apache.org/jira/browse/DTACLOUD-443
API-Change: Added the 'resource_types' attribute to Realm model Signed-off-by: marios <mar...@redhat.com> --- .../drivers/openstack/openstack_driver.rb | 67 ++++++++++++++++------ server/lib/deltacloud/models/realm.rb | 5 +- server/views/realms/index.html.haml | 2 + server/views/realms/show.html.haml | 4 ++ server/views/realms/show.xml.haml | 3 + 5 files changed, 63 insertions(+), 18 deletions(-) diff --git a/server/lib/deltacloud/drivers/openstack/openstack_driver.rb b/server/lib/deltacloud/drivers/openstack/openstack_driver.rb index b9be595..31bb028 100644 --- a/server/lib/deltacloud/drivers/openstack/openstack_driver.rb +++ b/server/lib/deltacloud/drivers/openstack/openstack_driver.rb @@ -127,21 +127,42 @@ module Deltacloud def realms(credentials, opts={}) os = new_client(credentials) - limits = "" - safely do - lim = os.limits - limits << "ABSOLUTE >> Max. Instances: #{lim[:absolute][:maxTotalInstances]} Max. RAM: #{lim[:absolute][:maxTotalRAMSize]} || " - lim[:rate].each do |rate| - if rate[:regex] =~ /servers/ - limits << "SERVERS >> Total: #{rate[:limit].first[:value]} Remaining: #{rate[:limit].first[:remaining]} Time Unit: per #{rate[:limit].first[:unit]}" - end - end + realms = [] + if opts[:id] + resource_types = [] + os.connection.regions_list[opts[:id]].each do |service| + resource_types << service[:service] if ["compute", "volume", "object-store"].include?(service[:service]) + realms << Realm.new( { :id => opts[:id], + :name => opts[:id], + :state =>'AVAILABLE', + :resource_types => resource_types}) unless resource_types.empty? + end + else + os.connection.regions_list.each_pair do |region, services| + resource_types = services.inject([]){|res, cur| res << cur[:service] if ["compute", "volume", "object-store"].include?(cur[:service]); res } + next if resource_types.empty? #nothing here deltacloud manages + realms << Realm.new( { :id => region, + :name => region, + :state =>'AVAILABLE', + :resource_types => resource_types}) + end end - return [] if opts[:id] and opts[:id] != 'default' - [ Realm.new( { :id=>'default', - :name=>'default', - :limit => limits, - :state=>'AVAILABLE' })] + realms +# limits = "" +# safely do +# lim = os.limits +# limits << "ABSOLUTE >> Max. Instances: #{lim[:absolute][:maxTotalInstances]} Max. RAM: #{lim[:absolute][:maxTotalRAMSize]} || " +# lim[:rate].each do |rate| +# if rate[:regex] =~ /servers/ +# limits << "SERVERS >> Total: #{rate[:limit].first[:value]} Remaining: #{rate[:limit].first[:remaining]} Time Unit: per #{rate[:limit].first[:unit]}" +# end +# end +# end +# return [] if opts[:id] and opts[:id] != 'default' +# [ Realm.new( { :id=>'default', +# :name=>'default', +# :limit => limits, +# :state=>'AVAILABLE' })] end def instances(credentials, opts={}) @@ -167,7 +188,11 @@ module Deltacloud end def create_instance(credentials, image_id, opts) - os = new_client( credentials ) + if opts[:realm_id] && opts[:realm_id].length > 0 + os = new_client( credentials, "compute", opts[:realm_id]) + else + os = new_client( credentials) + end result = nil #opts[:personality]: path1='server_path1'. content1='contents1', path2='server_path2', content2='contents2' etc params = {} @@ -468,7 +493,7 @@ module Deltacloud private #for v2 authentication credentials.name == "username+tenant_name" - def new_client(credentials, type = "compute") + def new_client(credentials, type="compute", realm_id=nil) tokens = credentials.user.split("+") if credentials.user.empty? raise AuthenticationFailure.new(Exception.new("Error: you must supply the username")) @@ -478,10 +503,18 @@ private else user_name, tenant_name = tokens.first, tokens.last end + #check if region specified with provider: + provider = api_provider + if (realm_id || provider.include?("::")) + region = realm_id || provider.split("::").last + provider = provider.chomp("::#{region}") + end + connection_params = {:username => user_name, :api_key => credentials.password, :authtenant => tenant_name, :auth_url => provider, :service_type => type} + connection_params.merge!({:region => region}) if region safely do raise ValidationFailure.new(Exception.new("Error: tried to initialise Openstack connection using" + " an unknown service_type: #{type}")) unless ["volume", "compute", "object-store"].include? type - OpenStack::Connection.create(:username => user_name, :api_key => credentials.password, :authtenant => tenant_name, :auth_url => api_provider, :service_type => type) + OpenStack::Connection.create(connection_params) end end diff --git a/server/lib/deltacloud/models/realm.rb b/server/lib/deltacloud/models/realm.rb index 00dd36f..1c25ba2 100644 --- a/server/lib/deltacloud/models/realm.rb +++ b/server/lib/deltacloud/models/realm.rb @@ -20,15 +20,18 @@ class Realm < BaseModel attr_accessor :name attr_accessor :limit attr_accessor :state + attr_accessor :resource_types def to_hash(context) - { + r = { :id => self.id, :href => context.realm_url(self.id), :name => name, :state => state, :limit => limit } + r.merge!({:resource_types => resource_types}) if resource_types + r end end diff --git a/server/views/realms/index.html.haml b/server/views/realms/index.html.haml index 5e91e56..91636a8 100644 --- a/server/views/realms/index.html.haml +++ b/server/views/realms/index.html.haml @@ -10,4 +10,6 @@ %img{ :class => 'ui-link-thumb', :src => '/images/realm.png'} %h3= realm.name %p=[realm.id, realm.limit].join(', ') + -if realm.resource_types + %p=realm.resource_types.join(",") %span{ :class => 'ui-li-count'}=realm.state diff --git a/server/views/realms/show.html.haml b/server/views/realms/show.html.haml index f2c52f7..e1c16d4 100644 --- a/server/views/realms/show.html.haml +++ b/server/views/realms/show.html.haml @@ -15,3 +15,7 @@ %li{ :'data-role' => 'list-divider'} Limit %li %p{ :'data-role' => 'fieldcontain'}=@realm.limit + - if @realm.resource_types + %li{ :'data-role' => 'list-divider'} Resource Types + %li + %p{ :'data-role' => 'fieldcontain'}=@realm.resource_types diff --git a/server/views/realms/show.xml.haml b/server/views/realms/show.xml.haml index 5c06c06..268cb65 100644 --- a/server/views/realms/show.xml.haml +++ b/server/views/realms/show.xml.haml @@ -9,3 +9,6 @@ %limit< =cdata do =@realm.limit + - @realm.resource_types.each do |r| + %resource_type< + =r -- 1.7.11.7