Class: Puppet::Provider::AwsTargetGroup::AwsTargetGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/provider/aws_target_group/aws_target_group.rb

Overview

AwsTargetGroup class

Instance Method Summary collapse

Instance Method Details

#build_hash(resource) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 145

def build_hash(resource)
  target_group = {}
  target_group['health_check_enabled'] = resource[:health_check_enabled] unless resource[:health_check_enabled].nil?
  target_group['health_check_interval_seconds'] = resource[:health_check_interval_seconds] unless resource[:health_check_interval_seconds].nil?
  target_group['health_check_path'] = resource[:health_check_path] unless resource[:health_check_path].nil?
  target_group['health_check_port'] = resource[:health_check_port] unless resource[:health_check_port].nil?
  target_group['health_check_protocol'] = resource[:health_check_protocol] unless resource[:health_check_protocol].nil?
  target_group['health_check_timeout_seconds'] = resource[:health_check_timeout_seconds] unless resource[:health_check_timeout_seconds].nil?
  target_group['healthy_threshold_count'] = resource[:healthy_threshold_count] unless resource[:healthy_threshold_count].nil?
  target_group['load_balancer_arn'] = resource[:load_balancer_arn] unless resource[:load_balancer_arn].nil?
  target_group['matcher'] = resource[:matcher] unless resource[:matcher].nil?
  target_group['name'] = resource[:name] unless resource[:name].nil?
  target_group['names'] = resource[:names] unless resource[:names].nil?
  target_group['page_size'] = resource[:page_size] unless resource[:page_size].nil?
  target_group['port'] = resource[:port] unless resource[:port].nil?
  target_group['protocol'] = resource[:protocol] unless resource[:protocol].nil?
  target_group['target_group_arn'] = resource[:target_group_arn] unless resource[:target_group_arn].nil?
  target_group['target_group_arns'] = resource[:target_group_arns] unless resource[:target_group_arns].nil?
  target_group['target_type'] = resource[:target_type] unless resource[:target_type].nil?
  target_group['unhealthy_threshold_count'] = resource[:unhealthy_threshold_count] unless resource[:unhealthy_threshold_count].nil?
  target_group['vpc_id'] = resource[:vpc_id] unless resource[:vpc_id].nil?
  target_group
end

#build_key_valuesObject



169
170
171
172
173
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 169

def build_key_values
  key_values = {}

  key_values
end

#canonicalize(_context, _resources) ⇒ Object



13
14
15
16
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 13

def canonicalize(_context, _resources)
  # nout to do here but seems we need to implement it
  resources
end

#create(context, name, should) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 121

def create(context, name, should)
  context.creating(name) do
    new_hash = symbolize(build_hash(should))
    client = Aws::ElasticLoadBalancingV2::Client.new(region: region)
    client.create_target_group(new_hash)
  end
rescue StandardError => ex
  Puppet.alert("Exception during create. The state of the resource is unknown.  ex is #{ex} and backtrace is #{ex.backtrace}")
  raise
end

#delete(should) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 179

def delete(should)
  client = Aws::ElasticLoadBalancingV2::Client.new(region: region)
  myhash = {}
  @property_hash.each do |response|
    if name_from_tag(response) == should[:title]
      myhash = response
    end
  end
  client.delete_target_group(namevar => myhash[namevar])
rescue StandardError => ex
  Puppet.alert("Exception during destroy. ex is #{ex} and backtrace is #{ex.backtrace}")
  raise
end

#destroyObject



175
176
177
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 175

def destroy
  delete(resource)
end

#get(context) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 17

def get(context)

  Puppet.debug("Calling instances for region #{region}")
  client = Aws::ElasticLoadBalancingV2::Client.new(region: region)
  all_instances = []
  client.describe_target_groups.each do |response|
    response.target_groups.each do |i|
      hash = instance_to_hash(i)
      all_instances << hash if name?(hash)
    end
  end
  @property_hash = all_instances
  context.debug("Completed get, returning hash #{all_instances}")
  all_instances
