Class: Miasma::Contrib::OpenStackApiCore

Inherits:
Object
  • Object
show all
Defined in:
lib/miasma/contrib/open_stack.rb

Overview

OpenStack API core helper

Direct Known Subclasses

RackspaceApiCore

Defined Under Namespace

Modules: ApiCommon Classes: Authenticate

Constant Summary collapse

API_MAP =

Returns Mapping to external service name.

Returns:

  • (Smash)

    Mapping to external service name

Smash.new(
  'compute' => 'nova',
  'orchestration' => 'heat',
  'network' => 'neutron',
  'identity' => 'keystone'
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(creds) ⇒ self

Create a new api instance

Parameters:

  • creds (Smash)

    credential hash



270
271
272
273
274
275
276
277
278
279
280
# File 'lib/miasma/contrib/open_stack.rb', line 270

def initialize(creds)
  @credentials = creds
  if(creds[:open_stack_identity_url].include?('v3'))
    @identity = identity_class('Authenticate::Version3').new(creds)
  elsif(creds[:open_stack_identity_url].include?('v2'))
    @identity = identity_class('Authenticate::Version2').new(creds)
  else
    # @todo allow attribute to override?
    raise ArgumentError.new('Failed to determine Identity service version')
  end
end

Instance Attribute Details

#identityMiasma::Contrib::OpenStackApiCore::Authenticate (readonly)



264
265
266
# File 'lib/miasma/contrib/open_stack.rb', line 264

def identity
  @identity
end

Instance Method Details

#api_tokenString

Returns API token.

Returns:

  • (String)

    API token



326
327
328
# File 'lib/miasma/contrib/open_stack.rb', line 326

def api_token
  identity.api_token
end

#endpoint_for(api_name, region) ⇒ String

Provide end point URL for service

Parameters:

  • api_name (String)

    name of api

  • region (String)

    region in use

Returns:

  • (String)

    public URL



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/miasma/contrib/open_stack.rb', line 300

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.

Returns:

  • (Class)

    class from instance class, falls back to parent



283
284
285
286
287
288
289
290
291
292
293
# File 'lib/miasma/contrib/open_stack.rb', line 283

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