Prefetch in custom types in Puppet v4?

229 views
Skip to first unread message

Jakov Sosic

unread,
Jan 18, 2016, 8:52:18 PM1/18/16
to Puppet Users
Hi guys,


I've noticed the following problem with one of my providers in v4.

This is the original prefetch method I used:

def self.prefetch(resources)
instances.each do |prov|
if resource = resources[prov[:name]]
resource.provider = prov
+ resource.provider = new(prov)
end
end
end



This was throwing out following errors:

Error: Failed to apply catalog: undefined method `intern' for
#<Hash:0x000000038f4708>



After that I changed it to:

def self.prefetch(resources)
instances.each do |prov|
if resource = resources[prov[:name]]
resource.provider = new(prov)
end
end
end

Now, puppet agent doesn't throw out any error, but when I run:

# puppet resource myprovider
Error: Could not run: undefined method `name' for #<Hash:0x00000002705bf0>


Any ideas??

Jakov Sosic

unread,
Jan 18, 2016, 8:54:06 PM1/18/16
to Puppet Users
On 01/19/2016 02:52 AM, Jakov Sosic wrote:
> Hi guys,
>
>
> I've noticed the following problem with one of my providers in v4.
>
> This is the original prefetch method I used:
>
> def self.prefetch(resources)
> instances.each do |prov|
> if resource = resources[prov[:name]]
> resource.provider = prov
> + resource.provider = new(prov)
> end
> end
> end

Sorry, ignore the line with `+` prefix :)

Stefan Schulte

unread,
Jan 26, 2016, 8:47:50 PM1/26/16
to puppet...@googlegroups.com
Are you sure the error is in the prefetch method? Looks to me like the
instances method does not return an array of provider instances but
something else (array of hashes?)

Gareth Rushgrove

unread,
Jan 27, 2016, 4:11:25 AM1/27/16
to puppet...@googlegroups.com
My guess is it's an issue in your self.instances method and a matter
of exactly where you're called new. I nearly always having something
like:


def self.prefetch(resources)
instances.each do |prov|
if resource = resources[prov.name]
resource.provider = prov
end
end
end

Where instances returns an array of objects created using new.

def self.instances
some_list_of_things.collect do |obj|
new({
name: obj.name,
ensure: :present,
})
end
end

Hopefully that helps, if not I'd recommend posting a gist of the
self.instances method or a pointer to the whole provider.

Thanks

Gareth

> Any ideas??
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/569D96C4.6070309%40gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
Gareth Rushgrove
@garethr

devopsweekly.com
morethanseven.net
garethrushgrove.com

Felix Frank

unread,
Feb 11, 2016, 6:02:15 PM2/11/16
to puppet...@googlegroups.com
On 01/27/2016 10:11 AM, Gareth Rushgrove wrote:
I nearly always having something
like:


def self.prefetch(resources)
  instances.each do |prov|
    if resource = resources[prov.name]
      resource.provider = prov
    end
  end
end

Where instances returns an array of objects created using new.

def self.instances
  some_list_of_things.collect do |obj|
    new({
     name: obj.name,
      ensure: :present,
    })
  end
end


Yeah. Come to think of it - that's so boilerplate-y...should we abstract from this? Can we? I'm thinking of a kind of provider DSL call that will take care of creating these methods, and/or make that very simple.

Thoughts?

Cheers,
Felix
Reply all
Reply to author
Forward
0 new messages