Class: Puppet::Type

Inherits:
Object show all
Extended by:
Enumerable, MetaType::Manager, Util, Util::ClassGen, Util::Logging, Util::ProviderFeatures, Util::Warnings
Includes:
Comparable, Enumerable, Util, Util::Errors, Util::LogPaths, Util::Logging, Util::Tagging
Defined in:
lib/vendor/puppet/type.rb

Defined Under Namespace

Classes: RelationshipMetaparam

Constant Summary

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Util::Docs

#doc, #nodoc

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Logging

clear_deprecation_warnings, deprecation_warning, send_log

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, execfail, execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from MetaType::Manager

allclear, eachtype, loadall, newtype, rmtype, typeloader

Methods included from Util::ClassGen

genclass, genmodule, rmclass

Methods included from Util::MethodHelper

#requiredopts, #set_options, #symbolize_options

Methods included from Util::Warnings

clear_warnings, notice_once, warnonce

Methods included from Util::ProviderFeatures

feature, feature_module, featuredocs, features, provider_feature

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

Methods included from Util::Tagging

#tag, #tagged?, #tags

Methods included from Util::LogPaths

#path, #source_descriptors

Methods included from Util::Errors

#adderrorcontext, #devfail, #error_context, #exceptwrap, #fail

Constructor Details

#initialize(resource) ⇒ Type

initialize the type instance

Raises:



1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
# File 'lib/vendor/puppet/type.rb', line 1778

def initialize(resource)
  raise Puppet::DevError, "Got TransObject instead of Resource or hash" if resource.is_a?(Puppet::TransObject)
  resource = self.class.hash2resource(resource) unless resource.is_a?(Puppet::Resource)

  # The list of parameter/property instances.
  @parameters = {}

  # Set the title first, so any failures print correctly.
  if resource.type.to_s.downcase.to_sym == self.class.name
    self.title = resource.title
  else
    # This should only ever happen for components
    self.title = resource.ref
  end

  [:file, :line, :catalog, :exported, :virtual].each do |getter|
    setter = getter.to_s + "="
    if val = resource.send(getter)
      self.send(setter, val)
    end
  end

  @tags = resource.tags

  @original_parameters = resource.to_hash

  set_name(@original_parameters)

  set_default(:provider)

  set_parameters(@original_parameters)

  self.validate if self.respond_to?(:validate)
end

Class Attribute Details

.defaultproviderObject

Find the default provider.



1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
# File 'lib/vendor/puppet/type.rb', line 1402

def self.defaultprovider
  return @defaultprovider if @defaultprovider

  suitable = suitableprovider

  # Find which providers are a default for this system.
  defaults = suitable.find_all { |provider| provider.default? }

  # If we don't have any default we use suitable providers
  defaults = suitable if defaults.empty?
  max = defaults.collect { |provider| provider.specificity }.max
  defaults = defaults.find_all { |provider| provider.specificity == max }

  if defaults.length > 1
    Puppet.warning(
      "Found multiple default providers for #{self.name}: #{defaults.collect { |i| i.name.to_s }.join(", ")}; using #{defaults[0].name}"
    )
  end

  @defaultprovider = defaults.shift unless defaults.empty?
end

.nameObject (readonly)

Returns the value of attribute name.



1691
1692
1693
# File 'lib/vendor/puppet/type.rb', line 1691

def name
  @name
end

.propertiesObject (readonly)

Returns the value of attribute properties.



40
41
42
# File 'lib/vendor/puppet/type.rb', line 40

def properties
  @properties
end

.providerloaderObject

Returns the value of attribute providerloader.



1397
1398
1399
# File 'lib/vendor/puppet/type.rb', line 1397

def providerloader
  @providerloader
end

.self_refreshObject

Returns the value of attribute self_refresh.



1692
1693
1694
# File 'lib/vendor/puppet/type.rb', line 1692

def self_refresh
  @self_refresh
end

Instance Attribute Details

#catalogObject

The catalog that this resource is stored in.



1749
1750
1751
# File 'lib/vendor/puppet/type.rb', line 1749

def catalog
  @catalog
end

#exportedObject

is the resource exported



1752
1753
1754
# File 'lib/vendor/puppet/type.rb', line 1752

def exported
  @exported
end

#fileObject

Origin information.



1746
1747
1748
# File 'lib/vendor/puppet/type.rb', line 1746

def file
  @file
end

#lineObject

Origin information.



1746
1747
1748
# File 'lib/vendor/puppet/type.rb', line 1746

def line
  @line
end

#noopObject



770
771
772
# File 'lib/vendor/puppet/type.rb', line 770

def noop
  noop?
end

#original_parametersObject (readonly)

Returns the value of attribute original_parameters.



1775
1776
1777
# File 'lib/vendor/puppet/type.rb', line 1775

def original_parameters
  @original_parameters
end

#providerObject

Returns the value of attribute provider.



1393
1394
1395
# File 'lib/vendor/puppet/type.rb', line 1393

def provider
  @provider
end

#titleObject

Retrieve the title of an object. If no title was set separately, then use the object’s name.



1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
# File 'lib/vendor/puppet/type.rb', line 1912

def title
  unless @title
    if self.class.validparameter?(name_var)
      @title = self[:name]
    elsif self.class.validproperty?(name_var)
      @title = self.should(name_var)
    else
      self.devfail "Could not find namevar #{name_var} for #{self.class.name}"
    end
  end

  @title
end

#virtualObject

is the resource virtual (it should not :-))



1755
1756
1757
# File 'lib/vendor/puppet/type.rb', line 1755

def virtual
  @virtual
end

Class Method Details

.[](name) ⇒ Object

retrieve a named instance of the current type



779
780
781
782
# File 'lib/vendor/puppet/type.rb', line 779

def self.[](name)
  raise "Global resource access is deprecated"
  @objects[name] || @aliases[name]
end

.[]=(name, object) ⇒ Object

add an instance by name to the class list of instances



785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/vendor/puppet/type.rb', line 785

def self.[]=(name,object)
  raise "Global resource storage is deprecated"
  newobj = nil
  if object.is_a?(Puppet::Type)
    newobj = object
  else
    raise Puppet::DevError, "must pass a Puppet::Type object"
  end

  if exobj = @objects[name] and self.isomorphic?
    msg = "Object '#{newobj.class.name}[#{name}]' already exists"

    msg += ("in file #{object.file} at line #{object.line}") if exobj.file and exobj.line
    msg += ("and cannot be redefined in file #{object.file} at line #{object.line}") if object.file and object.line
    error = Puppet::Error.new(msg)
    raise error
  else
    #Puppet.info("adding %s of type %s to class list" %
    #    [name,object.class])
    @objects[name] = newobj
  end
