Class: ItamaeMitsurin::Resource::AwsRoute53Rrset

Inherits:
Base
  • Object
show all
Defined in:
lib/itamae-mitsurin/resource/aws_route53_rrset.rb

Instance Attribute Summary

Attributes inherited from Base

#attributes, #current_attributes, #notifications, #recipe, #resource_name, #subscriptions, #updated

Instance Method Summary collapse

Methods inherited from Base

#action_nothing, define_attribute, inherited, #initialize, #resource_type, #run

Constructor Details

This class inherits a constructor from ItamaeMitsurin::Resource::Base

Instance Method Details

#action_create(options) ⇒ Object

[View source]

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/itamae-mitsurin/resource/aws_route53_rrset.rb', line 72

def action_create(options)
  if @record.resource_record_sets.empty?
    resp = @route53.change_resource_record_sets(@rrset_hash)
    ItamaeMitsurin.logger.debug "#{resp}"
    ItamaeMitsurin.logger.color(:green) do
      ItamaeMitsurin.logger.info "aws_route53_rrset[#{attributes.name}] created record"
    end
    updated!
  else
    unless /#{attributes.name}/ === @record[0][0][0]
      resp = @route53.change_resource_record_sets(@rrset_hash)
      ItamaeMitsurin.logger.debug "#{resp}"
      ItamaeMitsurin.logger.color(:green) do
        ItamaeMitsurin.logger.info "aws_route53_rrset[#{attributes.name}] created record"
      end
      updated!
    end
  end
end

#action_delete(options) ⇒ Object

[View source]

101
102
103
104
105
106
107
108
109
110
# File 'lib/itamae-mitsurin/resource/aws_route53_rrset.rb', line 101

def action_delete(options)
  if /#{attributes.name}/ === @record[0][0][0]
    resp = @route53.change_resource_record_sets(@rrset_hash)
    ItamaeMitsurin.logger.debug "#{resp}"
    ItamaeMitsurin.logger.color(:green) do
      ItamaeMitsurin.logger.info "aws_route53_rrset[#{attributes.name}] deleted record"
    end
    updated!
  end
end

#action_upsert(options) ⇒ Object

[View source]

92
93
94
95
96
97
98
99
# File 'lib/itamae-mitsurin/resource/aws_route53_rrset.rb', line 92

def action_upsert(options)
  resp = @route53.change_resource_record_sets(@rrset_hash)
  ItamaeMitsurin.logger.debug "#{resp}"
  ItamaeMitsurin.logger.color(:green) do
    ItamaeMitsurin.logger.info "aws_route53_rrset[#{attributes.name}] upserted record"
  end
  updated!
end

#pre_actionObject

[View source]

19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/itamae-mitsurin/resource/aws_route53_rrset.rb', line 19

def pre_action
  @route53 = ::Aws::Route53::Client.new(region: attributes.region)

  @record = @route53.list_resource_record_sets({
      hosted_zone_id: attributes.hosted_zone_id,
      start_record_name: attributes.name,
      start_record_type: attributes.type,
      max_items: 1,
  })

  if attributes.failover == "PRIMARY"
    set_identifier = "PRIMARY-" + attributes.name.split(".")[0]
  elsif attributes.failover == "SECONDARY"
    set_identifier = "SECONDARY-" + attributes.name.split(".")[0]
  else
    set_identifier = nil
  end

  @rrset_hash = {
    hosted_zone_id: attributes.hosted_zone_id,
    change_batch: {
      comment: nil,
      changes: [
        {
          action: attributes.action.to_s.upcase,
          resource_record_set: {
            name: attributes.name,
            type: attributes.type,
            set_identifier: set_identifier,
            failover: attributes.failover,
            ttl: attributes.ttl,
            resource_records: [
              {
                value: attributes.value,
              },
            ],
            health_check_id: attributes.health_check_id,
            traffic_policy_instance_id: attributes.traffic_policy_instance_id,
            },
          },
        ],
      },
    }

  resource_records = []
  if attributes.value.class == Array
    attributes.value.each do |v|
      resource_records << Aws::Route53::Types::ResourceRecord.new(value: v).orig_to_h
    end
    @rrset_hash[:change_batch][:changes][0][:resource_record_set][:resource_records] = resource_records
  end
end