How can I write a Chef provider that extends an existing provider?

泪湿孤枕 提交于 2019-12-11 06:46:08

问题


Cookbook A provides a LWRP that I would like to extend with a LWRP or HWRP in cookbook B, so that I could do something like the following, where provider_b would use the existing code/resources in provider_a and accept additional attributes it could use for it's own actions:

provider_a "A" do
    attr_from_a :value
end

provider_b "B" do
    attr_from_a :value
    attr_from_b :value
end

Is this possible, and is it still possible if I want to avoid editing cookbook A?


回答1:


It sounds like you're trying to create a sub-resource of an existing LWRP, so you're not "wrapping" it - you're "extending it". The LWRP syntax makes this less than desirable, because the resources are dynamically compiled into Ruby classes at runtime.

You could switch to HWRPs (the new Jenkins cookbook is a good example that uses inheritance and OO to extend resources and share attributes). By their nature, LWRPs are not very extensible, since they are dynamically rebuilt and reloaded at runtime.




回答2:


I think the section about Custom LWRPs in the documentation and the tutorial linked at the end should help you.

EDIT: Okay, maybe looking at this LWRP or this LWRP goes a bit more into the right direction (as I think that's a very common pattern for LWRPs, I didn't go so much into detail). You can access the parameters using new_resource.param_a.

So something like this should work:

action :install do
  provider_a "A" do
    param_a new_resource.param_a
  end

  provider_b "B" do
    param_a new_resource.param_a
    param_b new_resource.param_b
  end
end


来源:https://stackoverflow.com/questions/21143396/how-can-i-write-a-chef-provider-that-extends-an-existing-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!