end

.alias(name, obj) ⇒ Object

Create an alias. We keep these in a separate hash so that we don’t encounter the objects multiple times when iterating over them.



810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'lib/vendor/puppet/type.rb', line 810

def self.alias(name, obj)
  raise "Global resource aliasing is deprecated"
  if @objects.include?(name)
    unless @objects[name] == obj
      raise Puppet::Error.new(
        "Cannot create alias #{name}: object already exists"
      )
    end
  end

  if @aliases.include?(name)
    unless @aliases[name] == obj
      raise Puppet::Error.new(
        "Object #{@aliases[name].name} already has alias #{name}"
      )
    end
  end

  @aliases[name] = obj
end

.allattrsObject

All parameters, in the appropriate order. The key_attributes come first, then the provider, then the properties, and finally the params and metaparams in the order they were specified in the files.



51
52
53
# File 'lib/vendor/puppet/type.rb', line 51

def self.allattrs
  key_attributes | (parameters & [:provider]) | properties.collect { |property| property.name } | parameters | metaparams
end

.apply_toObject



145
146
147
# File 'lib/vendor/puppet/type.rb', line 145

def self.apply_to
  @apply_to ||= :host
end

.apply_to_allObject



141
142
143
# File 'lib/vendor/puppet/type.rb', line 141

def self.apply_to_all
  @apply_to = :both
end

.apply_to_deviceObject