end

#instance_to_hash(instance) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 33

def instance_to_hash(instance)
  health_check_enabled = instance.respond_to?(:health_check_enabled) ? (instance.health_check_enabled.respond_to?(:to_hash) ? instance.health_check_enabled.to_hash : instance.health_check_enabled) : nil
  health_check_interval_seconds = instance.respond_to?(:health_check_interval_seconds) ? (instance.health_check_interval_seconds.respond_to?(:to_hash) ? instance.health_check_interval_seconds.to_hash : instance.health_check_interval_seconds) : nil
  health_check_path = instance.respond_to?(:health_check_path) ? (instance.health_check_path.respond_to?(:to_hash) ? instance.health_check_path.to_hash : instance.health_check_path) : nil
  health_check_port = instance.respond_to?(:health_check_port) ? (instance.health_check_port.respond_to?(:to_hash) ? instance.health_check_port.to_hash : instance.health_check_port) : nil
  health_check_protocol = instance.respond_to?(:health_check_protocol) ? (instance.health_check_protocol.respond_to?(:to_hash) ? instance.health_check_protocol.to_hash : instance.health_check_protocol) : nil
  health_check_timeout_seconds = instance.respond_to?(:health_check_timeout_seconds) ? (instance.health_check_timeout_seconds.respond_to?(:to_hash) ? instance.health_check_timeout_seconds.to_hash : instance.health_check_timeout_seconds) : nil
  healthy_threshold_count = instance.respond_to?(:healthy_threshold_count) ? (instance.healthy_threshold_count.respond_to?(:to_hash) ? instance.healthy_threshold_count.to_hash : instance.healthy_threshold_count) : nil
  load_balancer_arn = instance.respond_to?(:load_balancer_arn) ? (instance.load_balancer_arn.respond_to?(:to_hash) ? instance.load_balancer_arn.to_hash : instance.load_balancer_arn) : nil
  matcher = instance.respond_to?(:matcher) ? (instance.matcher.respond_to?(:to_hash) ? instance.matcher.to_hash : instance.matcher) : nil
  names = instance.respond_to?(:names) ? (instance.names.respond_to?(:to_hash) ? instance.names.to_hash : instance.names) : nil
  page_size = instance.respond_to?(:page_size) ? (instance.page_size.respond_to?(:to_hash) ? instance.page_size.to_hash : instance.page_size) : nil
  port = instance.respond_to?(:port) ? (instance.port.respond_to?(:to_hash) ? instance.port.to_hash : instance.port) : nil
  protocol = instance.respond_to?(:protocol) ? (instance.protocol.respond_to?(:to_hash) ? instance.protocol.to_hash : instance.protocol) : nil
  target_group_arn = instance.respond_to?(:target_group_arn) ? (instance.target_group_arn.respond_to?(:to_hash) ? instance.target_group_arn.to_hash : instance.target_group_arn) : nil
  target_group_arns = instance.respond_to?(:target_group_arns) ? (instance.target_group_arns.respond_to?(:to_hash) ? instance.target_group_arns.to_hash : instance.target_group_arns) : nil
  target_type = instance.respond_to?(:target_type) ? (instance.target_type.respond_to?(:to_hash) ? instance.target_type.to_hash : instance.target_type) : nil
  unhealthy_threshold_count = instance.respond_to?(:unhealthy_threshold_count) ? (instance.unhealthy_threshold_count.respond_to?(:to_hash) ? instance.unhealthy_threshold_count.to_hash : instance.unhealthy_threshold_count) : nil
  vpc_id = instance.respond_to?(:vpc_id) ? (instance.vpc_id.respond_to?(:to_hash) ? instance.vpc_id.to_hash : instance.vpc_id) : nil
  hash = {}
  hash[:ensure] = :present
  hash[:object] = instance
  hash[:name] = instance[:target_group_name]
  hash[:tags] = instance.tags if instance.respond_to?(:tags) && !instance.tags.empty?
  hash[:tag_set] = instance.tag_set if instance.respond_to?(:tag_set) && !instance.tag_set.empty?

  hash[:health_check_enabled] = health_check_enabled unless health_check_enabled.nil?
  hash[:health_check_interval_seconds] = health_check_interval_seconds unless health_check_interval_seconds.nil?
  hash[:health_check_path] = health_check_path unless health_check_path.nil?
  hash[:health_check_port] = health_check_port unless health_check_port.nil?
  hash[:health_check_protocol] = health_check_protocol unless health_check_protocol.nil?
  hash[:health_check_timeout_seconds] = health_check_timeout_seconds unless health_check_timeout_seconds.nil?
  hash[:healthy_threshold_count] = healthy_threshold_count unless healthy_threshold_count.nil?
  hash[:load_balancer_arn] = load_balancer_arn unless load_balancer_arn.nil?
  hash[:matcher] = matcher unless matcher.nil?
  hash[:names] = names unless names.nil?
  hash[:page_size] = page_size unless page_size.nil?
  hash[:port] = port unless port.nil?
  hash[:protocol] = protocol unless protocol.nil?
  hash[:target_group_arn] = target_group_arn unless target_group_arn.nil?
  hash[:target_group_arns] = target_group_arns unless target_group_arns.nil?
  hash[:target_type] = target_type unless target_type.nil?
  hash[:unhealthy_threshold_count] = unhealthy_threshold_count unless unhealthy_threshold_count.nil?
  hash[:vpc_id] = vpc_id unless vpc_id.nil?
  hash
