Module: GClouder::Resources::DNS

Includes:
Config::CLIArgs, Config::Project, GCloud, Logging
Defined in:
lib/gclouder/resources/dns.rb

Defined Under Namespace

Modules: Local, Records, Remote, Zone

Class Method Summary collapse

Methods included from Logging

#add, #bad, #change, #debug, #error, #fatal, #good, included, #info, log, loggers, #remove, report, #resource_state, setup, #warn, #warning

Methods included from Config::CLIArgs

check, #cli_args, cli_args, included, load, valid_resources

Methods included from Config::Project

load, #project, project

Methods included from Helpers

#hash_to_args, included, #module_exists?, #to_arg, #to_deep_merge_hash, #valid_json?

Methods included from GCloud

#gcloud, included, #verify

Methods included from Shell

included, #shell

Class Method Details

.cleanObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gclouder/resources/dns.rb', line 11

def self.clean
  return if undefined.empty?
  header :clean

  undefined.each do |region, zones|
    info region, indent: 2, heading: true
    zones.each do |zone|
      next unless zone.key?("records")
      info zone["name"], indent: 3, heading: true
      zone["records"].each do |record|
        warning "#{record['name']} IN A #{record['type']} #{record['ttl']} #{record['rrdatas'].join(' ')}", indent: 4
      end
    end
  end
end

.ensureObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/gclouder/resources/dns.rb', line 160

def self.ensure
  return if Local.list.empty?

  header

  Local.list.each do |region, zones|
    info region, heading: true, indent: 2

    zones.each do |zone|
      project_id = zone_project_id(zone)

      next if skip?(project_id, zone)

      info
      Zone.ensure(project_id, zone["name"], zone["zone"])
      Records.ensure(project_id, zone)
    end
  end
end

.header(stage = :ensure) ⇒ Object



156
157
158
# File 'lib/gclouder/resources/dns.rb', line 156

def self.header(stage = :ensure)
  info "[#{stage}] dns", indent: 1, title: true
end

.record?(zones, zone, record) ⇒ Boolean

Returns:



33
34
35
# File 'lib/gclouder/resources/dns.rb', line 33

def self.record?(zones, zone, record)
  zones.fetch_with_default("name", zone, {}).fetch("records", []).fetch_with_default("name", record, {}).empty?
end

.skip?(project_id, zone) ⇒ Boolean

Returns:



180
181
182
183
184
185
186
187
# File 'lib/gclouder/resources/dns.rb', line 180

def self.skip?(project_id, zone)
  return false if project_id == project["project_id"]
  return false if !cli_args[:skip_cross_project_resources]

  extra_info = " [#{project_id}]" if project_id != project["project_id"]
  warning "#{zone['name']}#{extra_info} [skipping] (cross project resource)", indent: 3, heading: true
  true
end

.undefinedObject



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
79
80
# File 'lib/gclouder/resources/dns.rb', line 53

def self.undefined
  return {} if Remote.list.empty?

  Remote.list.each_with_object({ "global" => [] }) do |(_region, zones), collection|
    zones.each do |zone|
      # if zone isnt defined locally, then add it along with its associated records
      if !zone?(zones, zone["name"])
        collection["global"] << zone
        next
      end

      next unless zone.key?("records")

      # if record isnt defined locally, create a zone in global (if one doesn't exist), then append record to records field
      zone["records"].each do |record|
        if !zone?(collection["global"], zone["name"])
          zone_collection = zone.dup
          zone_collection["records"] = []
          collection["global"] << zone_collection
        end

        if !record?(zones, zone["name"], record["name"])
          zone_records_append(collection["global"], zone["name"], record)
        end
      end
    end
  end
end

.update_zone_record(zones, zone, record, key, value) ⇒ Object



27
28
29
30
31
# File 'lib/gclouder/resources/dns.rb', line 27

def self.update_zone_record(zones, zone, record, key, value)
  record = zones.fetch_with_default("name", zone, {}).fetch("records", []).fetch_with_default("name", record, {})
  fatal "couldn't update zone record" if record.empty?
  record[key] = value
end

.validateObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/gclouder/resources/dns.rb', line 82

def self.validate
  return if Local.list.empty?
  header :validate

  failure = false

  Local.list.each do |region, zones|
    info region, indent: 2, heading: true

    unless zones.is_a?(Array)
      failure = true
      bad "zones value should be an array", indent: 3, heading: true
      next
    end

    zones.each do |zone|
      unless zone.is_a?(Hash)
        failure = true
        bad "zone value should be a hash", indent: 3, heading: true
        next
      end

      unless zone.key?("name")
        failure = true
        bad "zone with missing key: name", indent: 3, heading: true
        next
      end

      if zone["name"] !~ /^[a-z0-9\-]+$/
        failure = true
        bad "zone name must only contain lower-case letters, digits or dashes"
        next
      end

      info zone["name"], indent: 3, heading: true

      if zone.key?("zone")
        good "resource has zone specified (#{zone['zone']})", indent: 4
      else
        failure = true
        bad "missing key: zone", indent: 4
      end

      next unless zone.key?("records")

      zone["records"].each do |record|
        info record["name"], indent: 4, heading: true
        if ["A", "CNAME", "PTR", "NS", "TXT"].include?(record["type"])
          good "record has valid type (#{record['type']})", indent: 5
        else
          bad "unknown record type: #{record['type']}", indent: 5
          failure = true
        end

        if record["ttl"].is_a?(Integer)
          good "record has valid ttl (#{record['ttl']})", indent: 5
        else
          bad "record has invalid ttl: #{record['ttl']}", indent: 5
          failure = true
        end

        if record.key?("value") || record.key?("static_ips")
          good "record has a target", indent: 5
        else
          bad "record has no target", indent: 5
          failure = true
        end
      end
    end
  end

  fatal "failure due to invalid config" if failure
end

.zone?(zones, zone) ⇒ Boolean

Returns:



37
38
39
# File 'lib/gclouder/resources/dns.rb', line 37

def self.zone?(zones, zone)
  zones.fetch_with_default("name", zone, {}).empty?
end

.zone_project_id(zone_config) ⇒ Object



189
190
191
192
# File 'lib/gclouder/resources/dns.rb', line 189

def self.zone_project_id(zone_config)
  return project["project_id"] unless zone_config
  zone_config.key?("project_id") ? zone_config["project_id"] : project["project_id"]
end

.zone_record?(zones, zone_name, record_name) ⇒ Boolean

Returns:



41
42
43
44
45
# File 'lib/gclouder/resources/dns.rb', line 41

def self.zone_record?(zones, zone_name, record_name)
  found_zone = zones.find { |z| z["name"] == zone_name }
  return unless found_zone
  found["records"].find { |r| r["name"] == record_name }.nil?
end

.zone_records_append(zones, zone, record) ⇒ Object



47
48
49
50
51
# File 'lib/gclouder/resources/dns.rb', line 47

def self.zone_records_append(zones, zone, record)
  zone = zones.fetch_with_default("name", zone, {})
  fatal "couldn't update zone" if zone.empty?
  zone["records"] << record
end