These ‘apply_to` methods are horrible. They should really be implemented as part of the usual system of constraints that apply to a type and provider pair, but were implemented as a separate shadow system.

We should rip them out in favour of a real constraint pattern around the target device - whatever that looks like - and not have this additional magic here. –daniel 2012-03-08



133
134
135
# File 'lib/vendor/puppet/type.rb', line 133

def self.apply_to_device
  @apply_to = :device
end

.apply_to_hostObject



137
138
139
# File 'lib/vendor/puppet/type.rb', line 137

def self.apply_to_host
  @apply_to = :host
end

.attr_alias(param) ⇒ Object

Retrieve an attribute alias, if there is one.



56
57
58
59
60
# File 'lib/vendor/puppet/type.rb', line 56

def self.attr_alias(param)
  # Intern again, because this might be called by someone who doesn't
  # understand the calling convention and all.
  @attr_aliases[param.intern]
end

.attrclass(name) ⇒ Object

Find the class associated with any given attribute.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vendor/puppet/type.rb', line 71

def self.attrclass(name)
  @attrclasses ||= {}

  # We cache the value, since this method gets called such a huge number
  # of times (as in, hundreds of thousands in a given run).
  unless @attrclasses.include?(name)
    @attrclasses[name] = case self.attrtype(name)
    when :property; @validproperties[name]
    when :meta; @@metaparamhash[name]
    when :param; @paramhash[name]
    end
  end
  @attrclasses[name]
end

.attrtype(attr) ⇒ Object

What type of parameter are we dealing with? Cache the results, because this method gets called so many times.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/vendor/puppet/type.rb', line 88

def self.attrtype(attr)
  @attrtypes ||= {}
  unless @attrtypes.include?(attr)
    @attrtypes[attr] = case
      when @validproperties.include?(attr); :property
      when @paramhash.include?(attr); :param
      when @@metaparamhash.include?(attr); :meta
      end
  end

  @attrtypes[attr]
end

.autorequire(name, &block) ⇒ Object

Specify a block for generating a list of objects to autorequire. This makes it so that you don’t have to manually specify things that you clearly require.



1611
1612
1613
1614
# File 'lib/vendor/puppet/type.rb', line 1611

def self.autorequire(name, &block)
  @autorequires ||= {}
  @autorequires[name] = block
end

.can_apply_to(target) ⇒ Object



149
150
151
# File 'lib/vendor/puppet/type.rb', line 149

def self.can_apply_to(target)
  [ target == :device ? :device : :host, :both ].include?(apply_to)
end

.clearObject

remove all of the instances of a single type



832
833
834
835
836
837
838
839
840
841
# File 'lib/vendor/puppet/type.rb', line 832

def self.clear
  raise "Global resource removal is deprecated"
  if defined?(@objects)
    @objects.each do |name, obj|
      obj.remove(true)
    end
    @objects.clear
  end
  @aliases.clear if defined?(@aliases)
end

.create(args) ⇒ Object

Force users to call this, so that we can merge objects if necessary.



845
846
847
848
849
# File 'lib/vendor/puppet/type.rb', line 845

def self.create(args)
  # LAK:DEP Deprecation notice added 12/17/2008
  Puppet.warning "Puppet::Type.create is deprecated; use Puppet::Type.new"
  new(args)
end

.delete(resource) ⇒ Object

remove a specified object



852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
# File 'lib/vendor/puppet/type.rb', line 852

def self.delete(resource)
  raise "Global resource removal is deprecated"
  return unless defined?(@objects)
  @objects.delete(resource.title) if @objects.include?(resource.title)
  @aliases.delete(resource.title) if @aliases.include?(resource.title)
  if @aliases.has_value?(resource)
    names = []
    @aliases.each do |name, otherres|
      if otherres == resource
        names << name
      end
    end
    names.each { |name| @aliases.delete(name) }
  end
end

.eachObject

iterate across each of the type’s instances



869
870
871
872
873
874
875
# File 'lib/vendor/puppet/type.rb', line 869

def self.each
  raise "Global resource iteration is deprecated"
  return unless defined?(@objects)
  @objects.each { |name,instance|
    yield instance
  }
end

.eachautorequireObject

Yield each of those autorequires in turn, yo.



1617
1618
1619
1620
1621
1622
# File 'lib/vendor/puppet/type.rb', line 1617

def self.eachautorequire
  @autorequires ||= {}
  @autorequires.each { |type, block|
    yield(type, block)
  }
end

.eachmetaparamObject



101
102
103
# File 'lib/vendor/puppet/type.rb', line 101

def self.eachmetaparam
  @@metaparams.each { |p| yield p.name }
end

.ensurable(&block) ⇒ Object

Create the ‘ensure’ class. This is a separate method so other types can easily call it and create their own ‘ensure’ values.



107
108
109
110
111
112
113
114
115
# File 'lib/vendor/puppet/type.rb', line 107

def self.ensurable(&block)
  if block_given?
    self.newproperty(:ensure, :parent => Puppet::Property::Ensure, &block)
  else
    self.newproperty(:ensure, :parent => Puppet::Property::Ensure) do
      self.defaultvalues
    end
  end
end

.ensurable?Boolean

Should we add the ‘ensure’ property to this class?

Returns:

  • (Boolean)


118
119
120
121
122
123
124
# File 'lib/vendor/puppet/type.rb', line 118

def self.ensurable?
  # If the class has all three of these methods defined, then it's
  # ensurable.
  [:exists?, :create, :destroy].all? { |method|
    self.public_method_defined?(method)
  }
end

.handle_param_options(name, options) ⇒ Object

Deal with any options passed into parameters.



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/vendor/puppet/type.rb', line 154

def self.handle_param_options(name, options)
  # If it's a boolean parameter, create a method to test the value easily
  if options[:boolean]
    define_method(name.to_s + "?") do
      val = self[name]
      if val == :true or val == true
        return true
      end
    end
  end
end

.has_key?(name) ⇒ Boolean

does the type have an object with the given name?

Returns:

  • (Boolean)


878
879
880
881
# File 'lib/vendor/puppet/type.rb', line 878

def self.has_key?(name)
  raise "Global resource access is deprecated"
  @objects.has_key?(name)
end

.hash2resource(hash) ⇒ Object

Convert a simple hash into a Resource instance.

Raises:



927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
# File 'lib/vendor/puppet/type.rb', line 927

def self.hash2resource(hash)
  hash = hash.inject({}) { |result, ary| result[ary[0].to_sym] = ary[1]; result }

  title = hash.delete(:title)
  title ||= hash[:name]
  title ||= hash[key_attributes.first] if key_attributes.length == 1

  raise Puppet::Error, "Title or name must be provided" unless title

  # Now create our resource.
  resource = Puppet::Resource.new(self.name, title)
  [:catalog].each do |attribute|
    if value = hash[attribute]
      hash.delete(attribute)
      resource.send(attribute.to_s + "=", value)
    end
  end

  hash.each do |param, value|
    resource[param] = value
  end
  resource
end

.initvarsObject

all of the variables that must be initialized for each subclass



1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
# File 'lib/vendor/puppet/type.rb', line 1701

def self.initvars
  # all of the instances of this class
  @objects = Hash.new
  @aliases = Hash.new

  @defaults = {}

  @parameters ||= []

  @validproperties = {}
  @properties = []
  @parameters = []
  @paramhash = {}

  @attr_aliases = {}

  @paramdoc = Hash.new { |hash,key|
    key = key.intern if key.is_a?(String)
    if hash.include?(key)
      hash[key]
    else
      "Param Documentation for #{key} not found"
    end
  }

  @doc ||= ""

end

.instancesObject

Retrieve all known instances. Either requires providers or must be overridden.

Raises:



884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
# File 'lib/vendor/puppet/type.rb', line 884

def self.instances
  raise Puppet::DevError, "#{self.name} has no providers and has not overridden 'instances'" if provider_hash.empty?

  # Put the default provider first, then the rest of the suitable providers.
  provider_instances = {}
  providers_by_source.collect do |provider|
    all_properties = self.properties.find_all do |property|
      provider.supports_parameter?(property)
    end.collect do |property|
      property.name
    end

    provider.instances.collect do |instance|
      # We always want to use the "first" provider instance we find, unless the resource
      # is already managed and has a different provider set
      if other = provider_instances[instance.name]
        Puppet.warning "%s %s found in both %s and %s; skipping the %s version" %
          [self.name.to_s.capitalize, instance.name, other.class.name, instance.class.name, instance.class.name]
        next
      end
      provider_instances[instance.name] = instance

      result = new(:name => instance.name, :provider => instance)
      properties.each { |name| result.newattr(name) }
      result
    end
  end.flatten.compact
end

.isomorphic?Boolean

Is this type’s name isomorphic with the object? That is, if the name conflicts, does it necessarily mean that the objects conflict? Defaults to true.

Returns:

  • (Boolean)


590
591
592
593
594
595
596
# File 'lib/vendor/puppet/type.rb', line 590

def self.isomorphic?
  if defined?(@isomorphic)
    return @isomorphic
  else
    return true
  end
end

.key_attribute_parametersObject



213
214
215
216
217
218
219
# File 'lib/vendor/puppet/type.rb', line 213

def self.key_attribute_parameters
  @key_attribute_parameters ||= (
    params = @parameters.find_all { |param|
      param.isnamevar? or param.name == :name
    }
  )
end

.key_attributesObject



221
222
223
224
# File 'lib/vendor/puppet/type.rb', line 221

def self.key_attributes
  # This is a cache miss around 0.05 percent of the time. --daniel 2012-07-17
  @key_attributes_cache ||= key_attribute_parameters.collect { |p| p.name }
end

.metaparam?(param) ⇒ Boolean

Is the parameter in question a meta-parameter?

Returns:

  • (Boolean)


167
168
169
# File 'lib/vendor/puppet/type.rb', line 167

def self.metaparam?(param)
  @@metaparamhash.include?(param.intern)
end

.metaparamclass(name) ⇒ Object

Find the metaparameter class associated with a given metaparameter name. Must accept a ‘nil` name, and return nil.



173
174
175
176
# File 'lib/vendor/puppet/type.rb', line 173

def self.metaparamclass(name)
  return nil if name.nil?
  @@metaparamhash[name.intern]
end

.metaparamdoc(metaparam) ⇒ Object



182
183
184
# File 'lib/vendor/puppet/type.rb', line 182

def self.metaparamdoc(metaparam)
  @@metaparamhash[metaparam].doc
end

.metaparamsObject



178
179
180
# File 'lib/vendor/puppet/type.rb', line 178

def self.metaparams
  @@metaparams.collect { |param| param.name }
end

.newmetaparam(name, options = {}, &block) ⇒ Object

Create a new metaparam. Requires a block and a name, stores it in the



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/vendor/puppet/type.rb', line 188

def self.newmetaparam(name, options = {}, &block)
  @@metaparams ||= []
  @@metaparamhash ||= {}
  name = name.intern

  param = genclass(
    name,
    :parent => options[:parent] || Puppet::Parameter,
    :prefix => "MetaParam",
    :hash => @@metaparamhash,
    :array => @@metaparams,
    :attributes => options[:attributes],
    &block
  )

  # Grr.
  param.required_features = options[:required_features] if options[:required_features]

  handle_param_options(name, options)

  param.metaparam = true

  param
