gcloud 0.12.2 → 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.
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,158 +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/list"
17
- require "time"
18
-
19
- module Gcloud
20
- module Dns
21
- ##
22
- # # DNS Change
23
- #
24
- # Represents a request containing additions or deletions or records.
25
- # Additions and deletions can be done in bulk, in a single atomic
26
- # transaction, and take effect at the same time in each authoritative DNS
27
- # server.
28
- #
29
- # @example
30
- # require "gcloud"
31
- #
32
- # gcloud = Gcloud.new
33
- # dns = gcloud.dns
34
- # zone = dns.zone "example-com"
35
- # zone.changes.each do |change|
36
- # puts "Change includes #{change.additions.count} additions " \
37
- # "and #{change.additions.count} deletions."
38
- # end
39
- #
40
- class Change
41
- ##
42
- # @private The Zone object this Change belongs to.
43
- attr_accessor :zone
44
-
45
- ##
46
- # @private The Google API Client object.
47
- attr_accessor :gapi
48
-
49
- ##
50
- # @private Create an empty Change object.
51
- def initialize
52
- @zone = nil
53
- @gapi = {}
54
- end
55
-
56
- ##
57
- # Unique identifier for the resource; defined by the server.
58
- #
59
- def id
60
- @gapi.id
61
- end
62
-
63
- ##
64
- # The records added in this change request.
65
- #
66
- def additions
67
- Array(@gapi.additions).map { |gapi| Record.from_gapi gapi }
68
- end
69
-
70
- ##
71
- # The records removed in this change request.
72
- #
73
- def deletions
74
- Array(@gapi.deletions).map { |gapi| Record.from_gapi gapi }
75
- end
76
-
77
- ##
78
- # Status of the operation. Values are `"done"` and `"pending"`.
79
- #
80
- def status
81
- @gapi.status
82
- end
83
-
84
- ##
85
- # Checks if the status is `"done"`.
86
- def done?
87
- return false if status.nil?
88
- "done".casecmp(status).zero?
89
- end
90
-
91
- ##
92
- # Checks if the status is `"pending"`.
93
- def pending?
94
- return false if status.nil?
95
- "pending".casecmp(status).zero?
96
- end
97
-
98
- ##
99
- # The time that this operation was started by the server.
100
- #
101
- def started_at
102
- Time.parse @gapi.start_time
103
- rescue
104
- nil
105
- end
106
-
107
- ##
108
- # Reloads the change with updated status from the DNS service.
109
- def reload!
110
- ensure_service!
111
- @gapi = zone.service.get_change @zone.id, id
112
- end
113
- alias_method :refresh!, :reload!
114
-
115
- ##
116
- # Refreshes the change until the status is `done`.
117
- # The delay between refreshes will incrementally increase.
118
- #
119
- # @example
120
- # require "gcloud"
121
- #
122
- # gcloud = Gcloud.new
123
- # dns = gcloud.dns
124
- # zone = dns.zone "example-com"
125
- # change = zone.change 1234567890
126
- # change.done? #=> false
127
- # change.wait_until_done!
128
- # change.done? #=> true
129
- #
130
- def wait_until_done!
131
- backoff = ->(retries) { sleep 2 * retries + 5 }
132
- retries = 0
133
- until done?
134
- backoff.call retries
135
- retries += 1
136
- reload!
137
- end
138
- end
139
-
140
- ##
141
- # @private New Change from a Google API Client object.
142
- def self.from_gapi gapi, zone
143
- new.tap do |f|
144
- f.gapi = gapi
145
- f.zone = zone
146
- end
147
- end
148
-
149
- protected
150
-
151
- ##
152
- # Raise an error unless an active service is available.
153
- def ensure_service!
154
- fail "Must have active connection" unless zone && zone.service
155
- end
156
- end
157
- end
158
- end
@@ -1,173 +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 "delegate"
17
-
18
- module Gcloud
19
- module Dns
20
- class Change
21
- ##
22
- # Change::List is a special case Array with additional values.
23
- class List < DelegateClass(::Array)
24
- ##
25
- # If not empty, indicates that there are more changes that match
26
- # the request and this value should be passed to continue.
27
- attr_accessor :token
28
-
29
- ##
30
- # @private Create a new Change::List with an array of Change instances.
31
- def initialize arr = []
32
- super arr
33
- end
34
-
35
- ##
36
- # Whether there a next page of changes.
37
- #
38
- # @return [Boolean]
39
- #
40
- # @example
41
- # require "gcloud"
42
- #
43
- # gcloud = Gcloud.new
44
- # dns = gcloud.dns
45
- # zone = dns.zone "example-com"
46
- #
47
- # changes = zone.changes
48
- # if changes.next?
49
- # next_changes = changes.next
50
- # end
51
- #
52
- def next?
53
- !token.nil?
54
- end
55
-
56
- ##
57
- # Retrieve the next page of changes.
58
- #
59
- # @return [Change::List]
60
- #
61
- # @example
62
- # require "gcloud"
63
- #
64
- # gcloud = Gcloud.new
65
- # dns = gcloud.dns
66
- # zone = dns.zone "example-com"
67
- #
68
- # changes = zone.changes
69
- # if changes.next?
70
- # next_changes = changes.next
71
- # end
72
- #
73
- def next
74
- return nil unless next?
75
- ensure_zone!
76
- @zone.changes token: token, max: @max, order: @order
77
- end
78
-
79
- ##
80
- # Retrieves all changes by repeatedly loading {#next} until {#next?}
81
- # returns `false`. Calls the given block once for each change, which is
82
- # passed as the parameter.
83
- #
84
- # An Enumerator is returned if no block is given.
85
- #
86
- # This method may make several API calls until all changes are
87
- # retrieved. Be sure to use as narrow a search criteria as possible.
88
- # Please use with caution.
89
- #
90
- # @param [Integer] request_limit The upper limit of API requests to make
91
- # to load all changes. Default is no limit.
92
- # @yield [change] The block for accessing each change.
93
- # @yieldparam [Change] change The change object.
94
- #
95
- # @return [Enumerator]
96
- #
97
- # @example Iterating each change by passing a block:
98
- # require "gcloud"
99
- #
100
- # gcloud = Gcloud.new
101
- # dns = gcloud.dns
102
- # zone = dns.zone "example-com"
103
- # changes = zone.changes
104
- #
105
- # changes.all do |change|
106
- # puts change.name
107
- # end
108
- #
109
- # @example Using the enumerator by not passing a block:
110
- # require "gcloud"
111
- #
112
- # gcloud = Gcloud.new
113
- # dns = gcloud.dns
114
- # zone = dns.zone "example-com"
115
- # changes = zone.changes
116
- #
117
- # all_names = changes.all.map do |change|
118
- # change.name
119
- # end
120
- #
121
- # @example Limit the number of API calls made:
122
- # require "gcloud"
123
- #
124
- # gcloud = Gcloud.new
125
- # dns = gcloud.dns
126
- # zone = dns.zone "example-com"
127
- # changes = zone.changes
128
- #
129
- # changes.all(request_limit: 10) do |change|
130
- # puts change.name
131
- # end
132
- #
133
- def all request_limit: nil
134
- request_limit = request_limit.to_i if request_limit
135
- unless block_given?
136
- return enum_for(:all, request_limit: request_limit)
137
- end
138
- results = self
139
- loop do
140
- results.each { |r| yield r }
141
- if request_limit
142
- request_limit -= 1
143
- break if request_limit < 0
144
- end
145
- break unless results.next?
146
- results = results.next
147
- end
148
- end
149
-
150
- ##
151
- # @private New Changes::List from a response object.
152
- def self.from_gapi gapi, zone, max = nil, order = nil
153
- changes = new(Array(gapi.changes).map do |g|
154
- Change.from_gapi g, zone
155
- end)
156
- changes.instance_variable_set "@token", gapi.next_page_token
157
- changes.instance_variable_set "@zone", zone
158
- changes.instance_variable_set "@max", max
159
- changes.instance_variable_set "@order", order
160
- changes
161
- end
162
-
163
- protected
164
-
165
- ##
166
- # Raise an error unless an active connection is available.
167
- def ensure_zone!
168
- fail "Must have active connection" unless @zone
169
- end
170
- end
171
- end
172
- end
173
- end
@@ -1,29 +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/credentials"
17
-
18
- module Gcloud
19
- module Dns
20
- ##
21
- # @private Represents the Oauth2 signing logic for DNS.
22
- class Credentials < Gcloud::Credentials
23
- SCOPE = ["https://www.googleapis.com/auth/ndev.clouddns.readwrite"]
24
- PATH_ENV_VARS = %w(DNS_KEYFILE GCLOUD_KEYFILE GOOGLE_CLOUD_KEYFILE)
25
- JSON_ENV_VARS = %w(DNS_KEYFILE_JSON GCLOUD_KEYFILE_JSON
26
- GOOGLE_CLOUD_KEYFILE_JSON)
27
- end
28
- end
29
- end
@@ -1,183 +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 "zonefile"
17
- require "gcloud/dns/record"
18
-
19
- module Gcloud
20
- module Dns
21
- ##
22
- # @private
23
- # # DNS Importer
24
- #
25
- # Reads a [DNS zone
26
- # file](https://en.wikipedia.org/wiki/Zone_file) and parses it, creating a
27
- # collection of Record instances. The returned records are unsaved,
28
- # as they are not yet associated with a Zone. Use {Zone#import} to add zone
29
- # file records to a Zone.
30
- #
31
- # Because the Google Cloud DNS API only accepts a single resource record for
32
- # each `name` and `type` combination (with multiple `data` elements), the
33
- # zone file's records are merged as necessary. During this merge, the lowest
34
- # `ttl` of the merged records is used. If none of the merged records have a
35
- # `ttl` value, the zone file's global TTL is used for the record.
36
- #
37
- # The following record types are supported: A, AAAA, CNAME, MX, NAPTR, NS,
38
- # PTR, SOA, SPF, SRV, and TXT.
39
- class Importer
40
- ##
41
- # Creates a new Importer that immediately parses the provided zone file
42
- # data and creates Record instances.
43
- #
44
- # @param [String, IO] path_or_io The path to a zone file on the
45
- # filesystem, or an IO instance from which zone file data can be read.
46
- #
47
- def initialize zone, path_or_io
48
- @zone = zone
49
- @merged_zf_records = {}
50
- @records = []
51
- @zonefile = create_zonefile path_or_io
52
- merge_zonefile_records
53
- from_zonefile_records
54
- @records.unshift soa_record
55
- end
56
-
57
- ##
58
- # Returns the Record instances created from the zone file.
59
- #
60
- # @param [String, Array<String>] only Include only records of this type or
61
- # types.
62
- # @param [String, Array<String>] except Exclude records of this type or
63
- # types.
64
- #
65
- # @return [Array<Record>] An array of unsaved {Record} instances
66
- #
67
- def records only: nil, except: nil
68
- ret = @records
69
- ret = ret.select { |r| Array(only).include? r.type } if only
70
- ret = ret.reject { |r| Array(except).include? r.type } if except
71
- ret
72
- end
73
-
74
- protected
75
-
76
- ##
77
- # The zonefile library returns a two-element array in which the first
78
- # element is a symbol type (:a, :mx, and so on), and the second element
79
- # is an array containing the records of that type. Group the records by
80
- # name and type instead.
81
- def merge_zonefile_records
82
- @zonefile.records.map do |r|
83
- type = r.first
84
- type = :aaaa if type == :a4
85
- r.last.each do |zf_record|
86
- name = Service.fqdn(zf_record[:name], @zonefile.origin)
87
- key = [name, type]
88
- (@merged_zf_records[key] ||= []) << zf_record
89
- end
90
- end
91
- end
92
-
93
- ##
94
- # Convert the grouped records to single array of records, merging records
95
- # of the same name and type into a single record with an array of rrdatas.
96
- def from_zonefile_records
97
- @records = @merged_zf_records.map do |key, zf_records|
98
- ttl = ttl_from_zonefile_records zf_records
99
- data = zf_records.map do |zf_record|
100
- data_from_zonefile_record(key[1], zf_record)
101
- end
102
- @zone.record key[0], key[1], ttl, data
103
- end
104
- end
105
-
106
- def soa_record
107
- zf_soa = @zonefile.soa
108
- ttl = ttl_to_i(zf_soa[:ttl]) || ttl_to_i(@zonefile.ttl)
109
- data = data_from_zonefile_record :soa, zf_soa
110
- @zone.record zf_soa[:origin], "SOA", ttl, data
111
- end
112
-
113
- ##
114
- # From a collection of records, take the lowest ttl
115
- def ttl_from_zonefile_records zf_records
116
- ttls = zf_records.map do |zf_record|
117
- ttl_to_i(zf_record[:ttl])
118
- end
119
- min_ttl = ttls.compact.sort.first
120
- min_ttl || ttl_to_i(@zonefile.ttl)
121
- end
122
-
123
- # rubocop:disable all
124
- # Rubocop's line-length and branch condition restrictions prevent
125
- # the most straightforward approach to converting zonefile's records
126
- # to our own. So disable rubocop for this operation.
127
-
128
- def data_from_zonefile_record type, zf_record
129
- case type.to_s.upcase
130
- when "A"
131
- "#{zf_record[:host]}"
132
- when "AAAA"
133
- "#{zf_record[:host]}"
134
- when "CNAME"
135
- "#{zf_record[:host]}"
136
- when "MX"
137
- "#{zf_record[:pri]} #{zf_record[:host]}"
138
- when "NAPTR"
139
- "#{zf_record[:order]} #{zf_record[:preference]} #{zf_record[:flags]} #{zf_record[:service]} #{zf_record[:regexp]} #{zf_record[:replacement]}"
140
- when "NS"
141
- "#{zf_record[:host]}"
142
- when "PTR"
143
- "#{zf_record[:host]}"
144
- when "SOA"
145
- "#{zf_record[:primary]} #{zf_record[:email]} #{zf_record[:serial]} #{zf_record[:refresh]} #{zf_record[:retry]} #{zf_record[:expire]} #{zf_record[:minimumTTL]}"
146
- when "SPF"
147
- "#{zf_record[:data]}"
148
- when "SRV"
149
- "#{zf_record[:pri]} #{zf_record[:weight]} #{zf_record[:port]} #{zf_record[:host]}"
150
- when "TXT"
151
- "#{zf_record[:text]}"
152
- else
153
- fail ArgumentError, "record type '#{type}' is not supported"
154
- end
155
- end
156
-
157
- # rubocop:enable all
158
-
159
- MULTIPLIER = { "s" => (1),
160
- "m" => (60),
161
- "h" => (60 * 60),
162
- "d" => (60 * 60 * 24),
163
- "w" => (60 * 60 * 24 * 7) } # :nodoc:
164
-
165
- def ttl_to_i ttl
166
- if ttl.respond_to?(:to_int) || ttl.to_s =~ /\A\d+\z/
167
- return ttl.to_i
168
- elsif (m = /\A(\d+)(w|d|h|m|s)\z/.match ttl)
169
- return m[1].to_i * MULTIPLIER[m[2]].to_i
170
- end
171
- nil
172
- end
173
-
174
- def create_zonefile path_or_io # :nodoc:
175
- if path_or_io.respond_to? :read
176
- Zonefile.new path_or_io.read
177
- else
178
- Zonefile.from_file path_or_io
179
- end
180
- end
181
- end
182
- end
183
- end