UNPKG

@google-cloud/bigtable

Version:
357 lines 12.9 kB
"use strict"; // Copyright 2018 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. Object.defineProperty(exports, "__esModule", { value: true }); exports.AppProfile = void 0; const promisify_1 = require("@google-cloud/promisify"); const is = require("is"); const snakeCase = require("lodash.snakecase"); const cluster_1 = require("./cluster"); /** * Create an app profile object to interact with your app profile. * * @class * @param {Instance} instance The parent instance of this app profile. * @param {string} name Name of the app profile. * * @example * ``` * const {Bigtable} = require('@google-cloud/bigtable'); * const bigtable = new Bigtable(); * const instance = bigtable.instance('my-instance'); * const appProfile = instance.appProfile('my-app-profile'); * ``` */ class AppProfile { bigtable; instance; name; id; metadata; constructor(instance, id) { this.bigtable = instance.bigtable; this.instance = instance; let name; if (id.includes('/')) { if (id.startsWith(`${instance.name}/appProfiles/`)) { name = id; } else { throw new Error(`AppProfile id '${id}' is not formatted correctly. Please use the format 'my-app-profile' or '${instance.name}/appProfiles/my-app-profile'.`); } } else { name = `${instance.name}/appProfiles/${id}`; } this.id = name.split('/').pop(); this.name = name; } /** * Formats a app profile options object into proto format. * * @private * * @param {object} options The options object. * @returns {object} * * @example * ``` * // Any cluster routing: * Family.formatAppProfile_({ * routing: 'any', * description: 'My App Profile', * }); * // { * // multiClusterRoutingUseAny: {}, * // description: 'My App Profile', * // } * * // Single cluster routing: * const cluster = myInstance.cluster('my-cluster'); * Family.formatAppProfile_({ * routing: cluster, * allowTransactionalWrites: true, * description: 'My App Profile', * }); * // { * // singleClusterRouting: { * // clusterId: 'my-cluster', * // allowTransactionalWrites: true, * // }, * // description: 'My App Profile', * // } * ``` */ static formatAppProfile_(options) { const appProfile = {}; const errMessage = 'An app profile routing policy can only contain "any" for multi cluster routing, a `Cluster` for single routing, or a set of clusterIds as strings or `Clusters` for multi cluster routing.'; if (options.routing) { if (options.routing === 'any') { appProfile.multiClusterRoutingUseAny = {}; } else if (options.routing instanceof Set) { const routingAsArray = [...options.routing]; if (isClusterArray(routingAsArray)) { // Runs if routing is a set and every element in it is a cluster appProfile.multiClusterRoutingUseAny = { clusterIds: routingAsArray.map(cluster => cluster.id), }; } else if (isStringArray(routingAsArray)) { // Runs if routing is a set and every element in it is a string appProfile.multiClusterRoutingUseAny = { clusterIds: routingAsArray, }; } else { throw new Error(errMessage); } } else if (options.routing instanceof cluster_1.Cluster) { appProfile.singleClusterRouting = { clusterId: options.routing.id, }; if (is.boolean(options.allowTransactionalWrites)) { appProfile.singleClusterRouting.allowTransactionalWrites = options.allowTransactionalWrites; } } else { throw new Error(errMessage); } } if (is.string(options.description)) { appProfile.description = options.description; } return appProfile; } /** * Create an app profile. * * @param {object} [options] See {@link Instance#createAppProfile}. * * @example * ``` * <caption>include:samples/api-reference-doc-snippets/app-profile.js</caption> * region_tag:bigtable_api_create_app_profile * ``` */ create(optionsOrCallback, cb) { const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb; const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {}; this.instance.createAppProfile(this.id, options, callback); } /** * Delete the app profile. * * @param {object} [options] Cluster creation options. * @param {object} [options.gaxOptions] Request configuration options, outlined * here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions. * @param {boolean} [options.ignoreWarnings] Whether to ignore safety checks * when deleting the app profile. * @param {function} [callback] The callback function. * @param {?error} callback.err An error returned while making this * request. * @param {object} callback.apiResponse The full API response. * * @example * ``` * <caption>include:samples/api-reference-doc-snippets/app-profile.js</caption> * region_tag:bigtable_api_delete_app_profile * ``` */ delete(optionsOrCallback, cb) { const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb; const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {}; const reqOpts = { name: this.name, }; if (is.boolean(options.ignoreWarnings)) { reqOpts.ignoreWarnings = options.ignoreWarnings; } this.bigtable.request({ client: 'BigtableInstanceAdminClient', method: 'deleteAppProfile', reqOpts, gaxOpts: options.gaxOptions, }, callback); } /** * Check if an app profile exists. * * @param {object} [gaxOptions] Request configuration options, outlined here: * https://googleapis.github.io/gax-nodejs/CallSettings.html. * @param {function} callback The callback function. * @param {?error} callback.err An error returned while making this * request. * @param {boolean} callback.exists Whether the app profile exists or not. * * @example * ``` * <caption>include:samples/api-reference-doc-snippets/app-profile.js</caption> * region_tag:bigtable_api_exists_app_profile * ``` */ exists(optionsOrCallback, cb) { const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb; const gaxOptions = typeof optionsOrCallback === 'object' ? optionsOrCallback : {}; this.getMetadata(gaxOptions, err => { if (err) { if (err.code === 5) { callback(null, false); return; } callback(err); return; } callback(null, true); }); } /** * Get a appProfile if it exists. * * @param {object} [gaxOptions] Request configuration options, outlined here: * https://googleapis.github.io/gax-nodejs/CallSettings.html. * * @example * ``` * <caption>include:samples/api-reference-doc-snippets/app-profile.js</caption> * region_tag:bigtable_api_get_app_profile * ``` */ get(optionsOrCallback, cb) { const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb; const gaxOptions = typeof optionsOrCallback === 'object' ? optionsOrCallback : {}; this.getMetadata(gaxOptions, (err, metadata) => { if (err) { callback(err, undefined, metadata); } else { callback(null, this, metadata); } }); } /** * Get the app profile metadata. * * @param {object} [gaxOptions] Request configuration options, outlined * here: https://googleapis.github.io/gax-nodejs/CallSettings.html. * @param {function} callback The callback function. * @param {?error} callback.err An error returned while making this * request. * @param {object} callback.metadata The metadata. * @param {object} callback.apiResponse The full API response. * * @example * ``` * <caption>include:samples/api-reference-doc-snippets/app-profile.js</caption> * region_tag:bigtable_api_app_profile_get_meta * ``` */ getMetadata(optionsOrCallback, cb) { const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb; const gaxOptions = typeof optionsOrCallback === 'object' ? optionsOrCallback : {}; this.bigtable.request({ client: 'BigtableInstanceAdminClient', method: 'getAppProfile', reqOpts: { name: this.name, }, gaxOpts: gaxOptions, }, (err, resp) => { if (resp) { this.metadata = resp; } callback(err, resp, resp); }); } /** * Set the app profile metadata. * * @param {object} metadata See {@link Instance#createAppProfile} for the * available metadata options. * @param {object} [gaxOptions] Request configuration options, outlined here: * https://googleapis.github.io/gax-nodejs/CallSettings.html. * @param {function} callback The callback function. * @param {?error} callback.err An error returned while making this request. * @param {object} callback.apiResponse The full API response. * * @example * ``` * <caption>include:samples/api-reference-doc-snippets/app-profile.js</caption> * region_tag:bigtable_api_app_profile_set_meta * ``` */ setMetadata(metadata, optionsOrCallback, cb) { const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb; const gaxOptions = typeof optionsOrCallback === 'object' ? optionsOrCallback : {}; const reqOpts = { appProfile: AppProfile.formatAppProfile_(metadata), updateMask: { paths: [], }, }; reqOpts.appProfile.name = this.name; const fieldsForMask = [ 'description', 'singleClusterRouting', 'multiClusterRoutingUseAny', 'allowTransactionalWrites', ]; fieldsForMask.forEach(field => { if (reqOpts.appProfile[field]) { reqOpts.updateMask.paths.push(snakeCase(field)); } }); if (is.boolean(metadata.ignoreWarnings)) { reqOpts.ignoreWarnings = metadata.ignoreWarnings; } this.bigtable.request({ client: 'BigtableInstanceAdminClient', method: 'updateAppProfile', reqOpts, gaxOpts: gaxOptions, }, callback); } } exports.AppProfile = AppProfile; function isStringArray(array) { return array.every((cluster) => { return typeof cluster === 'string'; }); } function isClusterArray(array) { return array.every((cluster) => { return isCluster(cluster); }); } function isCluster(cluster) { return (cluster.bigtable !== undefined && cluster.instance !== undefined && cluster.id !== undefined && cluster.name !== undefined); } /*! Developer Documentation * * All async methods (except for streams) will return a Promise in the event * that a callback is omitted. */ (0, promisify_1.promisifyAll)(AppProfile); /** * Reference to the {@link AppProfile} class. * @name module:@google-cloud/bigtable.AppProfile * @see AppProfile */ //# sourceMappingURL=app-profile.js.map