end

.newparam(name, options = {}, &block) ⇒ Object

Create a new parameter. Requires a block and a name, stores it in the



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/vendor/puppet/type.rb', line 242

def self.newparam(name, options = {}, &block)
  options[:attributes] ||= {}

  param = genclass(
    name,
    :parent     => options[:parent] || Puppet::Parameter,
    :attributes => options[:attributes],
    :block      => block,
    :prefix     => "Parameter",
    :array      => @parameters,
    :hash       => @paramhash
  )

  handle_param_options(name, options)

  # Grr.
  param.required_features = options[:required_features] if options[:required_features]

  param.isnamevar if options[:namevar]

  param
end

.newproperty(name, options = {}, &block) ⇒ Object

Create a new property. The first parameter must be the name of the property; this is how users will refer to the property when creating new instances. The second parameter is a hash of options; the options are:

  • :parent: The parent class for the property. Defaults to Puppet::Property.

  • :retrieve: The method to call on the provider or @parent object (if the provider is not set) to retrieve the current value.

Raises:



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/vendor/puppet/type.rb', line 276

def self.newproperty(name, options = {}, &block)
  name = name.intern

  # This is here for types that might still have the old method of defining
  # a parent class.
  unless options.is_a? Hash
    raise Puppet::DevError,
      "Options must be a hash, not #{options.inspect}"
  end

  raise Puppet::DevError, "Class #{self.name} already has a property named #{name}" if @validproperties.include?(name)

  if parent = options[:parent]
    options.delete(:parent)
  else
    parent = Puppet::Property
  end

  # We have to create our own, new block here because we want to define
  # an initial :retrieve method, if told to, and then eval the passed
  # block if available.
  prop = genclass(name, :parent => parent, :hash => @validproperties, :attributes => options) do
    # If they've passed a retrieve method, then override the retrieve
    # method on the class.
    if options[:retrieve]
      define_method(:retrieve) do
        provider.send(options[:retrieve])
      end
    end

    class_eval(&block) if block
  end

  # If it's the 'ensure' property, always put it first.
  if name == :ensure
    @properties.unshift prop
  else
    @properties << prop
  end

  prop
end

.newstate(name, options = {}, &block) ⇒ Object



265
266
267
268
# File 'lib/vendor/puppet/type.rb', line 265

def self.newstate(name, options = {}, &block)
  Puppet.warning "newstate() has been deprecrated; use newproperty(#{name})"
  newproperty(name, options, &block)
end

.paramclass(name) ⇒ Object

Find the parameter class associated with a given parameter name.



330
331
332
# File 'lib/vendor/puppet/type.rb', line 330

def self.paramclass(name)
  @paramhash[name]
end

.paramdoc(param) ⇒ Object



319
320
321
# File 'lib/vendor/puppet/type.rb', line 319

def self.paramdoc(param)
  @paramhash[param].doc
end

.parametersObject

Return the parameter names



324
325
326
327
# File 'lib/vendor/puppet/type.rb', line 324

def self.parameters
  return [] unless defined?(@parameters)
  @parameters.collect { |klass| klass.name }
end

.propertybyname(name) ⇒ Object

Return the property class associated with a name



335
336
337
# File 'lib/vendor/puppet/type.rb', line 335

def self.propertybyname(name)
  @validproperties[name]
end

.provide(name, options = {}, &block) ⇒ Object

Create a new provider of a type. This method must be called directly on the type that it’s implementing.



1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
# File 'lib/vendor/puppet/type.rb', line 1455

def self.provide(name, options = {}, &block)
  name = name.intern

  if unprovide(name)
    Puppet.debug "Reloading #{name} #{self.name} provider"
  end

  parent = if pname = options[:parent]
    options.delete(:parent)
    if pname.is_a? Class
      pname
    else
      if provider = self.provider(pname)
        provider
      else
        raise Puppet::DevError,
          "Could not find parent provider #{pname} of #{name}"
      end
    end
  else
    Puppet::Provider
  end

  options[:resource_type] ||= self

  self.providify

  provider = genclass(
    name,
    :parent     => parent,
    :hash       => provider_hash,
    :prefix     => "Provider",
    :block      => block,
    :include    => feature_module,
    :extend     => feature_module,
    :attributes => options
  )

  provider
end

.provider(name) ⇒ Object

Retrieve a provider by name.



1434
1435
1436
1437
1438
1439
1440
# File 'lib/vendor/puppet/type.rb', line 1434

def self.provider(name)
  name = name.intern

  # If we don't have it yet, try loading it.
  @providerloader.load(name) unless provider_hash.has_key?(name)
  provider_hash[name]
end

.provider_hashObject



1429
1430
1431
# File 'lib/vendor/puppet/type.rb', line 1429

def self.provider_hash
  Puppet::Type.provider_hash_by_type(self.name)
end

.provider_hash_by_type(type) ⇒ Object



1424
1425
1426
1427
# File 'lib/vendor/puppet/type.rb', line 1424

def self.provider_hash_by_type(type)
  @provider_hashes ||= {}
  @provider_hashes[type] ||= {}
end

.providersObject

Just list all of the providers.



1443
1444
1445
# File 'lib/vendor/puppet/type.rb', line 1443

def self.providers
  provider_hash.keys
end

.providers_by_sourceObject

Return a list of one suitable provider per source, with the default provider first.



914
915
916
917
918
919
920
921
922
923
924
# File 'lib/vendor/puppet/type.rb', line 914

def self.providers_by_source
  # Put the default provider first (can be nil), then the rest of the suitable providers.
  sources = []
  [defaultprovider, suitableprovider].flatten.uniq.collect do |provider|
    next if provider.nil?
    next if sources.include?(provider.source)

    sources << provider.source
    provider
  end.compact
end

.providifyObject

Make sure we have a :provider parameter defined. Only gets called if there are providers.



1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
# File 'lib/vendor/puppet/type.rb', line 1498

