-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathchange_batch.rb
162 lines (136 loc) · 5.02 KB
/
change_batch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
78
79
80
81
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
155
156
157
158
159
160
161
162
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
module AWS
class Route53
# # Modify resource record sets with ChangeBatch
#
# batch = AWS::Route53::ChangeBatch.new(hosted_zone_id)
# batch << AWS::Route53::CreateRequest.new('foo.example.com.', 'A', :resource_records => [{:value => '192.168.0.1'}])
# batch << AWS::Route53::DeleteRequest.new('bar.example.com.', 'CNAME')
# batch << AWS::Route53::DeleteRequest.new('baz.example.com.', 'AAAA')
# batch << AWS::Route53::CreateRequest.new('baz.example.com.', 'AAAA', :resource_records => [{:value => '192.168.0.3'}])
#
# batch.call
#
class ChangeBatch
include Enumerable
include Core::Model
# @api private
def initialize hosted_zone_id, options = {}
super(options)
@hosted_zone_id = hosted_zone_id
@comment = options[:comment]
@changes = []
end
# @return [String]
attr_reader :hosted_zone_id
# @return [Array<ChangeRequest>]
attr_reader :changes
# @return [String]
attr_reader :comment
# @param [ChangeRequest] change
# @return [Array]
def push change
@changes.push(change)
end
alias_method :<<, :push
# Calls change batch request.
# @option (see Client#change_resource_record_sets)
# @return [ChangeInfo]
def call options={}
resp = client.change_resource_record_sets(options.merge(self.to_hash))
if resp[:change_info][:id]
ChangeInfo.new_from(:change_resource_record_sets,
resp[:change_info],
resp[:change_info][:id],
:config => config)
end
end
# Enumerates over changes.
def each(&block)
@changes.each(&block)
end
# Returns length of changes.
# @return [Integer]
def length
@changes.length
end
alias_method :size, :length
# Build query from change batch.
# @return [Hash]
def to_hash
q = {}
q[:hosted_zone_id] = hosted_zone_id
q[:change_batch] = {}
q[:change_batch][:comment] = comment if comment
q[:change_batch][:changes] = []
self.each { |change|
q[:change_batch][:changes] << change.to_hash
}
q
end
end
class ChangeRequest
# @api private
def initialize(action, name, type, options={})
@action = action
@name = name
@type = type
@change_options = options
end
# @return [String]
attr_reader :action
# @return [String]
attr_reader :name
# @return [String]
attr_reader :type
# Build query for change request.
# @return [Hash]
def to_hash
q = {}
q[:action] = action
q[:resource_record_set] = {}
q[:resource_record_set][:name] = name
q[:resource_record_set][:type] = type
q[:resource_record_set][:set_identifier] = @change_options[:set_identifier] if @change_options[:set_identifier]
q[:resource_record_set][:weight] = @change_options[:weight] if @change_options[:weight]
q[:resource_record_set][:region] = @change_options[:region] if @change_options[:region]
q[:resource_record_set][:ttl] = @change_options[:ttl] if @change_options[:ttl]
q[:resource_record_set][:resource_records] = @change_options[:resource_records] if @change_options[:resource_records]
q[:resource_record_set][:alias_target] = @change_options[:alias_target] if @change_options[:alias_target]
q[:resource_record_set][:geo_location] = @change_options[:geo_location] if @change_options[:geo_location]
q[:resource_record_set][:failover] = @change_options[:failover] if @change_options[:failover]
q[:resource_record_set][:health_check_id] = @change_options[:health_check_id] if @change_options[:health_check_id]
q
end
end
# A change request to create a resource record set.
class CreateRequest < ChangeRequest
# @param [String] name
# @param [String] type
# @param [Hash] options
def initialize name, type, options = {}
super('CREATE', name, type, options)
end
end
# A change request to delete a resource record set.
class DeleteRequest < ChangeRequest
# @param [String] name
# @param [String] type
# @param [Hash] options
def initialize name, type, options = {}
super('DELETE', name, type, options)
end
end
end
end