Implementing the self.instances method

Puppet provides an additional mode of operation, that being the discovery of resources using the puppet resource command. The self.instances method should implement the return of any instances of a particular resource type that the provider is able to find on the underlying system.

The following example illustrates the use of the rpm -qa command to query for all packages installed on the underlying Red Hat system:

Puppet::Type.type(:mynewtype).provide(:yum) do
...
def self.instances
pkgs = rpm('-qa','--qf','%{NAME} %{VERSION}-%{RELEASE} ')
pkgs.split(" ").collect do |entry|
name, version = entry.split(' ', 2)
new( :name => name,
:ensure => :present,
:version => version
)
end
end
...
end

Each resource returned by self.instances stores the attributes in the @property_hash instance variable. All the other methods in the provider have access to the property hash, so we could implement the exists? and version methods in our provider in a much simpler way, as shown in the following code:

Puppet::Type.type(:mynewtype).provide(:yum) do
...
def exists?
@property_hash[:ensure] == :present
end

def version
@property_hash[:version]
end
...
end
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.150.41