@pulumi/esc-sdk
Version:
NodeJS SDK for Pulumi ESC
671 lines • 30 kB
JavaScript
"use strict";
// Copyright 2024, Pulumi Corporation. All rights reserved.
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultClient = exports.DefaultConfiguration = exports.EscApi = exports.EscRawApi = exports.Configuration = void 0;
const index_1 = require("./raw/index");
Object.defineProperty(exports, "EscRawApi", { enumerable: true, get: function () { return index_1.EscApi; } });
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return index_1.Configuration; } });
const yaml = __importStar(require("js-yaml"));
const axios_1 = require("axios");
/**
*
* EscApi is a client for the ESC API.
* It wraps the raw API client and provides a more convenient interface.
* @export
* @class EscApi
*/
class EscApi {
constructor(config) {
// Normalize backend url
if (config.basePath) {
const url = new URL(config.basePath);
const appendedUrl = new URL(`/api/esc`, `${url.protocol}//${url.hostname}`);
config.basePath = appendedUrl.toString();
}
this.config = config;
this.rawApi = new index_1.EscApi(config);
}
/**
* listEnvironments lists the environments in an organization.
* @summary List environments
* @param {string} orgName Organization name
* @param {string} continuationToken continuation Token from previous query to fetch next page of results
* @returns {Promise<OrgEnvironments | undefined>} A list of environments
*/
listEnvironments(orgName, continuationToken) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.listEnvironments(orgName, continuationToken);
if (resp.status === 200) {
return resp.data;
}
throw new Error(`Failed to list environments: ${resp.statusText}`);
});
}
/**
* getEnvironment gets the definition of an environment.
* @summary Get environment
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @returns {Promise<EnvironmentDefinitionResponse | undefined>} The environment definition and the YAML representation
*/
getEnvironment(orgName, projectName, envName) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.getEnvironment(orgName, projectName, envName);
if (resp.status === 200) {
const doc = yaml.load(resp.data);
return {
definition: doc,
yaml: resp.data,
};
}
throw new Error(`Failed to get environment: ${resp.statusText}`);
});
}
/**
* getEnvironmentAtVersion gets the definition of an environment at a specific version.
* @summary Get environment at version
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} version Version of the environment
* @returns {Promise<EnvironmentDefinitionResponse | undefined>} The environment definition and the YAML representation
*/
getEnvironmentAtVersion(orgName, projectName, envName, version) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.getEnvironmentAtVersion(orgName, projectName, envName, version);
if (resp.status === 200) {
const doc = yaml.load(resp.data);
return {
definition: doc,
yaml: resp.data,
};
}
throw new Error(`Failed to get environment: ${resp.statusText}`);
});
}
/**
* openEnvironment opens an environment session
* @summary Open environment
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @returns {Promise<OpenEnvironment | undefined>} The open environment session information
*/
openEnvironment(orgName, projectName, envName) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.openEnvironment(orgName, projectName, envName);
if (resp.status === 200) {
return resp.data;
}
throw new Error(`Failed to open environment: ${resp.statusText}`);
});
}
/**
* openEnvironmentAtVersion opens an environment session at a specific version
* @summary Open environment at version
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} version Version of the environment
* @returns {Promise<OpenEnvironment | undefined>} The open environment session information
*/
openEnvironmentAtVersion(orgName, projectName, envName, version) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.openEnvironmentAtVersion(orgName, projectName, envName, version);
if (resp.status === 200) {
return resp.data;
}
throw new Error(`Failed to open environment: ${resp.statusText}`);
});
}
/**
* readOpenEnvironment reads the environment properties in an open session,
* resolving configuration variables and secrets.
* @summary Read environment
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} openSessionID Open session ID
* @returns {Promise<EnvironmentResponse | undefined>} The environment and its values
*/
readOpenEnvironment(orgName, projectName, envName, openSessionID) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.readOpenEnvironment(orgName, projectName, envName, openSessionID);
if (resp.status === 200) {
return {
environment: resp.data,
values: convertEnvPropertiesToValues(resp.data.properties),
};
}
throw new Error(`Failed to read environment: ${resp.statusText}`);
});
}
/**
* openAndReadEnvironment opens an environment session and reads the environment properties,
* resolving configuration variables and secrets.
* @summary Open and read environment
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @returns {Promise<EnvironmentResponse | undefined>} The environment and its values
*/
openAndReadEnvironment(orgName, projectName, envName) {
return __awaiter(this, void 0, void 0, function* () {
const open = yield this.openEnvironment(orgName, projectName, envName);
if (open === null || open === void 0 ? void 0 : open.id) {
return yield this.readOpenEnvironment(orgName, projectName, envName, open.id);
}
throw new Error(`Failed to open and read environment: ${open}`);
});
}
/**
* openAndReadEnvironmentAtVersion opens an environment session at a specific version and reads the environment properties,
* resolving configuration variables and secrets.
* @summary Open and read environment at version
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} version Version of the environment
* @returns {Promise<EnvironmentResponse | undefined>} The environment and its values
*/
openAndReadEnvironmentAtVersion(orgName, projectName, envName, version) {
return __awaiter(this, void 0, void 0, function* () {
const open = yield this.openEnvironmentAtVersion(orgName, projectName, envName, version);
if (open === null || open === void 0 ? void 0 : open.id) {
return yield this.readOpenEnvironment(orgName, projectName, envName, open.id);
}
throw new Error(`Failed to open and read environment: ${open}`);
});
}
/**
* readOpenEnvironmentProperty reads a specific environment property in an open session,
* resolving configuration variables and secrets.
* @summary Read environment property
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} openSessionID Open session ID
* @param {string} property Property name
* @returns {Promise<EnvironmentPropertyResponse | undefined>} The environment property and its value
*/
readOpenEnvironmentProperty(orgName, projectName, envName, openSessionID, property) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.readOpenEnvironmentProperty(orgName, projectName, envName, openSessionID, property);
if (resp.status === 200) {
return {
property: resp.data,
value: convertPropertyToValue(resp.data),
};
}
throw new Error(`Failed to read environment property: ${resp.statusText}`);
});
}
/**
* createEnvironment creates a new environment.
* @summary Create environment
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @returns {Promise<void>} A promise that resolves when the environment is created
*/
createEnvironment(orgName, projectName, envName) {
return __awaiter(this, void 0, void 0, function* () {
const body = {
project: projectName,
name: envName,
};
const resp = yield this.rawApi.createEnvironment(orgName, body);
if (resp.status === 204) {
return;
}
throw new Error(`Failed to create environment: ${resp.statusText}`);
});
}
/**
* cloneEnvironment clones an environment
* @summary Clone environment
* @param {string} orgName Organization name
* @param {string} cloneProjectName Clone project name
* @param {string} cloneEnvName Clone environment name
* @param {string} destProjectName Destination project name
* @param {string} destEnvName Destionation environment name
* @param {CloneEnvironmentOptions} cloneOptions Clone options
* @returns {Promise<void>} A promise that resolves when the environment is created
*/
cloneEnvironment(orgName, srcProjectName, srcEnvName, destProjectName, destEnvName, cloneOptions) {
return __awaiter(this, void 0, void 0, function* () {
const body = Object.assign({ project: destProjectName, name: destEnvName }, cloneOptions);
const resp = yield this.rawApi.cloneEnvironment(orgName, srcProjectName, srcEnvName, body);
if (resp.status === 204) {
return;
}
throw new Error(`Failed to clone environment: ${resp.statusText}`);
});
}
/**
* updateEnvironmentYaml updates the environment definition from a YAML string.
* @summary Update environment YAML
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} yaml YAML representation of the environment
* @returns {Promise<EnvironmentDiagnostics | undefined>} The environment diagnostics
*/
updateEnvironmentYaml(orgName, projectName, envName, yaml) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.updateEnvironmentYaml(orgName, projectName, envName, yaml);
if (resp.status === 200) {
return resp.data;
}
throw new Error(`Failed to update environment: ${resp.statusText}`);
});
}
/**
* updateEnvironment updates the environment definition.
* @summary Update environment
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {EnvironmentDefinition} values The environment definition
* @returns {Promise<EnvironmentDiagnostics | undefined>} The environment diagnostics
*/
updateEnvironment(orgName, projectName, envName, values) {
return __awaiter(this, void 0, void 0, function* () {
const body = yaml.dump(values);
const resp = yield this.rawApi.updateEnvironmentYaml(orgName, projectName, envName, body);
if (resp.status === 200) {
return resp.data;
}
throw new Error(`Failed to update environment: ${resp.statusText}`);
});
}
/**
* deleteEnvironment deletes an environment.
* @summary Delete environment
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @returns {Promise<void>} A promise that resolves when the environment is deleted
*/
deleteEnvironment(orgName, projectName, envName) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.deleteEnvironment(orgName, projectName, envName);
if (resp.status === 204) {
return;
}
throw new Error(`Failed to delete environment: ${resp.statusText}`);
});
}
/**
* checkEnvironmentYaml checks the environment definition from a YAML string.
* @summary Check environment YAML
* @param {string} orgName Organization name
* @param {string} yaml YAML representation of the environment
* @returns {Promise<CheckEnvironment | undefined>} The environment diagnostics
*/
checkEnvironmentYaml(orgName, yaml) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
try {
const resp = yield this.rawApi.checkEnvironmentYaml(orgName, yaml);
if (resp.status === 200) {
return resp.data;
}
throw new Error(`Failed to check environment: ${resp.statusText}`);
}
catch (err) {
if (err instanceof axios_1.AxiosError) {
if (((_a = err.response) === null || _a === void 0 ? void 0 : _a.status) === 400) {
return (_b = err.response) === null || _b === void 0 ? void 0 : _b.data;
}
}
throw err;
}
});
}
/**
* checkEnvironment checks the environment definition.
* @summary Check environment
* @param {string} orgName Organization name
* @param {EnvironmentDefinition} env The environment definition
* @returns {Promise<CheckEnvironment | undefined>} The environment diagnostics
*/
checkEnvironment(orgName, env) {
return __awaiter(this, void 0, void 0, function* () {
const body = yaml.dump(env);
return yield this.checkEnvironmentYaml(orgName, body);
});
}
/**
* decryptEnvironment decrypts the environment definition.
* @summary Decrypt environment
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @returns {Promise<EnvironmentDefinitionResponse | undefined>} The decrypted environment definition and the YAML representation
*/
decryptEnvironment(orgName, projectName, envName) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.decryptEnvironment(orgName, projectName, envName);
if (resp.status === 200) {
const doc = yaml.load(resp.data);
return {
definition: doc,
yaml: resp.data,
};
}
throw new Error(`Failed to decrypt environment: ${resp.statusText}`);
});
}
/**
* listEnvironmentRevisions lists the environment revisions, from oldest to newest.
* @summary List environment revisions
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {number} before The revision number to start listing from
* @param {number} count The number of revisions to list
* @returns {Promise<Array<EnvironmentRevision> | undefined>} A list of environment revisions
*/
listEnvironmentRevisions(orgName, projectName, envName, before, count) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.listEnvironmentRevisions(orgName, projectName, envName, before, count);
if (resp.status === 200) {
return resp.data;
}
throw new Error(`Failed to list environment revisions: ${resp.statusText}`);
});
}
/**
* listEnvironmentRevisionTags lists the environment revision tags.
* @summary List environment revision tags
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} after The tag to start listing from
* @param {number} count The number of tags to list
* @returns {Promise<EnvironmentRevisionTags | undefined>} A list of environment revision tags
*/
listEnvironmentRevisionTags(orgName, projectName, envName, after, count) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.listEnvironmentRevisionTags(orgName, projectName, envName, after, count);
if (resp.status === 200) {
return resp.data;
}
throw new Error(`Failed to list environment revision tags: ${resp.statusText}`);
});
}
/**
* getEnvironmentRevisionTag gets the environment revision tag.
* @summary Get environment revision tag
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} tag The tag name
* @returns {Promise<EnvironmentRevisionTag | undefined>} The environment revision tag
*/
getEnvironmentRevisionTag(orgName, projectName, envName, tag) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.getEnvironmentRevisionTag(orgName, projectName, envName, tag);
if (resp.status === 200) {
return resp.data;
}
throw new Error(`Failed to get environment revision tag: ${resp.statusText}`);
});
}
/**
* createEnvironmentRevisionTag creates a new environment revision tag.
* @summary Create environment revision tag
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} tag The tag name
* @param {number} revision The revision number
* @returns {Promise<void>} A promise that resolves when the tag is created
*/
createEnvironmentRevisionTag(orgName, projectName, envName, tag, revision) {
return __awaiter(this, void 0, void 0, function* () {
const createTag = {
name: tag,
revision: revision,
};
const resp = yield this.rawApi.createEnvironmentRevisionTag(orgName, projectName, envName, createTag);
if (resp.status === 204) {
return;
}
throw new Error(`Failed to create environment revision tag: ${resp.statusText}`);
});
}
/**
* updateEnvironmentRevisionTag updates the environment revision tag.
* @summary Update environment revision tag
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} tag The tag name
* @param {number} revision The revision number
* @returns {Promise<void>} A promise that resolves when the tag is updated
*/
updateEnvironmentRevisionTag(orgName, projectName, envName, tag, revision) {
return __awaiter(this, void 0, void 0, function* () {
const updateTag = {
revision: revision,
};
const resp = yield this.rawApi.updateEnvironmentRevisionTag(orgName, projectName, envName, tag, updateTag);
if (resp.status === 204) {
return;
}
throw new Error(`Failed to update environment revision tag: ${resp.statusText}`);
});
}
/**
* deleteEnvironmentRevisionTag deletes the environment revision tag.
* @summary Delete environment revision tag
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} tag The tag name
* @returns {Promise<void>} A promise that resolves when the tag is deleted
*/
deleteEnvironmentRevisionTag(orgName, projectName, envName, tag) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.deleteEnvironmentRevisionTag(orgName, projectName, envName, tag);
if (resp.status === 204) {
return;
}
throw new Error(`Failed to delete environment revision tag: ${resp.statusText}`);
});
}
/**
* listEnvironmentTags lists the environment tags.
* @summary List environment tags
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} after The tag to start listing from
* @param {number} count The number of tags to list
* @returns {Promise<ListEnvironmentTags | undefined>} A list of environment tags
*/
listEnvironmentTags(orgName, projectName, envName, after, count) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.listEnvironmentTags(orgName, projectName, envName, after, count);
if (resp.status === 200) {
return resp.data;
}
throw new Error(`Failed to list environment tags: ${resp.statusText}`);
});
}
/**
* getEnvironmentTag gets the environment tag.
* @summary Get environment tag
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} tag The tag name
* @returns {Promise<EnvironmentTag | undefined>} The environment tag
*/
getEnvironmentTag(orgName, projectName, envName, tag) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.getEnvironmentTag(orgName, projectName, envName, tag);
if (resp.status === 200) {
return resp.data;
}
throw new Error(`Failed to get environment tag: ${resp.statusText}`);
});
}
/**
* createEnvironmentTag creates a new environment tag.
* @summary Create environment tag
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} tag The tag name
* @param {string} value The tag value
* @returns {Promise<EnvironmentTag>} A promise that resolves when the tag is created
*/
createEnvironmentTag(orgName, projectName, envName, tag, value) {
return __awaiter(this, void 0, void 0, function* () {
const createTag = {
name: tag,
value: value,
};
const resp = yield this.rawApi.createEnvironmentTag(orgName, projectName, envName, createTag);
if (resp.status === 200) {
return;
}
throw new Error(`Failed to create environment tag: ${resp.statusText}`);
});
}
/**
* updateEnvironmentTag updates the environment tag.
* @summary Update environment tag
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} tag The tag name
* @param {string} current_value The tag value
* @param {string} new_tag The new tag name
* @param {string} new_value The new tag value
* @returns {Promise<EnvironmentTag>} A promise that resolves when the tag is updated
*/
updateEnvironmentTag(orgName, projectName, envName, tag, current_value, new_tag, new_value) {
return __awaiter(this, void 0, void 0, function* () {
const updateTag = {
currentTag: {
value: current_value,
},
newTag: {
name: new_tag,
value: new_value,
},
};
const resp = yield this.rawApi.updateEnvironmentTag(orgName, projectName, envName, tag, updateTag);
if (resp.status === 200) {
return;
}
throw new Error(`Failed to update environment tag: ${resp.statusText}`);
});
}
/**
* deleteEnvironmentTag deletes the environment tag.
* @summary Delete environment tag
* @param {string} orgName Organization name
* @param {string} projectName Project name
* @param {string} envName Environment name
* @param {string} tag The tag name
* @returns {Promise<void>} A promise that resolves when the tag is deleted
*/
deleteEnvironmentTag(orgName, projectName, envName, tag) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.rawApi.deleteEnvironmentTag(orgName, projectName, envName, tag);
if (resp.status === 204) {
return;
}
throw new Error(`Failed to delete environment tag: ${resp.statusText}`);
});
}
}
exports.EscApi = EscApi;
function convertEnvPropertiesToValues(env) {
if (!env) {
return {};
}
const values = {};
for (const key in env) {
const value = env[key];
values[key] = convertPropertyToValue(value);
}
return values;
}
function convertPropertyToValue(property) {
if (!property) {
return property;
}
let value = property;
if (typeof property === "object" && "value" in property) {
value = convertPropertyToValue(property.value);
}
if (!value) {
return value;
}
if (Array.isArray(value)) {
const array = value;
return array.map((v) => convertPropertyToValue(v));
}
if (typeof value === "object") {
const result = {};
for (const key in value) {
result[key] = convertPropertyToValue(value[key]);
}
return result;
}
return value;
}
function DefaultConfiguration(config) {
var _a, _b;
if (!config) {
config = new index_1.Configuration();
}
(_a = config.accessToken) !== null && _a !== void 0 ? _a : (config.accessToken = process.env.PULUMI_ACCESS_TOKEN);
(_b = config.basePath) !== null && _b !== void 0 ? _b : (config.basePath = process.env.PULUMI_BACKEND_URL);
return config;
}
exports.DefaultConfiguration = DefaultConfiguration;
function DefaultClient(config) {
return new EscApi(DefaultConfiguration(config));
}
exports.DefaultClient = DefaultClient;
//# sourceMappingURL=index.js.map