dirk.heinrichs@altum.de
2013-Mar-13 10:31 UTC
[Puppet Users] Custom ensure instead of ensurable in type provider
Hi, I''m currently writing a custom type for managing Windows environment variables. A variable should be (re-)created if it doesn''t exist or the value doesn''t match the desired value. So exists? checks exactly this. It returns true only if the variable exists and the value matches. This, however, creates a problem when the variable should be destroyed. When destroying a variable, I don''t care for the value anymore. I just need to know wether it exists or not. So I need to different tests (existswithvalue? and exists?). My problem now is that I don''t know how to implement "ensure" for this case, as I cannot use "ensurable" in the type. Thanks... Dirk -- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
jcbollinger
2013-Mar-13 16:00 UTC
[Puppet Users] Re: Custom ensure instead of ensurable in type provider
On Wednesday, March 13, 2013 5:31:09 AM UTC-5, dirk.he...@altum.de wrote:> > Hi, > > I''m currently writing a custom type for managing Windows environment > variables. > > A variable should be (re-)created if it doesn''t exist or the value doesn''t > match the desired value. So exists? checks exactly this. It returns true > only if the variable exists and the value matches. > > This, however, creates a problem when the variable should be destroyed. > When destroying a variable, I don''t care for the value anymore. I just need > to know wether it exists or not. So I need to different tests > (existswithvalue? and exists?). > > My problem now is that I don''t know how to implement "ensure" for this > case, as I cannot use "ensurable" in the type. >Your problem is that you are conflating distinct (for your purposes) aspects of your resource''s state. If you care at times whether the variable is declared at all or not, and at other times what its value is, then those should be separate properties. Your resource declarations will then look like this: env_var { ''AWESOMENESS'': value => ''meh'', # optional: ensure => present } or env_var { ''AWESOMENESS'': ensure => absent } John -- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
dirk.heinrichs@altum.de
2013-Mar-14 07:28 UTC
[Puppet Users] Re: Custom ensure instead of ensurable in type provider
Am Mittwoch, 13. März 2013 17:00:18 UTC+1 schrieb jcbollinger:> > > My problem now is that I don''t know how to implement "ensure" for this >> case, as I cannot use "ensurable" in the type. >> > > Your problem is that you are conflating distinct (for your purposes) > aspects of your resource''s state. If you care at times whether the > variable is declared at all or not, and at other times what its value is, > then those should be separate properties. Your resource declarations will > then look like this: > > env_var { ''AWESOMENESS'': > value => ''meh'', > # optional: > ensure => present > } > > or > > env_var { ''AWESOMENESS'': > ensure => absent > } >I''ve changed the logic of exists? a bit to reflect this, and it works. However, one has to remember not to provide a value if "ensure => absent" was specified. Or is there a way to check this in the provider? Thanks... -- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
jcbollinger
2013-Mar-14 13:52 UTC
[Puppet Users] Re: Custom ensure instead of ensurable in type provider
On Thursday, March 14, 2013 2:28:13 AM UTC-5, dirk.he...@altum.de wrote:> > Am Mittwoch, 13. März 2013 17:00:18 UTC+1 schrieb jcbollinger: > >> >> >> My problem now is that I don''t know how to implement "ensure" for this >>> case, as I cannot use "ensurable" in the type. >>> >> >> Your problem is that you are conflating distinct (for your purposes) >> aspects of your resource''s state. If you care at times whether the >> variable is declared at all or not, and at other times what its value is, >> then those should be separate properties. Your resource declarations will >> then look like this: >> >> env_var { ''AWESOMENESS'': >> value => ''meh'', >> # optional: >> ensure => present >> } >> >> or >> >> env_var { ''AWESOMENESS'': >> ensure => absent >> } >> > > I''ve changed the logic of exists? a bit to reflect this, and it works. > However, one has to remember not to provide a value if "ensure => absent" > was specified. >That''s under your control. Were I you, I would probably implement flushing for this resource type instead of applying changes directly in response to property changes. See http://docs.puppetlabs.com/guides/provider_development.html#flushing. That way your provider can act based on all of its properties together, instead of acting separately on each one. Alternatively, your provider''s value=() method could condition its behavior on its resource''s should(:ensure) value (and perhaps vise versa, too), so that it does not perform needless operations.> Or is there a way to check this in the provider? >There are validation and munging hooks for resource parameters, but they only work on a parameter-by-parameter basis. As far as I know, there is no whole-resource validation hook built into the framework. John -- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Stefan Schulte
2013-Mar-15 22:16 UTC
Re: [Puppet Users] Re: Custom ensure instead of ensurable in type provider
On Wed, 13 Mar 2013 09:00:18 -0700 (PDT) jcbollinger <John.Bollinger@stJude.org> wrote:> Your problem is that you are conflating distinct (for your purposes) > aspects of your resource''s state. If you care at times whether the > variable is declared at all or not, and at other times what its value > is, then those should be separate properties. Your resource > declarations will then look like this: > > env_var { ''AWESOMENESS'': > value => ''meh'', > # optional: > ensure => present > } > > or > > env_var { ''AWESOMENESS'': > ensure => absent > } > > > John >I tend to disagree here. If the presence of an environment variable inherently means that it has a value and the absence of a value inherently means that the resource is absent I don''t see the point in having two properties. I''d suggest to not use ensurable and define the ensure property yourself: in your type: newproperty(:ensure) do newvalues :absent newvalues /.*/ # or whatever is valid for a value end in your provider, drop exists?, create and destroy and use def ensure if value = get_environment_variable(resource[:name]) value else :absent end end def ensure=(new_value) if new_value == :absent destroy_environment_variable(resource[:name]) else set_environment_variable(resource[:name], new_value) end end you can then either specify env_var { ''TMP'': ensure => ''C:\TEMP'' } or env_var { ''TMP'': ensure => absent, } -Stefan -- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Rich Siegel
2013-Mar-15 22:59 UTC
Re: [Puppet Users] Re: Custom ensure instead of ensurable in type provider
Can you publish your work on github. I might be able to help it along. -- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.