Class: Terrafying::Components::Zone

Inherits:
Terrafying::Context
  • Object
show all
Defined in:
lib/terrafying/components/zone.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZone

Returns a new instance of Zone.



22
23
24
# File 'lib/terrafying/components/zone.rb', line 22

def initialize
  super
end

Instance Attribute Details

#fqdnObject (readonly)

Returns the value of attribute fqdn.



8
9
10
# File 'lib/terrafying/components/zone.rb', line 8

def fqdn
  @fqdn
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/terrafying/components/zone.rb', line 8

def id
  @id
end

Class Method Details

.create(fqdn, options = {}) ⇒ Object



18
19
20
# File 'lib/terrafying/components/zone.rb', line 18

def self.create(fqdn, options = {})
  Zone.new.create fqdn, options
end

.find(fqdn) ⇒ Object



10
11
12
# File 'lib/terrafying/components/zone.rb', line 10

def self.find(fqdn)
  Zone.new.find fqdn
end

.find_by_tag(tag) ⇒ Object



14
15
16
# File 'lib/terrafying/components/zone.rb', line 14

def self.find_by_tag(tag)
  Zone.new.find_by_tag tag
end

Instance Method Details

#add_alias(name, config) ⇒ Object



97
98
99
# File 'lib/terrafying/components/zone.rb', line 97

def add_alias(name, config)
  add_alias_in(self, name, config)
end

#add_alias_in(ctx, name, config) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/terrafying/components/zone.rb', line 101

def add_alias_in(ctx, name, config)
  fqdn = qualify(name)
  ctx.resource :aws_route53_record, tf_safe(fqdn),
               zone_id: @id,
               name: fqdn,
               type: 'A',
               alias: config
end

#add_cname(name, *records) ⇒ Object



126
127
128
# File 'lib/terrafying/components/zone.rb', line 126

def add_cname(name, *records)
  add_cname_in(self, name, *records)
end

#add_cname_in(ctx, name, *records) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/terrafying/components/zone.rb', line 130

def add_cname_in(ctx, name, *records)
  fqdn = qualify(name)
  ident = fqdn.tr('.', '-')
  ctx.resource :aws_route53_record, ident,
               zone_id: @id,
               name: fqdn,
               type: 'CNAME',
               ttl: 300,
               records: records
end

#add_record(name, records, options = {}) ⇒ Object



80
81
82
# File 'lib/terrafying/components/zone.rb', line 80

def add_record(name, records, options = {})
  add_record_in(self, name, records, options)
end

#add_record_in(ctx, name, records, options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/terrafying/components/zone.rb', line 84

def add_record_in(ctx, name, records, options = {})
  options = {
    type: 'A',
    ttl: 300,
    name: qualify(name)
  }.merge(options)

  ctx.resource :aws_route53_record, options[:resource_name] || tf_safe(options[:name]), {
    zone_id: @id,
    records: records
  }.merge(options.slice(:type, :ttl, :name, :records, :zone_id, :weight))
end

#add_srv(name, service_name, port, type, hosts) ⇒ Object



110
111
112
# File 'lib/terrafying/components/zone.rb', line 110

def add_srv(name, service_name, port, type, hosts)
  add_srv_in(self, name, service_name, port, type, hosts)
end

#add_srv_in(ctx, name, service_name, port, type, hosts) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/terrafying/components/zone.rb', line 114

def add_srv_in(ctx, name, service_name, port, type, hosts)
  fqdn = qualify(name)
  ident = tf_safe(fqdn)

  ctx.resource :aws_route53_record, "srv-#{ident}-#{service_name}",
               zone_id: @id,
               name: "_#{service_name}._#{type}.#{fqdn}",
               type: 'SRV',
               ttl: '60',
               records: hosts.map { |host| "0 0 #{port} #{host}" }
end

#create(fqdn, options = {}) ⇒ Object



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/terrafying/components/zone.rb', line 43

def create(fqdn, options = {})
  options = {
    tags: {}
  }.merge(options)

  ident = tf_safe(fqdn)

  zone_config = {
    name: fqdn,
    tags: options[:tags]
  }

  if options[:vpc]
    zone_config[:vpc] = {
      vpc_id: options[:vpc].id
    }

    ident = tf_safe("#{options[:vpc].name}-#{ident}")
  end

  @fqdn = fqdn
  @id = resource :aws_route53_zone, ident, zone_config

  if options[:parent_zone]
    ns = (0..3).map { |i| output_of(:aws_route53_zone, ident, "name_servers.#{i}") }

    resource :aws_route53_record, "#{ident}-ns",
             zone_id: options[:parent_zone].id,
             name: fqdn,
             type: 'NS',
             ttl: '30',
             records: ns
  end

  self
end

#find(fqdn) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/terrafying/components/zone.rb', line 26

def find(fqdn)
  zone = aws.hosted_zone(fqdn)

  @id = zone.id
  @fqdn = fqdn

  self
end

#find_by_tag(tag) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/terrafying/components/zone.rb', line 35

def find_by_tag(tag)
  zone = aws.hosted_zone_by_tag(tag)
  @id = zone.id
  @fqdn = zone.name.chomp('.')

  self
end

#qualify(name) ⇒ Object



141
142
143
# File 'lib/terrafying/components/zone.rb', line 141

def qualify(name)
  "#{name}.#{@fqdn}"
end