Uploaded image for project: 'Puppet'
  1. Puppet
  2. PUP-1299

Deprecate and eliminate magic parameter array handling

    XMLWordPrintable

    Details

    • Template:
    • Story Points:
      2
    • Sprint:
      Week 2014-6-18 to 2014-6-25

      Description

      Puppet's parser will automatically de-arrayify single element arrays for parameters (and properties). See `lib/puppet/parser/resource.rb`

      result[p] = if v.is_a?(Array) and v.length == 1
                          v[0]
                        else
                          v
                        end
      

      As a result when writing a custom type, the parameter's validate and munge methods may receive an array or string depending on what values were specified in the manifest. This leads to idioms such as:

      munge do |value|
        # I don't really know how this is happening.
        value = value.shift if value.is_a?(Array)
      

      and

      munge do |value|
        value = [value] unless value.is_a?(Array)
      

      However, the magic only happens when parsing a manifest, not when writing tests that create resources. For example:

      $ puppet apply -e "exec { '/bin/true': environment => [ 'foo=bar']}"
      values = [values] unless values.is_a? Array
      (rdb:1) values
      "foo=bar"
      

      But:

      $ irb
      >> require 'puppet'
      >> Puppet::Type.type(:exec).new(:command => '/bin/true', :environment => ['foo=bar'])
      values = [values] unless values.is_a? Array
      (rdb:1) values
      ["foo=bar"]
      

      If a parameter only accepts a single argument, then the DSL should support this easily, e.g. `newparam(:timeout, :array => false)`

      See also #6746 and https://github.com/puppetlabs/puppet/commit/46252b5bb858a1f2b87cc8646f3a59f935c58061

        Attachments

          Activity

            People

            Assignee:
            Unassigned
            Reporter:
            redmine.exporter redmine.exporter
            QA Contact:
            Kurt Wall
            Votes:
            2 Vote for this issue
            Watchers:
            7 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Zendesk Support