Class: Asg::Rebooter::Wrappers::TargetGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/asg/rebooter/wrappers/target_group.rb

Constant Summary collapse

TARGET_GROUP_INSTANCE_HEALTHY =
'healthy'
POLL_TIMEOUT =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arn) ⇒ TargetGroup

Returns a new instance of TargetGroup.



12
13
14
15
16
# File 'lib/asg/rebooter/wrappers/target_group.rb', line 12

def initialize(arn)
  raise TargetGroupArnRequiredError unless arn

  @arn = arn
end

Instance Attribute Details

#arnObject (readonly)

Returns the value of attribute arn.



10
11
12
# File 'lib/asg/rebooter/wrappers/target_group.rb', line 10

def arn
  @arn
end

Instance Method Details

#attributesObject



18
19
20
21
# File 'lib/asg/rebooter/wrappers/target_group.rb', line 18

def attributes
  target_group_attributes = client.describe_target_group_attributes(target_group_arn: arn)
  target_group_attributes.attributes.map(&:to_hash)
end

#health_for_instance(instance) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/asg/rebooter/wrappers/target_group.rb', line 37

def health_for_instance(instance)
  #
  # TODO: Write specs and error handling
  #
  health_description_for_instance = target_health_descriptions.keep_if do |target_health_description|
    target_health_description.target.id == instance.id
  end

  if health_description_for_instance.any?
    health_description_for_instance.first.target_health.state
  else
    'unknown'
  end
end

#restore_attributesObject



23
24
25
# File 'lib/asg/rebooter/wrappers/target_group.rb', line 23

def restore_attributes
  update_attributes(original_attributes)
end

#target_health_descriptionsObject



33
34
35
# File 'lib/asg/rebooter/wrappers/target_group.rb', line 33

def target_health_descriptions
  client.describe_target_health(target_group_arn: arn).target_health_descriptions
end

#update_attributes(attributes_to_update) ⇒ Object



27
28
29
30
31
# File 'lib/asg/rebooter/wrappers/target_group.rb', line 27

def update_attributes(attributes_to_update)
  client.modify_target_group_attributes \
    target_group_arn: arn,
    attributes: attributes_to_update
end

#wait_for_healthy(instance) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/asg/rebooter/wrappers/target_group.rb', line 52

def wait_for_healthy(instance)
  loop do
    healthy = health_for_instance(instance) == TARGET_GROUP_INSTANCE_HEALTHY

    break if healthy

    sleep(POLL_TIMEOUT)
  end

  true
end