UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

166 lines 7.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Uninstall = exports.Install = exports.Create = exports.LocationCmd = void 0; const command_1 = require("../cli/command"); const query_1 = require("./query"); const generic_1 = require("./generic"); const resolver_1 = require("./resolver"); const options_1 = require("./options"); const functions_1 = require("../util/functions"); const links_1 = require("../util/links"); /*** Commands ***/ class LocationCmd extends command_1.Command { constructor() { super(...arguments); this.command = 'location'; this.aliases = ['loc']; this.describe = 'Manage locations'; } builder(yargs) { const resolver = (0, resolver_1.kindResolver)('location'); const schema = { props: [...query_1.defaultProps, 'origin', 'provider', 'region'], }; const opts = [options_1.withOrgOptions, options_1.withStandardOptions]; const commandName = 'location'; const commandNamePlural = 'locations'; const commandNameA = 'a location'; return (yargs .demandCommand() .version(false) .help() // generic .command(new generic_1.Delete(commandNamePlural, resolver, ...opts).toYargs()) .command(new generic_1.Get(commandNamePlural, resolver, ...opts).toYargs()) .command(new generic_1.Query(commandNamePlural, resolver, schema, ...opts).toYargs()) .command(new generic_1.Tag(commandNamePlural, resolver, ...opts).toYargs()) .command(new generic_1.Edit(commandName, resolver, ...opts).toYargs()) .command(new generic_1.Patch(commandNamePlural, resolver, ...opts).toYargs()) .command(new generic_1.ListPermissions(commandNameA, resolver, ...opts).toYargs()) .command(new generic_1.ViewAccessReport(commandName, resolver, ...opts).toYargs()) // specific .command(new Create(resolver).toYargs()) .command(new Install(resolver).toYargs()) .command(new Uninstall(resolver).toYargs())); } handle() { } } exports.LocationCmd = LocationCmd; class Create extends command_1.Command { constructor(resolve) { super(); this.resolve = resolve; this.command = 'create'; this.describe = 'Create a new BYOK location'; } builder(yargs) { return (0, functions_1.pipe)( // (yargs) => { return yargs.options({ name: { describe: 'Name of the new BYOK location', requiresArg: true, demandOption: true, }, description: { alias: 'desc', describe: 'Optional description, defaults to the name if not set', }, }); }, generic_1.withTagOptions, options_1.withOrgOptions, options_1.withStandardOptions)(yargs); } async handle(args) { let parentLink = this.resolve.parentLink(this.session.context); const location = { name: args.name, description: args.description, tags: (0, generic_1.fromTagOptions)(args), provider: 'byok', }; const body = await this.client.create(parentLink, location); this.session.outFormat(body); } } exports.Create = Create; class Install extends command_1.Command { constructor(resolve) { super(); this.resolve = resolve; this.command = 'install <ref>'; this.describe = 'Get instructions for obtaining the installation script for a BYOK location'; } builder(yargs) { return (0, functions_1.pipe)( // generic_1.withSingleRef, options_1.withOrgOptions, options_1.withStandardOptions)(yargs); } async handle(args) { // Fetch the location const selfLink = this.resolve.resourceLink(args.ref, this.session.context); const location = await this.client.get(selfLink); // Validate that the location's provider is set to BYOK if (location.provider !== 'byok') { this.session.abort({ message: `ERROR: The specified location '${location.name}' is not a BYOK location. Ensure that the location you specify has its provider property set to 'byok'.`, }); } // Lookup BYOK API endpoint const byokEndpoint = (await this.session.discovery).endpoints['byok']; // Fetch installation instructions const locationInstallLink = (0, links_1.linksOf)(location).install; if (!locationInstallLink) { this.session.abort({ message: `ERROR: The specified location '${location.name}' does not have a link with "rel" = "install"` }); } const response = await this.client.post(`${byokEndpoint}${locationInstallLink}`, {}); // Print the instructions if (this.session.format.output === 'text') { this.session.out(`# this commmand must be performed before ${new Date(response.expires * 1000).toLocaleString()}`); this.session.out(response.command); return; } this.session.outFormat(response); } } exports.Install = Install; class Uninstall extends command_1.Command { constructor(resolve) { super(); this.resolve = resolve; this.command = 'uninstall <ref>'; this.describe = 'Get instructions for obtaining the uninstallation script for a BYOK location'; } builder(yargs) { return (0, functions_1.pipe)( // generic_1.withSingleRef, options_1.withOrgOptions, options_1.withStandardOptions)(yargs); } async handle(args) { // Fetch the location const selfLink = this.resolve.resourceLink(args.ref, this.session.context); const location = await this.client.get(selfLink); // Validate that the location's provider is set to BYOK if (location.provider !== 'byok') { this.session.abort({ message: `ERROR: The specified location '${location.name}' is not a BYOK location. Ensure that the location you specify has its provider property set to 'byok'.`, }); } // Lookup BYOK API endpoint const byokEndpoint = (await this.session.discovery).endpoints['byok']; // Fetch installation instructions const locationUninstallLink = (0, links_1.linksOf)(location).uninstall; if (!locationUninstallLink) { this.session.abort({ message: `ERROR: The specified location '${location.name}' does not have a link with "rel" = "uninstall"` }); } const response = await this.client.post(`${byokEndpoint}${locationUninstallLink}`, {}); // Print the instructions if (this.session.format.output === 'text') { this.session.out(`# this commmand must be performed before ${new Date(response.expires * 1000).toLocaleString()}`); this.session.out(response.command); return; } this.session.outFormat(response); } } exports.Uninstall = Uninstall; //# sourceMappingURL=location.js.map