def self.providify
  return if @paramhash.has_key? :provider

  newparam(:provider) do
    # We're using a hacky way to get the name of our type, since there doesn't
    # seem to be a correct way to introspect this at the time this code is run.
    # We expect that the class in which this code is executed will be something
    # like Puppet::Type::Ssh_authorized_key::ParameterProvider.
    desc <<-EOT
      The specific backend to use for this `#{self.to_s.split('::')[2].downcase}`
      resource. You will seldom need to specify this --- Puppet will usually
      discover the appropriate provider for your platform.
    EOT

    # This is so we can refer back to the type to get a list of
    # providers for documentation.
    class << self
      attr_accessor :parenttype
    end

    # We need to add documentation for each provider.
    def self.doc
      # Since we're mixing @doc with text from other sources, we must normalize
      # its indentation with scrub. But we don't need to manually scrub the
      # provider's doc string, since markdown_definitionlist sanitizes its inputs.
      scrub(@doc) + "Available providers are:\n\n" + parenttype.providers.sort { |a,b|
        a.to_s <=> b.to_s
      }.collect { |i|
        markdown_definitionlist( i, scrub(parenttype().provider(i).doc) )
      }.join
    end

    defaultto {
      prov = @resource.class.defaultprovider
      prov.name if prov
    }

    validate do |provider_class|
      provider_class = provider_class[0] if provider_class.is_a? Array
      provider_class = provider_class.class.name if provider_class.is_a?(Puppet::Provider)

      unless provider = @resource.class.provider(provider_class)
        raise ArgumentError, "Invalid #{@resource.class.name} provider '#{provider_class}'"
      end
    end

    munge do |provider|
      provider = provider[0] if provider.is_a? Array
      provider = provider.intern if provider.is_a? String
      @resource.provider = provider

      if provider.is_a?(Puppet::Provider)
        provider.class.name
      else
        provider
      end
    end
  end.parenttype = self
end

.relationship_paramsObject



1243
1244
1245
# File 'lib/vendor/puppet/type.rb', line 1243

def self.relationship_params
  RelationshipMetaparam.subclasses
end

.set_attr_alias(hash) ⇒ Object

Create an alias to an existing attribute. This will cause the aliased attribute to be valid when setting and retrieving values on the instance.



64
65
66
67
68
# File 'lib/vendor/puppet/type.rb', line 64

def self.set_attr_alias(hash)
  hash.each do |new, old|
    @attr_aliases[new.intern] = old.intern
  end
end

.statesObject



43
44
45
46
# File 'lib/vendor/puppet/type.rb', line 43

def self.states
  warnonce "The states method is deprecated; use properties"
  properties
end

.suitableproviderObject

Return an array of all of the suitable providers.



1567
1568
1569
1570
1571
1572
1573
1574
# File 'lib/vendor/puppet/type.rb', line 1567

def self.suitableprovider
  providerloader.loadall if provider_hash.empty?
  provider_hash.find_all { |name, provider|
    provider.suitable?
  }.collect { |name, provider|
    provider
  }.reject { |p| p.name == :fake } # For testing
end

.title_patternsObject



226
227
228
229
230
231
232
233
234
# File 'lib/vendor/puppet/type.rb', line 226

def self.title_patterns
  case key_attributes.length
  when 0; []
  when 1;
    [ [ /(.*)/m, [ [key_attributes.first] ] ] ]
  else
    raise Puppet::DevError,"you must specify title patterns when there are two or more key attributes"
  end
end

.to_sObject



1730
1731
1732
1733
1734
1735
1736
# File 'lib/vendor/puppet/type.rb', line 1730

def self.to_s
  if defined?(@name)
    "Puppet::Type::#{@name.to_s.capitalize}"
  else
    super
  end
end

.unprovide(name) ⇒ Object



1558
1559
1560
1561
1562
1563
1564
# File 'lib/vendor/puppet/type.rb', line 1558

def self.unprovide(name)
  if @defaultprovider and @defaultprovider.name == name
    @defaultprovider = nil
  end

  rmclass(name, :hash => provider_hash, :prefix => "Provider")
end

.valid_parameter?(name) ⇒ Boolean

This is a forward-compatibility method - it’s the validity interface we’ll use in Puppet::Resource.

Returns:

  • (Boolean)


371
372
373
# File 'lib/vendor/puppet/type.rb', line 371

def self.valid_parameter?(name)
  validattr?(name)
end

.validate(&block) ⇒ Object

Create a block to validate that our object is set up entirely. This will be run before the object is operated on.



1740
1741
1742
1743
# File 'lib/vendor/puppet/type.rb', line 1740

def self.validate(&block)
  define_method(:validate, &block)
  #@validate = block
end

.validattr?(name) ⇒ Boolean

Returns:

  • (Boolean)


339
340
341
342
343
344
345
346
347
348
349
# File 'lib/vendor/puppet/type.rb', line 339

def self.validattr?(name)
  name = name.intern
  return true if name == :name
  @validattrs ||= {}

  unless @validattrs.include?(name)
    @validattrs[name] = !!(self.validproperty?(name) or self.validparameter?(name) or self.metaparam?(name))
  end

  @validattrs[name]
end

.validparameter?(name) ⇒ Boolean

does the name reflect a valid parameter?

Returns:

  • (Boolean)

Raises:



365
366
367
368
# File 'lib/vendor/puppet/type.rb', line 365

def self.validparameter?(name)
  raise Puppet::DevError, "Class #{self} has not defined parameters" unless defined?(@parameters)
  !!(@paramhash.include?(name) or @@metaparamhash.include?(name))
end

.validpropertiesObject

Return the list of validproperties



358
359
360
361
362
# File 'lib/vendor/puppet/type.rb', line 358

def self.validproperties
  return {} unless defined?(@parameters)

  @validproperties.keys
end

.validproperty?(name) ⇒ Boolean

does the name reflect a valid property?

Returns:

  • (Boolean)


352
353
354
355
# File 'lib/vendor/puppet/type.rb', line 352

def self.validproperty?(name)
  name = name.intern
  @validproperties.include?(name) && @validproperties[name]
end

.validprovider?(name) ⇒ Boolean

Returns:

  • (Boolean)


1447
1448
1449
1450
1451
# File 'lib/vendor/puppet/type.rb', line 1447

def self.validprovider?(name)
  name = name.intern

  (provider_hash.has_key?(name) && provider_hash[name].suitable?)
end

Instance Method Details

#<=>(other) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/vendor/puppet/type.rb', line 27

def <=>(other)
  # We only order against other types, not arbitrary objects.
  return nil unless other.is_a? Puppet::Type
  # Our natural order is based on the reference name we use when comparing
  # against other type instances.
  self.ref <=> other.ref
end

#[](name) ⇒ Object

abstract accessing parameters and properties, and normalize access to always be symbols, not strings This returns a value, not an object. It returns the ‘is’ value, but you can also specifically return ‘is’ and ‘should’ values using ‘object.is(:property)’ or ‘object.should(:property)’.



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/vendor/puppet/type.rb', line 413

