Defining custom type with a mandatory property

35 views
Skip to first unread message

Angel L. Mateo

unread,
Feb 10, 2015, 6:49:01 AM2/10/15
to puppet...@googlegroups.com
Hello,

I'm trying to create my own custom type according to guide at
http://garylarizza.com/blog/2013/12/15/seriously-what-is-this-provider-doing/.

My problem is that I want one of the resource's properties to be
mandatory and failed if the property is not defined. So in the type I have:

newproperty(:bp) do
defaultto do :absent end
validate do |value|
raise ArgumentError, "'bp' property is mandatory"
end
end

With this code, evertyhing seems fine when I'm programming puppet and
if I try to create a resource without a "bp" property, I get the error
message.

But my problem is that with this code, I can't use "puppet resource"
order, because I get the error too when it tries to get the list of
resources already defined.

Any help?

--
Angel L. Mateo Martínez
Sección de Telemática
Área de Tecnologías de la Información
y las Comunicaciones Aplicadas (ATICA)
http://www.um.es/atica
Tfo: 868887590
Fax: 868888337

Felix Frank

unread,
Mar 1, 2015, 6:03:27 PM3/1/15
to puppet...@googlegroups.com
Hi,

please note that this is a very unusual structure. Properties that are not assigned a desired value will just be left as is by the agent. Upon creating a new resource on the managed system, the *ensure* property can impose a default value.

Are you sure that you want to force the user to supply a property value that Puppet will keep enforcing?

If so, your code is almost correct, assuming that :absent is not a valid value for your property. If it is, you may want to pick another default such as :foo.


newproperty(:bp) do
  defaultto do :absent end
  validate do |value|
    if value == :absent
      raise ArgumentError, "'bp' property is mandatory"
    end
  end
end

HTH,
Felix


On 02/10/2015 11:15 AM, Angel L. Mateo wrote:
Reply all
Reply to author
Forward
0 new messages