@controlplane/cli
Version:
Control Plane Corporation CLI
360 lines (351 loc) • 13.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CloudaccountCmd = void 0;
const generic_1 = require("./generic");
const command_1 = require("../cli/command");
const query_1 = require("./query");
const resolver_1 = require("./resolver");
const options_1 = require("./options");
const functions_1 = require("../util/functions");
class CloudaccountCmd extends command_1.Command {
constructor() {
super(...arguments);
this.command = 'cloudaccount';
this.describe = 'Manage cloud accounts';
}
builder(yargs) {
const resolver = (0, resolver_1.kindResolver)('cloudaccount');
const schema = {
props: [...query_1.defaultProps, 'provider'],
};
const opts = [options_1.withOrgOptions, options_1.withStandardOptions];
const commandName = 'cloud account';
const commandNamePlural = 'cloud accounts';
const commandNameA = 'a cloud account';
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.Patch(commandName, resolver, ...opts).toYargs())
.command(new generic_1.Query(commandNamePlural, resolver, schema, ...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())
// specific
.command(new CreateAws(resolver).toYargs())
.command(new CreateGcp(resolver).toYargs())
.command(new CreateAzure(resolver).toYargs())
.command(new CreateNGS(resolver).toYargs()));
}
handle() { }
}
exports.CloudaccountCmd = CloudaccountCmd;
class CreateAws extends command_1.Command {
constructor(resolve) {
super();
this.resolve = resolve;
this.command = 'create-aws';
this.describe = 'Create an AWS cloud account';
}
withCreateOptions(yargs) {
return yargs.options({
name: {
//demandOption: true,
requiresArg: true,
describe: 'Name of the new account',
},
description: {
alias: 'desc',
requiresArg: true,
describe: 'Optional description, defaults to the name if not set',
},
'role-arn': {
//demandOption: true,
requiresArg: true,
describe: 'ARN of the role used by Control Plane to manage the account, in format `arn:aws:iam::<account number>:role/<name>`',
},
how: {
boolean: true,
describe: 'Show the steps on how to create and configure your AWS account',
},
});
}
builder(yargs) {
return (0, functions_1.pipe)(
//
this.withCreateOptions, generic_1.withTagOptions, options_1.withOrgOptions, options_1.withStandardOptions)(yargs);
}
async handle(args) {
const parentLink = this.resolve.parentLink(this.session.context);
if (args.how) {
const instr = await this.client.get(parentLink + '/-instructions/aws');
this.session.out(instr.message);
if (instr.data.cli) {
this.session.out('\n * The AWS account configuration changes can be performed using the `aws` tool:\n\n' + indent(instr.data.cli, ' '));
}
return;
}
if (!args.name) {
this.session.abort({ message: 'The option --name is required.' });
}
if (!args.roleArn) {
this.session.abort({ message: 'The option --role-arn is required.' });
}
const body = {
name: args.name,
provider: 'aws',
description: args.description,
tags: (0, generic_1.fromTagOptions)(args),
data: {
roleArn: args.roleArn,
},
};
const data = await this.client.create(parentLink, body);
this.session.outFormat(data);
}
}
class CreateGcp extends command_1.Command {
constructor(resolve) {
super();
this.resolve = resolve;
this.command = 'create-gcp';
this.describe = 'Create a GCP cloud account';
}
withCreateOptions(yargs) {
return yargs.options({
name: {
requiresArg: true,
describe: 'Name of the new account',
},
description: {
alias: 'desc',
requiresArg: true,
describe: 'Optional description, defaults to the name if not set',
},
'project-id': {
requiresArg: true,
describe: 'The ID of the project that Control Plane will manage',
},
how: {
boolean: true,
describe: 'Show the steps on how to create and configure your GCP project',
},
});
}
builder(yargs) {
return (0, functions_1.pipe)(
//
this.withCreateOptions, generic_1.withTagOptions, options_1.withOrgOptions, options_1.withStandardOptions)(yargs);
}
async handle(args) {
const parentLink = this.resolve.parentLink(this.session.context);
if (args.how) {
const instr = await this.client.get(parentLink + '/-instructions/gcp');
this.session.out(instr.message);
if (instr.data.cli) {
this.session.out('\n * The GCP project configuration steps can be performed using the `gcloud` tool:\n\n' + indent(instr.data.cli, ' '));
}
return;
}
if (!args.name) {
this.session.abort({ message: 'The option --name is required.' });
}
if (!args.projectId) {
this.session.abort({ message: 'The option --project-id is required.' });
}
const body = {
name: args.name,
provider: 'gcp',
description: args.description,
tags: (0, generic_1.fromTagOptions)(args),
data: {
projectId: args.projectId,
numericProjectId: args.numericProjectId,
},
};
const data = await this.client.create(parentLink, body);
this.session.outFormat(data);
}
}
class CreateAzure extends command_1.Command {
constructor(resolve) {
super();
this.resolve = resolve;
this.command = 'create-azure';
this.describe = 'Create an Azure cloud account';
}
withCreateOptions(yargs) {
return yargs.options({
name: {
requiresArg: true,
describe: 'Name of the new account',
},
description: {
alias: 'desc',
requiresArg: true,
describe: 'Optional description, defaults to the name if not set',
},
secret: {
requiresArg: true,
describe: 'The name of the secret which will be created.',
},
url: {
requiresArg: true,
describe: 'The URL of the deployed function app.',
},
code: {
requiresArg: true,
describe: 'The URL of the deployed function app.',
},
how: {
boolean: true,
describe: 'Show the steps on how to create and configure your Azure subscription',
},
});
}
builder(yargs) {
return (0, functions_1.pipe)(
//
this.withCreateOptions, generic_1.withTagOptions, options_1.withOrgOptions, options_1.withStandardOptions)(yargs);
}
async handle(args) {
const parentLink = this.resolve.parentLink(this.session.context);
if (args.how) {
this.session.out(getAzureInstructions());
return;
}
if (!args.name) {
this.session.abort({ message: 'The option --name is required.' });
}
if (!args.secret) {
this.session.abort({ message: 'The option --secret is required.' });
}
if (!args.url) {
this.session.abort({ message: 'The option --url is required.' });
}
if (!args.code) {
this.session.abort({ message: 'The option --code is required.' });
}
const secretBody = {
name: args.secret,
type: 'azure-connector',
data: {
url: args.url,
code: args.code,
},
};
const secretResolver = (0, resolver_1.kindResolver)('secret');
await this.client.create(secretResolver.parentLink(this.session.context), secretBody);
const body = {
name: args.name,
provider: 'azure',
description: args.description,
tags: (0, generic_1.fromTagOptions)(args),
data: {
secretLink: `//secret/${args.secret}`,
},
};
const data = await this.client.create(parentLink, body);
this.session.outFormat(data);
}
}
class CreateNGS extends command_1.Command {
constructor(resolve) {
super();
this.resolve = resolve;
this.command = 'create-ngs';
this.describe = 'Create a NGS cloud account';
}
withCreateOptions(yargs) {
return yargs.options({
name: {
demandOption: true,
requiresArg: true,
describe: 'Name of the new account',
},
description: {
alias: 'desc',
requiresArg: true,
describe: 'Optional description, defaults to the name if not set',
},
secret: {
demandOption: true,
requiresArg: true,
describe: 'The secret used to authenticate to NGS',
},
});
}
builder(yargs) {
return (0, functions_1.pipe)(
//
this.withCreateOptions, generic_1.withTagOptions, options_1.withOrgOptions, options_1.withStandardOptions)(yargs);
}
async handle(args) {
const parentLink = this.resolve.parentLink(this.session.context);
const body = {
name: args.name,
provider: 'ngs',
description: args.description,
tags: (0, generic_1.fromTagOptions)(args),
data: {
secretLink: `//secret/${args.secret}`,
},
};
const data = await this.client.create(parentLink, body);
this.session.outFormat(data);
}
}
function indent(s, offest) {
return s.map((x) => offest + x).join('\n\n');
}
function getAzureInstructions() {
return `
1. Make sure you have Azure CLI setup and you are logged in.
2. Execute command below in succession to create and deploy a new function app.
Available locations are:
Central US, East US, East US 2, France Central, Germany West Central, North Central US, North Europe, Norway East, Switzerland North, UK South, UK West, West Central US, West Europe, West US, West US 2, West US 3
az functionapp create \\
--subscription $SUBSCRIPTION_ID \\
--resource-group $RESOURCE_GROUP \\
--name $FUNCTION_APP \\
--storage-account $STORAGE_ACCOUNT \\
--assign-identity \\
--role Owner \\
--scope /subscriptions/$SUBSCRIPTION_ID \\
--os-type linux \\
--runtime node \\
--functions-version 3 \\
--runtime-version 14 \\
--disable-app-insights \\
--consumption-plan-location $LOCATION
wget https://storage.googleapis.com/artifacts.cpln-build.appspot.com/binaries/azure-cpln-connector/latest/azure-cpln-connector.zip
az functionapp deployment source config-zip \\
--subscription $SUBSCRIPTION_ID \\
--resource-group $RESOURCE_GROUP \\
--name $FUNCTION_APP \\
--src azure-cpln-connector.zip
3. Get the URL of the deployed Function App
az functionapp function show \\
--subscription $SUBSCRIPTION_ID \\
--resource-group $RESOURCE_GROUP \\
--name $FUNCTION_APP \\
--function iam-broker \\
--query invokeUrlTemplate \\
-o tsv
4. Get the code of the deployed Function App
az functionapp function keys list \\
--subscription $SUBSCRIPTION_ID \\
--resource-group $RESOURCE_GROUP \\
--name $FUNCTION_APP \\
--function-name iam-broker \\
--query default \\
-o tsv
5. Use the URL and code you fetched as input to "cpln cloudaccount create-azure" command.
`;
}
//# sourceMappingURL=cloudaccount.js.map