Module: PuppetX::IntechWIFI::Tags_Property

Defined in:
lib/puppet_x/intechwifi/tags_property.rb

Class Method Summary collapse

Class Method Details

.delete_tags(region, resource_id, tags, &aws_command) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/puppet_x/intechwifi/tags_property.rb', line 74

def self.delete_tags(region, resource_id, tags, &aws_command)
  args = [
      'ec2', 'delete-tags', '--region', region,
      '--resources', resource_id,
      '--tags'
  ]

  args << tags.map{|x| "Key=#{x}"}

  aws_command.call(args.flatten)

end

.insync?(is, should) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
# File 'lib/puppet_x/intechwifi/tags_property.rb', line 28

def self.insync?(is, should)
  is.class == should.class and
      (!is.is_a?(Hash) or (
        is.keys.all?{|x| should.keys.include? x} and
            should.keys.all?{|x| is.keys.include? x} and
            is.keys.all?{|x| insync?(is[x], should[x])})
      ) and
      (!is.is_a?(Array) or (is.all?{|x| should.include? x} and should.all?{|x| is.include? x}))
end

.parse_tags(tags) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet_x/intechwifi/tags_property.rb', line 38

def self.parse_tags(tags)
  tags.select{|x| x["Key"].downcase != 'name'}.reduce({}) do |h, x|
    value = x["Value"]

    begin
      value = JSON.parse(value)
    rescue StandardError
    end

    h[x["Key"]] = value

    h
  end
end

.set_tags(region, resource_id, tags, &aws_command) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/puppet_x/intechwifi/tags_property.rb', line 62

def self.set_tags(region, resource_id, tags, &aws_command)
  args = [
      'ec2', 'create-tags', '--region', region,
      '--resources', resource_id,
      '--tags'
  ]

  args << tags.map{|x| "Key=#{x[0]},Value='#{x[1].is_a?(String) ? x[1] : x[1].to_json}'"}
  aws_command.call(args.flatten)

end

.update_tags(region, resource_id, current, desired, &aws_command) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/puppet_x/intechwifi/tags_property.rb', line 54

def self.update_tags(region, resource_id, current, desired, &aws_command)
  add = desired.keys.select{|x| !current.keys.include?(x) or desired[x] != current[x]}.map{|x| [x, desired[x]] }
  del = current.keys.select{|x| !desired.keys.include?(x) or desired[x] != current[x]}

  delete_tags(region, resource_id, del, &aws_command) if del.length > 0
  set_tags(region, resource_id, add, &aws_command) if add.length > 0
end

.validate_value(value) ⇒ Object



21
22
23
24
25
26
# File 'lib/puppet_x/intechwifi/tags_property.rb', line 21

def self.validate_value(value)
  fail('The tags property should be a hash of tags and values') if !value.is_a?(Hash)
  fail('We can only support 8 tags') if value.keys.length > 8
  # We block lowercase 'name' as well to avoid confusion later.
  fail('Puppet already uses the tag "Name", it cannot be used inside the tags property') if value.keys.map{|x| x.downcase }.include? 'name'
end