This repository was archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.ts
More file actions
409 lines (394 loc) · 13.4 KB
/
index.ts
File metadata and controls
409 lines (394 loc) · 13.4 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*!
* Copyright 2014 Google Inc. 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.
* You may obtain a copy of the License at
*
* http://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.
*/
'use strict';
import {Stream} from 'stream';
import * as arrify from 'arrify';
import {Service, GoogleAuthOptions} from '@google-cloud/common';
import {paginator} from '@google-cloud/paginator';
import {promisifyAll} from '@google-cloud/promisify';
import * as extend from 'extend';
import {teenyRequest} from 'teeny-request';
import {Zone} from './zone';
import * as r from 'request';
export {Record, RecordMetadata} from './record';
export interface GetZonesRequest {
autoPaginate?: boolean;
maxApiCalls?: number;
maxResults?: number;
pageToken?: string;
}
export interface DNSConfig extends GoogleAuthOptions {
autoRetry?: boolean;
maxRetries?: number;
}
export interface GetZonesCallback {
(err: Error|null, zones: Zone[]|null, nextQuery?: GetZonesRequest|null,
apiResponse?: r.Response): void;
}
export type GetZonesResponse = [Zone[], GetZonesRequest | null, r.Response];
export interface GetZoneCallback {
(err: Error|null, zone?: Zone|null, apiResponse?: r.Response): void;
}
export interface CreateZoneRequest {
dnsName?: string;
description?: string;
name?: string;
}
export type CreateZoneResponse = [Zone, r.Response];
/**
* @typedef {object} ClientConfig
* @property {string} [projectId] The project ID from the Google Developer's
* Console, e.g. 'grape-spaceship-123'. We will also check the environment
* variable `GCLOUD_PROJECT` for your project ID. If your app is running in
* an environment which supports {@link
* https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application
* Application Default Credentials}, your project ID will be detected
* automatically.
* @property {string} [keyFilename] Full path to the a .json, .pem, or .p12 key
* downloaded from the Google Developers Console. If you provide a path to a
* JSON file, the `projectId` option above is not necessary. NOTE: .pem and
* .p12 require you to specify the `email` option as well.
* @property {string} [email] Account email address. Required when using a .pem
* or .p12 keyFilename.
* @property {object} [credentials] Credentials object.
* @property {string} [credentials.client_email]
* @property {string} [credentials.private_key]
* @property {boolean} [autoRetry=true] Automatically retry requests if the
* response is related to rate limits or certain intermittent server errors.
* We will exponentially backoff subsequent requests by default.
* @property {number} [maxRetries=3] Maximum number of automatic retries
* attempted before returning the error.
* @property {Constructor} [promise] Custom promise module to use instead of
* native Promises.
*/
/**
* [Cloud DNS](https://cloud.google.com/dns/what-is-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.
*
* @class
*
* @see [What is Cloud DNS?]{@link https://cloud.google.com/dns/what-is-cloud-dns}
*
* @param {ClientConfig} [options] Configuration options.
*
* @example <caption>Import the client library</caption>
* const {DNS} = require('@google-cloud/dns');
*
* @example <caption>Create a client that uses <a
* href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application">Application
* Default Credentials (ADC)</a>:</caption> const dns = new DNS();
*
* @example <caption>Create a client with <a
* href="https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually">explicit
* credentials</a>:</caption> const dns = new DNS({ projectId:
* 'your-project-id', keyFilename: '/path/to/keyfile.json'
* });
*
* @example <caption>include:samples/quickstart.js</caption>
* region_tag:dns_quickstart
* Full quickstart example:
*/
class DNS extends Service {
getZonesStream: (query: GetZonesRequest) => Stream;
constructor(options?: GoogleAuthOptions) {
options = options || {};
const config = {
baseUrl: 'https://www.googleapis.com/dns/v1',
scopes: [
'https://www.googleapis.com/auth/ndev.clouddns.readwrite',
'https://www.googleapis.com/auth/cloud-platform',
],
packageJson: require('../../package.json'),
requestModule: teenyRequest as typeof r,
};
super(config, options);
/**
* Get {@link Zone} objects for all of the zones in your project as
* a readable object stream.
*
* @method DNS#getZonesStream
* @param {GetZonesRequest} [query] Query object for listing zones.
* @returns {ReadableStream} A readable stream that emits {@link Zone} instances.
*
* @example
* const {DNS} = require('@google-cloud/dns');
* const dns = new DNS();
*
* dns.getZonesStream()
* .on('error', console.error)
* .on('data', function(zone) {
* // zone is a Zone object.
* })
* .on('end', () => {
* // All zones retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* dns.getZonesStream()
* .on('data', function(zone) {
* this.end();
* });
*/
this.getZonesStream = paginator.streamify('getZones');
}
/**
* Config to set for the zone.
*
* @typedef {object} CreateZoneRequest
* @property {string} dnsName DNS name for the zone. E.g. "example.com."
* @property {string} [description] Description text for the zone.
*/
/**
* @typedef {array} CreateZoneResponse
* @property {Zone} 0 The new {@link Zone}.
* @property {object} 1 The full API response.
*/
/**
* @callback CreateZoneCallback
* @param {?Error} err Request error, if any.
* @param {Zone} zone The new {@link Zone}.
* @param {object} apiResponse The full API response.
*/
/**
* Create a managed zone.
*
* @see [ManagedZones: create API Documentation]{@link https://cloud.google.com/dns/api/v1/managedZones/create}
*
* @throws {error} If a zone name is not provided.
* @throws {error} If a zone dnsName is not provided.
*
* @param {string} name Name of the zone to create, e.g. "my-zone".
* @param {CreateZoneRequest} [config] Config to set for the zone.
* @param {CreateZoneCallback} [callback] Callback function.
* @returns {Promise<CreateZoneResponse>}
* @throws {Error} If a name is not provided.
* @see Zone#create
*
* @example
* const {DNS} = require('@google-cloud/dns');
* const dns = new DNS();
*
* const config = {
* dnsName: 'example.com.', // note the period at the end of the domain.
* description: 'This zone is awesome!'
* };
*
* dns.createZone('my-awesome-zone', config, (err, zone, apiResponse) => {
* if (!err) {
* // The zone was created successfully.
* }
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* dns.createZone('my-awesome-zone', config).then((data) => {
* const zone = data[0];
* const apiResponse = data[1];
* });
*/
createZone(name: string, config: CreateZoneRequest):
Promise<CreateZoneResponse>;
createZone(
name: string, config: CreateZoneRequest, callback: GetZoneCallback): void;
createZone(
name: string, config: CreateZoneRequest,
callback?: GetZoneCallback): void|Promise<CreateZoneResponse> {
if (!name) {
throw new Error('A zone name is required.');
}
if (!config || !config.dnsName) {
throw new Error('A zone dnsName is required.');
}
config.name = name;
// Required by the API.
config.description = config.description || '';
this.request(
{
method: 'POST',
uri: '/managedZones',
json: config,
},
(err, resp) => {
if (err) {
callback!(err, null, resp);
return;
}
const zone = this.zone(resp.name);
zone.metadata = resp;
callback!(null, zone, resp);
});
}
/**
* Query object for listing zones.
*
* @typedef {object} GetZonesRequest
* @property {boolean} [autoPaginate=true] Have pagination handled
* automatically.
* @property {number} [maxApiCalls] Maximum number of API calls to make.
* @property {number} [maxResults] Maximum number of items plus prefixes to
* return.
* @property {string} [pageToken] A previously-returned page token
* representing part of the larger set of results to view.
*/
/**
* @typedef {array} GetZonesResponse
* @property {Zone[]} 0 Array of {@link Zone} instances.
* @property {object} 1 The full API response.
*/
/**
* @callback GetZonesCallback
* @param {?Error} err Request error, if any.
* @param {Zone[]} zones Array of {@link Zone} instances.
* @param {object} apiResponse The full API response.
*/
/**
* Gets a list of managed zones for the project.
*
* @see [ManagedZones: list API Documentation]{@link https://cloud.google.com/dns/api/v1/managedZones/list}
*
* @param {GetZonesRequest} [query] Query object for listing zones.
* @param {GetZonesCallback} [callback] Callback function.
* @returns {Promise<GetZonesResponse>}
*
* @example
* const {DNS} = require('@google-cloud/dns');
* const dns = new DNS();
*
* dns.getZones((err, zones, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* dns.getZones().then(data => {
* const zones = data[0];
* });
*/
getZones(query?: GetZonesRequest): Promise<GetZonesResponse>;
getZones(callback: GetZonesCallback): void;
getZones(query: GetZonesRequest, callback: GetZonesCallback): void;
getZones(
queryOrCallback?: GetZonesRequest|GetZonesCallback,
callback?: GetZonesCallback): void|Promise<GetZonesResponse> {
const query = typeof queryOrCallback === 'object' ? queryOrCallback : {};
callback =
typeof queryOrCallback === 'function' ? queryOrCallback : callback;
this.request(
{
uri: '/managedZones',
qs: query,
},
(err, resp) => {
if (err) {
callback!(err, null, null, resp);
return;
}
const zones = arrify(resp.managedZones).map(zone => {
const zoneInstance = this.zone(zone.name);
zoneInstance.metadata = zone;
return zoneInstance;
});
let nextQuery: GetZonesRequest|null = null;
if (resp.nextPageToken) {
nextQuery = extend({}, query, {
pageToken: resp.nextPageToken,
});
}
callback!(null, zones, nextQuery, resp);
});
}
/**
* Get a reference to a Zone.
*
* @param {string} name The unique name of the zone.
* @returns {Zone}
* @see Zone
*
* @throws {error} If a zone name is not provided.
*
* @example
* const {DNS} = require('@google-cloud/dns');
* const dns = new DNS();
*
* const zone = dns.zone('my-zone');
*/
zone(name: string) {
if (!name) {
throw new Error('A zone name is required.');
}
return new Zone(this, name);
}
}
/*! Developer Documentation
*
* These methods can be auto-paginated.
*/
paginator.extend(DNS, 'getZones');
/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
promisifyAll(DNS, {
exclude: ['zone'],
});
/**
* {@link Zone} class.
*
* @name DNS.Zone
* @see Zone
* @type {Constructor}
*/
export {Zone};
/**
* The default export of the `@google-cloud/dns` package is the {@link DNS}
* class.
*
* See {@link DNS} and {@link ClientConfig} for client methods and
* configuration options.
*
* @module {DNS} @google-cloud/dns
* @alias nodejs-dns
*
* @example <caption>Install the client library with <a
* href="https://www.npmjs.com/">npm</a>:</caption> npm install --save
* @google-cloud/dns
*
* @example <caption>Import the client library</caption>
* const {DNS} = require('@google-cloud/dns');
*
* @example <caption>Create a client that uses <a
* href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application">Application
* Default Credentials (ADC)</a>:</caption> const dns = new DNS();
*
* @example <caption>Create a client with <a
* href="https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually">explicit
* credentials</a>:</caption> const dns = new DNS({ projectId:
* 'your-project-id', keyFilename: '/path/to/keyfile.json'
* });
*
* @example <caption>include:samples/quickstart.js</caption>
* region_tag:dns_quickstart
* Full quickstart example:
*/
export {DNS};