google-cloud-dns 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 025d3478ed1472d4863139cf2f6f675a378fe259
4
+ data.tar.gz: 90e33a610dfc052ddc27363229740ed9b4ebfacb
5
+ SHA512:
6
+ metadata.gz: 5355ae28e234dd0656f582e39c2e01d56d42aaf47ac519e9ffd6d9288e3bf20be335e35469c1fa3bd3ff9196e5ec025a78cb4bf92c1af78ac59c39ea948d45e4
7
+ data.tar.gz: f21afd99e86197708a53f0b24f218c4bf2cf8a24a3d460d7e8597068f7835223b537f532343f27317253bd669930174b5096ba98249e469a4808400e4542f4b0
@@ -0,0 +1,121 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ ##
16
+ # This file is here to be autorequired by bundler, so that the .bigquery and
17
+ # #bigquery methods can be available, but the library and all dependencies won't
18
+ # be loaded until required and used.
19
+
20
+
21
+ gem "google-cloud-core"
22
+ require "google/cloud"
23
+
24
+ module Google
25
+ module Cloud
26
+ ##
27
+ # Creates a new object for connecting to the DNS service.
28
+ # Each call creates a new connection.
29
+ #
30
+ # For more information on connecting to Google Cloud see the [Authentication
31
+ # Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
32
+ #
33
+ # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
34
+ # set of resources and operations that the connection can access. See
35
+ # [Using OAuth 2.0 to Access Google
36
+ # APIs](https://developers.google.com/identity/protocols/OAuth2).
37
+ #
38
+ # The default scope is:
39
+ #
40
+ # * `https://www.googleapis.com/auth/ndev.clouddns.readwrite`
41
+ # @param [Integer] retries Number of times to retry requests on server
42
+ # error. The default value is `3`. Optional.
43
+ # @param [Integer] timeout Default timeout to use in requests. Optional.
44
+ #
45
+ # @return [Google::Cloud::Dns::Project]
46
+ #
47
+ # @example
48
+ # require "google/cloud"
49
+ #
50
+ # gcloud = Google::Cloud.new
51
+ # dns = gcloud.dns
52
+ # zone = dns.zone "example-zone"
53
+ # zone.records.each do |record|
54
+ # puts record.name
55
+ # end
56
+ #
57
+ # @example The default scope can be overridden with the `scope` option:
58
+ # require "google/cloud"
59
+ #
60
+ # gcloud = Google::Cloud.new
61
+ # dns_readonly = "https://www.googleapis.com/auth/ndev.clouddns.readonly"
62
+ # dns = gcloud.dns scope: dns_readonly
63
+ #
64
+ def dns scope: nil, retries: nil, timeout: nil
65
+ Google::Cloud.dns @project, @keyfile, scope: scope,
66
+ retries: (retries || @retries),
67
+ timeout: (timeout || @timeout)
68
+ end
69
+
70
+ ##
71
+ # Creates a new `Project` instance connected to the DNS service.
72
+ # Each call creates a new connection.
73
+ #
74
+ # For more information on connecting to Google Cloud see the [Authentication
75
+ # Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
76
+ #
77
+ # @param [String] project Identifier for a DNS project. If not present, the
78
+ # default project for the credentials is used.
79
+ # @param [String, Hash] keyfile Keyfile downloaded from Google Cloud. If
80
+ # file path the file must be readable.
81
+ # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
82
+ # set of resources and operations that the connection can access. See
83
+ # [Using OAuth 2.0 to Access Google
84
+ # APIs](https://developers.google.com/identity/protocols/OAuth2).
85
+ #
86
+ # The default scope is:
87
+ #
88
+ # * `https://www.googleapis.com/auth/ndev.clouddns.readwrite`
89
+ # @param [Integer] retries Number of times to retry requests on server
90
+ # error. The default value is `3`. Optional.
91
+ # @param [Integer] timeout Default timeout to use in requests. Optional.
92
+ #
93
+ # @return [Google::Cloud::Dns::Project]
94
+ #
95
+ # @example
96
+ # require "google/cloud"
97
+ #
98
+ # dns = Google::Cloud.dns "my-dns-project",
99
+ # "/path/to/keyfile.json"
100
+ #
101
+ # zone = dns.zone "example-com"
102
+ #
103
+ def self.dns project = nil, keyfile = nil, scope: nil, retries: nil,
104
+ timeout: nil
105
+ require "google/cloud/dns"
106
+ project ||= Google::Cloud::Dns::Project.default_project
107
+ project = project.to_s # Always cast to a string
108
+ fail ArgumentError, "project is missing" if project.empty?
109
+
110
+ if keyfile.nil?
111
+ credentials = Google::Cloud::Dns::Credentials.default scope: scope
112
+ else
113
+ credentials = Google::Cloud::Dns::Credentials.new keyfile, scope: scope
114
+ end
115
+
116
+ Google::Cloud::Dns::Project.new(
117
+ Google::Cloud::Dns::Service.new(
118
+ project, credentials, retries: retries, timeout: timeout))
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,290 @@
1
+ # Copyright 2015 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "google-cloud-dns"
17
+ require "google/cloud/dns/project"
18
+
19
+ module Google
20
+ module Cloud
21
+ ##
22
+ # # Google Cloud DNS
23
+ #
24
+ # Google Cloud DNS is a high-performance, resilient, global DNS service that
25
+ # provides a cost-effective way to make your applications and services
26
+ # available to your users. This programmable, authoritative DNS service can
27
+ # be used to easily publish and manage DNS records using the same
28
+ # infrastructure relied upon by Google. To learn more, read [What is Google
29
+ # Cloud DNS?](https://cloud.google.com/dns/what-is-cloud-dns).
30
+ #
31
+ # The goal of google-cloud is to provide an API that is comfortable to
32
+ # Rubyists. Authentication is handled by {Google::Cloud#dns}. You can
33
+ # provide the project and credential information to connect to the Cloud DNS
34
+ # service, or if you are running on Google Compute Engine this configuration
35
+ # is taken care of for you. You can read more about the options for
36
+ # connecting in the [Authentication
37
+ # Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
38
+ #
39
+ # ## Creating Zones
40
+ #
41
+ # To get started with Google Cloud DNS, use your DNS Project to create a new
42
+ # Zone. The second argument to {Google::Cloud::Dns::Project#create_zone}
43
+ # must be a unique domain name for which you can [verify
44
+ # ownership](https://www.google.com/webmasters/verification/home).
45
+ # Substitute a domain name of your own (ending with a dot to signify that it
46
+ # is [fully
47
+ # qualified](https://en.wikipedia.org/wiki/Fully_qualified_domain_name)) as
48
+ # you follow along with these examples.
49
+ #
50
+ # ```ruby
51
+ # require "google/cloud"
52
+ #
53
+ # gcloud = Google::Cloud.new
54
+ # dns = gcloud.dns
55
+ # zone = dns.create_zone "example-com", "example.com."
56
+ # puts zone.id # unique identifier defined by the server
57
+ # ```
58
+ #
59
+ # For more information, see [Managing
60
+ # Zones](https://cloud.google.com/dns/zones/).
61
+ #
62
+ # ## Listing Zones
63
+ #
64
+ # You can retrieve all the zones in your project.
65
+ #
66
+ # ```ruby
67
+ # require "google/cloud"
68
+ #
69
+ # gcloud = Google::Cloud.new
70
+ # dns = gcloud.dns
71
+ # zones = dns.zones
72
+ # zones.each do |zone|
73
+ # puts "#{zone.name} - #{zone.dns}"
74
+ # end
75
+ # ```
76
+ #
77
+ # You can also retrieve a single zone by either name or id.
78
+ #
79
+ # ```ruby
80
+ # require "google/cloud"
81
+ #
82
+ # gcloud = Google::Cloud.new
83
+ # dns = gcloud.dns
84
+ # zone = dns.zone "example-com"
85
+ # ```
86
+ #
87
+ # ## Listing Records
88
+ #
89
+ # When you create a zone, the Cloud DNS service automatically creates two
90
+ # Record instances for it, providing configuration for Cloud DNS
91
+ # nameservers. Let's take a look at these records.
92
+ #
93
+ # ```ruby
94
+ # require "google/cloud"
95
+ #
96
+ # gcloud = Google::Cloud.new
97
+ # dns = gcloud.dns
98
+ # zone = dns.zone "example-com"
99
+ # records = zone.records
100
+ # records.count #=> 2
101
+ # records.map &:type #=> ["NS", "SOA"]
102
+ # zone.records.first.data.count #=> 4
103
+ # zone.records.first.data #=> ["ns-cloud-d1.googledomains.com.", ...]
104
+ # ```
105
+ #
106
+ # Note that {Google::Cloud::Dns::Record#data} returns an array. The Cloud
107
+ # DNS service only allows the zone to have one Record instance for each name
108
+ # and type combination. It supports multiple "resource records" (in this
109
+ # case, the four nameserver addresses) via this `data` collection.
110
+ #
111
+ # ## Managing Records
112
+ #
113
+ # You can easily add your own records to the zone. Each call to
114
+ # {Google::Cloud::Dns::Zone#add} results in a new Cloud DNS Change instance.
115
+ #
116
+ # ```ruby
117
+ # require "google/cloud"
118
+ #
119
+ # gcloud = Google::Cloud.new
120
+ # dns = gcloud.dns
121
+ # zone = dns.zone "example-com"
122
+ # change = zone.add "www", "A", 86400, ["1.2.3.4"]
123
+ # change.additions.map &:type #=> ["A", "SOA"]
124
+ # change.deletions.map &:type #=> ["SOA"]
125
+ # ```
126
+ #
127
+ # Whenever you change the set of records belonging to a zone, the zone's
128
+ # start of authority (SOA) record should be updated with a higher serial
129
+ # number. The google-cloud library automates this update for you, deleting
130
+ # the old SOA record and adding an updated one, as shown in the example
131
+ # above. You can disable or modify this behavior, of course. See
132
+ # {Google::Cloud::Dns::Zone#update} for details.
133
+ #
134
+ # You can retrieve records by name and type. The name argument can be a
135
+ # subdomain (e.g., `www`) fragment for convenience, but notice that the
136
+ # retrieved record's domain name is always fully-qualified.
137
+ #
138
+ # ```ruby
139
+ # require "google/cloud"
140
+ #
141
+ # gcloud = Google::Cloud.new
142
+ # dns = gcloud.dns
143
+ # zone = dns.zone "example-com"
144
+ # records = zone.records "www", "A"
145
+ # records.first.name #=> "www.example.com."
146
+ # ```
147
+ #
148
+ # You can use {Google::Cloud::Dns::Zone#replace} to update the `ttl` and
149
+ # `data` for a record.
150
+ #
151
+ # ```ruby
152
+ # require "google/cloud"
153
+ #
154
+ # gcloud = Google::Cloud.new
155
+ # dns = gcloud.dns
156
+ # zone = dns.zone "example-com"
157
+ # change = zone.replace "www", "A", 86400, ["5.6.7.8"]
158
+ # ```
159
+ #
160
+ # Or, you can use {Google::Cloud::Dns::Zone#modify} to update just the `ttl`
161
+ # or `data`, without the risk of inadvertently changing values that you wish
162
+ # to leave unchanged.
163
+ #
164
+ # ```ruby
165
+ # require "google/cloud"
166
+ #
167
+ # gcloud = Google::Cloud.new
168
+ # dns = gcloud.dns
169
+ # zone = dns.zone "example-com"
170
+ # change = zone.modify "www", "A" do |r|
171
+ # r.ttl = 3600 # change only the TTL
172
+ # end
173
+ # ```
174
+ #
175
+ # You can also delete records by name and type.
176
+ #
177
+ # ```ruby
178
+ # require "google/cloud"
179
+ #
180
+ # gcloud = Google::Cloud.new
181
+ # dns = gcloud.dns
182
+ # zone = dns.zone "example-com"
183
+ # change = zone.remove "www", "A"
184
+ # record = change.deletions.first
185
+ # ```
186
+ #
187
+ # The best way to add, remove, and update multiple records in a single
188
+ # [transaction](https://cloud.google.com/dns/records) is to call
189
+ # {Google::Cloud::Dns::Zone#update} with a block. See
190
+ # {Google::Cloud::Dns::Zone::Transaction}.
191
+ #
192
+ # ```ruby
193
+ # require "google/cloud"
194
+ #
195
+ # gcloud = Google::Cloud.new
196
+ # dns = gcloud.dns
197
+ # zone = dns.zone "example-com"
198
+ # change = zone.update do |tx|
199
+ # tx.add "www", "A", 86400, "1.2.3.4"
200
+ # tx.remove "example.com.", "TXT"
201
+ # tx.replace "example.com.", "MX", 86400, ["10 mail1.example.com.",
202
+ # "20 mail2.example.com."]
203
+ # tx.modify "www.example.com.", "CNAME" do |r|
204
+ # r.ttl = 86400 # only change the TTL
205
+ # end
206
+ # end
207
+ # ```
208
+ #
209
+ # Finally, you can add and delete records by reference, using
210
+ # {Google::Cloud::Dns::Zone#update}.
211
+ #
212
+ # ```ruby
213
+ # require "google/cloud"
214
+ #
215
+ # gcloud = Google::Cloud.new
216
+ # dns = gcloud.dns
217
+ # zone = dns.zone "example-com"
218
+ # to_add = zone.record "www", "AAAA", 86400, ["2607:f8b0:400a:801::1005"]
219
+ # to_delete = zone.records "www", "A"
220
+ # change = zone.update to_add, to_delete
221
+ # ```
222
+ #
223
+ # ## Listing Changes
224
+ #
225
+ # Because the transactions you execute against your zone do not always
226
+ # complete immediately, you can retrieve and inspect changes.
227
+ #
228
+ # ```ruby
229
+ # require "google/cloud"
230
+ #
231
+ # gcloud = Google::Cloud.new
232
+ # dns = gcloud.dns
233
+ # zone = dns.zone "example-com"
234
+ # changes = zone.changes
235
+ # changes.each do |change|
236
+ # puts "#{change.id} - #{change.started_at} - #{change.status}"
237
+ # end
238
+ # ```
239
+ #
240
+ # ## Importing and exporting zone files
241
+ #
242
+ # You can import from a zone file. Because the Cloud DNS service only allows
243
+ # the zone to have one Record instance for each name and type combination,
244
+ # lines may be merged as needed into records with multiple `data` values.
245
+ #
246
+ # ```ruby
247
+ # require "google/cloud"
248
+ #
249
+ # gcloud = Google::Cloud.new
250
+ # dns = gcloud.dns
251
+ # zone = dns.zone "example-com"
252
+ # change = zone.import "path/to/db.example.com"
253
+ # ```
254
+ #
255
+ # You can also export to a zone file.
256
+ #
257
+ # ```ruby
258
+ # require "google/cloud"
259
+ #
260
+ # gcloud = Google::Cloud.new
261
+ # dns = gcloud.dns
262
+ # zone = dns.zone "example-com"
263
+ #
264
+ # zone.export "path/to/db.example.com"
265
+ # ```
266
+ #
267
+ # ## Configuring retries and timeout
268
+ #
269
+ # You can configure how many times API requests may be automatically
270
+ # retried. When an API request fails, the response will be inspected to see
271
+ # if the request meets criteria indicating that it may succeed on retry,
272
+ # such as `500` and `503` status codes or a specific internal error code
273
+ # such as `rateLimitExceeded`. If it meets the criteria, the request will be
274
+ # retried after a delay. If another error occurs, the delay will be
275
+ # increased before a subsequent attempt, until the `retries` limit is
276
+ # reached.
277
+ #
278
+ # You can also set the request `timeout` value in seconds.
279
+ #
280
+ # ```ruby
281
+ # require "google/cloud"
282
+ #
283
+ # gcloud = Google::Cloud.new
284
+ # dns = gcloud.dns retries: 10, timeout: 120
285
+ # ```
286
+ #
287
+ module Dns
288
+ end
289
+ end
290
+ end
@@ -0,0 +1,160 @@
1
+ # Copyright 2015 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "google/cloud/dns/change/list"
17
+ require "time"
18
+
19
+ module Google
20
+ module Cloud
21
+ module Dns
22
+ ##
23
+ # # DNS Change
24
+ #
25
+ # Represents a request containing additions or deletions or records.
26
+ # Additions and deletions can be done in bulk, in a single atomic
27
+ # transaction, and take effect at the same time in each authoritative DNS
28
+ # server.
29
+ #
30
+ # @example
31
+ # require "google/cloud"
32
+ #
33
+ # gcloud = Google::Cloud.new
34
+ # dns = gcloud.dns
35
+ # zone = dns.zone "example-com"
36
+ # zone.changes.each do |change|
37
+ # puts "Change includes #{change.additions.count} additions " \
38
+ # "and #{change.additions.count} deletions."
39
+ # end
40
+ #
41
+ class Change
42
+ ##
43
+ # @private The Zone object this Change belongs to.
44
+ attr_accessor :zone
45
+
46
+ ##
47
+ # @private The Google API Client object.
48
+ attr_accessor :gapi
49
+
50
+ ##
51
+ # @private Create an empty Change object.
52
+ def initialize
53
+ @zone = nil
54
+ @gapi = {}
55
+ end
56
+
57
+ ##
58
+ # Unique identifier for the resource; defined by the server.
59
+ #
60
+ def id
61
+ @gapi.id
62
+ end
63
+
64
+ ##
65
+ # The records added in this change request.
66
+ #
67
+ def additions
68
+ Array(@gapi.additions).map { |gapi| Record.from_gapi gapi }
69
+ end
70
+
71
+ ##
72
+ # The records removed in this change request.
73
+ #
74
+ def deletions
75
+ Array(@gapi.deletions).map { |gapi| Record.from_gapi gapi }
76
+ end
77
+
78
+ ##
79
+ # Status of the operation. Values are `"done"` and `"pending"`.
80
+ #
81
+ def status
82
+ @gapi.status
83
+ end
84
+
85
+ ##
86
+ # Checks if the status is `"done"`.
87
+ def done?
88
+ return false if status.nil?
89
+ "done".casecmp(status).zero?
90
+ end
91
+
92
+ ##
93
+ # Checks if the status is `"pending"`.
94
+ def pending?
95
+ return false if status.nil?
96
+ "pending".casecmp(status).zero?
97
+ end
98
+
99
+ ##
100
+ # The time that this operation was started by the server.
101
+ #
102
+ def started_at
103
+ Time.parse @gapi.start_time
104
+ rescue
105
+ nil
106
+ end
107
+
108
+ ##
109
+ # Reloads the change with updated status from the DNS service.
110
+ def reload!
111
+ ensure_service!
112
+ @gapi = zone.service.get_change @zone.id, id
113
+ end
114
+ alias_method :refresh!, :reload!
115
+
116
+ ##
117
+ # Refreshes the change until the status is `done`.
118
+ # The delay between refreshes will incrementally increase.
119
+ #
120
+ # @example
121
+ # require "google/cloud"
122
+ #
123
+ # gcloud = Google::Cloud.new
124
+ # dns = gcloud.dns
125
+ # zone = dns.zone "example-com"
126
+ # change = zone.change 1234567890
127
+ # change.done? #=> false
128
+ # change.wait_until_done!
129
+ # change.done? #=> true
130
+ #
131
+ def wait_until_done!
132
+ backoff = ->(retries) { sleep 2 * retries + 5 }
133
+ retries = 0
134
+ until done?
135
+ backoff.call retries
136
+ retries += 1
137
+ reload!
138
+ end
139
+ end
140
+
141
+ ##
142
+ # @private New Change from a Google API Client object.
143
+ def self.from_gapi gapi, zone
144
+ new.tap do |f|
145
+ f.gapi = gapi
146
+ f.zone = zone
147
+ end
148
+ end
149
+
150
+ protected
151
+
152
+ ##
153
+ # Raise an error unless an active service is available.
154
+ def ensure_service!
155
+ fail "Must have active connection" unless zone && zone.service
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end