@controlplane/cli
Version:
Control Plane Corporation CLI
225 lines • 9.57 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Create = exports.IpSetCmd = void 0;
const command_1 = require("../cli/command");
const resolver_1 = require("./resolver");
const options_1 = require("./options");
const generic_1 = require("./generic");
const functions_1 = require("../util/functions");
const objects_1 = require("../util/objects");
const links_1 = require("../util/links");
const DEFAULT_RETENTION_POLICY = 'keep';
class IpSetCmd extends command_1.Command {
constructor() {
super(...arguments);
this.command = 'ipset';
this.describe = 'Manage IP Sets';
}
builder(yargs) {
const resolver = (0, resolver_1.kindResolver)('ipset');
const opts = [options_1.withStandardOptions, options_1.withOrgOptions];
const commandName = 'ip set';
const commandNamePlural = 'ip sets';
const commandNameA = 'an ip set';
return (yargs
.demandCommand()
.version(false)
.help()
// generic
.command(new generic_1.Get(commandName, resolver, ...opts).toYargs())
.command(new generic_1.Edit(commandName, resolver, ...opts).toYargs())
.command(new generic_1.Patch(commandName, resolver, ...opts).toYargs())
.command(new generic_1.Query(commandNamePlural, resolver, undefined, ...opts).toYargs())
.command(new generic_1.Delete(commandNamePlural, resolver, ...opts).toYargs())
.command(new generic_1.Eventlog(commandName, resolver, ...opts).toYargs())
.command(new generic_1.Tag(commandNamePlural, 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.link',
},
], ...opts).toYargs())
// specific
.command(new Create(resolver).toYargs())
.command(new AddLocation(resolver).toYargs())
.command(new UpdateLocation(resolver).toYargs())
.command(new RemoveLocation(resolver).toYargs()));
}
handle() { }
}
exports.IpSetCmd = IpSetCmd;
class Create extends command_1.Command {
constructor(resolve) {
super();
this.resolve = resolve;
this.command = 'create';
this.describe = 'Create a new ip set';
}
builder(yargs) {
return (0, functions_1.pipe)(
//
(yargs) => {
return yargs.options({
name: {
describe: 'Name of the new volume set',
requiresArg: true,
demandOption: true,
},
description: {
alias: 'desc',
describe: 'Optional description, defaults to the name if not set',
},
link: {
describe: 'A link to a workload',
requiresArg: true,
},
location: {
describe: 'Specifies a location and optional retention policy (keep or free), e.g., aws-eu-central-1,keep',
requiresArg: true,
multiple: true,
},
});
}, generic_1.withTagOptions, options_1.withOrgOptions, options_1.withStandardOptions)(yargs);
}
async handle(args) {
var _a;
const req = {
name: args.name,
description: (_a = args.description) !== null && _a !== void 0 ? _a : args.name,
tags: (0, generic_1.fromTagOptions)(args),
spec: {
link: args.link,
},
};
if (args.location !== undefined) {
req.spec.locations = parseLocations((0, objects_1.toArray)(args.location), this.session.context);
}
const link = this.resolve.parentLink(this.session.context);
const body = await this.client.create(link, req);
await this.session.outFormat(body);
}
}
exports.Create = Create;
function withLocationOptions(description) {
return function (yargs) {
return yargs.options({
location: {
describe: description,
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 IP set';
this.alias = ['update-location'];
}
builder(yargs) {
return (0, functions_1.pipe)(
//
generic_1.withSingleRef, withLocationOptions('Add a location with an optional retention policy (`keep` or `free`), e.g., aws-eu-central-1,keep.'), options_1.withOrgOptions, options_1.withStandardOptions)(yargs);
}
async handle(args) {
var _a;
const link = this.resolver.resourceLink(args.ref, this.session.context);
// Fetch the referenced IP set
const resource = await this.client.get(link);
// Parse locations to add
const locationsToAdd = parseLocations((0, objects_1.toArray)(args.location), this.session.context);
// Merge existing and new locations, this will update existing location as necessary
const locations = this.mergeLocations(((_a = resource.spec) === null || _a === void 0 ? void 0 : _a.locations) || [], locationsToAdd);
// Construct the payload with the updated locations to send to the data service
const body = {
spec: {
locations: locations,
},
};
// Perform update
const data = await this.client.patch(link, body);
// Output the updated IP set
await this.session.outFormat(data);
}
mergeLocations(existingLocations, newLocations) {
// Create a map from existing locations using location name as the key
const locationMap = new Map();
// Add all existing locations to the map
for (const location of existingLocations) {
const locationName = (0, links_1.getLastPartOfLink)(location.name);
locationMap.set(locationName, location);
}
// Add or update locations
for (const location of newLocations) {
const locationName = (0, links_1.getLastPartOfLink)(location.name);
locationMap.set(locationName, location); // This will overwrite if the key already exists
}
// Return the merged array of locations
return Array.from(locationMap.values());
}
}
class UpdateLocation extends AddLocation {
constructor() {
super(...arguments);
this.command = 'update-location <ref>';
this.describe = 'Update one or more locations from the referenced IP set';
}
}
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 IP set';
}
builder(yargs) {
return (0, functions_1.pipe)(
//
generic_1.withSingleRef, withLocationOptions('Remove a location by name, e.g., aws-eu-central-1.'), options_1.withOrgOptions, options_1.withStandardOptions)(yargs);
}
async handle(args) {
var _a, _b;
const link = this.resolver.resourceLink(args.ref, this.session.context);
// Fetch the referenced IP set
const resource = await this.client.get(link);
// Parse locations to remove
const locationsToRemove = parseLocations((0, objects_1.toArray)(args.location), this.session.context).map((location) => (0, links_1.getLastPartOfLink)(location.name));
// Filter out the locations to remove from the locations within the referenced IP set
const filteredLocations = ((_b = (_a = resource.spec) === null || _a === void 0 ? void 0 : _a.locations) === null || _b === void 0 ? void 0 : _b.filter((location) => !locationsToRemove.includes((0, links_1.getLastPartOfLink)(location.name)))) || [];
// Construct the payload with the updated locations to send to the data service
const body = {
spec: {
locations: filteredLocations,
},
};
// Perform update
const data = await this.client.patch(link, body);
// Output the updated IP set
await this.session.outFormat(data);
}
}
/*** Helpers ***/
function parseLocations(locations, ctx) {
return locations.map((item) => {
// Split the input string by a comma into location and retentionPolicy
const [location, retentionPolicy] = item.split(',');
// Construct the IpSetLocation object
return {
name: (0, resolver_1.resolveToLink)('location', location.trim(), ctx),
retentionPolicy: (retentionPolicy === null || retentionPolicy === void 0 ? void 0 : retentionPolicy.trim()) || DEFAULT_RETENTION_POLICY,
};
});
}
//# sourceMappingURL=ipset.js.map
;