Sha256: 06e6158615e803ee2fb19449b3bc2b9bd9e62be40d59cc05afebd14c85303afa

Contents?: true

Size: 1.94 KB

Versions: 5

Compression:

Stored size: 1.94 KB

Contents

module Puppet::ResourceApi
  # A trivial class to provide the functionality required to push data through the existing type/provider parts of puppet
  class TypeShim
    attr_reader :values, :typename, :namevar, :attr_def

    def initialize(title, resource_hash, typename, namevarname, attr_def)
      # internalize and protect - needs to go deeper
      @values = resource_hash.dup
      # "name" is a privileged key
      @values[namevarname] = title
      @values.freeze

      @typename = typename
      @namevar = namevarname
      @attr_def = attr_def
    end

    def to_resource
      ResourceShim.new(@values, @typename, @namevar, @attr_def)
    end

    def name
      values[@namevar]
    end
  end

  # A trivial class to provide the functionality required to push data through the existing type/provider parts of puppet
  class ResourceShim
    attr_reader :values, :typename, :namevar, :attr_def

    def initialize(resource_hash, typename, namevarname, attr_def)
      @values = resource_hash.dup.freeze # whatevs
      @typename = typename
      @namevar = namevarname
      @attr_def = attr_def
    end

    def title
      values[@namevar]
    end

    def prune_parameters(*_args)
      # puts "not pruning #{args.inspect}" if args.length > 0
      self
    end

    def to_manifest
      (["#{@typename} { #{values[@namevar].inspect}: "] + values.keys.reject { |k| k == @namevar }.map do |k|
        cs = ' '
        ce = ''
        if attr_def[k][:behaviour] && attr_def[k][:behaviour] == :read_only
          cs = '#'
          ce = ' # Read Only'
        end
        "#{cs} #{k} => #{Puppet::Parameter.format_value_for_display(values[k])},#{ce}"
      end + ['}']).join("\n")
    end

    # Convert our resource to yaml for Hiera purposes.
    def to_hierayaml
      (["  #{values[@namevar]}: "] + values.keys.reject { |k| k == @namevar }.map { |k| "    #{k}: #{Puppet::Parameter.format_value_for_display(values[k])}" }).join("\n") + "\n"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
puppet-resource_api-1.1.0 lib/puppet/resource_api/glue.rb
puppet-resource_api-1.0.3 lib/puppet/resource_api/glue.rb
puppet-resource_api-1.0.2 lib/puppet/resource_api/glue.rb
puppet-resource_api-1.0.1 lib/puppet/resource_api/glue.rb
puppet-resource_api-1.0.0 lib/puppet/resource_api/glue.rb