Custom type: Ensure array properties are sorted

25 views
Skip to first unread message

Dirk Heinrichs

unread,
Mar 15, 2019, 2:54:36 AM3/15/19
to puppet...@googlegroups.com
Hi,

I've written a custom type that has an array property. I quickly found out that it isn't idempotent unless the array is sorted. Turned out that it was easy to sort the current values in the provider but I couldn't find out how to properly sort the input values w/o putting that burden on the user of the type. I tried with munge like this:

newproperty(:array_prop, :array_matching => :all) do
munge do |value|
value.sort
end
end
but it seems that just gets the first array element as string, which is neither obvious nor intuitive.

Is there any other way than having to use

mytype { 'title':
...
array_prop => sort($array),
...
}

?

Thanks...

Dirk
-- 
Dirk Heinrichs
Senior Systems Engineer, Delivery Pipeline
OpenText ™ Discovery | Recommind
Recommind GmbH, Von-Liebig-Straße 1, 53359 Rheinbach
Vertretungsberechtigte Geschäftsführer Gordon Davies, Madhu Ranganathan, Christian Waida, Registergericht Amtsgericht Bonn, Registernummer HRB 10646
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail sind nicht gestattet.

David Schmitt

unread,
Mar 15, 2019, 5:47:39 AM3/15/19
to puppet...@googlegroups.com
If you use the Resource API (official docs: https://puppet.com/docs/puppet/6.3/custom_resources.html, github: https://github.com/puppetlabs/puppet-resource_api ), adding a canonical internal representation is as easy as adding the `canonicalize` feature, and define the required change on the provider:

  def canonicalize(context, resources)
    resources.each do |r|
      r[:array_prop] = r[:array_prop].sort
    end
  end

The resource api also makes a lot of other pain of developing types and providers go away, and supports all puppet versions from 4.7


Regard, 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/b50e9e0461472e2300db026f567a8ddec77d5cfc.camel%40opentext.com.
For more options, visit https://groups.google.com/d/optout.
--

Dirk Heinrichs

unread,
Mar 15, 2019, 6:35:50 AM3/15/19
to puppet...@googlegroups.com
Am Freitag, den 15.03.2019, 09:47 +0000 schrieb David Schmitt:

If you use the Resource API (official docs: https://puppet.com/docs/puppet/6.3/custom_resources.html, github: https://github.com/puppetlabs/puppet-resource_api ), adding a canonical internal representation is as easy as adding the `canonicalize` feature, and define the required change on the provider:

[...]

The resource api also makes a lot of other pain of developing types and providers go away, and supports all puppet versions from 4.7

Thanks for the hint. Unfortunately that would require me to do a complete rewrite. Will have to see when I find some time...

David Schmitt

unread,
Mar 15, 2019, 12:05:52 PM3/15/19
to puppet...@googlegroups.com
On Fri, Mar 15, 2019 at 10:35 AM Dirk Heinrichs <dhei...@opentext.com> wrote:
Am Freitag, den 15.03.2019, 09:47 +0000 schrieb David Schmitt:

If you use the Resource API (official docs: https://puppet.com/docs/puppet/6.3/custom_resources.html, github: https://github.com/puppetlabs/puppet-resource_api ), adding a canonical internal representation is as easy as adding the `canonicalize` feature, and define the required change on the provider:

[...]

The resource api also makes a lot of other pain of developing types and providers go away, and supports all puppet versions from 4.7

Thanks for the hint. Unfortunately that would require me to do a complete rewrite. Will have to see when I find some time...

I understand that porting over can be a daunting task. After having run a few projects over the last year with the new API, I have to say that it's less work than you might expect: you can re-use most of the business logic code that you already have, and remove a lot of cruft and hacks that are not necessary anymore.


Cheers, David


Thanks...

Dirk
-- 
Dirk Heinrichs
Senior Systems Engineer, Delivery Pipeline
OpenText ™ Discovery | Recommind
Vertretungsberechtigte Geschäftsführer Gordon Davies, Madhu Ranganathan, Christian Waida, Registergericht Amtsgericht Bonn, Registernummer HRB 10646
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail sind nicht gestattet.

--
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.

For more options, visit https://groups.google.com/d/optout.

Branan Riley

unread,
Mar 18, 2019, 10:31:43 AM3/18/19
to puppet...@googlegroups.com
On Fri, Mar 15, 2019 at 8:54 AM Dirk Heinrichs <dhei...@opentext.com> wrote:
Hi,

I've written a custom type that has an array property. I quickly found out that it isn't idempotent unless the array is sorted. Turned out that it was easy to sort the current values in the provider but I couldn't find out how to properly sort the input values w/o putting that burden on the user of the type. I tried with munge like this:

newproperty(:array_prop, :array_matching => :all) do
munge do |value|
value.sort
end
end
but it seems that just gets the first array element as string, which is neither obvious nor intuitive.

Instead of using munge, you can write a custom insync? implementation to compare sorted versions of the current value and the suggested value. It should look something like this:

newproperty(:array_prop, :array_matching => :all) do
    def insync?(is)
        is.sort == should.sort
    end
end


Is there any other way than having to use

mytype { 'title':
...
array_prop => sort($array),
...
}

?

Thanks...

Dirk
-- 
Dirk Heinrichs
Senior Systems Engineer, Delivery Pipeline
OpenText ™ Discovery | Recommind
Recommind GmbH, Von-Liebig-Straße 1, 53359 Rheinbach
Vertretungsberechtigte Geschäftsführer Gordon Davies, Madhu Ranganathan, Christian Waida, Registergericht Amtsgericht Bonn, Registernummer HRB 10646
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail sind nicht gestattet.

--
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.
Reply all
Reply to author
Forward
0 new messages