@testcontainers/gcloud
Version:
GCloud module for Testcontainers
126 lines • 4.83 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpannerEmulatorHelper = void 0;
/**
* Helper class that encapsulates all Spanner client interactions against the emulator.
* Clients and configs are lazily instantiated.
*/
class SpannerEmulatorHelper {
emulator;
clientInstance;
instanceAdminClientInstance;
databaseAdminClientInstance;
instanceConfigValue;
constructor(emulator) {
this.emulator = emulator;
}
/**
* Lazily get or create the Spanner client.
*/
async client() {
const { Spanner } = await Promise.resolve().then(() => __importStar(require("@google-cloud/spanner")));
this.clientInstance ??= new Spanner({
projectId: this.emulator.getProjectId(),
apiEndpoint: this.emulator.getHost(),
port: this.emulator.getGrpcPort(),
sslCreds: this.emulator.getSslCredentials(),
// Provide fake credentials so the auth library never tries metadata
credentials: {
client_email: "test@example.com",
private_key: "not-a-real-key",
},
});
return this.clientInstance;
}
/**
* Lazily get or create the InstanceAdminClient.
*/
async instanceAdminClient() {
this.instanceAdminClientInstance ??= (await this.client()).getInstanceAdminClient();
return this.instanceAdminClientInstance;
}
/**
* Lazily get or create the DatabaseAdminClient.
*/
async databaseAdminClient() {
this.databaseAdminClientInstance ??= (await this.client()).getDatabaseAdminClient();
return this.databaseAdminClientInstance;
}
/**
* Lazily compute the instanceConfig path.
*/
async instanceConfig() {
this.instanceConfigValue ??= (await this.instanceAdminClient()).instanceConfigPath(this.emulator.getProjectId(), "emulator-config");
return this.instanceConfigValue;
}
/**
* Creates a new Spanner instance in the emulator.
*/
async createInstance(instanceId, options) {
const [operation] = await (await this.instanceAdminClient()).createInstance({
instanceId,
parent: (await this.instanceAdminClient()).projectPath(this.emulator.getProjectId()),
instance: options,
});
const [result] = await operation.promise();
return result;
}
/**
* Deletes an existing Spanner instance in the emulator.
*/
async deleteInstance(instanceId) {
await (await this.client()).instance(instanceId).delete();
}
/**
* Creates a new database under the specified instance in the emulator.
*/
async createDatabase(instanceId, databaseId) {
const [operation] = await (await this.databaseAdminClient()).createDatabase({
parent: (await this.databaseAdminClient()).instancePath(this.emulator.getProjectId(), instanceId),
createStatement: `CREATE DATABASE \`${databaseId}\``,
});
const [result] = await operation.promise();
return result;
}
/**
* Deletes a database under the specified instance in the emulator.
*/
async deleteDatabase(instanceId, databaseId) {
await (await this.client()).instance(instanceId).database(databaseId).delete();
}
}
exports.SpannerEmulatorHelper = SpannerEmulatorHelper;
//# sourceMappingURL=spanner-emulator-helper.js.map