Skip to content

Instantly share code, notes, and snippets.

@mgagne
Last active December 8, 2021 20:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgagne/7128846 to your computer and use it in GitHub Desktop.
Save mgagne/7128846 to your computer and use it in GitHub Desktop.
# Information taken from the book 'Puppet Types and Providers'
Puppet::Type.type(:customtype).provide(
:customprovider,
:parent => Puppet::Provider::CustomProvider
) do
# Automagically generates getter and setter for each resource property
mk_resource_methods
def initialize(value={})
super(value)
@property_flush = {}
end
# Prefetch all instances of a resource
def self.prefetch(resources)
# resources parameter is an hash of resources managed by Puppet (catalog)
resource_instances = instances
resources.keys.each do |name|
if provider = resource_instances.find{ |r| r.name == name }
r[name].provider = provider
end
end
end
# Discover and return all instances of the resource found on the system
# Only called when purging resources OR by prefetch
def self.instances
resources = Array.new
resources << new (
:name => 'resource_name',
:ensure => :present,
:version => '1.0'
)
# Return list of resources
resources
end
# Create a resource
def create
# Should update @property_hash accordingly
@property_hash[:ensure] = :present
end
# Destroy a resource
def destroy
@property_hash.clear
end
# Returns True if resource exists
def exists?
@property_hash[:ensure] == :present
end
# Handle refresh notifications (notify)
def refresh
# ...
end
# Called once after Puppet creates, destroys or updates a resource
def flush
if @property_flush
# update resource here
@property_flush = nil
end
@property_hash = resource.to_hash
end
# Returns a property of the resource
# Override magic method created by mk_resource_methods
def version
@property_hash[:version]
end
# Update a property of the resource
# Override magic method created by mk_resource_methods
def version=(value)
@property_hash[:version] = value
# Store updated properties for flush
@property_flush[:version] = value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment