gcloud 0.12.2 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. checksums.yaml +5 -13
  2. data/lib/gcloud.rb +27 -456
  3. data/lib/gcloud/bigquery.rb +2 -382
  4. data/lib/gcloud/datastore.rb +2 -576
  5. data/lib/gcloud/dns.rb +2 -321
  6. data/lib/gcloud/logging.rb +1 -322
  7. data/lib/gcloud/pubsub.rb +2 -476
  8. data/lib/gcloud/resource_manager.rb +2 -273
  9. data/lib/gcloud/storage.rb +2 -440
  10. data/lib/gcloud/translate.rb +1 -250
  11. data/lib/gcloud/version.rb +2 -2
  12. data/lib/gcloud/vision.rb +1 -501
  13. metadata +36 -332
  14. data/AUTHENTICATION.md +0 -75
  15. data/CHANGELOG.md +0 -382
  16. data/OVERVIEW.md +0 -259
  17. data/lib/gcloud/backoff.rb +0 -150
  18. data/lib/gcloud/bigquery/copy_job.rb +0 -97
  19. data/lib/gcloud/bigquery/credentials.rb +0 -29
  20. data/lib/gcloud/bigquery/data.rb +0 -239
  21. data/lib/gcloud/bigquery/dataset.rb +0 -753
  22. data/lib/gcloud/bigquery/dataset/access.rb +0 -507
  23. data/lib/gcloud/bigquery/dataset/list.rb +0 -169
  24. data/lib/gcloud/bigquery/extract_job.rb +0 -117
  25. data/lib/gcloud/bigquery/insert_response.rb +0 -81
  26. data/lib/gcloud/bigquery/job.rb +0 -299
  27. data/lib/gcloud/bigquery/job/list.rb +0 -172
  28. data/lib/gcloud/bigquery/load_job.rb +0 -202
  29. data/lib/gcloud/bigquery/project.rb +0 -475
  30. data/lib/gcloud/bigquery/query_data.rb +0 -234
  31. data/lib/gcloud/bigquery/query_job.rb +0 -137
  32. data/lib/gcloud/bigquery/schema.rb +0 -359
  33. data/lib/gcloud/bigquery/service.rb +0 -506
  34. data/lib/gcloud/bigquery/table.rb +0 -1141
  35. data/lib/gcloud/bigquery/table/list.rb +0 -180
  36. data/lib/gcloud/bigquery/view.rb +0 -475
  37. data/lib/gcloud/credentials.rb +0 -129
  38. data/lib/gcloud/datastore/commit.rb +0 -148
  39. data/lib/gcloud/datastore/credentials.rb +0 -35
  40. data/lib/gcloud/datastore/cursor.rb +0 -76
  41. data/lib/gcloud/datastore/dataset.rb +0 -660
  42. data/lib/gcloud/datastore/dataset/lookup_results.rb +0 -219
  43. data/lib/gcloud/datastore/dataset/query_results.rb +0 -386
  44. data/lib/gcloud/datastore/entity.rb +0 -449
  45. data/lib/gcloud/datastore/errors.rb +0 -41
  46. data/lib/gcloud/datastore/gql_query.rb +0 -211
  47. data/lib/gcloud/datastore/grpc_utils.rb +0 -132
  48. data/lib/gcloud/datastore/key.rb +0 -281
  49. data/lib/gcloud/datastore/properties.rb +0 -128
  50. data/lib/gcloud/datastore/query.rb +0 -348
  51. data/lib/gcloud/datastore/service.rb +0 -167
  52. data/lib/gcloud/datastore/transaction.rb +0 -362
  53. data/lib/gcloud/dns/change.rb +0 -158
  54. data/lib/gcloud/dns/change/list.rb +0 -173
  55. data/lib/gcloud/dns/credentials.rb +0 -29
  56. data/lib/gcloud/dns/importer.rb +0 -183
  57. data/lib/gcloud/dns/project.rb +0 -247
  58. data/lib/gcloud/dns/record.rb +0 -170
  59. data/lib/gcloud/dns/record/list.rb +0 -174
  60. data/lib/gcloud/dns/service.rb +0 -167
  61. data/lib/gcloud/dns/zone.rb +0 -759
  62. data/lib/gcloud/dns/zone/list.rb +0 -168
  63. data/lib/gcloud/dns/zone/transaction.rb +0 -176
  64. data/lib/gcloud/errors.rb +0 -206
  65. data/lib/gcloud/gce.rb +0 -56
  66. data/lib/gcloud/grpc_utils.rb +0 -87
  67. data/lib/gcloud/logging/credentials.rb +0 -29
  68. data/lib/gcloud/logging/entry.rb +0 -465
  69. data/lib/gcloud/logging/entry/http_request.rb +0 -141
  70. data/lib/gcloud/logging/entry/list.rb +0 -177
  71. data/lib/gcloud/logging/entry/operation.rb +0 -90
  72. data/lib/gcloud/logging/logger.rb +0 -307
  73. data/lib/gcloud/logging/metric.rb +0 -169
  74. data/lib/gcloud/logging/metric/list.rb +0 -172
  75. data/lib/gcloud/logging/project.rb +0 -642
  76. data/lib/gcloud/logging/resource.rb +0 -84
  77. data/lib/gcloud/logging/resource_descriptor.rb +0 -137
  78. data/lib/gcloud/logging/resource_descriptor/list.rb +0 -174
  79. data/lib/gcloud/logging/service.rb +0 -267
  80. data/lib/gcloud/logging/sink.rb +0 -227
  81. data/lib/gcloud/logging/sink/list.rb +0 -171
  82. data/lib/gcloud/pubsub/credentials.rb +0 -29
  83. data/lib/gcloud/pubsub/message.rb +0 -94
  84. data/lib/gcloud/pubsub/policy.rb +0 -204
  85. data/lib/gcloud/pubsub/project.rb +0 -482
  86. data/lib/gcloud/pubsub/received_message.rb +0 -160
  87. data/lib/gcloud/pubsub/service.rb +0 -334
  88. data/lib/gcloud/pubsub/subscription.rb +0 -565
  89. data/lib/gcloud/pubsub/subscription/list.rb +0 -208
  90. data/lib/gcloud/pubsub/topic.rb +0 -511
  91. data/lib/gcloud/pubsub/topic/list.rb +0 -174
  92. data/lib/gcloud/pubsub/topic/publisher.rb +0 -85
  93. data/lib/gcloud/resource_manager/credentials.rb +0 -30
  94. data/lib/gcloud/resource_manager/manager.rb +0 -266
  95. data/lib/gcloud/resource_manager/policy.rb +0 -211
  96. data/lib/gcloud/resource_manager/project.rb +0 -484
  97. data/lib/gcloud/resource_manager/project/list.rb +0 -167
  98. data/lib/gcloud/resource_manager/project/updater.rb +0 -130
  99. data/lib/gcloud/resource_manager/service.rb +0 -127
  100. data/lib/gcloud/storage/bucket.rb +0 -775
  101. data/lib/gcloud/storage/bucket/acl.rb +0 -810
  102. data/lib/gcloud/storage/bucket/cors.rb +0 -153
  103. data/lib/gcloud/storage/bucket/list.rb +0 -172
  104. data/lib/gcloud/storage/credentials.rb +0 -29
  105. data/lib/gcloud/storage/errors.rb +0 -65
  106. data/lib/gcloud/storage/file.rb +0 -842
  107. data/lib/gcloud/storage/file/acl.rb +0 -425
  108. data/lib/gcloud/storage/file/list.rb +0 -191
  109. data/lib/gcloud/storage/file/verifier.rb +0 -67
  110. data/lib/gcloud/storage/project.rb +0 -316
  111. data/lib/gcloud/storage/service.rb +0 -347
  112. data/lib/gcloud/translate/api.rb +0 -241
  113. data/lib/gcloud/translate/detection.rb +0 -137
  114. data/lib/gcloud/translate/language.rb +0 -69
  115. data/lib/gcloud/translate/service.rb +0 -80
  116. data/lib/gcloud/translate/translation.rb +0 -112
  117. data/lib/gcloud/vision/annotate.rb +0 -224
  118. data/lib/gcloud/vision/annotation.rb +0 -455
  119. data/lib/gcloud/vision/annotation/entity.rb +0 -234
  120. data/lib/gcloud/vision/annotation/face.rb +0 -1750
  121. data/lib/gcloud/vision/annotation/properties.rb +0 -245
  122. data/lib/gcloud/vision/annotation/safe_search.rb +0 -161
  123. data/lib/gcloud/vision/annotation/text.rb +0 -236
  124. data/lib/gcloud/vision/annotation/vertex.rb +0 -108
  125. data/lib/gcloud/vision/credentials.rb +0 -29
  126. data/lib/gcloud/vision/image.rb +0 -590
  127. data/lib/gcloud/vision/location.rb +0 -115
  128. data/lib/gcloud/vision/project.rb +0 -278
  129. data/lib/gcloud/vision/service.rb +0 -66
  130. data/lib/google/api/annotations.rb +0 -14
  131. data/lib/google/api/http.rb +0 -30
  132. data/lib/google/api/label.rb +0 -24
  133. data/lib/google/api/monitored_resource.rb +0 -25
  134. data/lib/google/datastore/v1beta3/datastore.rb +0 -115
  135. data/lib/google/datastore/v1beta3/datastore_services.rb +0 -33
  136. data/lib/google/datastore/v1beta3/entity.rb +0 -63
  137. data/lib/google/datastore/v1beta3/query.rb +0 -128
  138. data/lib/google/devtools/cloudtrace/v1/trace.rb +0 -78
  139. data/lib/google/devtools/cloudtrace/v1/trace_services.rb +0 -32
  140. data/lib/google/example/library/v1/library.rb +0 -91
  141. data/lib/google/example/library/v1/library_services.rb +0 -40
  142. data/lib/google/iam/v1/iam_policy.rb +0 -33
  143. data/lib/google/iam/v1/iam_policy_services.rb +0 -30
  144. data/lib/google/iam/v1/policy.rb +0 -25
  145. data/lib/google/logging/type/http_request.rb +0 -28
  146. data/lib/google/logging/type/log_severity.rb +0 -27
  147. data/lib/google/logging/v2/log_entry.rb +0 -44
  148. data/lib/google/logging/v2/logging.rb +0 -56
  149. data/lib/google/logging/v2/logging_config.rb +0 -59
  150. data/lib/google/logging/v2/logging_config_services.rb +0 -32
  151. data/lib/google/logging/v2/logging_metrics.rb +0 -51
  152. data/lib/google/logging/v2/logging_metrics_services.rb +0 -32
  153. data/lib/google/logging/v2/logging_services.rb +0 -31
  154. data/lib/google/longrunning/operations.rb +0 -50
  155. data/lib/google/longrunning/operations_services.rb +0 -29
  156. data/lib/google/protobuf/descriptor.rb +0 -0
  157. data/lib/google/pubsub/v1/pubsub.rb +0 -129
  158. data/lib/google/pubsub/v1/pubsub_services.rb +0 -56
  159. data/lib/google/pubsub/v1beta2/pubsub.rb +0 -126
  160. data/lib/google/pubsub/v1beta2/pubsub_services.rb +0 -56
  161. data/lib/google/rpc/code.rb +0 -32
  162. data/lib/google/rpc/error_details.rb +0 -61
  163. data/lib/google/rpc/status.rb +0 -19
  164. data/lib/google/type/color.rb +0 -20
  165. data/lib/google/type/date.rb +0 -18
  166. data/lib/google/type/dayofweek.rb +0 -23
  167. data/lib/google/type/latlng.rb +0 -17
  168. data/lib/google/type/money.rb +0 -18
  169. data/lib/google/type/timeofday.rb +0 -19
