Class: PoolParty::Resources::Resource

Inherits:
PoolPartyBaseClass show all
Includes:
DependencyResolverResourceExtensions, PoolParty::ResourcingDsl
Defined in:
lib/poolparty/poolparty/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PoolParty::ResourcingDsl

#absent, #cancel, #cancelled?, #client_template_exists?, #client_templates_directory_exists?, #ensures, #get_client_or_gem_template, #ifnot, #is_absent, #is_present, #on_change, #present, #printed, #printed?, #render_template, #requires

Methods included from DependencyResolverResourceExtensions

#to_properties_hash

Methods inherited from PoolPartyBaseClass

add_has_and_does_not_have_methods_for, #add_resource, #add_service, #add_to_parent_if_parent_exists_and_is_a_service, #get_local_resource, #get_name_from_options_and_extra_options, #get_resource, #handle_option_values, #in_local_resources?, #is_plugin?, #ordered_resources, #plugin_store, #resource, #resources, #run_in_context, #store_in_local_resources

Methods included from DependencyResolverCloudExtensions

#to_properties_hash

Constructor Details

#initialize(opts = {}, extra_opts = {}, &block) ⇒ Resource

This is set in order of descending precedence The options are overwritten from the bottom up and the resource will use those as the values Then it takes the value of the block and sets whatever is sent there as the options Finally, it uses the parent’s options as the lowest priority



77
78
79
80
81
82
83
84
85
86
# File 'lib/poolparty/poolparty/resource.rb', line 77

def initialize(opts={}, extra_opts={}, &block)
  super(opts, extra_opts, &block)
          
  @resource_name = @base_name
  dsl_options[:name] = resource_name unless dsl_options.has_key?(:name)
  
  loaded(opts, &block)
  
  after_create
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &block) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/poolparty/poolparty/resource.rb', line 169

def method_missing(m,*a,&block)
  if parent && parent.dsl_options.has_key?(m) && is_in_plugin?
    parent.send m, *a, &block
  else
    super
  end
end

Instance Attribute Details

#poststringObject

Returns the value of attribute poststring.



17
18
19
# File 'lib/poolparty/poolparty/resource.rb', line 17

def poststring
  @poststring
end

#prestringObject

Returns the value of attribute prestring.



17
18
19
# File 'lib/poolparty/poolparty/resource.rb', line 17

def prestring
  @prestring
end

Class Method Details

.available_resourcesObject

Keep track of the resources that are available. This way we can show some pretty output later and ensure that we are only calling available resources



67
68
69
# File 'lib/poolparty/poolparty/resource.rb', line 67

def self.available_resources
  @available_resources ||= []
end

.custom_function(str) ⇒ Object



128
129
130
# File 'lib/poolparty/poolparty/resource.rb', line 128

def self.custom_function(str)
  custom_functions << str
end

.custom_functionsObject



131
132
133
# File 'lib/poolparty/poolparty/resource.rb', line 131

def self.custom_functions
  @custom_functions ||= []
end

.custom_functions_to_string(pre = "") ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/poolparty/poolparty/resource.rb', line 138

def self.custom_functions_to_string(pre="")
  returning Array.new do |output|
    PoolParty::Resources.available_custom_resources.each do |resource|
      resource.custom_functions.each do |func|
        output << "#{pre*2}#{func}"
      end
    end
  end.join("\n")
end

.inherited(subclass) ⇒ Object

When we subclass Resource, we want to add a few methods to the Resources class This will anable us to call out to these resources in our DSLified manner When we call a method from the subclass, say it’s the File class then we want to be able to have the method file() available. We also want to be able to fetch the resource with a get_file method. This will just call out to get the resource. If the resource isn’t available in a resource store, we expect to return a nil result. Finally, the has_ and does_not_have_ methods are appended. See below for those methods. Then we make sure we add these resources as available_resources onto the class so we know it’s available as a resource



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/poolparty/poolparty/resource.rb', line 39

