Class: Chef::Resource::AwsRoute53RecordSet

Inherits:
Provisioning::AWSDriver::SuperLWRP show all
Defined in:
lib/chef/resource/aws_route53_record_set.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Provisioning::AWSDriver::SuperLWRP

#_pv_is, attribute, lazy

Constructor Details

#initialize(name, *args) ⇒ AwsRoute53RecordSet

Returns a new instance of AwsRoute53RecordSet.



58
59
60
61
# File 'lib/chef/resource/aws_route53_record_set.rb', line 58

def initialize(name, *args)
  self.rr_name(name) unless @rr_name
  super(name, *args)
end

Class Method Details

.verify_unique!(record_sets) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/chef/resource/aws_route53_record_set.rb', line 135

def self.verify_unique!(record_sets)
  seen = {}

  record_sets.each do |rs|
    key = rs.aws_key
    if seen.has_key?(key)
      raise Chef::Exceptions::ValidationFailed.new("Duplicate RecordSet found in resource: [#{key}]")
    else
      seen[key] = 1
    end
  end

  # TODO: be helpful and print out all duplicates, not just the first.

  true
end

Instance Method Details

#aws_keyObject



105
106
107
# File 'lib/chef/resource/aws_route53_record_set.rb', line 105

def aws_key
  "#{fqdn}"
end

#fqdnObject



109
110
111
112
113
114
115
# File 'lib/chef/resource/aws_route53_record_set.rb', line 109

def fqdn
  if rr_name !~ /#{aws_route53_zone_name}\.?$/
    "#{rr_name}.#{aws_route53_zone_name}"
  else
    rr_name
  end
end

#to_aws_change_struct(aws_action) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/chef/resource/aws_route53_record_set.rb', line 126

def to_aws_change_struct(aws_action)
  # there are more elements which are optional, notably 'weight' and 'region': see the API doc at
  # http://redirx.me/?t3zo
  {
    action: aws_action,
    resource_record_set: self.to_aws_struct
  }
end

#to_aws_structObject



117
118
119
120
121
122
123
124
# File 'lib/chef/resource/aws_route53_record_set.rb', line 117

def to_aws_struct
  {
    name: fqdn,
    type: type,
    ttl: ttl,
    resource_records: resource_records.map { |rr| { value: rr } },
  }
end

#validate!Object

because these resources can’t actually converge themselves, we have to trigger the validations.



98
99
100
101
102
103
# File 'lib/chef/resource/aws_route53_record_set.rb', line 98

def validate!
  [:rr_name, :type, :ttl, :resource_records, :aws_route53_zone_name].each { |f| self.send(f) }

  # this was in an :is validator, but didn't play well with inheriting default values.
  validate_rr_type!(type, resource_records)
end

#validate_rr_type!(type, rr_list) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef/resource/aws_route53_record_set.rb', line 63

def validate_rr_type!(type, rr_list)
  case type
  # we'll check for integers, but leave the user responsible for valid DNS names.
  when "A"
    rr_list.all? { |v| v =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ } ||
        raise(::Chef::Exceptions::ValidationFailed,
              "A records are of the form '141.2.25.3'")
  when "MX"
    rr_list.all? { |v| v =~ /^\d+\s+[^ ]+/} ||
        raise(::Chef::Exceptions::ValidationFailed,
              "MX records must have a priority and mail server, of the form '15 mail.example.com.'")
  when "SRV"
    rr_list.all? { |v| v =~ /^\d+\s+\d+\s+\d+\s+[^ ]+$/ } ||
        raise(::Chef::Exceptions::ValidationFailed,
              "SRV records must have a priority, weight, port, and hostname, of the form '15 10 25 service.example.com.'")
  when "CNAME"
    rr_list.size == 1 ||
              raise(::Chef::Exceptions::ValidationFailed,
                    "CNAME records may only have a single value (a hostname).")

  when "TXT", "PTR", "AAAA", "SPF"
    true
  else
    raise ArgumentError, "Argument '#{type}' must be one of #{%w(A MX SRV CNAME TXT PTR AAAA SPF)}"
  end
end

#validate_zone_name!(rr_name, zone_name) ⇒ Object



90
91
92
93
94
95
# File 'lib/chef/resource/aws_route53_record_set.rb', line 90

def validate_zone_name!(rr_name, zone_name)
  if rr_name.end_with?('.') && rr_name !~ /#{zone_name}\.$/
    raise(::Chef::Exceptions::ValidationFailed, "RecordSet name #{rr_name} does not match parent HostedZone name #{zone_name}.")
  end
  true
end