From 191bb4e106bcff1a6be016e5a195dad1a764bb67 Mon Sep 17 00:00:00 2001 From: yaroslav-codefresh Date: Thu, 7 Sep 2023 19:41:47 +0300 Subject: [PATCH 1/3] add ability to assign and unassign accounts --- lib/interface/cli/commands/root/assign.cmd.js | 18 +++ .../cli/commands/root/unassign.cmd.js | 18 +++ .../systemRuntimeEnvironments/apply.cmd.js | 33 ++++++ .../systemRuntimeEnvironments/assign.cmd.js | 51 +++++++++ .../systemRuntimeEnvironments/unassign.cmd.js | 51 +++++++++ openapi.json | 107 +++++++++++++++++- 6 files changed, 276 insertions(+), 2 deletions(-) create mode 100644 lib/interface/cli/commands/root/assign.cmd.js create mode 100644 lib/interface/cli/commands/root/unassign.cmd.js create mode 100644 lib/interface/cli/commands/systemRuntimeEnvironments/assign.cmd.js create mode 100644 lib/interface/cli/commands/systemRuntimeEnvironments/unassign.cmd.js 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..8dcaafaa6 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,14 @@ const command = new Command({ alias: 'd', describe: 'Description of the new runtime environment', }) + .option('assign-accounts', { + alias: 'a', + describe: 'Assign runtime environment to accounts specified in the yaml', + }) + .option('unassign-accounts', { + alias: 'u', + 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 +69,30 @@ 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); + 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..594bef074 100644 --- a/openapi.json +++ b/openapi.json @@ -1036,7 +1036,19 @@ "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" + } + } + } + ], "x-sdk-interface": "accounts.listAccounts" }, "post": { @@ -1318,6 +1330,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 +8496,18 @@ } }, "components": { + "responses": { + "Json": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Json" + } + } + }, + "description": "json" + } + }, "parameters": { "accountName": { "in": "path", @@ -8523,6 +8623,9 @@ } } } + }, + "Json": { + "type": "object" } }, "requestBodies": { @@ -8669,7 +8772,7 @@ "content": { "application/json": { "schema": { - "$ref": "#/components/schemas/requestBody" + "$ref": "#/components/schemas/Json" } } } From 5fe7b44f2a9a3883f76abd68bafc3275c9d25bf8 Mon Sep 17 00:00:00 2001 From: yaroslav-codefresh Date: Thu, 7 Sep 2023 19:42:05 +0300 Subject: [PATCH 2/3] bump version --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 +} From d2289df2a16fe5ad957a63a1d2977464ec8b08ca Mon Sep 17 00:00:00 2001 From: yaroslav-codefresh Date: Mon, 11 Sep 2023 16:19:02 +0300 Subject: [PATCH 3/3] add account check --- .../commands/systemRuntimeEnvironments/apply.cmd.js | 11 +++++++++++ openapi.json | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/interface/cli/commands/systemRuntimeEnvironments/apply.cmd.js b/lib/interface/cli/commands/systemRuntimeEnvironments/apply.cmd.js index 8dcaafaa6..20e7412fc 100644 --- a/lib/interface/cli/commands/systemRuntimeEnvironments/apply.cmd.js +++ b/lib/interface/cli/commands/systemRuntimeEnvironments/apply.cmd.js @@ -41,10 +41,14 @@ const command = new Command({ }) .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'); @@ -85,6 +89,13 @@ const command = new Command({ 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}`); diff --git a/openapi.json b/openapi.json index 594bef074..d3932dac6 100644 --- a/openapi.json +++ b/openapi.json @@ -1047,6 +1047,17 @@ "type": "string" } } + }, + { + "name": "_id", + "in": "query", + "required": false, + "schema": { + "type": "array", + "items": { + "type": "string" + } + } } ], "x-sdk-interface": "accounts.listAccounts"