def self.inherited(subclass)
  subclass = subclass.to_s.split("::")[-1] if subclass.to_s.index("::")
  lowercase_class_name = subclass.to_s.underscore.downcase || subclass.downcase
  method_name = "__#{lowercase_class_name}".to_sym
  
  # Add add resource method to the Resources module
  unless PoolParty::PoolPartyBaseClass.respond_to?(method_name)
    method =<<-EOE
      private 
      def #{method_name}(opts={}, &blk)              
        add_resource(:#{lowercase_class_name}, opts, &blk)
      end
      def get_#{lowercase_class_name}(n, opts={}, &block)
        res = get_resource(:#{lowercase_class_name}, n, opts, &block)
        raise PackageException.new("Oops. Check that you specified the #{lowercase_class_name} \#\{n\}.") unless res
        res
      end
      public
    EOE
    PoolParty::PoolPartyBaseClass.module_eval method
    PoolParty::PoolPartyBaseClass.add_has_and_does_not_have_methods_for(lowercase_class_name.to_sym)
    
    available_resources << subclass
  end
end

Instance Method Details

#after_createObject

After create callback



103
104
# File 'lib/poolparty/poolparty/resource.rb', line 103

def after_create
end

#class_name_symObject

Private method just for resource retrievling purposes



178
179
180
# File 'lib/poolparty/poolparty/resource.rb', line 178

def class_name_sym
  self.class.to_s.top_level_class.downcase.to_sym
end

#class_type_nameObject

This way we can subclass resources without worry



125
126
127
# File 'lib/poolparty/poolparty/resource.rb', line 125

def class_type_name
  self.class.to_s.top_level_class.underscore.downcase
end

#cloudObject



111
112
113
114
115
116
# File 'lib/poolparty/poolparty/resource.rb', line 111

def cloud
  2.upto(context_stack.size) do |i|
    return ::PoolParty.context_stack[-i] if ::PoolParty.context_stack[-i].is_a?(PoolParty::Cloud::Cloud)
  end
  nil
end

#custom_function(str) ⇒ Object



134
135
136
# File 'lib/poolparty/poolparty/resource.rb', line 134

def custom_function(str)
  self.class.custom_functions << str
end

#disallowed_optionsObject

Some things in puppet aren’t allowed, so let’s override them here



148
149
150
# File 'lib/poolparty/poolparty/resource.rb', line 148

def disallowed_options
  []
end

#duplicatable?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/poolparty/poolparty/resource.rb', line 118

def duplicatable?
  false
end

#get_modified_optionsObject

We want to gather the options, but if the option sent is nil then we want to override the option value by sending the key as a method so that we can override this if necessary. Only runs on objects that have options defined, otherwise it returns an empty hash



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/poolparty/poolparty/resource.rb', line 186

def get_modified_options
  unless @modified_options
    if dsl_options
      opts = dsl_options.inject({}) do |sum,h|
        sum.merge!({h[0].to_sym => ((h[1].nil?) ? self.send(h[0].to_sym) : h[1]) })
      end
    else
      opts = {}
    end
    @full_allowed_options ||= allowed_options.reject {|ele| disallowed_options.include?(ele) }
    @modified_options = opts.reject do |k,v|
      !@full_allowed_options.include?(k) || 
        @parent && @parent.respond_to?(:dsl_options) && @parent != self && @parent.dsl_options.has_key?(k) && @parent.dsl_options[k] == dsl_options[k]
    end
  end
  @modified_options
end

#is_a_resource?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/poolparty/poolparty/resource.rb', line 165

def is_a_resource?
  true
end

#is_in_plugin?Boolean

def virtual_resource?

false

end def printable?

true

end

Returns:

  • (Boolean)


161
162
163
# File 'lib/poolparty/poolparty/resource.rb', line 161

def is_in_plugin?
  parent.is_plugin?
end

#keyObject



152
153
154
# File 'lib/poolparty/poolparty/resource.rb', line 152

def key
  name
end

#loaded(opts = {}, &block) ⇒ Object

Stub, so you can create virtual resources This is called after the resource is initialized with the options given to it in the init-block



91
92
# File 'lib/poolparty/poolparty/resource.rb', line 91

def loaded(opts={}, &block)
end

#name(*args) ⇒ Object



98
99
100
# File 'lib/poolparty/poolparty/resource.rb', line 98

def name(*args)
  resource_name
end

#resource?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/poolparty/poolparty/resource.rb', line 121

def resource?
  true
end

#resource_nameObject



94
95
96
# File 'lib/poolparty/poolparty/resource.rb', line 94

def resource_name
  @resource_name ||= nil
end

#servicesObject

We don’t want to inherit the services on a resource, as resources are a base and should never have services.



108
109
# File 'lib/poolparty/poolparty/resource.rb', line 108

def services
end