UNPKG

@pulumi/esc-sdk

Version:
223 lines 15.7 kB
"use strict"; // Copyright 2024, Pulumi Corporation. // // 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. 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const node_test_1 = require("node:test"); const assert_1 = __importDefault(require("assert")); const esc = __importStar(require("../esc")); const PROJECT_NAME = "sdk-ts-test"; const ENV_PREFIX = "env"; (0, node_test_1.describe)("ESC", () => __awaiter(void 0, void 0, void 0, function* () { const PULUMI_ORG = process.env.PULUMI_ORG; if (!PULUMI_ORG) { throw new Error("PULUMI_ORG not set"); } const client = esc.DefaultClient(); const baseEnvName = `${ENV_PREFIX}-base-${Date.now()}`; (0, node_test_1.before)(() => __awaiter(void 0, void 0, void 0, function* () { const envDef = { values: { base: baseEnvName, }, }; yield removeAllTestEnvs(client, PULUMI_ORG); yield client.createEnvironment(PULUMI_ORG, PROJECT_NAME, baseEnvName); yield client.updateEnvironment(PULUMI_ORG, PROJECT_NAME, baseEnvName, envDef); })); (0, node_test_1.after)(() => __awaiter(void 0, void 0, void 0, function* () { yield removeAllTestEnvs(client, PULUMI_ORG); })); (0, node_test_1.it)("should create, clone, list, update, get, decrypt, open and delete an environment", () => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t; const name = `${ENV_PREFIX}-${Date.now()}`; yield assert_1.default.doesNotReject(client.createEnvironment(PULUMI_ORG, PROJECT_NAME, name)); const cloneProject = `${PROJECT_NAME}-clone`; const cloneName = `${name}-clone`; yield assert_1.default.doesNotReject(client.cloneEnvironment(PULUMI_ORG, PROJECT_NAME, name, cloneProject, cloneName)); yield assert_1.default.doesNotReject(client.getEnvironment(PULUMI_ORG, PROJECT_NAME, name), "created env should exist"); yield assert_1.default.doesNotReject(client.getEnvironment(PULUMI_ORG, cloneProject, cloneName), "cloned env should exist"); let openEmptyEnv = yield client.openAndReadEnvironment(PULUMI_ORG, PROJECT_NAME, name); assert_1.default.deepEqual(openEmptyEnv === null || openEmptyEnv === void 0 ? void 0 : openEmptyEnv.environment, {}); assert_1.default.deepEqual(openEmptyEnv === null || openEmptyEnv === void 0 ? void 0 : openEmptyEnv.values, {}); const envDef = { imports: [fullyQualifiedName(baseEnvName)], values: { foo: "bar", my_secret: { "fn::secret": "shh! don't tell anyone", }, my_array: [1, 2, 3], pulumiConfig: { foo: "${foo}", }, environmentVariables: { FOO: "${foo}", }, }, }; const diags = yield client.updateEnvironment(PULUMI_ORG, PROJECT_NAME, name, envDef); assert_1.default.notEqual(diags, undefined); assert_1.default.equal(diags === null || diags === void 0 ? void 0 : diags.diagnostics, undefined); let env = yield client.getEnvironment(PULUMI_ORG, PROJECT_NAME, name); assert_1.default.notEqual(env, undefined); assertEnvDef(env, baseEnvName); assert_1.default.notEqual((_b = (_a = env === null || env === void 0 ? void 0 : env.definition) === null || _a === void 0 ? void 0 : _a.values) === null || _b === void 0 ? void 0 : _b.my_secret, undefined); const decryptEnv = yield client.decryptEnvironment(PULUMI_ORG, PROJECT_NAME, name); assert_1.default.notEqual(decryptEnv, undefined); assertEnvDef(decryptEnv, baseEnvName); assert_1.default.equal((_d = (_c = decryptEnv === null || decryptEnv === void 0 ? void 0 : decryptEnv.definition) === null || _c === void 0 ? void 0 : _c.values) === null || _d === void 0 ? void 0 : _d.my_secret["fn::secret"], "shh! don't tell anyone"); let openEnv = yield client.openAndReadEnvironment(PULUMI_ORG, PROJECT_NAME, name); assert_1.default.equal((_e = openEnv === null || openEnv === void 0 ? void 0 : openEnv.values) === null || _e === void 0 ? void 0 : _e.base, baseEnvName); assert_1.default.equal((_f = openEnv === null || openEnv === void 0 ? void 0 : openEnv.values) === null || _f === void 0 ? void 0 : _f.foo, "bar"); assert_1.default.deepEqual((_g = openEnv === null || openEnv === void 0 ? void 0 : openEnv.values) === null || _g === void 0 ? void 0 : _g.my_array, [1, 2, 3]); assert_1.default.deepEqual((_h = openEnv === null || openEnv === void 0 ? void 0 : openEnv.values) === null || _h === void 0 ? void 0 : _h.my_secret, "shh! don't tell anyone"); assert_1.default.equal((_k = (_j = openEnv === null || openEnv === void 0 ? void 0 : openEnv.values) === null || _j === void 0 ? void 0 : _j.pulumiConfig) === null || _k === void 0 ? void 0 : _k.foo, "bar"); assert_1.default.equal((_m = (_l = openEnv === null || openEnv === void 0 ? void 0 : openEnv.values) === null || _l === void 0 ? void 0 : _l.environmentVariables) === null || _m === void 0 ? void 0 : _m.FOO, "bar"); const openInfo = yield client.openEnvironment(PULUMI_ORG, PROJECT_NAME, name); assert_1.default.notEqual(openInfo, undefined); const value = yield client.readOpenEnvironmentProperty(PULUMI_ORG, PROJECT_NAME, name, openInfo === null || openInfo === void 0 ? void 0 : openInfo.id, "pulumiConfig.foo"); assert_1.default.equal(value === null || value === void 0 ? void 0 : value.value, "bar"); env = yield client.getEnvironmentAtVersion(PULUMI_ORG, PROJECT_NAME, name, "2"); let values = (_o = env === null || env === void 0 ? void 0 : env.definition) === null || _o === void 0 ? void 0 : _o.values; values.versioned = "true"; yield client.updateEnvironment(PULUMI_ORG, PROJECT_NAME, name, env === null || env === void 0 ? void 0 : env.definition); const revisions = yield client.listEnvironmentRevisions(PULUMI_ORG, PROJECT_NAME, name); assert_1.default.notEqual(revisions, undefined); assert_1.default.equal(revisions === null || revisions === void 0 ? void 0 : revisions.length, 3); yield client.createEnvironmentRevisionTag(PULUMI_ORG, PROJECT_NAME, name, "testTag", 2); openEnv = yield client.openAndReadEnvironmentAtVersion(PULUMI_ORG, PROJECT_NAME, name, "testTag"); values = openEnv === null || openEnv === void 0 ? void 0 : openEnv.values; assert_1.default.equal("versioned" in values, false); const tags = yield client.listEnvironmentRevisionTags(PULUMI_ORG, PROJECT_NAME, name); assert_1.default.notEqual(tags, undefined); assert_1.default.equal((_p = tags === null || tags === void 0 ? void 0 : tags.tags) === null || _p === void 0 ? void 0 : _p.length, 2); assert_1.default.equal((_q = tags === null || tags === void 0 ? void 0 : tags.tags) === null || _q === void 0 ? void 0 : _q[0].name, "latest"); assert_1.default.equal((_r = tags === null || tags === void 0 ? void 0 : tags.tags) === null || _r === void 0 ? void 0 : _r[1].name, "testTag"); yield client.updateEnvironmentRevisionTag(PULUMI_ORG, PROJECT_NAME, name, "testTag", 3); openEnv = yield client.openAndReadEnvironmentAtVersion(PULUMI_ORG, PROJECT_NAME, name, "testTag"); assert_1.default.equal((_s = openEnv === null || openEnv === void 0 ? void 0 : openEnv.values) === null || _s === void 0 ? void 0 : _s.versioned, "true"); const testTag = yield client.getEnvironmentRevisionTag(PULUMI_ORG, PROJECT_NAME, name, "testTag"); assert_1.default.notEqual(testTag, undefined); assert_1.default.equal(testTag === null || testTag === void 0 ? void 0 : testTag.revision, 3); yield client.deleteEnvironmentRevisionTag(PULUMI_ORG, PROJECT_NAME, name, "testTag"); const tagsAfterDelete = yield client.listEnvironmentRevisionTags(PULUMI_ORG, PROJECT_NAME, name); assert_1.default.notEqual(tagsAfterDelete, undefined); assert_1.default.equal((_t = tagsAfterDelete === null || tagsAfterDelete === void 0 ? void 0 : tagsAfterDelete.tags) === null || _t === void 0 ? void 0 : _t.length, 1); yield client.createEnvironmentTag(PULUMI_ORG, PROJECT_NAME, name, "owner", "esc-sdk-test"); let envTags = yield client.listEnvironmentTags(PULUMI_ORG, PROJECT_NAME, name); assert_1.default.notEqual(envTags, undefined); assert_1.default.equal(envTags === null || envTags === void 0 ? void 0 : envTags.tags["owner"].name, "owner"); assert_1.default.equal(envTags === null || envTags === void 0 ? void 0 : envTags.tags["owner"].value, "esc-sdk-test"); yield client.updateEnvironmentTag(PULUMI_ORG, PROJECT_NAME, name, "owner", "esc-sdk-test", "new-owner", "esc-sdk-test-updated"); const envTag = yield client.getEnvironmentTag(PULUMI_ORG, PROJECT_NAME, name, "new-owner"); assert_1.default.notEqual(envTag, undefined); assert_1.default.equal(envTag === null || envTag === void 0 ? void 0 : envTag.name, "new-owner"); assert_1.default.equal(envTag === null || envTag === void 0 ? void 0 : envTag.value, "esc-sdk-test-updated"); yield client.deleteEnvironmentTag(PULUMI_ORG, PROJECT_NAME, name, "new-owner"); envTags = yield client.listEnvironmentTags(PULUMI_ORG, PROJECT_NAME, name); assert_1.default.notEqual(envTags, undefined); assert_1.default.equal(Object.keys(envTags.tags).length, 0); yield client.deleteEnvironment(PULUMI_ORG, PROJECT_NAME, name); })); (0, node_test_1.it)("check environment valid", () => __awaiter(void 0, void 0, void 0, function* () { var _u; const envDef = { values: { foo: "bar", }, }; const diags = yield client.checkEnvironment(PULUMI_ORG, envDef); assert_1.default.notEqual(diags, undefined); assert_1.default.equal((_u = diags === null || diags === void 0 ? void 0 : diags.diagnostics) === null || _u === void 0 ? void 0 : _u.length, undefined); })); (0, node_test_1.it)("check environment invalid", () => __awaiter(void 0, void 0, void 0, function* () { var _v, _w; const envDef = { values: { foo: "bar", pulumiConfig: { foo: "${bad_ref}", }, }, }; const diags = yield client.checkEnvironment(PULUMI_ORG, envDef); assert_1.default.notEqual(diags, undefined); assert_1.default.equal((_v = diags === null || diags === void 0 ? void 0 : diags.diagnostics) === null || _v === void 0 ? void 0 : _v.length, 1); assert_1.default.equal((_w = diags === null || diags === void 0 ? void 0 : diags.diagnostics) === null || _w === void 0 ? void 0 : _w[0].summary, `unknown property "bad_ref"`); })); })); function assertEnvDef(env, baseEnvName) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p; assert_1.default.equal((_b = (_a = env.definition) === null || _a === void 0 ? void 0 : _a.imports) === null || _b === void 0 ? void 0 : _b.length, 1); assert_1.default.equal((_d = (_c = env.definition) === null || _c === void 0 ? void 0 : _c.imports) === null || _d === void 0 ? void 0 : _d[0], fullyQualifiedName(baseEnvName)); assert_1.default.equal((_f = (_e = env.definition) === null || _e === void 0 ? void 0 : _e.values) === null || _f === void 0 ? void 0 : _f.foo, "bar"); assert_1.default.deepEqual((_h = (_g = env.definition) === null || _g === void 0 ? void 0 : _g.values) === null || _h === void 0 ? void 0 : _h.my_array, [1, 2, 3]); assert_1.default.equal((_l = (_k = (_j = env.definition) === null || _j === void 0 ? void 0 : _j.values) === null || _k === void 0 ? void 0 : _k.pulumiConfig) === null || _l === void 0 ? void 0 : _l.foo, "${foo}"); assert_1.default.equal((_p = (_o = (_m = env.definition) === null || _m === void 0 ? void 0 : _m.values) === null || _o === void 0 ? void 0 : _o.environmentVariables) === null || _p === void 0 ? void 0 : _p.FOO, "${foo}"); } function removeAllTestEnvs(client, orgName) { return __awaiter(this, void 0, void 0, function* () { var _a; let continuationToken = undefined; do { const orgs = yield client.listEnvironments(orgName, continuationToken); assert_1.default.notEqual(orgs, undefined); (_a = orgs === null || orgs === void 0 ? void 0 : orgs.environments) === null || _a === void 0 ? void 0 : _a.forEach((e) => __awaiter(this, void 0, void 0, function* () { if ((e.project === PROJECT_NAME || e.project === `${PROJECT_NAME}-clone`) && e.name.startsWith(ENV_PREFIX)) { yield client.deleteEnvironment(orgName, e.project, e.name); } })); continuationToken = orgs === null || orgs === void 0 ? void 0 : orgs.nextToken; } while (continuationToken !== undefined && continuationToken !== ""); }); } function fullyQualifiedName(env) { return `${PROJECT_NAME}/${env}`; } //# sourceMappingURL=client.spec.js.map