Optional / mutually exclusive resource parameters with the Puppet Resource API

13 views
Skip to first unread message

Benjamin Ridley

unread,
Dec 4, 2019, 11:35:01 AM12/4/19
to Puppet Users
Hi all, 

I'm developing a resource that allows the declaration of keys in the Etcd key value store. 

My type looks like this:

Puppet::ResourceApi.register_type(
  name
: 'etcd_key',
  docs
: 'This type allows the management of etcd keys as Puppet resources.',
  attributes
: {
   
ensure: {
      type
: 'Enum[present, absent]',
      desc
: 'Whether the key should be present in the etcd structure.',
     
default: 'present',
   
},
    path
: {
      type
: 'String',
      desc
: 'The path of the key you want to manage in the etcd structure.',
      behaviour
: :namevar,
   
},
    directory
: {
      type
: 'Boolean',
      desc
: 'Whether this key is a directory or not.',
     
default: false,
   
},
    value
: {
      type
: 'String',
      desc
: 'The value of the key. Mutually exclusive if with is_directory => true.',
   
},
 
},
)


What I want to be able to do is make the value/directory parameters mutually exclusive as a directory does not have a 'value' in etcd, and similarly a key with a value cannot be a directory. Currently though, Puppet complains that I'm excluding required parameters when I declare them.

Is this possible with the resource API or do I need to use the low level provider implementation?

Thanks! Ben

David Schmitt

unread,
Dec 5, 2019, 2:51:00 AM12/5/19
to Puppet Users
Hi Ben,

to avoid having server-side code, the Resource API doesn't have a way to validate complex restrictions like this at compile time. You can either collapse this into a single attribute of type `Variant[Boolean, String]` or adding the validation in the provider at the time `set`/`create`/`update`/`delete` get called. throwing an exception from those methods will tell puppet that the resource application failed and present the exception message to the user.


Regards, David

--
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/5bb1535f-0c14-4aee-98f6-91fd7d68c4f7%40googlegroups.com.

Benjamin Ridley

unread,
Dec 5, 2019, 4:13:57 AM12/5/19
to puppet...@googlegroups.com
Thanks David, I ended up reimplementing with the low level API and just had custom values for the ensure property. Thanks for the help though the Variant method seems like an elegant solution to keep in mind.

Cheers, 
Ben

Reply all
Reply to author
Forward
0 new messages