def [](name)
  name = attr_alias(name)

  fail("Invalid parameter #{name}(#{name.inspect})") unless self.class.validattr?(name)

  if name == :name && nv = name_var
    name = nv
  end

  if obj = @parameters[name]
    # Note that if this is a property, then the value is the "should" value,
    # not the current value.
    obj.value
  else
    return nil
  end
end

#[]=(name, value) ⇒ Object

Abstract setting parameters and properties, and normalize access to always be symbols, not strings. This sets the ‘should’ value on properties, and otherwise just sets the appropriate parameter.

Raises:



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/vendor/puppet/type.rb', line 434

def []=(name,value)
  name = attr_alias(name)

  fail("Invalid parameter #{name}") unless self.class.validattr?(name)

  if name == :name && nv = name_var
    name = nv
  end
  raise Puppet::Error.new("Got nil value for #{name}") if value.nil?

  property = self.newattr(name)

  if property
    begin
      # make sure the parameter doesn't have any errors
      property.value = value
    rescue => detail
      error = Puppet::Error.new("Parameter #{name} failed on #{ref}: #{detail}")
      error.set_backtrace(detail.backtrace)
      raise error
    end
  end

  nil
end

#add_property_parameter(prop_name) ⇒ Object

Create a new property if it is valid but doesn’t exist Returns: true if a new parameter was added, false otherwise



392
393
394
395
396
397
398
# File 'lib/vendor/puppet/type.rb', line 392

def add_property_parameter(prop_name)
  if self.class.validproperty?(prop_name) && !@parameters[prop_name]
    self.newattr(prop_name)
    return true
  end
  false
end

#ancestorsObject

Code related to evaluating the resources.



654
655
656
# File 'lib/vendor/puppet/type.rb', line 654

def ancestors
  []
end

#appliable_to_device?Boolean

Returns:

  • (Boolean)


1966
1967
1968
# File 'lib/vendor/puppet/type.rb', line 1966

def appliable_to_device?
  self.class.can_apply_to(:device)
end

#appliable_to_host?Boolean

Returns:

  • (Boolean)


1970
1971
1972
# File 'lib/vendor/puppet/type.rb', line 1970

def appliable_to_host?
  self.class.can_apply_to(:host)
end

#attr_alias(name) ⇒ Object

Return either the attribute alias or the attribute.



376
377
378
379
380
381
382
383
# File 'lib/vendor/puppet/type.rb', line 376

def attr_alias(name)
  name = name.intern
  if synonym = self.class.attr_alias(name)
    return synonym
  else
    return name
  end
end

#autorequire(rel_catalog = nil) ⇒ Object

Figure out of there are any objects we can automatically add as dependencies.

Raises:



1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
# File 'lib/vendor/puppet/type.rb', line 1626

def autorequire(rel_catalog = nil)
  rel_catalog ||= catalog
  raise(Puppet::DevError, "You cannot add relationships without a catalog") unless rel_catalog

  reqs = []
  self.class.eachautorequire { |type, block|
    # Ignore any types we can't find, although that would be a bit odd.
    next unless typeobj = Puppet::Type.type(type)

    # Retrieve the list of names from the block.
    next unless list = self.instance_eval(&block)
    list = [list] unless list.is_a?(Array)

    # Collect the current prereqs
    list.each { |dep|
      # Support them passing objects directly, to save some effort.
      unless dep.is_a? Puppet::Type
        # Skip autorequires that we aren't managing
        unless dep = rel_catalog.resource(type, dep)
          next
        end
      end

      reqs << Puppet::Relationship.new(dep, self)
    }
  }

  reqs
end

#builddependsObject

Build the dependencies associated with an individual object.



1657
1658
1659
1660
1661
1662
1663
1664
# File 'lib/vendor/puppet/type.rb', line 1657

def builddepends
  # Handle the requires
  self.class.relationship_params.collect do |klass|
    if param = @parameters[klass.name]
      param.to_edges
    end
  end.flatten.reject { |r| r.nil? }
end

#currentpropvaluesObject

Get a hash of the current properties. Returns a hash with the actual property instance as the key and the current value as the, um, value.



736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'lib/vendor/puppet/type.rb', line 736

def currentpropvalues
  # It's important to use the 'properties' method here, as it follows the order
  # in which they're defined in the class.  It also guarantees that 'ensure'
  # is the first property, which is important for skipping 'retrieve' on
  # all the properties if the resource is absent.
  ensure_state = false
  return properties.inject({}) do | prophash, property|
    if property.name == :ensure
      ensure_state = property.retrieve
      prophash[property] = ensure_state
    else
      if ensure_state == :absent
        prophash[property] = :absent
      else
        prophash[property] = property.retrieve
      end
    end
    prophash
  end
end

#delete(attr) ⇒ Object

remove a property from the object; useful in testing or in cleanup when an error has been encountered



462
463
464
465
466
467
468
469
# File 'lib/vendor/puppet/type.rb', line 462

def delete(attr)
  attr = attr.intern
  if @parameters.has_key?(attr)
    @parameters.delete(attr)
  else
    raise Puppet::DevError.new("Undefined attribute '#{attr}' in #{self}")
  end
end

#deleting?Boolean

Are we deleting this resource?

Returns:

  • (Boolean)


386
387
388
# File 'lib/vendor/puppet/type.rb', line 386

def deleting?
  obj = @parameters[:ensure] and obj.should == :absent
end

#depthfirst?Boolean

Code related to the container behaviour.

Returns:

  • (Boolean)


628
629
630
# File 'lib/vendor/puppet/type.rb', line 628

def depthfirst?
  false
end

#eachpropertyObject

iterate across the existing properties



472
473
474
475
476
477
# File 'lib/vendor/puppet/type.rb', line 472

def eachproperty
  # properties is a private method
  properties.each { |property|
    yield property
  }
end

#event(options = {}) ⇒ Object

Create a transaction event. Called by Transaction or by a property.



481
482
483
# File 'lib/vendor/puppet/type.rb', line 481

def event(options = {})
  Puppet::Transaction::Event.new({:resource => self, :file => file, :line => line, :tags => tags}.merge(options))
end

#exported?Boolean

Returns:

  • (Boolean)


1964
# File 'lib/vendor/puppet/type.rb', line 1964

def exported?; !!@exported; end

#finishObject

Set up all of our autorequires.



1852
1853
1854
1855
1856
1857
1858
1859
1860
# File 'lib/vendor/puppet/type.rb', line 1852