end

#name?(hash) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 84

def name?(hash)
  !hash[namevar].nil? && !hash[namevar].empty?
end

#name_from_tag(instance) ⇒ Object



88
89
90
91
92
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 88

def name_from_tag(instance)
  tags = instance.respond_to?(:tags) ? instance.tags : nil
  name = tags.find { |x| x.key == 'Name' } unless tags.nil?
  name.value unless name.nil?
end

#namevarObject



80
81
82
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 80

def namevar
  :target_group_arn
end

#regionObject



117
118
119
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 117

def region
  ENV['AWS_REGION'] || 'us-west-2'
end

#set(context, changes, noop: false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 94

def set(context, changes, noop: false)
  context.debug('Entered set')
  changes.each do |name, change|
    context.debug("set change with #{name} and #{change}")
    is = change.key?(:is) ? change[:is] : get(context).find { |key| key[:id] == name }
    should = change[:should]
    is = { name: name, ensure: 'absent' } if is.nil?
    should = { name: name, ensure: 'absent' } if should.nil?
    if is[:ensure].to_s == 'absent' && should[:ensure].to_s == 'present'
      create(context, name, should) unless noop
    elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'absent'
      context.deleting(name) do
        delete(should) unless noop
      end
    elsif is[:ensure].to_s == 'absent' && should[:ensure].to_s == 'absent'
      context.failed(name, message: 'Unexpected absent to absent change')
    elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'present'
      # if update method exists call update, else delete and recreate the resource
      update(context, name, should)
    end
  end
end

#symbolize(obj) ⇒ Object



193
194
195
196
197
198
199
200
201
202
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 193

def symbolize(obj)
  return obj.reduce({}) do |memo, (k, v)|
    memo.tap { |m| m[k.to_sym] = symbolize(v) }
  end if obj.is_a? Hash

  return obj.reduce([]) do |memo, v|
    memo << symbolize(v); memo
  end if obj.is_a? Array
  obj
end

#update(context, name, should) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/puppet/provider/aws_target_group/aws_target_group.rb', line 133

def update(context, name, should)
  context.updating(name) do
    new_hash = symbolize(build_hash(should))
    client = Aws::ElasticLoadBalancingV2::Client.new(region: region)
    client.modify_target_group(new_hash)
  end
rescue StandardError => ex
  Puppet.alert("Exception during flush. ex is #{ex} and backtrace is #{ex.backtrace}")
  raise
end