-
Notifications
You must be signed in to change notification settings - Fork 535
/
dns.rb
155 lines (144 loc) · 6.07 KB
/
dns.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
# Copyright 2015 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.
require "google-cloud-dns"
require "google/cloud/dns/project"
require "google/cloud/config"
require "google/cloud/env"
module Google
module Cloud
##
# # Google Cloud DNS
#
# Google Cloud DNS is a high-performance, resilient, global DNS service that
# provides a cost-effective way to make your applications and services
# available to your users. This programmable, authoritative DNS service can
# be used to easily publish and manage DNS records using the same
# infrastructure relied upon by Google. To learn more, read [What is Google
# Cloud DNS?](https://cloud.google.com/dns/what-is-cloud-dns).
#
# See {file:OVERVIEW.md Google Cloud DNS Overview}.
#
module Dns
##
# Creates a new `Project` instance connected to the DNS service.
# Each call creates a new connection.
#
# For more information on connecting to Google Cloud see the
# {file:AUTHENTICATION.md Authentication Guide}.
#
# @param [String] project_id Identifier for a DNS project. If not present,
# the default project for the credentials is used.
# @param [String, Hash, Google::Auth::Credentials] credentials The path to
# the keyfile as a String, the contents of the keyfile as a Hash, or a
# Google::Auth::Credentials object. (See {Dns::Credentials})
# @param [String, Array<String>] scope The OAuth 2.0 scopes controlling
# the set of resources and operations that the connection can access.
# See [Using OAuth 2.0 to Access Google
# APIs](https://developers.google.com/identity/protocols/OAuth2).
#
# The default scope is:
#
# * `https://www.googleapis.com/auth/ndev.clouddns.readwrite`
# @param [Integer] retries Number of times to retry requests on server
# error. The default value is `3`. Optional.
# @param [Integer] timeout Default timeout to use in requests. Optional.
# @param [String] endpoint Override of the endpoint host name. Optional.
# If the param is nil, uses the default endpoint.
# @param [String] project Alias for the `project_id` argument. Deprecated.
# @param [String] keyfile Alias for the `credentials` argument.
# Deprecated.
#
# @return [Google::Cloud::Dns::Project]
#
# @example
# require "google/cloud/dns"
#
# dns = Google::Cloud::Dns.new(
# project_id: "my-dns-project",
# credentials: "/path/to/keyfile.json"
# )
#
# zone = dns.zone "example-com"
#
def self.new project_id: nil, credentials: nil, scope: nil, retries: nil,
timeout: nil, endpoint: nil, project: nil, keyfile: nil
scope ||= configure.scope
retries ||= configure.retries
timeout ||= configure.timeout
endpoint ||= configure.endpoint
credentials ||= (keyfile || default_credentials(scope: scope))
unless credentials.is_a? Google::Auth::Credentials
credentials = Dns::Credentials.new credentials, scope: scope
end
project_id = resolve_project_id(project_id || project, credentials)
raise ArgumentError, "project_id is missing" if project_id.empty?
Dns::Project.new(
Dns::Service.new(
project_id, credentials,
retries: retries, timeout: timeout, host: endpoint, quota_project: configure.quota_project
)
)
end
##
# Configure the Google Cloud DNS library.
#
# The following DNS configuration parameters are supported:
#
# * `project_id` - (String) Identifier for a DNS project. (The
# parameter `project` is considered deprecated, but may also be used.)
# * `credentials` - (String, Hash, Google::Auth::Credentials) The path to
# the keyfile as a String, the contents of the keyfile as a Hash, or a
# Google::Auth::Credentials object. (See {Dns::Credentials}) (The
# parameter `keyfile` is considered deprecated, but may also be used.)
# * `scope` - (String, Array<String>) The OAuth 2.0 scopes controlling
# the set of resources and operations that the connection can access.
# * `retries` - (Integer) Number of times to retry requests on server
# error.
# * `timeout` - (Integer) Default timeout to use in requests.
# * `endpoint` - (String) Override of the endpoint host name, or `nil`
# to use the default endpoint.
#
# @return [Google::Cloud::Config] The configuration object the
# Google::Cloud::Dns library uses.
#
def self.configure
yield Google::Cloud.configure.dns if block_given?
Google::Cloud.configure.dns
end
##
# @private Default project.
def self.default_project_id
Google::Cloud.configure.dns.project_id ||
Google::Cloud.configure.project_id ||
Google::Cloud.env.project_id
end
##
# @private Default credentials.
def self.default_credentials scope: nil
Google::Cloud.configure.dns.credentials ||
Google::Cloud.configure.credentials ||
Dns::Credentials.default(scope: scope)
end
##
# @private Resolve project.
def self.resolve_project_id given_project, credentials
project_id = given_project || default_project_id
if credentials.respond_to? :project_id
project_id ||= credentials.project_id
end
project_id.to_s # Always cast to a string
end
end
end
end