@google-cloud/spanner 6.1.4 → 6.3.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.
@@ -15,7 +15,7 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.protos = exports.v1 = exports.Transaction = exports.Snapshot = exports.PartitionedDml = exports.Table = exports.SessionPool = exports.Session = exports.Backup = exports.Database = exports.Instance = exports.Spanner = void 0;
18
+ exports.protos = exports.v1 = exports.Transaction = exports.Snapshot = exports.PartitionedDml = exports.Table = exports.SessionPool = exports.Session = exports.Backup = exports.Database = exports.InstanceConfig = exports.Instance = exports.Spanner = void 0;
19
19
  const service_1 = require("./common-grpc/service");
20
20
  const precise_date_1 = require("@google-cloud/precise-date");
21
21
  const projectify_1 = require("@google-cloud/projectify");
@@ -32,6 +32,8 @@ const database_1 = require("./database");
32
32
  Object.defineProperty(exports, "Database", { enumerable: true, get: function () { return database_1.Database; } });
33
33
  const instance_1 = require("./instance");
34
34
  Object.defineProperty(exports, "Instance", { enumerable: true, get: function () { return instance_1.Instance; } });
35
+ const instance_config_1 = require("./instance-config");
36
+ Object.defineProperty(exports, "InstanceConfig", { enumerable: true, get: function () { return instance_config_1.InstanceConfig; } });
35
37
  const google_gax_1 = require("google-gax");
36
38
  const protos_1 = require("../protos/protos");
37
39
  const common_1 = require("./common");
@@ -143,6 +145,7 @@ class Spanner extends service_1.GrpcService {
143
145
  this.auth = new google_auth_library_1.GoogleAuth(this.options);
144
146
  this.clients_ = new Map();
145
147
  this.instances_ = new Map();
148
+ this.instanceConfigs_ = new Map();
146
149
  this.projectIdReplaced_ = false;
147
150
  this.projectFormattedName_ = 'projects/' + this.projectId;
148
151
  this.resourceHeader_ = {
@@ -337,6 +340,53 @@ class Spanner extends service_1.GrpcService {
337
340
  headers: this.resourceHeader_,
338
341
  });
339
342
  }
343
+ createInstanceConfig(name, config, callback) {
344
+ if (!name) {
345
+ throw new google_gax_1.GoogleError('A name is required to create an instance config.');
346
+ }
347
+ if (!config) {
348
+ throw new google_gax_1.GoogleError([
349
+ 'A configuration object is required to create an instance config.',
350
+ ].join(''));
351
+ }
352
+ if (!config.baseConfig) {
353
+ throw new google_gax_1.GoogleError(['Base instance config is required to create an instance config.'].join(''));
354
+ }
355
+ const formattedName = instance_config_1.InstanceConfig.formatName_(this.projectId, name);
356
+ const displayName = config.displayName || formattedName.split('/').pop();
357
+ const reqOpts = {
358
+ parent: this.projectFormattedName_,
359
+ instanceConfigId: formattedName.split('/').pop(),
360
+ instanceConfig: extend({
361
+ name: formattedName,
362
+ displayName,
363
+ }, config),
364
+ validateOnly: config.validateOnly,
365
+ };
366
+ if (config.baseConfig.indexOf('/') === -1) {
367
+ reqOpts.instanceConfig.baseConfig = `projects/${this.projectId}/instanceConfigs/${config.baseConfig}`;
368
+ }
369
+ // validateOnly need not be passed in if it is null.
370
+ if (reqOpts.validateOnly === null || reqOpts.validateOnly === undefined)
371
+ delete reqOpts.validateOnly;
372
+ // validateOnly and gaxOptions are not fields in InstanceConfig.
373
+ delete reqOpts.instanceConfig.validateOnly;
374
+ delete reqOpts.instanceConfig.gaxOptions;
375
+ this.request({
376
+ client: 'InstanceAdminClient',
377
+ method: 'createInstanceConfig',
378
+ reqOpts,
379
+ gaxOpts: config.gaxOptions,
380
+ headers: this.resourceHeader_,
381
+ }, (err, operation, resp) => {
382
+ if (err) {
383
+ callback(err, null, null, resp);
384
+ return;
385
+ }
386
+ const instanceConfig = this.instanceConfig(formattedName);
387
+ callback(null, instanceConfig, operation, resp);
388
+ });
389
+ }
340
390
  getInstanceConfigs(optionsOrCallback, cb) {
341
391
  const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
342
392
  const options = typeof optionsOrCallback === 'object'
@@ -448,6 +498,39 @@ class Spanner extends service_1.GrpcService {
448
498
  callback(err, instanceConfig);
449
499
  });
