Class: Miasma::Models::Orchestration::Stack::Resource

Inherits:
Types::Model show all
Includes:
Utils::Immutable
Defined in:
lib/miasma/models/orchestration/resource.rb

Overview

Stack resource

Instance Attribute Summary collapse

Attributes inherited from Types::Model

#api

Instance Method Summary collapse

Methods included from Utils::Immutable

#frozen_valid_state, included, #save

Methods inherited from Types::Model

#destroy, from_json, #id?, #persisted?, #reload, #save

Methods included from Utils::Memoization

#_memo, #clear_memoizations!, #memoize, #unmemoize

Methods inherited from Types::Data

from_json, #from_json, #to_json

Methods included from Utils::Lazy

included

Constructor Details

#initialize(stack, args = {}) ⇒ Resource

Returns a new instance of Resource.



21
22
23
24
# File 'lib/miasma/models/orchestration/resource.rb', line 21

def initialize(stack, args={})
  @stack = stack
  super stack.api, args
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



19
20
21
# File 'lib/miasma/models/orchestration/resource.rb', line 19

def stack
  @stack
end

Instance Method Details

#dereferenceMiasma::Types::Model

Returns provides mapped resource class.

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/miasma/models/orchestration/resource.rb', line 27

def dereference
  # Insert provider to namespace
  provider_const = self.class.name.split('::').insert(3, Utils.camel(api.provider))
  provider_const.slice!(-2, 2)
  # Insert mapping constant name and fetch
 const = provider_const.push(:RESOURCE_MAPPING).inject(Object) do |memo, konst|
    res = memo.const_get(konst)
    break unless res
    res
  end
  unless(const[self.type])
    raise KeyError.new "Failed to locate requested mapping! (`#{self.type}`)"
  end
  const[self.type]
end

#expandMiasma::Types::Model Also known as: instance

Provide proper instance from resource



46
47
48
49
# File 'lib/miasma/models/orchestration/resource.rb', line 46

def expand
  info = dereference
  api.api_for(info[:api]).send(info[:collection]).get(self.id)
end

#perform_reloadObject

Proxy reload action up to the API



53
54
55
# File 'lib/miasma/models/orchestration/resource.rb', line 53

def perform_reload
  api.resource_reload(self)
end

#within?(api, collection) ⇒ TrueClass, FalseClass

Resource is within the collection on the api

Parameters:

  • api (Symbol)

    api type (:compute, :auto_scale, etc)

  • collection (Symbol)

    collection within api (:servers, :groups, etc)

Returns:

  • (TrueClass, FalseClass)


62
63
64
65
66
67
68
69
70
# File 'lib/miasma/models/orchestration/resource.rb', line 62

def within?(api, collection)
  begin
    info = dereference
    info[:api] == api &&
      info[:collection] == collection
  rescue KeyError
    false
  end
end