@@ -1,167 +0,0 @@
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
- require "gcloud/version"
17
- require "gcloud/errors"
18
- require "google/apis/dns_v1"
19
-
20
- module Gcloud
21
- module Dns
22
- ##
23
- # @private
24
- # Represents the service to DNS, exposing the API calls.
25
- class Service
26
- ##
27
- # Alias to the Google Client API module
28
- API = Google::Apis::DnsV1
29
-
30
- attr_accessor :project
31
- attr_accessor :credentials
32
-
33
- ##
34
- # Creates a new Service instance.
35
- def initialize project, credentials, retries: nil, timeout: nil
36
- @project = project
37
- @credentials = credentials
38
- @service = API::DnsService.new
39
- @service.client_options.application_name = "gcloud-ruby"
40
- @service.client_options.application_version = Gcloud::VERSION
41
- @service.request_options.retries = retries || 3
42
- @service.request_options.timeout_sec = timeout if timeout
43
- @service.authorization = @credentials.client
44
- end
45
-
46
- def service
47
- return mocked_service if mocked_service
48
- @service
49
- end
50
- attr_accessor :mocked_service
51
-
52
- ##
53
- # Returns Google::Apis::DnsV1::Project
54
- def get_project project_id = @project
55
- service.get_project project_id
56
- rescue Google::Apis::Error => e
57
- raise Gcloud::Error.from_error(e)
58
- end
59
-
60
- ##
61
- # Returns Google::Apis::DnsV1::ManagedZone
62
- def get_zone zone_id
63
- service.get_managed_zone @project, zone_id
64
- rescue Google::Apis::Error => e
65
- raise Gcloud::Error.from_error(e)
66
- end
67
-
68
- ##
69
- # Returns Google::Apis::DnsV1::ListManagedZonesResponse
70
- def list_zones token: nil, max: nil
71
- service.list_managed_zones @project, max_results: max, page_token: token
72
- rescue Google::Apis::Error => e
73
- raise Gcloud::Error.from_error(e)
74
- end
75
-
76
- ##
77
- # Returns Google::Apis::DnsV1::ManagedZone
78
- def create_zone zone_name, zone_dns, description: nil,
79
- name_server_set: nil
80
- managed_zone = Google::Apis::DnsV1::ManagedZone.new(
81
- kind: "dns#managedZone",
82
- name: zone_name,
83
- dns_name: zone_dns,
84
- description: (description || ""),
85
- name_server_set: name_server_set
86
- )
87
- service.create_managed_zone @project, managed_zone
88
- rescue Google::Apis::Error => e
89
- raise Gcloud::Error.from_error(e)
90
- end
91
-
92
- def delete_zone zone_id
93
- service.delete_managed_zone @project, zone_id
94
- rescue Google::Apis::Error => e
95
- raise Gcloud::Error.from_error(e)
96
- end
97
-
98
- ##
99
- # Returns Google::Apis::DnsV1::Change
100
- def get_change zone_id, change_id
101
- service.get_change @project, zone_id, change_id
102
- rescue Google::Apis::Error => e
103
- raise Gcloud::Error.from_error(e)
104
- end
105
-
106
- ##
107
- # Returns Google::Apis::DnsV1::ListChangesResponse
108
- def list_changes zone_id, token: nil, max: nil, order: nil, sort: nil
109
- service.list_changes @project, zone_id, max_results: max,
110
- page_token: token,
111
- sort_by: sort,
112
- sort_order: order
113
- rescue Google::Apis::Error => e
114
- raise Gcloud::Error.from_error(e)
115
- end
116
-
117
- ##
118
- # Returns Google::Apis::DnsV1::Change
119
- def create_change zone_id, additions, deletions
120
- change = Google::Apis::DnsV1::Change.new(
121
- kind: "dns#change",
122
- additions: Array(additions),
123
- deletions: Array(deletions)
124
- )
125
- service.create_change @project, zone_id, change
126
- rescue Google::Apis::Error => e
127
- raise Gcloud::Error.from_error(e)
128
- end
129
-
130
- ##
131
- # Returns Google::Apis::DnsV1::ListResourceRecordSetsResponse
132
- def list_records zone_id, name = nil, type = nil, token: nil, max: nil
133
- service.list_resource_record_sets @project, zone_id, max_results: max,
134
- name: name,
135
- page_token: token,
136
- type: type
137
- rescue Google::Apis::Error => e
138
- raise Gcloud::Error.from_error(e)
139
- end
140
-
141
- ##
142
- # Fully Qualified Domain Name
143
- def self.fqdn name, origin_dns
144
- name = name.to_s.strip
145
- return name if self.ip_addr? name
146
- name = origin_dns if name.empty?
147
- name = origin_dns if name == "@"
148
- name = "#{name}.#{origin_dns}" unless name.include? "."
149
- name = "#{name}." unless name.end_with? "."
150
- name
151
- end
152
-
153
- require "ipaddr"
154
-
155
- def self.ip_addr? name
156
- IPAddr.new name
157
- true
158
- rescue IPAddr::Error
159
- false
160
- end
161
-
162
- def inspect
163
- "#{self.class}(#{@project})"
164
- end
165
- end
166
- end
167
- end
@@ -1,759 +0,0 @@
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 "gcloud/dns/change"
17
- require "gcloud/dns/zone/transaction"
18
- require "gcloud/dns/zone/list"
19
- require "gcloud/dns/record"
20
- require "gcloud/dns/importer"
21
- require "time"
22
-
23
- module Gcloud
24
- module Dns
25
- ##
26
- # # DNS Zone
27
- #
28
- # The managed zone is the container for DNS records for the same DNS name
29
- # suffix and has a set of name servers that accept and responds to queries.
30
- # A project can have multiple managed zones, but they must each have a
31
- # unique name.
32
- #
33
- # @example
34
- # require "gcloud"
35
- #
36
- # gcloud = Gcloud.new
37
- # dns = gcloud.dns
38
- # zone = dns.zone "example-com"
39
- # zone.records.each do |record|
40
- # puts record.name
41
- # end
42
- #
43
- # @see https://cloud.google.com/dns/zones/ Managing Zones
44
- #
45
- class Zone
46
- ##
47
- # @private The Service object.
48
- attr_accessor :service
49
-
50
- ##
51
- # @private The Google API Client object.
52
- attr_accessor :gapi
53
-
54
- ##
55
- # @private Create an empty Zone object.
56
- def initialize
57
- @service = nil
58
- @gapi = {}
59
- end
60
-
61
- ##
62
- # Unique identifier for the resource; defined by the server.
63
- #
64
- def id
65
- @gapi.id
66
- end
67
-
68
- ##
69
- # User assigned name for this resource. Must be unique within the project.
70
- # The name must be 1-32 characters long, must begin with a letter, end
71
- # with a letter or digit, and only contain lowercase letters, digits or
72
- # dashes.
73
- #
74
- def name
75
- @gapi.name
76
- end
77
-
78
- ##
79
- # The DNS name of this managed zone, for instance "example.com.".
80
- #
81
- def dns
82
- @gapi.dns_name
83
- end
84
-
85
- ##
86
- # A string of at most 1024 characters associated with this resource for
87
- # the user's convenience. Has no effect on the managed zone's function.
88
- #
89
- def description
90
- @gapi.description
91
- end
92
-
93
- ##
94
- # Delegate your managed_zone to these virtual name servers; defined by the
95
- # server.
96
- #
97
- def name_servers
98
- Array(@gapi.name_servers)
99
- end
100
-
101
- ##
102
- # Optionally specifies the NameServerSet for this ManagedZone. A
103
- # NameServerSet is a set of DNS name servers that all host the same
104
- # ManagedZones. Most users will leave this field unset.
105
- #
106
- def name_server_set
107
- @gapi.name_server_set
108
- end
109
-
110
- ##
111
- # The time that this resource was created on the server.
112
- #
113
- def created_at
114
- Time.parse @gapi.creation_time
115
- rescue
116
- nil
117
- end
118
-
119
- ##
120
- # Permanently deletes the zone.
121
- #
122
- # @param [Boolean] force If `true`, ensures the deletion of the zone by
123
- # first deleting all records. If `false` and the zone contains
124
- # non-essential records, the request will fail. Default is `false`.
125
- #
126
- # @return [Boolean] Returns `true` if the zone was deleted.
127
- #
128
- # @example
129
- # require "gcloud"
130
- #
131
- # gcloud = Gcloud.new
132
- # dns = gcloud.dns
133
- # zone = dns.zone "example-com"
134
- # zone.delete
135
- #
136
- # @example The zone can be forcefully deleted with the `force` option:
137
- # require "gcloud"
138
- #
139
- # gcloud = Gcloud.new
140
- # dns = gcloud.dns
141
- # zone = dns.zone "example-com"
142
- # zone.delete force: true
143
- #
144
- def delete force: false
145
- clear! if force
146
-
147
- ensure_service!
148
- service.delete_zone id
149
- true
150
- end
151
-
152
- ##
153
- # Removes non-essential records from the zone. Only NS and SOA records
154
- # will be kept.
155
- #
156
- # @example
157
- # require "gcloud"
158
- #
159
- # gcloud = Gcloud.new
160
- # dns = gcloud.dns
161
- # zone = dns.zone "example-com"
162
- # zone.clear!
163
- #
164
- def clear!
165
- non_essential = records.all.reject { |r| %w(SOA NS).include?(r.type) }
166
- change = update [], non_essential
167
- change.wait_until_done! unless change.nil?
168
- end
169
-
170
- ##
171
- # Retrieves an existing change by id.
172
- #
173
- # @param [String] change_id The id of a change.
174
- #
175
- # @return [Gcloud::Dns::Change, nil] Returns `nil` if the change does not
176
- # exist.
177
- #
178
- # @example
179
- # require "gcloud"
180
- #
181
- # gcloud = Gcloud.new
182
- # dns = gcloud.dns
183
- # zone = dns.zone "example-com"
184
- # change = zone.change "2"
185
- # if change
186
- # puts "#{change.id} - #{change.started_at} - #{change.status}"
187
- # end
188
- #
189
- def change change_id
190
- ensure_service!
191
- gapi = service.get_change id, change_id
192
- Change.from_gapi gapi, self
193
- rescue Gcloud::NotFoundError
194
- nil
195
- end
196
- alias_method :find_change, :change
197
- alias_method :get_change, :change
198
-
199
- ##
200
- # Retrieves the list of changes belonging to the zone.
201
- #
202
- # @param [String] token A previously-returned page token representing part
203
- # of the larger set of results to view.
204
- # @param [Integer] max Maximum number of changes to return.
205
- # @param [Symbol, String] order Sort the changes by change sequence.
206
- #
207
- # Acceptable values are:
208
- #
209
- # * `asc` - Sort by ascending change sequence
210
- # * `desc` - Sort by descending change sequence
211
- #
212
- # @return [Array<Gcloud::Dns::Change>] (See {Gcloud::Dns::Change::List})
213
- #
214
- # @example
215
- # require "gcloud"
216
- #
217
- # gcloud = Gcloud.new
218
- # dns = gcloud.dns
219
- # zone = dns.zone "example-com"
220
- # changes = zone.changes
221
- # changes.each do |change|
222
- # puts "#{change.id} - #{change.started_at} - #{change.status}"
223
- # end
224
- #
225
- # @example The changes can be sorted by change sequence:
226
- # require "gcloud"
227
- #
228
- # gcloud = Gcloud.new
229
- # dns = gcloud.dns
230
- # zone = dns.zone "example-com"
231
- # changes = zone.changes order: :desc
232
- #
233
- # @example Retrieve all changes: (See {Change::List#all})
234
- # require "gcloud"
235
- #
236
- # gcloud = Gcloud.new
237
- # dns = gcloud.dns
238
- # zone = dns.zone "example-com"
239
- # changes = zone.changes
240
- # changes.all do |change|
241
- # puts "#{change.name} - #{change.status}"
242
- # end
243
- #
244
- def changes token: nil, max: nil, order: nil
245
- ensure_service!
246
- # Fix the sort options
247
- order = adjust_change_sort_order order
248
- sort = "changeSequence" if order
249
- # Continue with the API call
250
- gapi = service.list_changes id, token: token, max: max,
251
- order: order, sort: sort
252
- Change::List.from_gapi gapi, self, max, order
253
- end
254
- alias_method :find_changes, :changes
255
-
256
- ##
257
- # Retrieves the list of records belonging to the zone.
258
- # Records can be filtered by name and type. The name argument can be a
259
- # subdomain (e.g., `www`) fragment for convenience, but notice that the
260
- # retrieved record's domain name is always fully-qualified.
261
- #
262
- # @param [String] name Return only records with this domain or subdomain
263
- # name.
264
- # @param [String] type Return only records with this [record
265
- # type](https://cloud.google.com/dns/what-is-cloud-dns). If present, the
266
- # `name` parameter must also be present.
267
- # @param [String] token A previously-returned page token representing part
268
- # of the larger set of results to view.
269
- # @param [Integer] max Maximum number of records to return.
270
- #
271
- # @return [Array<Gcloud::Dns::Record>] (See {Gcloud::Dns::Record::List})
272
- #
273
- # @example
274
- # require "gcloud"
275
- #
276
- # gcloud = Gcloud.new
277
- # dns = gcloud.dns
278
- # zone = dns.zone "example-com"
279
- # records = zone.records
280
- # records.each do |record|
281
- # puts record.name
282
- # end
283
- #
284
- # @example Records can be filtered by name and type:
285
- # require "gcloud"
286
- #
287
- # gcloud = Gcloud.new
288
- # dns = gcloud.dns
289
- # zone = dns.zone "example-com"
290
- # records = zone.records "www", "A"
291
- # records.first.name #=> "www.example.com."
292
- #
293
- # @example Retrieve all records: (See {Gcloud::Dns::Record::List#all})
294
- # require "gcloud"
295
- #
296
- # gcloud = Gcloud.new
297
- # dns = gcloud.dns
298
- # zone = dns.zone "example-com"
299
- # records = zone.records "example.com."
300
- #
301
- # records.all do |record|
302
- # puts record.name
303
- # end
304
- #
305
- def records name = nil, type = nil, token: nil, max: nil
306
- ensure_service!
307
-
308
- name = fqdn(name) if name
309
-
310
- gapi = service.list_records id, name, type, token: token, max: max
311
- Record::List.from_gapi gapi, self, name, type, max
312
- end
313
- alias_method :find_records, :records
314
-
315
- ##
316
- # Creates a new, unsaved Record that can be added to a Zone.
317
- #
318
- # @return [Gcloud::Dns::Record]
319
- #
320
- # @example
321
- # require "gcloud"
322
- #
323
- # gcloud = Gcloud.new
324
- # dns = gcloud.dns
325
- # zone = dns.zone "example-com"
326
- # record = zone.record "example.com.", "A", 86400, ["1.2.3.4"]
327
- # zone.add record
328
- #
329
- def record name, type, ttl, data
330
- Gcloud::Dns::Record.new fqdn(name), type, ttl, data
331
- end
332
- alias_method :new_record, :record
333
-
334
- ##
335
- # Exports the zone to a local [DNS zone
336
- # file](https://en.wikipedia.org/wiki/Zone_file).
337
- #
338
- # @param [String] path The path on the local file system to write the data
339
- # to.The path provided must be writable.
340
- #
341
- # @return [File] An object on the local file system.
342
- #
343
- # @example
344
- # require "gcloud"
345
- #
346
- # gcloud = Gcloud.new
347
- # dns = gcloud.dns
348
- # zone = dns.zone "example-com"
349
- #
350
- # zone.export "path/to/db.example.com"
351
- #
352
- def export path
353
- File.open path, "w" do |f|
354
- f.write to_zonefile
355
- end
356
- end
357
-
358
- ##
359
- # Imports resource records from a [DNS zone
360
- # file](https://en.wikipedia.org/wiki/Zone_file), adding the new records
361
- # to the zone, without removing any existing records from the zone.
362
- #
363
- # Because the Google Cloud DNS API only accepts a single resource record
364
- # for each `name` and `type` combination (with multiple `data` elements),
365
- # the zone file's records are merged as necessary. During this merge, the
366
- # lowest `ttl` of the merged records is used. If none of the merged
367
- # records have a `ttl` value, the zone file's global TTL is used for the
368
- # record.
369
- #
370
- # The zone file's SOA and NS records are not imported, because the zone
371
- # was given SOA and NS records when it was created. These generated
372
- # records point to Cloud DNS name servers.
373
- #
374
- # This operation automatically updates the SOA record serial number unless
375
- # prevented with the `skip_soa` option. See {#update} for details.
376
- #
377
- # The Google Cloud DNS service requires that record names and data use
378
- # fully-qualified addresses. The @ symbol is not accepted, nor are
379
- # unqualified subdomain addresses like www. If your zone file contains
380
- # such values, you may need to pre-process it in order for the import
381
- # operation to succeed.
382
- #
383
- # @param [String, IO] path_or_io The path to a zone file on the
384
- # filesystem, or an IO instance from which zone file data can be read.
385
- # @param [String, Array<String>] only Include only records of this type or
386
- # types.
387
- # @param [String, Array<String>] except Exclude records of this type or
388
- # types.
389
- # @param [Boolean] skip_soa Do not automatically update the SOA record
390
- # serial number. See {#update} for details.
391
- # @param [Integer, lambda, Proc] soa_serial A value (or a lambda or Proc
392
- # returning a value) for the new SOA record serial number. See {#update}
393
- # for details.
394
- #
395
- # @return [Gcloud::Dns::Change] A new change adding the imported Record
396
- # instances.
397
- #
398
- # @example
399
- # require "gcloud"
400
- #
401
- # gcloud = Gcloud.new
402
- # dns = gcloud.dns
403
- # zone = dns.zone "example-com"
404
- # change = zone.import "path/to/db.example.com"
405
- #
406
- def import path_or_io, only: nil, except: nil,
407
- skip_soa: nil, soa_serial: nil
408
- except = (Array(except).map(&:to_s).map(&:upcase) + %w(SOA NS)).uniq
409
- importer = Gcloud::Dns::Importer.new self, path_or_io
410
- additions = importer.records only: only, except: except
411
- update additions, [], skip_soa: skip_soa, soa_serial: soa_serial
412
- end
413
-
414
- # rubocop:disable all
415
- # Disabled rubocop because this complexity cannot easily be avoided.
416
-
417
- ##
418
- # Adds and removes Records from the zone. All changes are made in a single
419
- # API request.
420
- #
421
- # The best way to add, remove, and update multiple records in a single
422
- # [transaction](https://cloud.google.com/dns/records) is with a block. See
423
- # {Zone::Transaction}.
424
- #
425
- # If the SOA record for the zone is not present in `additions` or
426
- # `deletions` (and if present in one, it should be present in the other),
427
- # it will be added to both, and its serial number will be incremented by
428
- # adding `1`. This update to the SOA record can be prevented with the
429
- # `skip_soa` option. To provide your own value or behavior for the new
430
- # serial number, use the `soa_serial` option.
431
- #
432
- # @param [Record, Array<Record>] additions The Record or array of records
433
- # to add.
434
- # @param [Record, Array<Record>] deletions The Record or array of records
435
- # to remove.
436
- # @param [Boolean] skip_soa Do not automatically update the SOA record
437
- # serial number.
438
- # @param [Integer, lambda, Proc] soa_serial A value (or a lambda or Proc
439
- # returning a value) for the new SOA record serial number.
440
- # @yield [tx] a block yielding a new transaction
441
- # @yieldparam [Zone::Transaction] tx the transaction object
442
- #
443
- # @return [Gcloud::Dns::Change]
444
- #
445
- # @example Using a block:
446
- # require "gcloud"
447
- #
448
- # gcloud = Gcloud.new
449
- # dns = gcloud.dns
450
- # zone = dns.zone "example-com"
451
- # change = zone.update do |tx|
452
- # tx.add "example.com.", "A", 86400, "1.2.3.4"
453
- # tx.remove "example.com.", "TXT"
454
- # tx.replace "example.com.", "MX", 86400, ["10 mail1.example.com.",
455
- # "20 mail2.example.com."]
456
- # tx.modify "www.example.com.", "CNAME" do |cname|
457
- # cname.ttl = 86400 # only change the TTL
458
- # end
459
- # end
460
- #
461
- # @example Or you can provide the record objects to add and remove:
462
- # require "gcloud"
463
- #
464
- # gcloud = Gcloud.new
465
- # dns = gcloud.dns
466
- # zone = dns.zone "example-com"
467
- # new_record = zone.record "example.com.", "A", 86400, ["1.2.3.4"]
468
- # old_record = zone.record "example.com.", "A", 18600, ["1.2.3.4"]
469
- # change = zone.update [new_record], [old_record]
470
- #
471
- # @example Using a lambda or Proc to update the current SOA serial number:
472
- # require "gcloud"
473
- #
474
- # gcloud = Gcloud.new
475
- # dns = gcloud.dns
476
- # zone = dns.zone "example-com"
477
- # new_record = zone.record "example.com.", "A", 86400, ["1.2.3.4"]
478
- # change = zone.update new_record, soa_serial: lambda { |sn| sn + 10 }
479
- #
480
- def update additions = [], deletions = [], skip_soa: nil, soa_serial: nil
481
- # Handle only sending in options
482
- if additions.is_a?(::Hash) && deletions.empty? && options.empty?
483
- options = additions
484
- additions = []
485
- elsif deletions.is_a?(::Hash) && options.empty?
486
- options = deletions
487
- deletions = []
488
- end
489
-
490
- additions = Array additions
491
- deletions = Array deletions
492
-
493
- if block_given?
494
- updater = Zone::Transaction.new self
495
- yield updater
496
- additions += updater.additions
497
- deletions += updater.deletions
498
- end
499
-
500
- to_add = additions - deletions
501
- to_remove = deletions - additions
502
- return nil if to_add.empty? && to_remove.empty?
503
- unless skip_soa || detect_soa(to_add) || detect_soa(to_remove)
504
- increment_soa to_add, to_remove, soa_serial
505
- end
506
- create_change to_add, to_remove
507
- end
508
-
509
- # rubocop:enable all
510
-
511
- ##
512
- # Adds a record to the Zone. In order to update existing records, or add
513
- # and delete records in the same transaction, use #update.
514
- #
515
- # This operation automatically updates the SOA record serial number unless
516
- # prevented with the `skip_soa` option. See {#update} for details.
517
- #
518
- # @param [String] name The owner of the record. For example:
519
- # `example.com.`.
520
- # @param [String] type The identifier of a [supported record
521
- # type](https://cloud.google.com/dns/what-is-cloud-dns).
522
- # For example: `A`, `AAAA`, `CNAME`, `MX`, or `TXT`.
523
- # @param [Integer] ttl The number of seconds that the record can be cached
524
- # by resolvers.
525
- # @param [String, Array<String>] data The resource record data, as
526
- # determined by `type` and defined in [RFC
527
- # 1035 (section 5)](http://tools.ietf.org/html/rfc1035#section-5) and
528
- # [RFC 1034
529
- # (section 3.6.1)](http://tools.ietf.org/html/rfc1034#section-3.6.1).
530
- # For example: `192.0.2.1` or `example.com.`.
531
- # @param [Boolean] skip_soa Do not automatically update the SOA record
532
- # serial number. See {#update} for details.
533
- # @param [Integer+, lambda, Proc] soa_serial A value (or a lambda or Proc
534
- # returning a value) for the new SOA record serial number. See {#update}
535
- # for details.
536
- #
537
- # @return [Gcloud::Dns::Change]
538
- #
539
- # @example
540
- # require "gcloud"
541
- #
542
- # gcloud = Gcloud.new
543
- # dns = gcloud.dns
544
- # zone = dns.zone "example-com"
545
- # change = zone.add "example.com.", "A", 86400, ["1.2.3.4"]
546
- #
547
- def add name, type, ttl, data, skip_soa: nil, soa_serial: nil
548
- update [record(name, type, ttl, data)], [],
549
- skip_soa: skip_soa, soa_serial: soa_serial
550
- end
551
-
552
- ##
553
- # Removes records from the Zone. The records are looked up before they are
554
- # removed. In order to update existing records, or add and remove records
555
- # in the same transaction, use #update.
556
- #
557
- # This operation automatically updates the SOA record serial number unless
558
- # prevented with the `skip_soa` option. See {#update} for details.
559
- #
560
- # @param [String] name The owner of the record. For example:
561
- # `example.com.`.
562
- # @param [String] type The identifier of a [supported record
563
- # type](https://cloud.google.com/dns/what-is-cloud-dns).
564
- # For example: `A`, `AAAA`, `CNAME`, `MX`, or `TXT`.
565
- # @param [Boolean] skip_soa Do not automatically update the SOA record
566
- # serial number. See {#update} for details.
567
- # @param [Integer+, lambda, Proc] soa_serial A value (or a lambda or Proc
568
- # returning a value) for the new SOA record serial number. See {#update}
569
- # for details.
570
- #
571
- # @return [Gcloud::Dns::Change]
572
- #
573
- # @example
574
- # require "gcloud"
575
- #
576
- # gcloud = Gcloud.new
577
- # dns = gcloud.dns
578
- # zone = dns.zone "example-com"
579
- # change = zone.remove "example.com.", "A"
580
- #
581
- def remove name, type, skip_soa: nil, soa_serial: nil
582
- update [], records(name, type).all.to_a,
583
- skip_soa: skip_soa, soa_serial: soa_serial
584
- end
585
-
586
- ##
587
- # Replaces existing records on the Zone. Records matching the `name` and
588
- # `type` are replaced. In order to update existing records, or add and
589
- # delete records in the same transaction, use #update.
590
- #
591
- # This operation automatically updates the SOA record serial number unless
592
- # prevented with the `skip_soa` option. See {#update} for details.
593
- #
594
- # @param [String] name The owner of the record. For example:
595
- # `example.com.`.
596
- # @param [String] type The identifier of a [supported record
597
- # type](https://cloud.google.com/dns/what-is-cloud-dns).
598
- # For example: `A`, `AAAA`, `CNAME`, `MX`, or `TXT`.
599
- # @param [Integer] ttl The number of seconds that the record can be cached
600
- # by resolvers.
601
- # @param [String, Array<String>] data The resource record data, as
602
- # determined by `type` and defined in
603
- # [RFC 1035 (section 5)](http://tools.ietf.org/html/rfc1035#section-5)
604
- # and [RFC 1034 (section
605
- # 3.6.1)](http://tools.ietf.org/html/rfc1034#section-3.6.1). For
606
- # example: `192.0.2.1` or `example.com.`.
607
- # @param [Boolean] skip_soa Do not automatically update the SOA record
608
- # serial number. See {#update} for details.
609
- # @param [Integer+, lambda, Proc] soa_serial A value (or a lambda or Proc
610
- # returning a value) for the new SOA record serial number. See {#update}
611
- # for details.
612
- #
613
- # @return [Gcloud::Dns::Change]
614
- #
615
- # @example
616
- # require "gcloud"
617
- #
618
- # gcloud = Gcloud.new
619
- # dns = gcloud.dns
620
- # zone = dns.zone "example-com"
621
- # change = zone.replace "example.com.", "A", 86400, ["5.6.7.8"]
622
- #
623
- def replace name, type, ttl, data, skip_soa: nil, soa_serial: nil
624
- update [record(name, type, ttl, data)],
625
- records(name, type).all.to_a,
626
- skip_soa: skip_soa, soa_serial: soa_serial
627
- end
628
-
629
- # @private
630
- def to_zonefile
631
- records.all.map(&:to_zonefile_records).flatten.join("\n")
632
- end
633
-
634
- ##
635
- # Modifies records on the Zone. Records matching the `name` and `type` are
636
- # yielded to the block where they can be modified.
637
- #
638
- # This operation automatically updates the SOA record serial number unless
639
- # prevented with the `skip_soa` option. See {#update} for details.
640
- #
641
- # @param [String] name The owner of the record. For example:
642
- # `example.com.`.
643
- # @param [String] type The identifier of a [supported record
644
- # type](https://cloud.google.com/dns/what-is-cloud-dns).
645
- # For example: `A`, `AAAA`, `CNAME`, `MX`, or `TXT`.
646
- # @param [Boolean] skip_soa Do not automatically update the SOA record
647
- # serial number. See {#update} for details.
648
- # @param [Integer+, lambda, Proc] soa_serial A value (or a lambda or Proc
649
- # returning a value) for the new SOA record serial number. See {#update}
650
- # for details.
651
- # @yield [record] a block yielding each matching record
652
- # @yieldparam [Record] record the record to be modified
653
- #
654
- # @return [Gcloud::Dns::Change]
655
- #
656
- # @example
657
- # require "gcloud"
658
- #
659
- # gcloud = Gcloud.new
660
- # dns = gcloud.dns
661
- # zone = dns.zone "example-com"
662
- # change = zone.modify "example.com.", "MX" do |mx|
663
- # mx.ttl = 3600 # change only the TTL
664
- # end
665
- #
666
- def modify name, type, skip_soa: nil, soa_serial: nil
667
- existing = records(name, type).all.to_a
668
- updated = existing.map(&:dup)
669
- updated.each { |r| yield r }
670
- update updated, existing, skip_soa: skip_soa, soa_serial: soa_serial
671
- end
672
-
673
- ##
674
- # This helper converts the given domain name or subdomain (e.g., `www`)
675
- # fragment to a [fully qualified domain name
676
- # (FQDN)](https://en.wikipedia.org/wiki/Fully_qualified_domain_name) for
677
- # the zone's #dns. If the argument is already a FQDN, it is returned
678
- # unchanged.
679
- #
680
- # @param [String] domain_name The name to convert to a fully qualified
681
- # domain name.
682
- #
683
- # @return [String] A fully qualified domain name.
684
- #
685
- # @example
686
- # require "gcloud"
687
- #
688
- # gcloud = Gcloud.new
689
- # dns = gcloud.dns
690
- # zone = dns.zone "example-com"
691
- # zone.fqdn "www" #=> "www.example.com."
692
- # zone.fqdn "@" #=> "example.com."
693
- # zone.fqdn "mail.example.com." #=> "mail.example.com."
694
- #
695
- def fqdn domain_name
696
- Service.fqdn domain_name, dns
697
- end
698
-
699
- ##
700
- # @private New Zone from a Google API Client object.
701
- def self.from_gapi gapi, conn
702
- new.tap do |f|
703
- f.gapi = gapi
704
- f.service = conn
705
- end
706
- end
707
-
708
- protected
709
-
710
- ##
711
- # Raise an error unless an active connection is available.
712
- def ensure_service!
713
- fail "Must have active connection" unless service
714
- end
715
-
716
- def create_change additions, deletions
717
- ensure_service!
718
- gapi = service.create_change id, additions.map(&:to_gapi),
719
- deletions.map(&:to_gapi)
720
- Change.from_gapi gapi, self
721
- end
722
-
723
- def increment_soa to_add, to_remove, soa_serial
724
- current_soa = detect_soa records(dns, "SOA").all
725
- return false if current_soa.nil?
726
- updated_soa = current_soa.dup
727
- updated_soa.data[0] = replace_soa_serial updated_soa.data[0], soa_serial
728
- to_add << updated_soa
729
- to_remove << current_soa
730
- end
731
-
732
- def detect_soa records
733
- records.detect { |r| r.type == "SOA" }
734
- end
735
-
736
- def replace_soa_serial soa_data, soa_serial
737
- soa_data = soa_data.split " "
738
- current_serial = soa_data[2].to_i
739
- soa_data[2] = if soa_serial && soa_serial.respond_to?(:call)
740
- soa_serial.call current_serial
741
- elsif soa_serial
742
- soa_serial.to_i
743
- else
744
- current_serial + 1
745
- end
746
- soa_data.join " "
747
- end
748
-
749
- def adjust_change_sort_order order
750
- return nil if order.nil?
751
- if order.to_s.downcase.start_with? "d"
752
- "descending"
753
- else
754
- "ascending"
755
- end
756
- end
757
- end
758
- end
759
- end