Module: MovableInk::AWS::Route53

Included in:
MovableInk::AWS
Defined in:
lib/movable_ink/aws/route53.rb

Instance Method Summary collapse

Instance Method Details

#delete_resource_record_sets(zone, instance_name, client = nil, expected_errors = []) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/movable_ink/aws/route53.rb', line 19

def delete_resource_record_sets(zone, instance_name, client = nil, expected_errors = [])
  resource_record_sets = get_resource_record_sets_by_instance_name(zone, instance_name, client)
  return if resource_record_sets.empty?

  change_batch = {
    "changes": resource_record_sets.map { |resource_record_set|
      {
        "action": 'DELETE',
        "resource_record_set": resource_record_set
      }
    }
  }

  run_with_backoff(expected_errors: expected_errors) do
    route53(client).change_resource_record_sets({change_batch: change_batch, hosted_zone_id: zone})
  end
end

#find_health_check_by_tag(key, value, client = nil) ⇒ Object



69
70
71
72
73
# File 'lib/movable_ink/aws/route53.rb', line 69

def find_health_check_by_tag(key, value, client = nil)
  list_health_checks(client).detect do |health_check|
    get_health_check_tags(health_check.id, client).detect { |tag| tag.key == key && tag.value.include?(value) }
  end
end

#get_health_check_tags(health_check_id, client = nil) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/movable_ink/aws/route53.rb', line 60

def get_health_check_tags(health_check_id, client = nil)
  run_with_backoff do
    route53(client).list_tags_for_resource({
      resource_type: 'healthcheck',
      resource_id: health_check_id
    }).resource_tag_set.tags
  end
end

#get_resource_record_sets_by_instance_name(zone, instance_name, client = nil) ⇒ Object



15
16
17
# File 'lib/movable_ink/aws/route53.rb', line 15

def get_resource_record_sets_by_instance_name(zone, instance_name, client = nil)
  resource_record_sets(zone, client).select{|rrs| rrs.set_identifier == instance_name}.map(&:to_h)
end

#list_all_r53_resource_record_sets(hosted_zone_id, client = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/movable_ink/aws/route53.rb', line 37

def list_all_r53_resource_record_sets(hosted_zone_id, client = nil)
  resp = run_with_backoff { route53(client).list_resource_record_sets({ hosted_zone_id: hosted_zone_id }) }
  rrs = resp.resource_record_sets

  # https://docs.aws.amazon.com/Route53/latest/APIReference/API_ListResourceRecordSets.html
  while resp.is_truncated
    resp = run_with_backoff { route53(client).list_resource_record_sets({
      hosted_zone_id: hosted_zone_id,
      start_record_name: resp.next_record_name,
      start_record_type: resp.next_record_type,
      start_record_identifier: resp.next_record_identifier
    }) }
    rrs += resp.resource_record_sets
  end
  rrs
end

#list_health_checks(client = nil) ⇒ Object



54
55
56
57
58
# File 'lib/movable_ink/aws/route53.rb', line 54

def list_health_checks(client = nil)
  run_with_backoff do
    route53(client).list_health_checks().flat_map(&:health_checks)
  end
end

#list_hosted_zones(client: nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/movable_ink/aws/route53.rb', line 75

def list_hosted_zones(client: nil)
  resp = run_with_backoff { route53(client).list_hosted_zones() }
  zones = resp.hosted_zones

  # https://docs.aws.amazon.com/Route53/latest/APIReference/API_ListHostedZones.html
  while resp.is_truncated
    resp = run_with_backoff { route53(client).list_hosted_zones({marker: resp.next_marker}) }
    zones += resp.hosted_zones
  end
  zones
end

#resource_record_sets(hosted_zone_id, client = nil) ⇒ Object



10
11
12
13
# File 'lib/movable_ink/aws/route53.rb', line 10

def resource_record_sets(hosted_zone_id, client = nil)
  @resource_record_sets ||= {}
  @resource_record_sets[hosted_zone_id] ||= list_all_r53_resource_record_sets(hosted_zone_id, client)
end

#route53(client = nil) ⇒ Object



6
7
8
# File 'lib/movable_ink/aws/route53.rb', line 6

def route53(client = nil)
  @route53_client ||= (client) ? client : Aws::Route53::Client.new(region: 'us-east-1')
end