Class: Puppet::ResourceApi::ResourceShim

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/resource_api/glue.rb

Overview

A trivial class to provide the functionality required to push data through the existing type/provider parts of puppet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_hash, typename, namevars, attr_def, catalog = nil) ⇒ ResourceShim

Returns a new instance of ResourceShim.



9
10
11
12
13
14
15
# File 'lib/puppet/resource_api/glue.rb', line 9

def initialize(resource_hash, typename, namevars, attr_def, catalog = nil)
  @values = resource_hash.dup.freeze # whatevs
  @typename = typename
  @namevars = namevars
  @attr_def = attr_def
  @catalog = catalog
end

Instance Attribute Details

#attr_defObject (readonly)

Returns the value of attribute attr_def.



7
8
9
# File 'lib/puppet/resource_api/glue.rb', line 7

def attr_def
  @attr_def
end

#catalogObject (readonly)

Returns the value of attribute catalog.



7
8
9
# File 'lib/puppet/resource_api/glue.rb', line 7

def catalog
  @catalog
end

#namevarsObject (readonly)

Returns the value of attribute namevars.



7
8
9
# File 'lib/puppet/resource_api/glue.rb', line 7

def namevars
  @namevars
end

#typenameObject (readonly)

Returns the value of attribute typename.



7
8
9
# File 'lib/puppet/resource_api/glue.rb', line 7

def typename
  @typename
end

#valuesObject (readonly)

Returns the value of attribute values.



7
8
9
# File 'lib/puppet/resource_api/glue.rb', line 7

def values
  @values
end

Instance Method Details

#filtered_keysObject

attribute names that are not title or namevars



56
57
58
# File 'lib/puppet/resource_api/glue.rb', line 56

def filtered_keys
  values.keys.reject { |k| k == :title || !attr_def[k] || (attr_def[k][:behaviour] == :namevar && @namevars.size == 1) }
end

#prune_parameters(*_args) ⇒ Object



21
22
23
24
# File 'lib/puppet/resource_api/glue.rb', line 21

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

#titleObject



17
18
19
# File 'lib/puppet/resource_api/glue.rb', line 17

def title
  values[:title] || values[@namevars.first]
end

#to_hashObject



51
52
53
# File 'lib/puppet/resource_api/glue.rb', line 51

def to_hash
  values
end

#to_hierayamlObject

Convert our resource to yaml for Hiera purposes.



39
40
41
42
# File 'lib/puppet/resource_api/glue.rb', line 39

def to_hierayaml
  attributes = Hash[filtered_keys.map { |k| [k.to_s, values[k]] }]
  YAML.dump('type' => { title => attributes }).split("\n").drop(2).join("\n") + "\n"
end

#to_jsonObject



44
45
46
47
48
49
# File 'lib/puppet/resource_api/glue.rb', line 44

def to_json(*)
  attrs = filtered_keys.map { |k| [k.to_s, values[k]] unless values[k].nil? }
  attributes = Hash[*attrs.compact.flatten]
  resource = { title => attributes }
  resource.to_json
end

#to_manifestObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/puppet/resource_api/glue.rb', line 26

def to_manifest
  (["#{@typename} { #{Puppet::Parameter.format_value_for_display(title)}: "] + filtered_keys.map do |k|
    cs = ' '
    ce = ''
    if attr_def[k] && 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}" unless values[k].nil?
  end + ['}']).compact.join("\n")
end