Len,

You can call a class method from an instance just like any other method 
with a parameter. So if you might want to consider calling it with the ldap 
server as a parameter.

def self.ldap_connection(ldap_host)
    Puppet.debug("Creating new LDAP connection")
    unless @ldap_connection
      @ldap_conection = Net::LDAP.new(
        :host => ldap_host,
        .......
     @ldap_connection
   end

and call it from an instance method:

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

Hope this helps.

Bert
Op woensdag 12 februari 2014 04:04:14 UTC+1 schreef Leonard Smith:
>
> 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
>

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-dev/8ad4bb23-251a-4ef7-ac98-5c2221a1cd04%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to