@controlplane/cli
Version:
Control Plane Corporation CLI
311 lines • 12.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Delete = exports.Create = exports.GvcCmd = void 0;
const generic_1 = require("./generic");
const command_1 = require("../cli/command");
const query_1 = require("./query");
const options_1 = require("./options");
const resolver_1 = require("./resolver");
const functions_1 = require("../util/functions");
const objects_1 = require("../util/objects");
const resultFetcher_1 = require("../rest/resultFetcher");
const _ = require("lodash");
class GvcCmd extends command_1.Command {
constructor() {
super(...arguments);
this.command = 'gvc';
this.describe = 'Manage global virtual clouds';
}
builder(yargs) {
const resolver = (0, resolver_1.kindResolver)('gvc');
const gvcSchema = {
props: [...query_1.defaultProps, 'alias'],
};
const locationSchema = {
props: [...query_1.defaultProps],
};
const opts = [options_1.withOrgOptions, options_1.withStandardOptions];
const commandName = 'global virtual cloud';
const commandNamePlural = 'global virtual clouds';
const commandNameA = 'a global virtual cloud';
return (yargs
.demandCommand()
.version(false)
.help()
// generic
.command(new generic_1.Get(commandNamePlural, resolver, ...opts).toYargs())
.command(new generic_1.Edit(commandName, resolver, ...opts).toYargs())
.command(new generic_1.Query(commandNamePlural, resolver, gvcSchema, ...opts).toYargs())
.command(new generic_1.Eventlog(commandName, resolver, ...opts).toYargs())
.command(new generic_1.Tag(commandNamePlural, resolver, ...opts).toYargs())
.command(new generic_1.Patch(commandName, resolver, ...opts).toYargs())
.command(new generic_1.ListPermissions(commandNameA, resolver, ...opts).toYargs())
.command(new generic_1.ViewAccessReport(commandName, resolver, ...opts).toYargs())
.command(new generic_1.Clone(commandName, resolver, ...opts).toYargs())
.command(new generic_1.Update(commandName, resolver, [
{
path: 'description',
},
{
path: 'tags.<key>',
},
{
path: 'spec.pullSecretLinks',
array: true,
itemLinkResolver: (0, resolver_1.kindResolver)('secret'),
},
{
path: 'spec.staticPlacement.locationLinks',
array: true,
itemLinkResolver: (0, resolver_1.kindResolver)('location'),
},
{
path: 'spec.tracing.sampling',
type: 'number',
},
{
path: 'spec.tracing.lightstep.endpoint',
type: 'string',
},
{
path: 'spec.tracing.lightstep.credentials',
array: true,
itemLinkResolver: (0, resolver_1.kindResolver)('secret'),
},
{
path: 'spec.env.<name>.value',
},
], ...opts).toYargs())
// specific
.command(new Delete().toYargs())
.command(new Create(resolver, locationSchema).toYargs())
.command(new AddLocation(resolver).toYargs())
.command(new RemoveLocation(resolver).toYargs())
.command(new DeleteAllWorkloads(resolver).toYargs()));
}
handle() { }
}
exports.GvcCmd = GvcCmd;
class Create extends command_1.Command {
constructor(resolver, schema) {
super();
this.resolver = resolver;
this.schema = schema;
this.command = 'create';
this.describe = 'Create a new global virtual cloud';
}
withCreateOptions(yargs) {
return yargs.options({
name: {
describe: 'Name of the new global virtual cloud',
demandOption: true,
requiresArg: true,
},
description: {
alias: 'desc',
describe: 'Optional description, defaults to the name if not set',
requiresArg: true,
},
location: {
describe: 'One or more locations to associate with this new global virtual cloud',
requiresArg: true,
multiple: true,
demandOption: true,
},
env: {
describe: 'Environment variables in KEY=VALUE format',
requiresArg: true,
multiple: true,
},
});
}
builder(yargs) {
return (0, functions_1.pipe)(
//
this.withCreateOptions, (0, query_1.withQuerySpecOptions)(this.schema), generic_1.withTagOptions, options_1.withOrgOptions, options_1.withStandardOptions)(yargs);
}
async handle(args) {
var _a;
const locationQuery = (0, query_1.fromQuerySpecOptions)(args);
const locationLinks = (0, objects_1.toArray)(args.location).map((loc) => (0, resolver_1.resolveToLink)('location', loc, this.session.context));
if (!locationQuery && locationLinks.length < 1) {
this.session.abort({ message: 'GVC create command requires either location(s) or a query for locations' });
}
const body = {
name: args.name,
description: args.description,
tags: (0, generic_1.fromTagOptions)(args),
spec: {
staticPlacement: {
locationLinks,
locationQuery,
},
env: (0, objects_1.toEnv)((_a = (0, objects_1.toArray)(args.env)) !== null && _a !== void 0 ? _a : []),
},
};
const path = this.resolver.homeLink(this.session.context);
const data = await this.client.create(path, body);
this.session.outFormat(data);
}
}
exports.Create = Create;
class Delete extends command_1.Command {
constructor() {
super(...arguments);
this.command = 'delete <ref...>';
this.describe = 'Delete GVCs by name';
this.resolve = (0, resolver_1.kindResolver)('gvc');
}
builder(yargs) {
return (0, functions_1.pipe)(
//
generic_1.withMultipleRefs, options_1.withForceOption, options_1.withOrgOptions, options_1.withStandardOptions)(yargs);
}
async handle(args) {
args.ref = _.uniq((0, objects_1.toArray)(args.ref));
const failures = [];
for (let ref of args.ref) {
const gvcLink = this.resolve.resourceLink(ref, this.session.context);
try {
// Delete all resources in GVC first, if force enabled
if (args.force) {
const context = {
...this.session.context,
gvc: ref,
};
await this.listAndDelete('workload', context);
await this.listAndDelete('volumeset', context);
await this.listAndDelete('identity', context);
}
// Finally, delete GVC
await this.client.delete(gvcLink);
}
catch (e) {
failures.push(e);
}
}
if (failures.length > 0) {
this.session.abort({ error: failures });
}
}
}
exports.Delete = Delete;
function withLocationOptions(action) {
return function (yargs) {
return yargs.options({
location: {
describe: 'Location to ' + action,
requiresArg: true,
demandOption: true,
multiple: true,
},
});
};
}
class AddLocation extends command_1.Command {
constructor(resolver) {
super();
this.resolver = resolver;
this.command = 'add-location <ref>';
this.describe = 'Add one or more locations to the referenced global virtual cloud';
}
builder(yargs) {
return (0, functions_1.pipe)(
//
generic_1.withSingleRef, withLocationOptions('add'), options_1.withOrgOptions, options_1.withStandardOptions)(yargs);
}
async handle(args) {
var _a, _b;
const link = this.resolver.resourceLink(args.ref, this.session.context);
const resource = await this.client.get(link);
const locationLinks = ((_b = (_a = resource.spec) === null || _a === void 0 ? void 0 : _a.staticPlacement) === null || _b === void 0 ? void 0 : _b.locationLinks) || [];
const locations = locationLinks.map((loc) => loc.split('/').pop());
(0, objects_1.toArray)(args.location).forEach((location) => {
if (!locations.includes(location)) {
locations.push(location);
}
});
const body = {
spec: {
staticPlacement: {
locationLinks: locations.map((loc) => (0, resolver_1.resolveToLink)('location', loc, this.session.context)),
},
},
};
const data = await this.client.patch(link, body);
this.session.outFormat(data);
}
}
class RemoveLocation extends command_1.Command {
constructor(resolver) {
super();
this.resolver = resolver;
this.command = 'remove-location <ref>';
this.describe = 'Remove one or more locations from the referenced global virtual cloud';
}
builder(yargs) {
return (0, functions_1.pipe)(
//
generic_1.withSingleRef, withLocationOptions('remove'), options_1.withOrgOptions, options_1.withStandardOptions)(yargs);
}
async handle(args) {
var _a, _b;
const link = this.resolver.resourceLink(args.ref, this.session.context);
const resource = await this.client.get(link);
const locationLinks = ((_b = (_a = resource.spec) === null || _a === void 0 ? void 0 : _a.staticPlacement) === null || _b === void 0 ? void 0 : _b.locationLinks) || [];
let locations = locationLinks.map((link) => link.split('/').pop()).filter((loc) => !(0, objects_1.toArray)(args.location).includes(loc));
const body = {
spec: {
staticPlacement: {
locationLinks: locations.map((loc) => (0, resolver_1.resolveToLink)('location', loc, this.session.context)),
},
},
};
const data = await this.client.patch(link, body);
this.session.outFormat(data);
}
}
class DeleteAllWorkloads extends command_1.Command {
constructor(resolver) {
super();
this.resolver = resolver;
this.command = 'delete-all-workloads <ref>';
this.describe = 'Delete all workloads for the referenced global virtual cloud';
}
builder(yargs) {
return (0, functions_1.pipe)(
//
generic_1.withSingleRef, options_1.withOrgOptions, options_1.withStandardOptions)(yargs);
}
async handle(args) {
const workloadsLink = this.resolver.resourceLink(args.ref, this.session.context);
await this.client.get(workloadsLink);
const link = workloadsLink + '/workload';
const workloads = await this.client.get(link);
await (0, resultFetcher_1.fetchPages)(this.client, 100, workloads);
let failures = [];
let failedIndexes = [];
for (let index in workloads.items) {
const workload = workloads.items[index];
let path = workload.links.find((l) => l.rel === 'self').href;
try {
await this.client.delete(path);
}
catch (e) {
failures.push(e);
failedIndexes.push(Number(index));
}
}
for (let index of failedIndexes) {
workloads.items.splice(index, 1, undefined);
}
workloads.items = workloads.items.filter(Boolean);
if (workloads.items.length > 0) {
this.session.outFormat(workloads);
}
if (failures.length > 0) {
this.session.abort({ error: failures });
}
}
}
//# sourceMappingURL=gvc.js.map