diff --git a/lib/interface/cli/commands/root/assign.cmd.js b/lib/interface/cli/commands/root/assign.cmd.js new file mode 100644 index 000000000..8b46da64a --- /dev/null +++ b/lib/interface/cli/commands/root/assign.cmd.js @@ -0,0 +1,18 @@ +const Command = require('../../Command'); + +const assign = new Command({ + root: true, + command: 'assign', + description: 'Assign a resource', + usage: 'codefresh assign --help', + webDocs: { + title: 'Assign', + weight: 70, + }, + builder: (yargs) => { + return yargs + .demandCommand(1, 'You need at least one command before moving on'); + }, +}); + +module.exports = assign; diff --git a/lib/interface/cli/commands/root/unassign.cmd.js b/lib/interface/cli/commands/root/unassign.cmd.js new file mode 100644 index 000000000..a66f45f00 --- /dev/null +++ b/lib/interface/cli/commands/root/unassign.cmd.js @@ -0,0 +1,18 @@ +const Command = require('../../Command'); + +const unassign = new Command({ + root: true, + command: 'unassign', + description: 'Unassign a resource', + usage: 'codefresh assign --help', + webDocs: { + title: 'Unassign', + weight: 70, + }, + builder: (yargs) => { + return yargs + .demandCommand(1, 'You need at least one command before moving on'); + }, +}); + +module.exports = unassign; diff --git a/lib/interface/cli/commands/systemRuntimeEnvironments/apply.cmd.js b/lib/interface/cli/commands/systemRuntimeEnvironments/apply.cmd.js index 0d399604d..20e7412fc 100644 --- a/lib/interface/cli/commands/systemRuntimeEnvironments/apply.cmd.js +++ b/lib/interface/cli/commands/systemRuntimeEnvironments/apply.cmd.js @@ -4,6 +4,7 @@ const _ = require('lodash'); const applyRoot = require('../root/apply.cmd'); const { crudFilenameOption } = require('../../helpers/general'); const sysRe = require('../../helpers/sys-re'); +const { sdk } = require('../../../../logic'); const command = new Command({ @@ -38,6 +39,18 @@ const command = new Command({ alias: 'd', describe: 'Description of the new runtime environment', }) + .option('assign-accounts', { + alias: 'a', + type: Boolean, + // default: false, + describe: 'Assign runtime environment to accounts specified in the yaml', + }) + .option('unassign-accounts', { + alias: 'u', + type: Boolean, + // default: false, + describe: 'Unassign runtime environment from accounts based on diff with current state', + }) .example('codefresh patch system-runtime-environments -f ./re.yml', 'Apply changes to a system runtime environment'); }, handler: async (argv) => { @@ -60,6 +73,37 @@ const command = new Command({ } else { await sysRe.update(options, body); console.log(`Runtime-Environment: '${options.name}' patched.`); + const yamlAccounts = body.accounts; + if (yamlAccounts) { + if (argv.unassignAccounts) { + const re = await sysRe.get({ ...options, extend: false }); + const accounts = _.difference(re.accounts, yamlAccounts); + if (!_.isEmpty(accounts)) { + await sdk.onPrem.runtimeEnvs.account.delete({ name: options.name }, { accounts }); + console.log(`Runtime-Environment unassigned from accounts: ${accounts}`); + } else { + console.log('No accounts to unassign'); + } + } + + if (argv.assignAccounts) { + const re = await sysRe.get({ ...options, extend: false }); + const accounts = _.difference(yamlAccounts, re.accounts); + const existing = await sdk.accounts.listAccounts({ _id: accounts }); + const nonExisting = _.difference(accounts, existing.map(({id}) => id)); + if (!_.isEmpty(nonExisting)) { + throw new CFError({ + message: `Accounts do not exist: ${nonExisting}`, + }); + } + if (!_.isEmpty(accounts)) { + await sdk.onPrem.runtimeEnvs.account.modify({ name: options.name }, { accounts }); + console.log(`Runtime-Environment assigned to accounts: ${accounts}`); + } else { + console.log('No accounts to assign'); + } + } + } } }, }); diff --git a/lib/interface/cli/commands/systemRuntimeEnvironments/assign.cmd.js b/lib/interface/cli/commands/systemRuntimeEnvironments/assign.cmd.js new file mode 100644 index 000000000..e5a3a53de --- /dev/null +++ b/lib/interface/cli/commands/systemRuntimeEnvironments/assign.cmd.js @@ -0,0 +1,51 @@ +const CFError = require('cf-errors'); +const _ = require('lodash'); +const Command = require('../../Command'); +const assignRoot = require('../root/assign.cmd'); +const { crudFilenameOption } = require('../../helpers/general'); +const { sdk } = require('../../../../logic'); + +const command = new Command({ + command: 'system-runtime-environments ', + aliases: ['sys-re', 'system-runtime-environment'], + parent: assignRoot, + description: 'Assign system-runtime-environments to accounts', + onPremCommand: true, + usage: 'Use assign to add runtime environments to accounts by their name or id', + webDocs: { + title: 'Assign System Runtime-Environments', + weight: 90, + }, + builder: (yargs) => { + crudFilenameOption(yargs); + return yargs + .positional('name', { + describe: 'Runtime environments name', + required: true, + }) + .positional('accounts', { + describe: 'Accounts names', + type: Array, + required: true, + }) + .example( + 'codefresh assign sys-re my-sys-re acc1 acc2', + 'Add system runtime enviroment "my-sys-re" to accounts acc1 and acc2' , + ); + }, + handler: async (argv) => { + const { name, accounts: accountsNames } = argv; + const accounts = await sdk.accounts.listAccounts({ name: accountsNames }); + const nonExistentAccounts = _.difference(accountsNames, accounts.map((a) => a.name)); + if (!_.isEmpty(nonExistentAccounts)) { + throw new CFError({ + message: `Accounts do not exist: ${nonExistentAccounts}`, + }); + } + const accountIds = accounts.map((a) => a.id); + await sdk.onPrem.runtimeEnvs.account.modify({ name }, { accounts: accountIds }); + console.log(`Successfully assigned "${name}" to accounts: ${accountsNames}`); + }, +}); + +module.exports = command; diff --git a/lib/interface/cli/commands/systemRuntimeEnvironments/unassign.cmd.js b/lib/interface/cli/commands/systemRuntimeEnvironments/unassign.cmd.js new file mode 100644 index 000000000..2fb12a1c1 --- /dev/null +++ b/lib/interface/cli/commands/systemRuntimeEnvironments/unassign.cmd.js @@ -0,0 +1,51 @@ +const CFError = require('cf-errors'); +const _ = require('lodash'); +const Command = require('../../Command'); +const unassignRoot = require('../root/unassign.cmd'); +const { crudFilenameOption } = require('../../helpers/general'); +const { sdk } = require('../../../../logic'); + +const command = new Command({ + command: 'system-runtime-environments ', + aliases: ['sys-re', 'system-runtime-environment'], + parent: unassignRoot, + description: 'Unassign system-runtime-environments from accounts', + onPremCommand: true, + usage: 'Use unassign to remove runtime environments from accounts by their name or id', + webDocs: { + title: 'Unassign System Runtime-Environments', + weight: 90, + }, + builder: (yargs) => { + crudFilenameOption(yargs); + return yargs + .positional('name', { + describe: 'Runtime environment name', + required: true, + }) + .positional('accounts', { + describe: 'Accounts names', + type: Array, + required: true, + }) + .example( + 'codefresh unassign sys-re my-sys-re acc1 acc2', + 'Remove system runtime enviroment "my-sys-re" from accounts acc1 and acc2', + ); + }, + handler: async (argv) => { + const { name, accounts: accountsNames } = argv; + const accounts = await sdk.accounts.listAccounts({ name: accountsNames }); + const nonExistentAccounts = _.difference(accountsNames, accounts.map((a) => a.name)); + if (!_.isEmpty(nonExistentAccounts)) { + throw new CFError({ + message: `Accounts do not exist: ${nonExistentAccounts}`, + }); + } + const accountIds = accounts.map((a) => a.id); + await sdk.onPrem.runtimeEnvs.account.delete({ name }, { accounts: accountIds }); + console.log(`Successfully removed "${name}" from accounts: ${accountsNames}`); + }, +}); + +module.exports = command; diff --git a/openapi.json b/openapi.json index 026442f1e..d3932dac6 100644 --- a/openapi.json +++ b/openapi.json @@ -1036,7 +1036,30 @@ "description": "get all the accounts in the system", "operationId": "accounts-list-accounts", "summary": "List accounts", - "parameters": [], + "parameters": [ + { + "name": "name", + "in": "query", + "required": false, + "schema": { + "type": "array", + "items": { + "type": "string" + } + } + }, + { + "name": "_id", + "in": "query", + "required": false, + "schema": { + "type": "array", + "items": { + "type": "string" + } + } + } + ], "x-sdk-interface": "accounts.listAccounts" }, "post": { @@ -1318,6 +1341,82 @@ "x-sdk-interface": "onPrem.runtimeEnvs.account.list" } }, + "/admin/runtime-environments/account/modify/{name}": { + "put": { + "x-action": "addRuntimeEnvToAccountByAdmin", + "x-entityId": { + "pathName": "params.name" + }, + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + } + }, + "responses": { + "200": { + "$ref": "#/components/responses/Json" + } + }, + "tags": [ + "onPrem.runtimeEnvs.account" + ], + "operationId": "onPrem-runtimeEnvs-account-modify", + "parameters": [ + { + "in": "path", + "name": "name", + "schema": { + "type": "string" + }, + "required": true, + "description": "Runtime environment name" + } + ], + "summary": "Add to account", + "x-sdk-interface": "onPrem.runtimeEnvs.account.modify" + }, + "delete": { + "x-action": "removeRuntimeEnvFromAccountByAdmin", + "x-entityId": { + "pathName": "params.name" + }, + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + } + }, + "responses": { + "200": { + "$ref": "#/components/responses/Json" + } + }, + "tags": [ + "onPrem.runtimeEnvs.account" + ], + "operationId": "onPrem-runtimeEnvs-account-delete", + "parameters": [ + { + "in": "path", + "name": "name", + "schema": { + "type": "string" + }, + "required": true, + "description": "Runtime environment name" + } + ], + "summary": "Delete for account", + "x-sdk-interface": "onPrem.runtimeEnvs.account.delete" + } + }, "/admin/runtime-environments/default/{plan}/{name}": { "put": { "responses": { @@ -8408,6 +8507,18 @@ } }, "components": { + "responses": { + "Json": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Json" + } + } + }, + "description": "json" + } + }, "parameters": { "accountName": { "in": "path", @@ -8523,6 +8634,9 @@ } } } + }, + "Json": { + "type": "object" } }, "requestBodies": { @@ -8669,7 +8783,7 @@ "content": { "application/json": { "schema": { - "$ref": "#/components/schemas/requestBody" + "$ref": "#/components/schemas/Json" } } } diff --git a/package.json b/package.json index f7abfbe49..2e5619d66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codefresh", - "version": "0.86.0", + "version": "0.87.0", "description": "Codefresh command line utility", "main": "index.js", "preferGlobal": true, @@ -115,4 +115,4 @@ "./test-setup.js" ] } -} \ No newline at end of file +}