Skip to content

Instantly share code, notes, and snippets.

@fatmcgav
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fatmcgav/e4951f4f27c9884bb31b to your computer and use it in GitHub Desktop.
Save fatmcgav/e4951f4f27c9884bb31b to your computer and use it in GitHub Desktop.
Described_class.defaultprovider example
require 'puppet/provider'
require 'puppet/util/network_device/netapp/device'
class Puppet::Provider::Netapp < Puppet::Provider
attr_accessor :device
def self.transport
if Facter.value(:url) then
Puppet.debug "Puppet::Util::NetworkDevice::Netapp: connecting via facter url."
@device ||= Puppet::Util::NetworkDevice::Netapp::Device.new(Facter.value(:url))
else
@device ||= Puppet::Util::NetworkDevice.current
raise Puppet::Error, "Puppet::Util::NetworkDevice::Netapp: device not initialized #{caller.join("\n")}" unless @device
end
@tranport = @device.transport
end
def transport
# this calls the class instance of self.transport instead of the object instance which causes an infinite loop.
self.class.transport
end
# Helper function for simplifying the execution of NetApp API commands, in a similar fashion to the commands function.
# Arguments should be a hash of 'command name' => 'api command'.
def self.netapp_commands(command_specs)
command_specs.each do |name, apicommand|
# The `create_class_and_instance_method` method was added in puppet 3.0.0
if respond_to? :create_class_and_instance_method
create_class_and_instance_method(name) do |*args|
debug "Executing api call #{[apicommand, args].flatten.join(' ')}"
result = transport.invoke(apicommand, *args)
if result.results_status == 'failed'
raise Puppet::Error, "Executing api call #{[apicommand, args].flatten.join(' ')} failed: #{result.results_reason.inspect}"
end
result
end
else
# workaround for puppet 2.7.x
unless singleton_class.method_defined?(name)
meta_def(name) do |*args|
debug "Executing api call #{[apicommand, args].flatten.join(' ')}"
result = transport.invoke(apicommand, *args)
if result.results_status == 'failed'
raise Puppet::Error, "Executing api call #{[apicommand, args].flatten.join(' ')} failed: #{result.results_reason.inspect}"
end
result
end
end
unless method_defined?(name)
define_method(name) do |*args|
self.class.send(name, *args)
end
end
end
end
end
end
Puppet::Type.newtype(:netapp_export) do
@doc = "Manage Netapp NFS Export creation, modification and deletion."
apply_to_device
ensurable
...
end
require 'spec_helper'
describe Puppet::Type.type(:netapp_export) do
before do
@export_example = {
:name => '/vol/volume/export',
:persistent => true
}
@provider = stub('provider', :class => described_class.defaultprovider, :clear => nil)
described_class.defaultprovider.stubs(:new).returns(@provider)
end
...
end
require 'puppet/provider/netapp'
Puppet::Type.type(:netapp_export).provide(:sevenmode, :parent => Puppet::Provider::Netapp) do
@doc = "Manage Netapp export creation, modification and deletion."
confine :feature => :posix
defaultfor :feature => :posix
# Restrict to 7Mode
confine :false => begin
a = Puppet::Node::Facts.indirection
a.terminus_class = :network_device
a.find(Puppet::Indirector::Request.new(:facts, :find, "clustered", nil))
rescue
:true
end
netapp_commands :elist => 'nfs-exportfs-list-rules-2'
netapp_commands :edel => 'nfs-exportfs-delete-rules'
netapp_commands :eadd => 'nfs-exportfs-append-rules-2'
netapp_commands :emodify => 'nfs-exportfs-modify-rule-2'
mk_resource_methods
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment