Class: Miasma::Contrib::OpenStackApiCore
- Inherits:
-
Object
- Object
- Miasma::Contrib::OpenStackApiCore
- Includes:
- Utils::Memoization
- Defined in:
- lib/miasma/contrib/open_stack.rb
Overview
OpenStack API core helper
Defined Under Namespace
Modules: ApiCommon Classes: Authenticate
Constant Summary collapse
- API_MAP =
Returns Mapping to external service name.
Smash.new( 'compute' => 'nova', 'orchestration' => 'heat', 'network' => 'neutron', 'identity' => 'keystone', 'storage' => 'swift' )
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#api_token ⇒ String
API token.
-
#endpoint_for(api_name, region) ⇒ String
Provide end point URL for service.
-
#identity_class(i_name) ⇒ Class
Class from instance class, falls back to parent.
-
#initialize(creds) ⇒ self
constructor
Create a new api instance.
Constructor Details
#initialize(creds) ⇒ self
Create a new api instance
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/miasma/contrib/open_stack.rb', line 272 def initialize(creds) @credentials = creds memo_key = "miasma_open_stack_identity_#{creds.checksum}" if(creds[:open_stack_identity_url].include?('v3')) @identity = memoize(memo_key, :direct) do identity_class('Authenticate::Version3').new(creds) end elsif(creds[:open_stack_identity_url].include?('v2')) @identity = memoize(memo_key, :direct) do identity_class('Authenticate::Version2').new(creds) end else # @todo allow attribute to override? raise ArgumentError.new('Failed to determine Identity service version') end end |
Instance Attribute Details
#identity ⇒ Miasma::Contrib::OpenStackApiCore::Authenticate (readonly)
266 267 268 |
# File 'lib/miasma/contrib/open_stack.rb', line 266 def identity @identity end |
Instance Method Details
#api_token ⇒ String
Returns API token.
333 334 335 |
# File 'lib/miasma/contrib/open_stack.rb', line 333 def api_token identity.api_token end |
#endpoint_for(api_name, region) ⇒ String
Provide end point URL for service
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/miasma/contrib/open_stack.rb', line 307 def endpoint_for(api_name, region) api = self.class.const_get(:API_MAP)[api_name] srv = identity.service_catalog.detect do |info| info[:name] == api end unless(srv) raise NotImplementedError.new("No API mapping found for `#{api_name}`") end if(region) point = srv[:endpoints].detect do |endpoint| endpoint[:region].to_s.downcase == region.to_s.downcase end else point = srv[:endpoints].first end if(point) point.fetch( :publicURL, point[:url] ) else raise KeyError.new("Lookup failed for `#{api_name}` within region `#{region}`") end end |
#identity_class(i_name) ⇒ Class
Returns class from instance class, falls back to parent.
290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/miasma/contrib/open_stack.rb', line 290 def identity_class(i_name) [self.class, Miasma::Contrib::OpenStackApiCore].map do |klass| i_name.split('::').inject(klass) do |memo, key| if(memo.const_defined?(key)) memo.const_get(key) else break end end end.compact.first end |