450
500
  }
501
+ getInstanceConfigOperations(optionsOrCallback, cb) {
502
+ const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
503
+ const options = typeof optionsOrCallback === 'object'
504
+ ? optionsOrCallback
505
+ : {};
506
+ const gaxOpts = extend(true, {}, options.gaxOptions);
507
+ let reqOpts = extend({}, options, {
508
+ parent: this.projectFormattedName_,
509
+ });
510
+ delete reqOpts.gaxOptions;
511
+ // Copy over pageSize and pageToken values from gaxOptions.
512
+ // However, values set on options take precedence.
513
+ if (gaxOpts) {
514
+ reqOpts = extend({}, {
515
+ pageSize: gaxOpts.pageSize,
516
+ pageToken: gaxOpts.pageToken,
517
+ }, reqOpts);
518
+ delete gaxOpts.pageSize;
519
+ delete gaxOpts.pageToken;
520
+ }
521
+ this.request({
522
+ client: 'InstanceAdminClient',
523
+ method: 'listInstanceConfigOperations',
524
+ reqOpts,
525
+ gaxOpts,
526
+ headers: this.resourceHeader_,
527
+ }, (err, operations, nextPageRequest, ...args) => {
528
+ const nextQuery = nextPageRequest
529
+ ? extend({}, options, nextPageRequest)
530
+ : null;
531
+ callback(err, operations, nextQuery, ...args);
532
+ });
533
+ }
451
534
  /**
452
535
  * Get a reference to an Instance object.
453
536
  *
@@ -473,6 +556,31 @@ class Spanner extends service_1.GrpcService {
473
556
  }
474
557
  return this.instances_.get(key);
475
558
  }
559
+ /**
560
+ * Get a reference to an InstanceConfig object.
561
+ *
562
+ * @throws {GoogleError} If a name is not provided.
563
+ *
564
+ * @param {string} name The name of the instance config.
565
+ * @returns {InstanceConfig} An InstanceConfig object.
566
+ *
567
+ * @example
568
+ * ```
569
+ * const {Spanner} = require('@google-cloud/spanner');
570
+ * const spanner = new Spanner();
571
+ * const instanceConfig = spanner.instanceConfig('my-instance-config');
572
+ * ```
573
+ */
574
+ instanceConfig(name) {
575
+ if (!name) {
576
+ throw new google_gax_1.GoogleError('A name is required to access an InstanceConfig object.');
577
+ }
578
+ const key = name.split('/').pop();
579
+ if (!this.instanceConfigs_.has(key)) {
580
+ this.instanceConfigs_.set(key, new instance_config_1.InstanceConfig(this, name));
581
+ }
582
+ return this.instanceConfigs_.get(key);
583
+ }
476
584
  /**
477
585
  * Prepare a gapic request. This will cache the GAX client and replace
478
586
  * {{projectId}} placeholders, if necessary.
@@ -764,6 +872,7 @@ Spanner.GOOGLE_STANDARD_SQL = protos_1.google.spanner.admin.database.v1.Database
764
872
  'date',
765
873
  'float',
766
874
  'instance',
875
+ 'instanceConfig',
767
876
  'int',
768
877
  'numeric',
769
878
  'pgNumeric',
@@ -0,0 +1,294 @@
1
+ /**
2
+ * Copyright 2022 Google LLC
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
+ declare const common: any;
16
+ import { google as instanceAdmin } from '../protos/protos';
17
+ import { Operation as GaxOperation } from 'google-gax/build/src/longRunningCalls/longrunning';
18
+ import { LongRunningCallback, RequestCallback, ResourceCallback, NormalCallback } from './common';
19
+ import { GetInstanceConfigCallback, GetInstanceConfigOptions, GetInstanceConfigResponse, RequestConfig, Spanner } from './index';
20
+ import { CallOptions } from 'google-gax';
21
+ export declare type IOperation = instanceAdmin.longrunning.IOperation;
22
+ export declare type CreateInstanceConfigCallback = LongRunningCallback<InstanceConfig>;
23
+ export declare type SetInstanceConfigMetadataCallback = ResourceCallback<GaxOperation, IOperation>;
24
+ export declare type DeleteInstanceConfigCallback = NormalCallback<instanceAdmin.protobuf.IEmpty>;
25
+ export declare type ExistsInstanceConfigCallback = NormalCallback<boolean>;
26
+ export declare type CreateInstanceConfigResponse = [
27
+ InstanceConfig,
28
+ GaxOperation,
29
+ IOperation
30
+ ];
31
+ export declare type SetInstanceConfigMetadataResponse = [GaxOperation, IOperation];
32
+ export declare type DeleteInstanceConfigResponse = [instanceAdmin.protobuf.IEmpty];
33
+ export declare type ExistsInstanceConfigResponse = boolean;
34
+ export declare type IInstanceConfig = instanceAdmin.spanner.admin.instance.v1.IInstanceConfig;
35
+ interface InstanceConfigRequest {
36
+ (config: RequestConfig, callback: ResourceCallback<GaxOperation, IOperation>): void;
37
+ <T>(config: RequestConfig, callback: RequestCallback<T>): void;
38
+ <T, R>(config: RequestConfig, callback: RequestCallback<T, R>): void;
39
+ }
40
+ export interface SetInstanceConfigMetadataRequest {
41
+ instanceConfig: IInstanceConfig;
42
+ validateOnly?: boolean;
43
+ gaxOpts?: CallOptions;
44
+ }
45
+ interface DeleteInstanceConfigRequest {
46
+ etag?: string;
47
+ validateOnly?: boolean;
48
+ gaxOpts?: CallOptions;
49
+ }
50
+ /**
51
+ * The {@link InstanceConfig} class represents a possible configuration for a
52
+ * Cloud Spanner instance.
53
+ *
54
+ * Create an `InstanceConfig` object to interact with a Cloud Spanner instance
55
+ * config.
56
+ *
57
+ * @class
58
+ *
59
+ * @param {Spanner} spanner {@link Spanner} instance.
60
+ * @param {string} name Name of the instance config.
61
+ *
62
+ * @example
63
+ * ```
64
+ * const {Spanner} = require('@google-cloud/spanner');
65
+ * const spanner = new Spanner();
66
+ * const instance = spanner.instanceConfig('my-instance-config');
67
+ * ```
68
+ */
69
+ declare class InstanceConfig extends common.GrpcServiceObject {
70
+ formattedName_: string;
71
+ request: InstanceConfigRequest;
72
+ metadata?: IInstanceConfig;
73
+ resourceHeader_: {
74
+ [k: string]: string;
75
+ };
76
+ constructor(spanner: Spanner, name: string);
77
+ /**
78
+ * Gets the metadata from the backend for this InstanceConfig.
79
+ */
80
+ /**
81
+ * @typedef {array} GetInstanceConfigResponse
82
+ * @property {object[]} 0 The metadata of the instance config.
83
+ * @property {string} 0.name The unique identifier for the instance config.
84
+ * @property {string} 0.displayName The name of the instance config as it
85
+ * appears in UIs.
86
+ * @property {google.spanner.admin.instance.v1.IReplicaInfo[]} 0.replicas The replicas used by
87
+ * this instance config.
88
+ * @property {string[]} 0.leaderOptions The possible leader options for this instance config.
89
+ */
90
+ /**
91
+ * @callback GetInstanceConfigCallback
92
+ * @param {?Error} err Request error, if any.
93
+ * @param {object} instanceConfig The metadata of the instance config.
94
+ * @param {string} instanceConfig.name The unique identifier for the instance
95
+ * config.
96
+ * @param {string} instanceConfig.displayName The name of the instance config
97
+ * as it appears in UIs.
98
+ * @param {google.spanner.admin.instance.v1.IReplicaInfo[]} instanceConfig.replicas The replicas used by
99
+ * this instance config.
100
+ * @param {string[]} 0.leaderOptions The possible leader options for this instance config.
101
+ */
102
+ /**
103
+ * Gets the metadata from the backend for this InstanceConfig.
104
+ *
105
+ * Wrapper around {@link v1.InstanceAdminClient#getInstanceConfig}.
106
+ *
107
+ * @see {@link v1.InstanceAdminClient#getInstanceConfig}
108
+ * @see [GetInstanceConfig API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.GetInstanceConfig)
109
+ *
110
+ * @param {GetInstanceConfigCallback} [callback] Callback function.
111
+ * @returns {Promise<GetInstanceConfigResponse>}
112
+ *
113
+ * @example
114
+ * ```
115
+ * const {Spanner} = require('@google-cloud/spanner');
116
+ * const spanner = new Spanner();
117
+ *
118
+ * const instanceConfig = spanner.instanceConfig('custom-my-instance-config');
119
+ *
120
+ * instanceConfig.get(function(err, instanceConfig) {
121
+ * // `instanceConfig` is an instance configuration descriptor.
122
+ * });
123
+ *
124
+ * //-
125
+ * // If the callback is omitted, we'll return a Promise.
126
+ * //-
127
+ * instanceConfig.get().then(function(data) {
128
+ * const instanceConfig = data[0];
129
+ * });
130
+ * ```
131
+ */
132
+ get(options?: GetInstanceConfigOptions): Promise<GetInstanceConfigResponse>;
133
+ get(callback: GetInstanceConfigCallback): void;
134
+ get(options: GetInstanceConfigOptions, callback: GetInstanceConfigCallback): void;
135
+ /**
136
+ * Update the metadata for this instance config. Note that this method follows
137
+ * PATCH semantics, so previously-configured settings will persist.
138
+ * This function can only be called for deleting user managed instance
139
+ * configs.
140
+ *
141
+ * Wrapper around {@link v1.InstanceAdminClient#updateInstanceConfig}.
142
+ *
143
+ * @see {@link v1.InstanceAdminClient#updateInstanceConfig}
144
+ * @see [UpdateInstanceConfig API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.UpdateInstanceConfig)
145
+ *
146
+ * @param {object} metadata The metadata you wish to set.
147
+ * @param {object} [gaxOptions] Request configuration options,
148
+ * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
149
+ * for more details.
150
+ * @param {SetInstanceConfigMetadataCallback} [callback] Callback function.
151
+ * @returns {Promise<LongRunningOperationResponse>}
152
+ *
153
+ * @example
154
+ * ```
155
+ * const {Spanner} = require('@google-cloud/spanner');
156
+ * const spanner = new Spanner();
157
+ *
158
+ * const instanceConfig = spanner.instanceConfig('custom-my-instance-config');
159
+ *
160
+ * const metadata = {
161
+ * instanceConfig: {
162
+ * displayName: 'My Instance Config'
163
+ * }
164
+ * };
165
+ *
166
+ * instanceConfig.setMetadata(metadata, function(err, operation, apiResponse) {
167
+ * if (err) {
168
+ * // Error handling omitted.
169
+ * }
170
+ *
171
+ * operation
172
+ * .on('error', function(err) {})
173
+ * .on('complete', function() {
174
+ * // Metadata updated successfully.
175
+ * });
176
+ * });
177
+ *
178
+ * //-
179
+ * // If the callback is omitted, we'll return a Promise.
180
+ * //-
181
+ * instanceConfig.setMetadata(metadata).then(function(data) {
182
+ * const operation = data[0];
183
+ * const apiResponse = data[1];
184
+ * });
185
+ * ```
186
+ */
187
+ setMetadata(config: SetInstanceConfigMetadataRequest): Promise<SetInstanceConfigMetadataResponse>;
188
+ setMetadata(config: SetInstanceConfigMetadataRequest, callback: SetInstanceConfigMetadataCallback): void;
189
+ /**
190
+ * @typedef {array} DeleteInstanceConfigResponse
191
+ * @property {object} 0 The full API response.
192
+ */
193
+ /**
194
+ * @callback DeleteInstanceConfigCallback
195
+ * @param {?Error} err Request error, if any.
196
+ * @param {object} apiResponse The full API response.
197
+ */
198
+ /**
199
+ * Delete the instance config. This function can only be called for deleting
200
+ * user managed instance configs.
201
+ *
202
+ * Wrapper around {@link v1.InstanceAdminClient#deleteInstanceConfig}.
203
+ *
204
+ * @see {@link v1.InstanceAdminClient#deleteInstanceConfig}
205
+ * @see [DeleteInstanceConfig API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.DeleteInstanceConfig)
206
+ *
207
+ * @param {object} [gaxOptions] Request configuration options,
208
+ * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
209
+ * for more details.
210
+ * @param {DeleteInstanceConfigCallback} [callback] Callback function.
211
+ * @returns {Promise<DeleteInstanceConfigResponse>}
212
+ *
213
+ * @example
214
+ * ```
215
+ * const {Spanner} = require('@google-cloud/spanner');
216
+ * const spanner = new Spanner();
217
+ *
218
+ * const instanceConfig = spanner.instanceConfig('custom-my-instance-config');
219
+ *
220
+ * instanceConfig.delete(function(err, apiResponse) {
221
+ * if (err) {
222
+ * // Error handling omitted.
223
+ * }
224
+ *
225
+ * // Instance config was deleted successfully.
226
+ * });
227
+ *
228
+ * //-
229
+ * // If the callback is omitted, we'll return a Promise.
230
+ * //-
231
+ * instanceConfig.delete().then(function(data) {
232
+ * const apiResponse = data[0];
233
+ * });
234
+ * ```
235
+ */
236
+ delete(config?: DeleteInstanceConfigRequest): Promise<DeleteInstanceConfigResponse>;
237
+ delete(callback: DeleteInstanceConfigCallback): void;
238
+ delete(config: DeleteInstanceConfigRequest, callback: DeleteInstanceConfigCallback): void;
239
+ /**
240
+ * @typedef {array} ExistsInstanceConfigResponse
241
+ * @property {boolean} 0 Whether the {@link InstanceConfig} exists.
242
+ */
243
+ /**
244
+ * @callback ExistsInstanceConfigCallback
245
+ * @param {?Error} err Request error, if any.
246
+ * @param {boolean} exists Whether the {@link InstanceConfig} exists.
247
+ */
248
+ /**
249
+ * Check if an instance config exists.
250
+ *
251
+ * @method InstanceConfig#exists
252
+ * @param {object} [gaxOptions] Request configuration options,
253
+ * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
254
+ * for more details.
255
+ * @param {ExistsInstanceConfigCallback} [callback] Callback function.
256
+ * @returns {Promise<ExistsInstanceConfigResponse>} When resolved, contains
257
+ * true if the instance config exists and false if it does not exist.
258
+ *
259
+ * @example
260
+ * ```
261
+ * const {Spanner} = require('@google-cloud/spanner');
262
+ * const spanner = new Spanner();
263
+ *
264
+ * const instanceConfig = spanner.instanceConfig('custom-my-instance-config');
265
+ *
266
+ * const exists = await instanceConfig.exists();
267
+ * console.log(`Does instance config exist? ${exists}`);
268
+ * ```
269
+ */
270
+ exists(): Promise<ExistsInstanceConfigResponse>;
271
+ exists(callback: ExistsInstanceConfigCallback): void;
272
+ /**
273
+ * Format the instance config name to include the project ID.
274
+ *
275
+ * @private
276
+ *
277
+ * @param {string} projectId The project ID.
278
+ * @param {string} name The instance config name.
279
+ * @returns {string}
280
+ *
281
+ * @example
282
+ * ```
283
+ * InstanceConfig.formatName_('grape-spaceship-123', 'my-instance-config');
284
+ * // 'projects/grape-spaceship-123/instanceConfigs/my-instance-config'
285
+ * ```
286
+ */
287
+ static formatName_(projectId: string, name: string): string;
288
+ }
289
+ /**
290
+ * Reference to the {@link InstanceConfig} class.
291
+ * @name module:@google-cloud/spanner.InstanceConfig
292
+ * @see InstanceConfig
293
+ */
294
+ export { InstanceConfig };
@@ -0,0 +1,216 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright 2022 Google LLC
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.InstanceConfig = void 0;
18
+ const common = require('./common-grpc/service-object');
19
+ const snakeCase = require("lodash.snakecase");
20
+ const common_1 = require("./common");
21
+ const promisify_1 = require("@google-cloud/promisify");
22
+ const google_gax_1 = require("google-gax");
23
+ const extend = require("extend");
24
+ /**
25
+ * The {@link InstanceConfig} class represents a possible configuration for a
26
+ * Cloud Spanner instance.
27
+ *
28
+ * Create an `InstanceConfig` object to interact with a Cloud Spanner instance
29
+ * config.
30
+ *
31
+ * @class
32
+ *
33
+ * @param {Spanner} spanner {@link Spanner} instance.
34
+ * @param {string} name Name of the instance config.
35
+ *
36
+ * @example
37
+ * ```
38
+ * const {Spanner} = require('@google-cloud/spanner');
39
+ * const spanner = new Spanner();
40
+ * const instance = spanner.instanceConfig('my-instance-config');
41
+ * ```
42
+ */
43
+ class InstanceConfig extends common.GrpcServiceObject {
44
+ constructor(spanner, name) {
45
+ const formattedName_ = InstanceConfig.formatName_(spanner.projectId, name);
46
+ const methods = {
47
+ /**
48
+ * Create an instance config.
49
+ *
50
+ * Wrapper around {@link v1.InstanceAdminClient#createInstanceConfig}.
51
+ *
52
+ * @see {@link v1.InstanceAdminClient#createInstanceConfig}
53
+ * @see [CreateInstanceConfig API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.CreateInstanceConfig)
54
+ *
55
+ * @method InstanceConfig#create
56
+ * @param {CreateInstanceConfigRequest} config Configuration object.
57
+ * @param {CreateInstanceConfigCallback} [callback] Callback function.
58
+ * @returns {Promise<CreateInstanceConfigResponse>}
59
+ *
60
+ * @example
61
+ * ```
62
+ * const {Spanner} = require('@google-cloud/spanner');
63
+ * const spanner = new Spanner();
64
+ *
65
+ * const instanceConfig =
66
+ * spanner.instanceConfig('custom-my-instance-config');
67
+ *
68
+ * instanceConfig.create(function(err, instance, operation, apiResponse) {
69
+ * if (err) {
70
+ * // Error handling omitted.
71
+ * }
72
+ *
73
+ * operation
74
+ * .on('error', function(err) {})
75
+ * .on('complete', function() {
76
+ * // Instance config created successfully.
77
+ * });
78
+ * });
79
+ *
80
+ * //-
81
+ * // If the callback is omitted, we'll return a Promise.
82
+ * //-
83
+ * instanceConfig.create()
84
+ * .then(function(data) {
85
+ * const operation = data[0];
86
+ * const apiResponse = data[1];
87
+ *
88
+ * return operation.promise();
89
+ * })
90
+ * .then(function() {
91
+ * // Instance config created successfully.
92
+ * });
93
+ * ```
94
+ */
95
+ create: true,
96
+ };
97
+ super({
98
+ parent: spanner,
99
+ id: name,
100
+ methods,
101
+ createMethod(_, options, callback) {
102
+ spanner.createInstanceConfig(formattedName_, options, callback);
103
+ },
104
+ });
105
+ this.formattedName_ = formattedName_;
106
+ this.request = spanner.request.bind(spanner);
107
+ this.resourceHeader_ = {
108
+ [common_1.CLOUD_RESOURCE_HEADER]: this.formattedName_,
109
+ };
110
+ }
111
+ get(optionsOrCallback, cb) {
112
+ const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
113
+ const options = typeof optionsOrCallback === 'object'
114
+ ? optionsOrCallback
115
+ : {};
116
+ this.parent.getInstanceConfig(this.id, options, callback);
117
+ }
118
+ setMetadata(config, callback) {
119
+ const reqOpts = {
120
+ instanceConfig: extend({
121
+ name: this.formattedName_,
122
+ }, config.instanceConfig),
123
+ updateMask: {
124
+ paths: Object.keys(config.instanceConfig).map(snakeCase),
125
+ },
126
+ validateOnly: config.validateOnly,
127
+ };
128
+ // validateOnly need not be passed in if it is null or undefined.
129
+ if (reqOpts.validateOnly === null || reqOpts.validateOnly === undefined)
130
+ delete reqOpts.validateOnly;
131
+ return this.request({
132
+ client: 'InstanceAdminClient',
133
+ method: 'updateInstanceConfig',
134
+ reqOpts,
135
+ gaxOpts: config.gaxOpts === null || config.gaxOpts === undefined
136
+ ? {}
137
+ : config.gaxOpts,
138
+ headers: this.resourceHeader_,
139
+ }, callback);
140
+ }
141
+ delete(optionsOrCallback, cb) {
142
+ const config = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
143
+ const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
144
+ const reqOpts = {
145
+ name: this.formattedName_,
146
+ etag: config.etag,
147
+ validateOnly: config.validateOnly,
148
+ };
149
+ // etag/validateOnly need not be passed in if null or undefined.
150
+ if (reqOpts.etag === null || reqOpts.etag === undefined)
151
+ delete reqOpts.etag;
152
+ if (reqOpts.validateOnly === null || reqOpts.etag === undefined)
153
+ delete reqOpts.validateOnly;
154
+ this.request({
155
+ client: 'InstanceAdminClient',
156
+ method: 'deleteInstanceConfig',
157
+ reqOpts,
158
+ gaxOpts: config.gaxOpts === null || config.gaxOpts === undefined
159
+ ? {}
160
+ : config.gaxOpts,
161
+ headers: this.resourceHeader_,
162
+ }, (err, resp) => {
163
+ if (!err) {
164
+ this.parent.instanceConfigs_.delete(this.id);
165
+ }
166
+ callback(err, resp);
167
+ });
168
+ }
169
+ async exists() {
170
+ try {
171
+ // Attempt to read metadata to determine whether instance config exists
172
+ await this.get();
173
+ // Found, therefore it exists
174
+ return true;
175
+ }
176
+ catch (err) {
177
+ if (err.code === google_gax_1.grpc.status.NOT_FOUND) {
178
+ return false;
179
+ }
180
+ // Some other error occurred, rethrow
181
+ throw err;
182
+ }
183
+ }
184
+ /**
185
+ * Format the instance config name to include the project ID.
186
+ *
187
+ * @private
188
+ *
189
+ * @param {string} projectId The project ID.
190
+ * @param {string} name The instance config name.
191
+ * @returns {string}
192
+ *
193
+ * @example
194
+ * ```
195
+ * InstanceConfig.formatName_('grape-spaceship-123', 'my-instance-config');
196
+ * // 'projects/grape-spaceship-123/instanceConfigs/my-instance-config'
197
+ * ```
198
+ */
199
+ static formatName_(projectId, name) {
200
+ if (name.indexOf('/') > -1) {
201
+ return name;
202
+ }
203
+ const instanceConfigName = name.split('/').pop();
204
+ return 'projects/' + projectId + '/instanceConfigs/' + instanceConfigName;
205
+ }
206
+ }
207
+ exports.InstanceConfig = InstanceConfig;
208
+ /*! Developer Documentation
209
+ *
210
+ * All async methods (except for streams) will return a Promise in the event
211
+ * that a callback is omitted.
212
+ */
213
+ (0, promisify_1.promisifyAll)(InstanceConfig, {
214
+ exclude: ['exists'],
215
+ });
216
+ //# sourceMappingURL=instance-config.js.map