def finish
  # Make sure all of our relationships are valid.  Again, must be done
  # when the entire catalog is instantiated.
  self.class.relationship_params.collect do |klass|
    if param = @parameters[klass.name]
      param.validate_relationship
    end
  end.flatten.reject { |r| r.nil? }
end

#flushObject

Flush the provider, if it supports it. This is called by the transaction.



660
661
662
# File 'lib/vendor/puppet/type.rb', line 660

def flush
  self.provider.flush if self.provider and self.provider.respond_to?(:flush)
end

#insync?(is) ⇒ Boolean

if all contained objects are in sync, then we’re in sync FIXME I don’t think this is used on the type instances any more, it’s really only used for testing

Returns:

  • (Boolean)


667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# File 'lib/vendor/puppet/type.rb', line 667

def insync?(is)
  insync = true

  if property = @parameters[:ensure]
    unless is.include? property
      raise Puppet::DevError,
        "The is value is not in the is array for '#{property.name}'"
    end
    ensureis = is[property]
    if property.safe_insync?(ensureis) and property.should == :absent
      return true
    end
  end

  properties.each { |property|
    unless is.include? property
      raise Puppet::DevError,
        "The is value is not in the is array for '#{property.name}'"
    end

    propis = is[property]
    unless property.safe_insync?(propis)
      property.debug("Not in sync: #{propis.inspect} vs #{property.should.inspect}")
      insync = false
    #else
    #    property.debug("In sync")
    end
  }

  #self.debug("#{self} sync status is #{insync}")
  insync
end

#isomorphic?Boolean

Returns:

  • (Boolean)


598
599
600
# File 'lib/vendor/puppet/type.rb', line 598

def isomorphic?
  self.class.isomorphic?
end

#log(msg) ⇒ Object

create a log at specified level



1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
# File 'lib/vendor/puppet/type.rb', line 1758

def log(msg)

  Puppet::Util::Log.create(

    :level => @parameters[:loglevel].value,
    :message => msg,

    :source => self
  )
end

#managed?Boolean

is the instance a managed instance? A ‘yes’ here means that the instance was created from the language, vs. being created in order resolve other questions, such as finding a package in a list

Returns:

  • (Boolean)


606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'lib/vendor/puppet/type.rb', line 606

def managed?
  # Once an object is managed, it always stays managed; but an object
  # that is listed as unmanaged might become managed later in the process,
  # so we have to check that every time
  if @managed
    return @managed
  else
    @managed = false
    properties.each { |property|
      s = property.should
      if s and ! property.class.unmanaged
        @managed = true
        break
      end
    }
    return @managed
  end
end

#nameObject

For now, leave the ‘name’ method functioning like it used to. Once ‘title’ works everywhere, I’ll switch it.



1864
1865
1866
# File 'lib/vendor/puppet/type.rb', line 1864

def name
  self[:name]
end

#name_varObject

The name_var is the key_attribute in the case that there is only one.



403
404
405
406
# File 'lib/vendor/puppet/type.rb', line 403

def name_var
  key_attributes = self.class.key_attributes
  (key_attributes.length == 1) && key_attributes.first
end

#newattr(name) ⇒ Object

Create the actual attribute instance. Requires either the attribute name or class as the first argument, then an optional hash of attributes to set during initialization.



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/vendor/puppet/type.rb', line 494

def newattr(name)
  if name.is_a?(Class)
    klass = name
    name = klass.name
  end

  unless klass = self.class.attrclass(name)
    raise Puppet::Error, "Resource type #{self.class.name} does not support parameter #{name}"
  end

  if provider and ! provider.class.supports_parameter?(klass)
    missing = klass.required_features.find_all { |f| ! provider.class.feature?(f) }
    debug "Provider %s does not support features %s; not managing attribute %s" % [provider.class.name, missing.join(", "), name]
    return nil
  end

  return @parameters[name] if @parameters.include?(name)

  @parameters[name] = klass.new(:resource => self)
end

#noop?Boolean

Are we running in noop mode?

Returns:

  • (Boolean)


758
759
760
761
762
763
764
765
766
767
768
# File 'lib/vendor/puppet/type.rb', line 758

def noop?
  # If we're not a host_config, we're almost certainly part of
  # Settings, and we want to ignore 'noop'
  return false if catalog and ! catalog.host_config?

  if defined?(@noop)
    @noop
  else
    Puppet[:noop]
  end
end

#parameter(name) ⇒ Object

return the value of a parameter



516
517
518
# File 'lib/vendor/puppet/type.rb', line 516

def parameter(name)
  @parameters[name.to_sym]
end

#parametersObject



520
521
522
# File 'lib/vendor/puppet/type.rb', line 520

def parameters
  @parameters.dup
end

#parentObject

Look up our parent in the catalog, if we have one.



1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
# File 'lib/vendor/puppet/type.rb', line 1869

def parent
  return nil unless catalog

  unless defined?(@parent)
    if parents = catalog.adjacent(self, :direction => :in)
      # We should never have more than one parent, so let's just ignore
      # it if we happen to.
      @parent = parents.shift
    else
      @parent = nil
    end
  end
  @parent
end

#pathbuilderObject

Create the path for logging and such.



952
953
954
955
956
957
958
# File 'lib/vendor/puppet/type.rb', line 952

def pathbuilder
  if p = parent
    [p.pathbuilder, self.ref].flatten
  else
    [self.ref]
  end
end

#propertiesObject

Return all of the property objects, in the order specified in the class.



583
584
585
# File 'lib/vendor/puppet/type.rb', line 583

def properties
  self.class.properties.collect { |prop| @parameters[prop.name] }.compact
end

#property(name) ⇒ Object

Return an actual property instance by name; to return the value, use ‘resource’ LAK:NOTE(20081028) Since the ‘parameter’ method is now a superset of this method, this one should probably go away at some point.



533
534
535
# File 'lib/vendor/puppet/type.rb', line 533

def property(name)
  (obj = @parameters[name.intern] and obj.is_a?(Puppet::Property)) ? obj : nil
end

#propertydefined?(name) ⇒ Boolean

Is the named property defined?

Returns:

  • (Boolean)


525
526
527
528
# File 'lib/vendor/puppet/type.rb', line 525

def propertydefined?(name)
  name = name.intern unless name.is_a? Symbol
  @parameters.include?(name)
end

#purgingObject

Mark that we’re purging.



1896
1897
1898
# File 'lib/vendor/puppet/type.rb', line 1896

def purging
  @purging = true
end

#purging?Boolean

Is this resource being purged? Used by transactions to forbid deletion when there are dependencies.

