Class: Puppet::Type
- Extended by:
- Enumerable, MetaType::Manager, Util, Util::ClassGen, Util::Logging, Util::ProviderFeatures, Util::Warnings
- Includes:
- Enumerable, FileCollection::Lookup, Util, Util::Errors, Util::LogPaths, Util::Logging, Util::Tagging
- Defined in:
- lib/puppet/type.rb
Defined Under Namespace
Classes: RelationshipMetaparam
Class Attribute Summary collapse
-
.defaultprovider ⇒ Object
Find the default provider.
-
.name ⇒ Object
readonly
Returns the value of attribute name.
-
.properties ⇒ Object
readonly
Returns the value of attribute properties.
-
.providerloader ⇒ Object
Returns the value of attribute providerloader.
-
.self_refresh ⇒ Object
Returns the value of attribute self_refresh.
Instance Attribute Summary collapse
-
#catalog ⇒ Object
The catalog that this resource is stored in.
-
#exported ⇒ Object
is the resource exported.
- #noop ⇒ Object
-
#original_parameters ⇒ Object
readonly
Returns the value of attribute original_parameters.
-
#provider ⇒ Object
Returns the value of attribute provider.
-
#title ⇒ Object
Retrieve the title of an object.
-
#virtual ⇒ Object
is the resource virtual (it should not :-)).
Attributes included from Util::Docs
Attributes included from FileCollection::Lookup
Class Method Summary collapse
-
.[](name) ⇒ Object
retrieve a named instance of the current type.
-
.[]=(name, object) ⇒ Object
add an instance by name to the class list of instances.
-
.alias(name, obj) ⇒ Object
Create an alias.
-
.allattrs ⇒ Object
All parameters, in the appropriate order.
- .apply_to ⇒ Object
- .apply_to_all ⇒ Object
- .apply_to_device ⇒ Object
- .apply_to_host ⇒ Object
-
.attr_alias(param) ⇒ Object
Retrieve an attribute alias, if there is one.
-
.attrclass(name) ⇒ Object
Find the class associated with any given attribute.
-
.attrtype(attr) ⇒ Object
What type of parameter are we dealing with? Cache the results, because this method gets called so many times.
-
.autorequire(name, &block) ⇒ Object
Specify a block for generating a list of objects to autorequire.
- .can_apply_to(target) ⇒ Object
-
.clear ⇒ Object
remove all of the instances of a single type.
-
.create(args) ⇒ Object
Force users to call this, so that we can merge objects if necessary.
-
.delete(resource) ⇒ Object
remove a specified object.
-
.each ⇒ Object
iterate across each of the type’s instances.
-
.eachautorequire ⇒ Object
Yield each of those autorequires in turn, yo.
- .eachmetaparam ⇒ Object
-
.ensurable(&block) ⇒ Object
Create the ‘ensure’ class.
-
.ensurable? ⇒ Boolean
Should we add the ‘ensure’ property to this class?.
-
.handle_param_options(name, options) ⇒ Object
Deal with any options passed into parameters.
-
.has_key?(name) ⇒ Boolean
does the type have an object with the given name?.
-
.hash2resource(hash) ⇒ Object
Convert a simple hash into a Resource instance.
-
.initvars ⇒ Object
all of the variables that must be initialized for each subclass.
-
.instances ⇒ Object
Retrieve all known instances.
-
.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.
- .key_attribute_parameters ⇒ Object
- .key_attributes ⇒ Object
-
.metaparam?(param) ⇒ Boolean
Is the parameter in question a meta-parameter?.
-
.metaparamclass(name) ⇒ Object
Find the metaparameter class associated with a given metaparameter name.
- .metaparamdoc(metaparam) ⇒ Object
- .metaparams ⇒ Object
-
.newmetaparam(name, options = {}, &block) ⇒ Object
Create a new metaparam.
-
.newparam(name, options = {}, &block) ⇒ Object
Create a new parameter.
-
.newproperty(name, options = {}, &block) ⇒ Object
Create a new property.
- .newstate(name, options = {}, &block) ⇒ Object
-
.paramclass(name) ⇒ Object
Find the parameter class associated with a given parameter name.
- .paramdoc(param) ⇒ Object
-
.parameters ⇒ Object
Return the parameter names.
-
.propertybyname(name) ⇒ Object
Return the property class associated with a name.
-
.provide(name, options = {}, &block) ⇒ Object
Create a new provider of a type.
-
.provider(name) ⇒ Object
Retrieve a provider by name.
- .provider_hash ⇒ Object
- .provider_hash_by_type(type) ⇒ Object
-
.providers ⇒ Object
Just list all of the providers.
-
.providers_by_source ⇒ Object
Return a list of one suitable provider per source, with the default provider first.
-
.providify ⇒ Object
Make sure we have a :provider parameter defined.
- .relationship_params ⇒ Object
-
.set_attr_alias(hash) ⇒ Object
Create an alias to an existing attribute.
- .states ⇒ Object
-
.suitableprovider ⇒ Object
Return an array of all of the suitable providers.
- .title_patterns ⇒ Object
- .to_s ⇒ Object
- .unprovide(name) ⇒ Object
-
.valid_parameter?(name) ⇒ Boolean
This is a forward-compatibility method - it’s the validity interface we’ll use in Puppet::Resource.
-
.validate(&block) ⇒ Object
Create a block to validate that our object is set up entirely.
- .validattr?(name) ⇒ Boolean
-
.validparameter?(name) ⇒ Boolean
does the name reflect a valid parameter?.
-
.validproperties ⇒ Object
Return the list of validproperties.
-
.validproperty?(name) ⇒ Boolean
does the name reflect a valid property?.
- .validprovider?(name) ⇒ Boolean
Instance Method Summary collapse
-
#[](name) ⇒ Object
abstract accessing parameters and properties, and normalize access to always be symbols, not strings This returns a value, not an object.
-
#[]=(name, value) ⇒ Object
Abstract setting parameters and properties, and normalize access to always be symbols, not strings.
-
#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.
-
#ancestors ⇒ Object
Code related to evaluating the resources.
- #appliable_to_device? ⇒ Boolean
- #appliable_to_host? ⇒ Boolean
-
#attr_alias(name) ⇒ Object
Return either the attribute alias or the attribute.
-
#autorequire(rel_catalog = nil) ⇒ Object
Figure out of there are any objects we can automatically add as dependencies.
-
#builddepends ⇒ Object
Build the dependencies associated with an individual object.
-
#currentpropvalues ⇒ Object
Get a hash of the current properties.
-
#delete(attr) ⇒ Object
remove a property from the object; useful in testing or in cleanup when an error has been encountered.
-
#deleting? ⇒ Boolean
Are we deleting this resource?.
-
#depthfirst? ⇒ Boolean
Code related to the container behaviour.
-
#eachproperty ⇒ Object
iterate across the existing properties.
-
#event(options = {}) ⇒ Object
Create a transaction event.
- #exported? ⇒ Boolean
-
#finish ⇒ Object
Set up all of our autorequires.
-
#flush ⇒ Object
Flush the provider, if it supports it.
-
#initialize(resource) ⇒ Type
constructor
initialize the type instance.
-
#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.
- #isomorphic? ⇒ Boolean
-
#log(msg) ⇒ Object
create a log at specified level.
-
#managed? ⇒ Boolean
is the instance a managed instance? A ‘yes’ here means that the instance was created from the language, vs.
-
#name ⇒ Object
For now, leave the ‘name’ method functioning like it used to.
-
#name_var ⇒ Object
The name_var is the key_attribute in the case that there is only one.
-
#newattr(name) ⇒ Object
Create the actual attribute instance.
-
#noop? ⇒ Boolean
Are we running in noop mode?.
-
#parameter(name) ⇒ Object
return the value of a parameter.
- #parameters ⇒ Object
-
#parent ⇒ Object
Look up our parent in the catalog, if we have one.
-
#pathbuilder ⇒ Object
Create the path for logging and such.
-
#properties ⇒ Object
Return all of the property objects, in the order specified in the class.
-
#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.
-
#propertydefined?(name) ⇒ Boolean
Is the named property defined?.
-
#purging ⇒ Object
Mark that we’re purging.
-
#purging? ⇒ Boolean
Is this resource being purged? Used by transactions to forbid deletion when there are dependencies.
-
#ref ⇒ Object
Return the “type” style reference.
-
#remove(rmdeps = true) ⇒ Object
Remove an object.
-
#retrieve ⇒ Object
retrieve the current value of all contained properties.
- #retrieve_resource ⇒ Object
- #self_refresh? ⇒ Boolean
-
#set_default(attr) ⇒ Object
For any parameters or properties that have defaults and have not yet been set, set them now.
-
#should(name) ⇒ Object
retrieve the ‘should’ value for a specified property.
- #suitable? ⇒ Boolean
-
#tags=(list) ⇒ Object
Define the initial list of tags.
-
#to_hash ⇒ Object
Convert our object to a hash.
- #to_resource ⇒ Object
-
#to_s ⇒ Object
convert to a string.
-
#to_trans(ret = true) ⇒ Object
Convert to a transportable object.
- #type ⇒ Object
- #uniqueness_key ⇒ Object
-
#value(name) ⇒ Object
Return a specific value for an attribute.
- #version ⇒ Object
- #virtual? ⇒ Boolean
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, recmkdir, secure_open, 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
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, #nodoc?, #pad, scrub
Methods included from Util::Tagging
Methods included from FileCollection::Lookup
#file, #file=, #file_collection
Methods included from Util::LogPaths
Methods included from Util::Errors
#adderrorcontext, #devfail, #error_context, #exceptwrap, #fail
Constructor Details
#initialize(resource) ⇒ Type
initialize the type instance
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 |
# File 'lib/puppet/type.rb', line 1755 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. @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
.defaultprovider ⇒ Object
Find the default provider.
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 |
# File 'lib/puppet/type.rb', line 1385 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 |
.name ⇒ Object (readonly)
Returns the value of attribute name.
1671 1672 1673 |
# File 'lib/puppet/type.rb', line 1671 def name @name end |
.properties ⇒ Object (readonly)
Returns the value of attribute properties.
31 32 33 |
# File 'lib/puppet/type.rb', line 31 def properties @properties end |
.providerloader ⇒ Object
Returns the value of attribute providerloader.
1380 1381 1382 |
# File 'lib/puppet/type.rb', line 1380 def providerloader @providerloader end |
.self_refresh ⇒ Object
Returns the value of attribute self_refresh.
1672 1673 1674 |
# File 'lib/puppet/type.rb', line 1672 def self_refresh @self_refresh end |
Instance Attribute Details
#catalog ⇒ Object
The catalog that this resource is stored in.
1726 1727 1728 |
# File 'lib/puppet/type.rb', line 1726 def catalog @catalog end |
#exported ⇒ Object
is the resource exported
1729 1730 1731 |
# File 'lib/puppet/type.rb', line 1729 def exported @exported end |
#noop ⇒ Object
754 755 756 |
# File 'lib/puppet/type.rb', line 754 def noop noop? end |
#original_parameters ⇒ Object (readonly)
Returns the value of attribute original_parameters.
1752 1753 1754 |
# File 'lib/puppet/type.rb', line 1752 def original_parameters @original_parameters end |
#provider ⇒ Object
Returns the value of attribute provider.
1376 1377 1378 |
# File 'lib/puppet/type.rb', line 1376 def provider @provider end |
#title ⇒ Object
Retrieve the title of an object. If no title was set separately, then use the object’s name.
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 |
# File 'lib/puppet/type.rb', line 1887 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 |
#virtual ⇒ Object
is the resource virtual (it should not :-))
1732 1733 1734 |
# File 'lib/puppet/type.rb', line 1732 def virtual @virtual end |
Class Method Details
.[](name) ⇒ Object
retrieve a named instance of the current type
763 764 765 766 |
# File 'lib/puppet/type.rb', line 763 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
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 |
# File 'lib/puppet/type.rb', line 769 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.
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 |
# File 'lib/puppet/type.rb', line 794 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 |
.allattrs ⇒ Object
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.
42 43 44 |
# File 'lib/puppet/type.rb', line 42 def self.allattrs key_attributes | (parameters & [:provider]) | properties.collect { |property| property.name } | parameters | end |
.apply_to ⇒ Object
129 130 131 |
# File 'lib/puppet/type.rb', line 129 def self.apply_to @apply_to ||= :host end |
.apply_to_all ⇒ Object
125 126 127 |
# File 'lib/puppet/type.rb', line 125 def self.apply_to_all @apply_to = :both end |
.apply_to_device ⇒ Object
117 118 119 |
# File 'lib/puppet/type.rb', line 117 def self.apply_to_device @apply_to = :device end |
.apply_to_host ⇒ Object
121 122 123 |
# File 'lib/puppet/type.rb', line 121 def self.apply_to_host @apply_to = :host end |
.attr_alias(param) ⇒ Object
Retrieve an attribute alias, if there is one.
47 48 49 |
# File 'lib/puppet/type.rb', line 47 def self.attr_alias(param) @attr_aliases[symbolize(param)] end |
.attrclass(name) ⇒ Object
Find the class associated with any given attribute.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/puppet/type.rb', line 60 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.
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/puppet/type.rb', line 77 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.
1591 1592 1593 1594 |
# File 'lib/puppet/type.rb', line 1591 def self.autorequire(name, &block) @autorequires ||= {} @autorequires[name] = block end |
.can_apply_to(target) ⇒ Object
133 134 135 |
# File 'lib/puppet/type.rb', line 133 def self.can_apply_to(target) [ target == :device ? :device : :host, :both ].include?(apply_to) end |
.clear ⇒ Object
remove all of the instances of a single type
816 817 818 819 820 821 822 823 824 825 |
# File 'lib/puppet/type.rb', line 816 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.
829 830 831 832 833 |
# File 'lib/puppet/type.rb', line 829 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
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 |
# File 'lib/puppet/type.rb', line 836 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 |
.each ⇒ Object
iterate across each of the type’s instances
853 854 855 856 857 858 859 |
# File 'lib/puppet/type.rb', line 853 def self.each raise "Global resource iteration is deprecated" return unless defined?(@objects) @objects.each { |name,instance| yield instance } end |
.eachautorequire ⇒ Object
Yield each of those autorequires in turn, yo.
1597 1598 1599 1600 1601 1602 |
# File 'lib/puppet/type.rb', line 1597 def self.eachautorequire @autorequires ||= {} @autorequires.each { |type, block| yield(type, block) } end |
.eachmetaparam ⇒ Object
90 91 92 |
# File 'lib/puppet/type.rb', line 90 def self. @@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.
96 97 98 99 100 101 102 103 104 |
# File 'lib/puppet/type.rb', line 96 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?
107 108 109 110 111 112 113 114 115 |
# File 'lib/puppet/type.rb', line 107 def self.ensurable? # If the class has all three of these methods defined, then it's # ensurable. ens = [:exists?, :create, :destroy].inject { |set, method| set &&= self.public_method_defined?(method) } ens end |
.handle_param_options(name, options) ⇒ Object
Deal with any options passed into parameters.
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/puppet/type.rb', line 138 def self.(name, ) # If it's a boolean parameter, create a method to test the value easily if [: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?
862 863 864 865 |
# File 'lib/puppet/type.rb', line 862 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.
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 |
# File 'lib/puppet/type.rb', line 910 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 |
.initvars ⇒ Object
all of the variables that must be initialized for each subclass
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 |
# File 'lib/puppet/type.rb', line 1681 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 |
.instances ⇒ Object
Retrieve all known instances. Either requires providers or must be overridden.
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 |
# File 'lib/puppet/type.rb', line 868 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.
574 575 576 577 578 579 580 |
# File 'lib/puppet/type.rb', line 574 def self.isomorphic? if defined?(@isomorphic) return @isomorphic else return true end end |
.key_attribute_parameters ⇒ Object
197 198 199 200 201 202 203 |
# File 'lib/puppet/type.rb', line 197 def self.key_attribute_parameters @key_attribute_parameters ||= ( params = @parameters.find_all { |param| param.isnamevar? or param.name == :name } ) end |
.key_attributes ⇒ Object
205 206 207 |
# File 'lib/puppet/type.rb', line 205 def self.key_attributes key_attribute_parameters.collect { |p| p.name } end |
.metaparam?(param) ⇒ Boolean
Is the parameter in question a meta-parameter?
151 152 153 |
# File 'lib/puppet/type.rb', line 151 def self.(param) @@metaparamhash.include?(symbolize(param)) end |
.metaparamclass(name) ⇒ Object
Find the metaparameter class associated with a given metaparameter name.
156 157 158 |
# File 'lib/puppet/type.rb', line 156 def self.(name) @@metaparamhash[symbolize(name)] end |
.metaparamdoc(metaparam) ⇒ Object
164 165 166 |
# File 'lib/puppet/type.rb', line 164 def self.() @@metaparamhash[].doc end |
.metaparams ⇒ Object
160 161 162 |
# File 'lib/puppet/type.rb', line 160 def self. @@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
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/puppet/type.rb', line 170 def self.(name, = {}, &block) @@metaparams ||= [] @@metaparamhash ||= {} name = symbolize(name) param = genclass( name, :parent => [:parent] || Puppet::Parameter, :prefix => "MetaParam", :hash => @@metaparamhash, :array => @@metaparams, :attributes => [:attributes], &block ) # Grr. param.required_features = [:required_features] if [:required_features] (name, ) param. = true param end |
.newparam(name, options = {}, &block) ⇒ Object
Create a new parameter. Requires a block and a name, stores it in the
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/puppet/type.rb', line 226 def self.newparam(name, = {}, &block) [:attributes] ||= {} param = genclass( name, :parent => [:parent] || Puppet::Parameter, :attributes => [:attributes], :block => block, :prefix => "Parameter", :array => @parameters, :hash => @paramhash ) (name, ) # Grr. param.required_features = [:required_features] if [:required_features] param.isnamevar if [: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.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 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 |
# File 'lib/puppet/type.rb', line 260 def self.newproperty(name, = {}, &block) name = symbolize(name) # This is here for types that might still have the old method of defining # a parent class. unless .is_a? Hash raise Puppet::DevError, "Options must be a hash, not #{.inspect}" end raise Puppet::DevError, "Class #{self.name} already has a property named #{name}" if @validproperties.include?(name) if parent = [:parent] .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 => ) do # If they've passed a retrieve method, then override the retrieve # method on the class. if [:retrieve] define_method(:retrieve) do provider.send([: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
249 250 251 252 |
# File 'lib/puppet/type.rb', line 249 def self.newstate(name, = {}, &block) Puppet.warning "newstate() has been deprecrated; use newproperty(#{name})" newproperty(name, , &block) end |
.paramclass(name) ⇒ Object
Find the parameter class associated with a given parameter name.
314 315 316 |
# File 'lib/puppet/type.rb', line 314 def self.paramclass(name) @paramhash[name] end |
.paramdoc(param) ⇒ Object
303 304 305 |
# File 'lib/puppet/type.rb', line 303 def self.paramdoc(param) @paramhash[param].doc end |
.parameters ⇒ Object
Return the parameter names
308 309 310 311 |
# File 'lib/puppet/type.rb', line 308 def self.parameters return [] unless defined?(@parameters) @parameters.collect { |klass| klass.name } end |
.propertybyname(name) ⇒ Object
Return the property class associated with a name
319 320 321 |
# File 'lib/puppet/type.rb', line 319 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.
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 |
# File 'lib/puppet/type.rb', line 1438 def self.provide(name, = {}, &block) name = Puppet::Util.symbolize(name) if unprovide(name) Puppet.debug "Reloading #{name} #{self.name} provider" end parent = if pname = [:parent] .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 [:resource_type] ||= self self.providify provider = genclass( name, :parent => parent, :hash => provider_hash, :prefix => "Provider", :block => block, :include => feature_module, :extend => feature_module, :attributes => ) provider end |
.provider(name) ⇒ Object
Retrieve a provider by name.
1417 1418 1419 1420 1421 1422 1423 |
# File 'lib/puppet/type.rb', line 1417 def self.provider(name) name = Puppet::Util.symbolize(name) # If we don't have it yet, try loading it. @providerloader.load(name) unless provider_hash.has_key?(name) provider_hash[name] end |
.provider_hash ⇒ Object
1412 1413 1414 |
# File 'lib/puppet/type.rb', line 1412 def self.provider_hash Puppet::Type.provider_hash_by_type(self.name) end |
.provider_hash_by_type(type) ⇒ Object
1407 1408 1409 1410 |
# File 'lib/puppet/type.rb', line 1407 def self.provider_hash_by_type(type) @provider_hashes ||= {} @provider_hashes[type] ||= {} end |
.providers ⇒ Object
Just list all of the providers.
1426 1427 1428 |
# File 'lib/puppet/type.rb', line 1426 def self.providers provider_hash.keys end |
.providers_by_source ⇒ Object
Return a list of one suitable provider per source, with the default provider first.
898 899 900 901 902 903 904 905 906 907 |
# File 'lib/puppet/type.rb', line 898 def self.providers_by_source # Put the default provider first, then the rest of the suitable providers. sources = [] [defaultprovider, suitableprovider].flatten.uniq.collect do |provider| next if sources.include?(provider.source) sources << provider.source provider end.compact end |
.providify ⇒ Object
Make sure we have a :provider parameter defined. Only gets called if there are providers.
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 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 |
# File 'lib/puppet/type.rb', line 1481 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 @doc + " Available providers are:\n\n" + parenttype.providers.sort { |a,b| a.to_s <=> b.to_s }.collect { |i| "* **#{i}**: #{parenttype().provider(i).doc}" }.join("\n") 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_params ⇒ Object
1226 1227 1228 |
# File 'lib/puppet/type.rb', line 1226 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.
53 54 55 56 57 |
# File 'lib/puppet/type.rb', line 53 def self.set_attr_alias(hash) hash.each do |new, old| @attr_aliases[symbolize(new)] = symbolize(old) end end |
.states ⇒ Object
34 35 36 37 |
# File 'lib/puppet/type.rb', line 34 def self.states warnonce "The states method is deprecated; use properties" properties end |
.suitableprovider ⇒ Object
Return an array of all of the suitable providers.
1547 1548 1549 1550 1551 1552 1553 1554 |
# File 'lib/puppet/type.rb', line 1547 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_patterns ⇒ Object
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/puppet/type.rb', line 209 def self.title_patterns case key_attributes.length when 0; [] when 1; identity = lambda {|x| x} [ [ /(.*)/m, [ [key_attributes.first, identity ] ] ] ] else raise Puppet::DevError,"you must specify title patterns when there are two or more key attributes" end end |
.to_s ⇒ Object
1710 1711 1712 1713 1714 1715 1716 |
# File 'lib/puppet/type.rb', line 1710 def self.to_s if defined?(@name) "Puppet::Type::#{@name.to_s.capitalize}" else super end end |
.unprovide(name) ⇒ Object
1538 1539 1540 1541 1542 1543 1544 |
# File 'lib/puppet/type.rb', line 1538 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.
355 356 357 |
# File 'lib/puppet/type.rb', line 355 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.
1720 1721 1722 1723 |
# File 'lib/puppet/type.rb', line 1720 def self.validate(&block) define_method(:validate, &block) #@validate = block end |
.validattr?(name) ⇒ Boolean
323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/puppet/type.rb', line 323 def self.validattr?(name) name = symbolize(name) return true if name == :name @validattrs ||= {} unless @validattrs.include?(name) @validattrs[name] = !!(self.validproperty?(name) or self.validparameter?(name) or self.(name)) end @validattrs[name] end |
.validparameter?(name) ⇒ Boolean
does the name reflect a valid parameter?
349 350 351 352 |
# File 'lib/puppet/type.rb', line 349 def self.validparameter?(name) raise Puppet::DevError, "Class #{self} has not defined parameters" unless defined?(@parameters) !!(@paramhash.include?(name) or @@metaparamhash.include?(name)) end |
.validproperties ⇒ Object
Return the list of validproperties
342 343 344 345 346 |
# File 'lib/puppet/type.rb', line 342 def self.validproperties return {} unless defined?(@parameters) @validproperties.keys end |
.validproperty?(name) ⇒ Boolean
does the name reflect a valid property?
336 337 338 339 |
# File 'lib/puppet/type.rb', line 336 def self.validproperty?(name) name = symbolize(name) @validproperties.include?(name) && @validproperties[name] end |
Instance Method Details
#[](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)’.
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/puppet/type.rb', line 397 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.
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/puppet/type.rb', line 418 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: #{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
376 377 378 379 380 381 382 |
# File 'lib/puppet/type.rb', line 376 def add_property_parameter(prop_name) if self.class.validproperty?(prop_name) && !@parameters[prop_name] self.newattr(prop_name) return true end false end |
#ancestors ⇒ Object
Code related to evaluating the resources.
638 639 640 |
# File 'lib/puppet/type.rb', line 638 def ancestors [] end |
#appliable_to_device? ⇒ Boolean
1941 1942 1943 |
# File 'lib/puppet/type.rb', line 1941 def appliable_to_device? self.class.can_apply_to(:device) end |
#appliable_to_host? ⇒ Boolean
1945 1946 1947 |
# File 'lib/puppet/type.rb', line 1945 def appliable_to_host? self.class.can_apply_to(:host) end |
#attr_alias(name) ⇒ Object
Return either the attribute alias or the attribute.
360 361 362 363 364 365 366 367 |
# File 'lib/puppet/type.rb', line 360 def attr_alias(name) name = symbolize(name) 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.
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 |
# File 'lib/puppet/type.rb', line 1606 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 |
#builddepends ⇒ Object
Build the dependencies associated with an individual object.
1637 1638 1639 1640 1641 1642 1643 1644 |
# File 'lib/puppet/type.rb', line 1637 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 |
#currentpropvalues ⇒ Object
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.
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 |
# File 'lib/puppet/type.rb', line 720 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
446 447 448 449 450 451 452 453 |
# File 'lib/puppet/type.rb', line 446 def delete(attr) attr = symbolize(attr) 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?
370 371 372 |
# File 'lib/puppet/type.rb', line 370 def deleting? obj = @parameters[:ensure] and obj.should == :absent end |
#depthfirst? ⇒ Boolean
Code related to the container behaviour.
612 613 614 |
# File 'lib/puppet/type.rb', line 612 def depthfirst? false end |
#eachproperty ⇒ Object
iterate across the existing properties
456 457 458 459 460 461 |
# File 'lib/puppet/type.rb', line 456 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.
465 466 467 |
# File 'lib/puppet/type.rb', line 465 def event( = {}) Puppet::Transaction::Event.new({:resource => self, :file => file, :line => line, :tags => }.merge()) end |
#exported? ⇒ Boolean
1939 |
# File 'lib/puppet/type.rb', line 1939 def exported?; !!@exported; end |
#finish ⇒ Object
Set up all of our autorequires.
1829 1830 1831 1832 1833 1834 1835 1836 1837 |
# File 'lib/puppet/type.rb', line 1829 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 |
#flush ⇒ Object
Flush the provider, if it supports it. This is called by the transaction.
644 645 646 |
# File 'lib/puppet/type.rb', line 644 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
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 |
# File 'lib/puppet/type.rb', line 651 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
582 583 584 |
# File 'lib/puppet/type.rb', line 582 def isomorphic? self.class.isomorphic? end |
#log(msg) ⇒ Object
create a log at specified level
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 |
# File 'lib/puppet/type.rb', line 1735 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
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
# File 'lib/puppet/type.rb', line 590 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 |
#name ⇒ Object
For now, leave the ‘name’ method functioning like it used to. Once ‘title’ works everywhere, I’ll switch it.
1841 1842 1843 |
# File 'lib/puppet/type.rb', line 1841 def name self[:name] end |
#name_var ⇒ Object
The name_var is the key_attribute in the case that there is only one.
387 388 389 390 |
# File 'lib/puppet/type.rb', line 387 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.
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/puppet/type.rb', line 478 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) } info "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?
742 743 744 745 746 747 748 749 750 751 752 |
# File 'lib/puppet/type.rb', line 742 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
500 501 502 |
# File 'lib/puppet/type.rb', line 500 def parameter(name) @parameters[name.to_sym] end |
#parameters ⇒ Object
504 505 506 |
# File 'lib/puppet/type.rb', line 504 def parameters @parameters.dup end |
#parent ⇒ Object
Look up our parent in the catalog, if we have one.
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 |
# File 'lib/puppet/type.rb', line 1846 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 |
#pathbuilder ⇒ Object
Create the path for logging and such.
935 936 937 938 939 940 941 |
# File 'lib/puppet/type.rb', line 935 def pathbuilder if p = parent [p.pathbuilder, self.ref].flatten else [self.ref] end end |
#properties ⇒ Object
Return all of the property objects, in the order specified in the class.
567 568 569 |
# File 'lib/puppet/type.rb', line 567 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.
517 518 519 |
# File 'lib/puppet/type.rb', line 517 def property(name) (obj = @parameters[symbolize(name)] and obj.is_a?(Puppet::Property)) ? obj : nil end |
#propertydefined?(name) ⇒ Boolean
Is the named property defined?
509 510 511 512 |
# File 'lib/puppet/type.rb', line 509 def propertydefined?(name) name = name.intern unless name.is_a? Symbol @parameters.include?(name) end |
#purging ⇒ Object
Mark that we’re purging.
1871 1872 1873 |
# File 'lib/puppet/type.rb', line 1871 def purging @purging = true end |
#purging? ⇒ Boolean
Is this resource being purged? Used by transactions to forbid deletion when there are dependencies.
1877 1878 1879 1880 1881 1882 1883 |
# File 'lib/puppet/type.rb', line 1877 def purging? if defined?(@purging) @purging else false end end |
#ref ⇒ Object
Return the “type” style reference.
1862 1863 1864 |
# File 'lib/puppet/type.rb', line 1862 def 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.
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 |
# File 'lib/puppet/type.rb', line 618 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 |
#retrieve ⇒ Object
retrieve the current value of all contained properties
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 |
# File 'lib/puppet/type.rb', line 685 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_resource ⇒ Object
711 712 713 714 715 |
# File 'lib/puppet/type.rb', line 711 def retrieve_resource resource = retrieve resource = Resource.new(type, title, :parameters => resource) if resource.is_a? Hash resource end |
#self_refresh? ⇒ Boolean
1866 1867 1868 |
# File 'lib/puppet/type.rb', line 1866 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.
524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/puppet/type.rb', line 524 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
470 471 472 473 |
# File 'lib/puppet/type.rb', line 470 def should(name) name = attr_alias(name) (prop = @parameters[name] and prop.is_a?(Puppet::Property)) ? prop.should : nil end |
#suitable? ⇒ Boolean
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 |
# File 'lib/puppet/type.rb', line 1556 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.
1647 1648 1649 1650 |
# File 'lib/puppet/type.rb', line 1647 def (list) tag(self.class.name) tag(*list) end |
#to_hash ⇒ Object
Convert our object to a hash. This just includes properties.
539 540 541 542 543 544 545 546 547 |
# File 'lib/puppet/type.rb', line 539 def to_hash rethash = {} @parameters.each do |name, obj| rethash[name] = obj.value end rethash end |
#to_resource ⇒ Object
1932 1933 1934 1935 1936 |
# File 'lib/puppet/type.rb', line 1932 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_s ⇒ Object
convert to a string
1902 1903 1904 |
# File 'lib/puppet/type.rb', line 1902 def to_s self.ref end |
#to_trans(ret = true) ⇒ Object
Convert to a transportable object
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 |
# File 'lib/puppet/type.rb', line 1907 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. = self. # FIXME I'm currently ignoring 'parent' and 'path' trans end |
#type ⇒ Object
549 550 551 |
# File 'lib/puppet/type.rb', line 549 def type self.class.name end |
#uniqueness_key ⇒ Object
220 221 222 |
# File 'lib/puppet/type.rb', line 220 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.
554 555 556 557 558 |
# File 'lib/puppet/type.rb', line 554 def value(name) name = attr_alias(name) (obj = @parameters[name] and obj.respond_to?(:value)) ? obj.value : nil end |
#version ⇒ Object
560 561 562 563 |
# File 'lib/puppet/type.rb', line 560 def version return 0 unless catalog catalog.version end |
#virtual? ⇒ Boolean
1938 |
# File 'lib/puppet/type.rb', line 1938 def virtual?; !!@virtual; end |