Puppet Custom Provider. Class level and instance level methods/variable access issues

167 views
Skip to first unread message

Len

unread,
Feb 12, 2014, 7:01:20 AM2/12/14
to puppet...@googlegroups.com
To all,

My ruby is failing me as I try to create a custom provider. I have a custom provider I am writing that uses the net-ldap gem that will, based on the custom type create, destroy and modify LDAP entries. What I am struggling with is the difference between the class level methods: self.instance and self.prefetch and instance level methods: create, destroy, etc.

As things currently stand I have in my custom provider code

  def self.ldap_connection  ## Class level method
    Puppet.debug("Creating new LDAP connection")
    unless @ldap_connection
      @ldap_conection = Net::LDAP.new(
        :host => '127.0.01',
        .......
     @ldap_connection
   end

   def self.prefetch           ## Class level method
      ldap_connection.search(:base => Services_Base, :filter => searchFilter ) do |entry|
       .... <code to parse output>
       results << new ( .... )
       results
   end

  def create   ## Instance level method
    self.class.ldap_connection.add(:dn => mydn, :attributes => myattr)
  end


The above all works fine, I can create and destory LDAP entries and modify attributes based on my custom type without a problem. But if you look at the self.ldap_connection I hard-coded the host. What I want to do, is create a parameter in the type, called ldapserver, which I then can use in self.ldap_connect. 

I tried 

@ldap_conection = Net::LDAP.new(
        :host => @resource[:ldapserver],

But when I debug @resource[:ldapserver] it is nil so I'm obviously not access it correctly. I also tried @@resource[:ldapserver] thinking resource is a class level variable, but still no luck. 

I've also tried to make def ldap_connection, so it is an instance level method,but the I run into issues in self.instances where I need to open a LDAP connection to prefetch, and the method is instance level, so not available at the class level, self.instances.

Thanks
Len

Dragu Emil

unread,
Jan 20, 2016, 3:31:25 PM1/20/16
to Puppet Users
I am having the same problem, trying to use the value of a 'param' in self.instances method.
Did you find a way to do this?
Reply all
Reply to author
Forward
0 new messages