@controlplane/cli
Version: 
Control Plane Corporation CLI
186 lines • 7.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Connect = exports.Create = exports.SpicedbClusterCmd = void 0;
const options_1 = require("./options");
const generic_1 = require("./generic");
const resolver_1 = require("./resolver");
const command_1 = require("../cli/command");
const functions_1 = require("../util/functions");
const objects_1 = require("../util/objects");
const versions = ['1.14.1'];
const spicedbcluster_defaults = {
    version: versions[0],
};
class SpicedbClusterCmd extends command_1.Command {
    constructor() {
        super(...arguments);
        this.command = 'spicedbcluster';
        this.aliases = ['spicedb'];
        this.describe = 'Manage SpiceDB clusters';
    }
    builder(yargs) {
        const resolver = (0, resolver_1.kindResolver)('spicedbcluster');
        const opts = [options_1.withStandardOptions, options_1.withOrgOptions];
        const commandName = 'spicedb cluster';
        const commandNamePlural = 'spicedb clusters';
        const commandNameA = 'a spicedb cluster';
        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.version',
                choices: versions,
            },
            {
                path: 'spec.locations',
                array: true,
                type: 'string',
            },
        ], ...opts).toYargs())
            // specific
            .command(new Create(resolver).toYargs())
            .command(new Connect(resolver).toYargs()));
    }
    handle() { }
}
exports.SpicedbClusterCmd = SpicedbClusterCmd;
class Create extends command_1.Command {
    constructor(resolve) {
        super();
        this.resolve = resolve;
        this.command = 'create';
        this.describe = 'Create a new spicedb cluster';
    }
    builder(yargs) {
        return (0, functions_1.pipe)(
        //
        (yargs) => {
            return yargs.options({
                name: {
                    describe: 'Name of the new spicedb cluster',
                    requiresArg: true,
                    demandOption: true,
                },
                description: {
                    alias: 'desc',
                    describe: 'Optional description, defaults to the name if not set',
                },
                location: {
                    describe: 'One or more locations to associate with this new spicedb cluster',
                    requiresArg: true,
                    multiple: true,
                    demandOption: true,
                },
                version: {
                    describe: 'Version',
                    default: spicedbcluster_defaults.version,
                    choices: versions,
                    requiresArg: true,
                },
            });
        }, generic_1.withTagOptions, options_1.withAllOptions)(yargs);
    }
    async handle(args) {
        const locationLinks = (0, objects_1.toArray)(args.location).map((loc) => (0, resolver_1.resolveToLink)('location', loc, this.session.context));
        const req = toCreateSpicedbClusterRequest(args);
        req.spec.locations = locationLinks;
        const link = this.resolve.parentLink(this.session.context);
        const body = await this.client.create(link, req);
        this.session.outFormat(body);
    }
}
exports.Create = Create;
function toCreateSpicedbClusterRequest(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: {},
    };
    req.spec = {
        version: args.version,
    };
    return req;
}
class Connect extends command_1.Command {
    constructor(resolve) {
        super();
        this.resolve = resolve;
        this.command = 'connect <ref>';
        this.describe = 'Gain access to a SpiceDB cluster';
    }
    builder(yargs) {
        return (0, functions_1.pipe)(
        //
        options_1.withRequestOptions, options_1.withDebugOptions, options_1.withOrgOptions, (yargs) => {
            return yargs.options({
                output: {
                    group: 'Format options:',
                    alias: 'o',
                    requiresArg: true,
                    default: 'export',
                    description: 'Set the output format',
                    choices: ['env', 'export', 'json'],
                },
                // session: {
                //   describe: 'Name of the session',
                //   requiresArg: true,
                // },
                // role: {
                //   describe: 'Permissions for the access token',
                //   requiresArg: true,
                //   demandOption: true,
                //   default: 'checkPermission',
                //   choices: ['checkPermission', 'read', 'write'],
                // },
                // ttl: {
                //   describe: 'TTL of the access token (seconds)',
                //   default: 20 * 60,
                //   number: true,
                //   requiresArg: true,
                // },
            });
        })(yargs);
    }
    async handle(args) {
        const path = this.resolve.resourceLink(args.ref, this.session.context);
        const obj = await this.client.get(path);
        const endpoint = obj.status.externalEndpoint;
        let accessToken = this.session.request.token;
        if (accessToken === null || accessToken === void 0 ? void 0 : accessToken.startsWith('Bearer ')) {
            accessToken = accessToken.substring(7);
        }
        switch (args.output) {
            case 'env':
                this.session.outFormat(`ZED_ENDPOINT='${endpoint}'` + '\n' + `ZED_TOKEN='${accessToken}'\n`);
                break;
            case 'export':
                this.session.outFormat(`export ZED_ENDPOINT='${endpoint}'` + '\n' + `export ZED_TOKEN='${accessToken}'\n`);
                break;
            case 'json':
                this.session.outFormat({ endpoint, accessToken });
        }
    }
}
exports.Connect = Connect;
//# sourceMappingURL=spicedbcluster.js.map