Class: BuildCloud::Zone

Inherits:
Object
  • Object
show all
Includes:
Component
Defined in:
lib/build-cloud/zone.rb

Constant Summary collapse

@@objects =
[]

Instance Method Summary collapse

Methods included from Component

included

Constructor Details

#initialize(fog_interfaces, log, options = {}) ⇒ Zone

Returns a new instance of Zone.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/build-cloud/zone.rb', line 7

def initialize ( fog_interfaces, log, options = {} )

    @r53     = fog_interfaces[:r53]
    @log     = log
    @options = options

    @log.debug( options.inspect )

    required_options(:domain)

end

Instance Method Details

#createObject



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
71
72
73
74
75
76
77
# File 'lib/build-cloud/zone.rb', line 19

def create
    
    return if exists?

    @log.info( "Creating zone for #{@options[:domain]}" )

    options = @options.dup

    zone = @r53.zones.new( options )
    zone.save

    if options.has_key?(:ttl)

        # Change TTL of default RR sets if required:

        record_sets = @r53.list_resource_record_sets( zone.id ).data[:body]['ResourceRecordSets']
        changes = []

        record_sets.each do |set|

            next if set['TTL'].to_i == options[:ttl].to_i

            rr = set['ResourceRecords']

            changes << {
                :action           => 'DELETE',
                :name             => set['Name'],
                :type             => set['Type'],
                :ttl              => set['TTL'],
                :resource_records => rr,
            }

            if( set['Type'] == 'SOA' )

                # Set min TTL in SOA too

                soa = rr.first.split(/\s+/)
                soa[6] = options[:ttl]
                rr = [ soa.join(' ') ]

            end

            changes << {
                :action           => 'CREATE',
                :name             => set['Name'],
                :type             => set['Type'],
                :ttl              => options[:ttl],
                :resource_records => rr,
            }

        end

        @r53.change_resource_record_sets( zone.id, changes )

    end

    @log.debug( zone.inspect )

end

#deleteObject



85
86
87
88
89
90
91
92
93
# File 'lib/build-cloud/zone.rb', line 85

def delete

    return unless exists?

    @log.info( "Deleting zone for #{@options[:domain]}" )

    fog_object.destroy

end

#readObject Also known as: fog_object



79
80
81
# File 'lib/build-cloud/zone.rb', line 79

def read
    @r53.zones.select { |z| z.domain == @options[:domain] }.first
end