Returns:

  • (Boolean)


1902
1903
1904
1905
1906
1907
1908
# File 'lib/vendor/puppet/type.rb', line 1902

def purging?
  if defined?(@purging)
    @purging
  else
    false
  end
end

#refObject

Return the “type” style reference.



1885
1886
1887
1888
1889
# File 'lib/vendor/puppet/type.rb', line 1885

def ref
  # memoizing this is worthwhile ~ 3 percent of calls are the "first time
  # around" in an average run of Puppet. --daniel 2012-07-17
  @ref ||= "#{self.class.name.to_s.capitalize}[#{self.title}]"
end

#remove(rmdeps = true) ⇒ Object

Remove an object. The argument determines whether the object’s subscriptions get eliminated, too.



634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/vendor/puppet/type.rb', line 634

def remove(rmdeps = true)
  # This is hackish (mmm, cut and paste), but it works for now, and it's
  # better than warnings.
  @parameters.each do |name, obj|
    obj.remove
  end
  @parameters.clear

  @parent = nil

  # Remove the reference to the provider.
  if self.provider
    @provider.clear
    @provider = nil
  end
end

#retrieveObject

retrieve the current value of all contained properties



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# File 'lib/vendor/puppet/type.rb', line 701

def retrieve
  fail "Provider #{provider.class.name} is not functional on this host" if self.provider.is_a?(Puppet::Provider) and ! provider.class.suitable?

  result = Puppet::Resource.new(type, title)

  # Provide the name, so we know we'll always refer to a real thing
  result[:name] = self[:name] unless self[:name] == title

  if ensure_prop = property(:ensure) or (self.class.validattr?(:ensure) and ensure_prop = newattr(:ensure))
    result[:ensure] = ensure_state = ensure_prop.retrieve
  else
    ensure_state = nil
  end

  properties.each do |property|
    next if property.name == :ensure
    if ensure_state == :absent
      result[property] = :absent
    else
      result[property] = property.retrieve
    end
  end

  result
end

#retrieve_resourceObject



727
728
729
730
731
# File 'lib/vendor/puppet/type.rb', line 727

def retrieve_resource
  resource = retrieve
  resource = Resource.new(type, title, :parameters => resource) if resource.is_a? Hash
  resource
end

#self_refresh?Boolean

Returns:

  • (Boolean)


1891
1892
1893
# File 'lib/vendor/puppet/type.rb', line 1891

def self_refresh?
  self.class.self_refresh
end

#set_default(attr) ⇒ Object

For any parameters or properties that have defaults and have not yet been set, set them now. This method can be handed a list of attributes, and if so it will only set defaults for those attributes.



540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/vendor/puppet/type.rb', line 540

def set_default(attr)
  return unless klass = self.class.attrclass(attr)
  return unless klass.method_defined?(:default)
  return if @parameters.include?(klass.name)

  return unless parameter = newattr(klass.name)

  if value = parameter.default and ! value.nil?
    parameter.value = value
  else
    @parameters.delete(parameter.name)
  end
end

#should(name) ⇒ Object

retrieve the ‘should’ value for a specified property



486
487
488
489
# File 'lib/vendor/puppet/type.rb', line 486

def should(name)
  name = attr_alias(name)
  (prop = @parameters[name] and prop.is_a?(Puppet::Property)) ? prop.should : nil
end

#suitable?Boolean

Returns:

  • (Boolean)


1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
# File 'lib/vendor/puppet/type.rb', line 1576

def suitable?
  # If we don't use providers, then we consider it suitable.
  return true unless self.class.paramclass(:provider)

  # We have a provider and it is suitable.
  return true if provider && provider.class.suitable?

  # We're using the default provider and there is one.
  if !provider and self.class.defaultprovider
    self.provider = self.class.defaultprovider.name
    return true
  end

  # We specified an unsuitable provider, or there isn't any suitable
  # provider.
  false
end

#tags=(list) ⇒ Object

Define the initial list of tags.



1667
1668
1669
1670
# File 'lib/vendor/puppet/type.rb', line 1667

def tags=(list)
  tag(self.class.name)
  tag(*list)
end

#to_hashObject

Convert our object to a hash. This just includes properties.



555
556
557
558
559
560
561
562
563
# File 'lib/vendor/puppet/type.rb', line 555

def to_hash
  rethash = {}

  @parameters.each do |name, obj|
    rethash[name] = obj.value
  end

  rethash
end

#to_resourceObject



1957
1958
1959
1960
1961
# File 'lib/vendor/puppet/type.rb', line 1957

def to_resource
  # this 'type instance' versus 'resource' distinction seems artificial
  # I'd like to see it collapsed someday ~JW
  self.to_trans.to_resource
end

#to_sObject

convert to a string



1927
1928
1929
# File 'lib/vendor/puppet/type.rb', line 1927

def to_s
  self.ref
end

#to_trans(ret = true) ⇒ Object

Convert to a transportable object



1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
# File 'lib/vendor/puppet/type.rb', line 1932

def to_trans(ret = true)
  trans = TransObject.new(self.title, self.class.name)

  values = retrieve_resource
  values.each do |name, value|
    name = name.name if name.respond_to? :name
    trans[name] = value
  end

  @parameters.each do |name, param|
    # Avoid adding each instance name twice
    next if param.class.isnamevar? and param.value == self.title

    # We've already got property values
    next if param.is_a?(Puppet::Property)
    trans[name] = param.value
  end

  trans.tags = self.tags

  # FIXME I'm currently ignoring 'parent' and 'path'

  trans
end

#typeObject



565
566
567
# File 'lib/vendor/puppet/type.rb', line 565

def type
  self.class.name
end

#uniqueness_keyObject



236
237
238
# File 'lib/vendor/puppet/type.rb', line 236

def uniqueness_key
  self.class.key_attributes.sort_by { |attribute_name| attribute_name.to_s }.map{ |attribute_name| self[attribute_name] }
end

#value(name) ⇒ Object

Return a specific value for an attribute.



570
571
572
573
574
# File 'lib/vendor/puppet/type.rb', line 570

def value(name)
  name = attr_alias(name)

  (obj = @parameters[name] and obj.respond_to?(:value)) ? obj.value : nil
end

#versionObject



576
577
578
579
# File 'lib/vendor/puppet/type.rb', line 576

def version
  return 0 unless catalog
  catalog.version
end

#virtual?Boolean

Returns:

  • (Boolean)


1963
# File 'lib/vendor/puppet/type.rb', line 1963

def virtual?;  !!@virtual;  end