Rich Rauenzahn
2011-Mar-16 16:51 UTC
[Puppet Users] parsing (and returning) json data in puppet in custom function
Now that puppet supports dictionaries, we would like to use it to import some data into puppet from some external tools. At the moment I''ve written: module Puppet::Parser::Functions newfunction(:json_parse, :type => :rvalue, :doc => " Parse json string and return result. ") do |args| begin ret = PSON.parse(args.shift) rescue ret = nil end ret end end This works fairly well, except we found that a json element that had a value of none (in json) came back with a weird value -- it was neither undef nor was it "" (as interpreted by puppet). I looked at the class.to_s of the item and it was a NilClass (if my memory is correct.. but it was definite Nil something). So it seems that I probably need to walk the results of PSON and change any leaf items of type NilClass to either "" or an undef class that puppet actually understands? also, is there an easy ruby way of walking that return from PSON.parse and changing all NilClass to something? -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Daniel Pittman
2011-Mar-16 20:56 UTC
Re: [Puppet Users] parsing (and returning) json data in puppet in custom function
On Wed, Mar 16, 2011 at 09:51, Rich Rauenzahn <rrauenza@gmail.com> wrote:> Now that puppet supports dictionaries, we would like to use it to > import some data into puppet from some external tools.[…]> This works fairly well, except we found that a json element that had a > value of none (in json) came back with a weird value -- it was neither > undef nor was it "" (as interpreted by puppet). I looked at the > class.to_s of the item and it was a NilClass (if my memory is > correct.. but it was definite Nil something). > > So it seems that I probably need to walk the results of PSON and > change any leaf items of type NilClass to either "" or an undef class > that puppet actually understands?Well, the only "value" that puppet considers false OOTB is "", and it doesn''t really have much of a boolean type, so that would be the best substitution.> also, is there an easy ruby way of walking that return from PSON.parse > and changing all NilClass to something?Off-hand, not that I know of. Sorry. Daniel -- ⎋ Puppet Labs Developer – http://puppetlabs.com ✉ Daniel Pittman <daniel@puppetlabs.com> ✆ Contact me via gtalk, email, or phone: +1 (877) 575-9775 ♲ Made with 100 percent post-consumer electrons -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Rich Rauenzahn
2011-Mar-17 01:43 UTC
Re: [Puppet Users] parsing (and returning) json data in puppet in custom function
I see there is a bug open about this, http://projects.puppetlabs.com/issues/6621 but I can''t login to puppetlabs. define test() { $foo3 = json_parse("bad|,'''':") notice($foo3) $type = type($foo3) # our custom function that returns class notice("type=$type") if $foo3 == "" { notice("foo3 is ''''") } if $foo3 == undef { notice("foo3 is undef") } if !$foo3 { notice("foo3 is not true") } if $foo3 { notice("foo3 is true") } $foo4 = undef if $foo4 == undef { notice("foo4 is undef") } if $foo4 == "" { notice("foo4 is ''''") } } test { a: } notice: Scope(Test[a]): type=NilClass notice: Scope(Test[a]): foo3 is true notice: Scope(Test[a]): foo4 is undef notice: Scope(Test[a]): foo4 is '''' So nil from a custom function evaluates as true, and neither "" nor undef. The bug mentions ... (actually, I''ll BCC Nigel, the author of that comment) ''and since we introduced the Ruby DSL, you’re free to incorporate manifests that use the actual nil'' How? Maybe that doesn''t do what I hope it does.. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Daniel Pittman
2011-Mar-17 03:48 UTC
Re: [Puppet Users] parsing (and returning) json data in puppet in custom function
On Wed, Mar 16, 2011 at 18:43, Rich Rauenzahn <rrauenza@gmail.com> wrote:> I see there is a bug open about this, > http://projects.puppetlabs.com/issues/6621 > but I can''t login to puppetlabs.If you didn''t already have an account you can just register; if you did, I think there is some interactive way to reset your own password, but ping me if not and I will get someone to reset it for you. […]> The bug mentions ... (actually, I''ll BCC Nigel, the author of that comment) > > ''and since we introduced the Ruby DSL, you’re free to incorporate > manifests that use the actual nil'' > > How? Maybe that doesn''t do what I hope it does..I suspect that only works in the Ruby DSL, not in the Puppet DSL, even if you are returning function values to it. (The Ruby DSL replaces the pp files with Ruby, which I imagine you knew, but just in case...) Daniel -- ⎋ Puppet Labs Developer – http://puppetlabs.com ✉ Daniel Pittman <daniel@puppetlabs.com> ✆ Contact me via gtalk, email, or phone: +1 (877) 575-9775 ♲ Made with 100 percent post-consumer electrons -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Nigel Kersten
2011-Mar-17 22:55 UTC
Re: [Puppet Users] parsing (and returning) json data in puppet in custom function
On Wed, Mar 16, 2011 at 8:48 PM, Daniel Pittman <daniel@puppetlabs.com> wrote:> On Wed, Mar 16, 2011 at 18:43, Rich Rauenzahn <rrauenza@gmail.com> wrote: > >> I see there is a bug open about this, >> http://projects.puppetlabs.com/issues/6621 >> but I can''t login to puppetlabs. > > If you didn''t already have an account you can just register; if you > did, I think there is some interactive way to reset your own password, > but ping me if not and I will get someone to reset it for you. > > […] >> The bug mentions ... (actually, I''ll BCC Nigel, the author of that comment) >> >> ''and since we introduced the Ruby DSL, you’re free to incorporate >> manifests that use the actual nil'' >> >> How? Maybe that doesn''t do what I hope it does.. > > I suspect that only works in the Ruby DSL, not in the Puppet DSL, even > if you are returning function values to it. (The Ruby DSL replaces > the pp files with Ruby, which I imagine you knew, but just in case...)Yep, exactly. I''m interested in a more general solution to this problem however. Can you elaborate on your use case a bit more? -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.