Skip to content

Commit d1f90a6

Browse files
CR-19944 -- assign and unassign sys re (#841)
## What ## Why ## Notes
1 parent e1961c8 commit d1f90a6

File tree

7 files changed

+300
-4
lines changed

7 files changed

+300
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const Command = require('../../Command');
2+
3+
const assign = new Command({
4+
root: true,
5+
command: 'assign',
6+
description: 'Assign a resource',
7+
usage: 'codefresh assign --help',
8+
webDocs: {
9+
title: 'Assign',
10+
weight: 70,
11+
},
12+
builder: (yargs) => {
13+
return yargs
14+
.demandCommand(1, 'You need at least one command before moving on');
15+
},
16+
});
17+
18+
module.exports = assign;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const Command = require('../../Command');
2+
3+
const unassign = new Command({
4+
root: true,
5+
command: 'unassign',
6+
description: 'Unassign a resource',
7+
usage: 'codefresh assign --help',
8+
webDocs: {
9+
title: 'Unassign',
10+
weight: 70,
11+
},
12+
builder: (yargs) => {
13+
return yargs
14+
.demandCommand(1, 'You need at least one command before moving on');
15+
},
16+
});
17+
18+
module.exports = unassign;

lib/interface/cli/commands/systemRuntimeEnvironments/apply.cmd.js

+44
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const _ = require('lodash');
44
const applyRoot = require('../root/apply.cmd');
55
const { crudFilenameOption } = require('../../helpers/general');
66
const sysRe = require('../../helpers/sys-re');
7+
const { sdk } = require('../../../../logic');
78

89

910
const command = new Command({
@@ -38,6 +39,18 @@ const command = new Command({
3839
alias: 'd',
3940
describe: 'Description of the new runtime environment',
4041
})
42+
.option('assign-accounts', {
43+
alias: 'a',
44+
type: Boolean,
45+
// default: false,
46+
describe: 'Assign runtime environment to accounts specified in the yaml',
47+
})
48+
.option('unassign-accounts', {
49+
alias: 'u',
50+
type: Boolean,
51+
// default: false,
52+
describe: 'Unassign runtime environment from accounts based on diff with current state',
53+
})
4154
.example('codefresh patch system-runtime-environments -f ./re.yml', 'Apply changes to a system runtime environment');
4255
},
4356
handler: async (argv) => {
@@ -60,6 +73,37 @@ const command = new Command({
6073
} else {
6174
await sysRe.update(options, body);
6275
console.log(`Runtime-Environment: '${options.name}' patched.`);
76+
const yamlAccounts = body.accounts;
77+
if (yamlAccounts) {
78+
if (argv.unassignAccounts) {
79+
const re = await sysRe.get({ ...options, extend: false });
80+
const accounts = _.difference(re.accounts, yamlAccounts);
81+
if (!_.isEmpty(accounts)) {
82+
await sdk.onPrem.runtimeEnvs.account.delete({ name: options.name }, { accounts });
83+
console.log(`Runtime-Environment unassigned from accounts: ${accounts}`);
84+
} else {
85+
console.log('No accounts to unassign');
86+
}
87+
}
88+
89+
if (argv.assignAccounts) {
90+
const re = await sysRe.get({ ...options, extend: false });
91+
const accounts = _.difference(yamlAccounts, re.accounts);
92+
const existing = await sdk.accounts.listAccounts({ _id: accounts });
93+
const nonExisting = _.difference(accounts, existing.map(({id}) => id));
94+
if (!_.isEmpty(nonExisting)) {
95+
throw new CFError({
96+
message: `Accounts do not exist: ${nonExisting}`,
97+
});
98+
}
99+
if (!_.isEmpty(accounts)) {
100+
await sdk.onPrem.runtimeEnvs.account.modify({ name: options.name }, { accounts });
101+
console.log(`Runtime-Environment assigned to accounts: ${accounts}`);
102+
} else {
103+
console.log('No accounts to assign');
104+
}
105+
}
106+
}
63107
}
64108
},
65109
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const CFError = require('cf-errors');
2+
const _ = require('lodash');
3+
const Command = require('../../Command');
4+
const assignRoot = require('../root/assign.cmd');
5+
const { crudFilenameOption } = require('../../helpers/general');
6+
const { sdk } = require('../../../../logic');
7+
8+
const command = new Command({
9+
command: 'system-runtime-environments <name> <accounts..>',
10+
aliases: ['sys-re', 'system-runtime-environment'],
11+
parent: assignRoot,
12+
description: 'Assign system-runtime-environments to accounts',
13+
onPremCommand: true,
14+
usage: 'Use assign to add runtime environments to accounts by their name or id',
15+
webDocs: {
16+
title: 'Assign System Runtime-Environments',
17+
weight: 90,
18+
},
19+
builder: (yargs) => {
20+
crudFilenameOption(yargs);
21+
return yargs
22+
.positional('name', {
23+
describe: 'Runtime environments name',
24+
required: true,
25+
})
26+
.positional('accounts', {
27+
describe: 'Accounts names',
28+
type: Array,
29+
required: true,
30+
})
31+
.example(
32+
'codefresh assign sys-re my-sys-re acc1 acc2',
33+
'Add system runtime enviroment "my-sys-re" to accounts acc1 and acc2' ,
34+
);
35+
},
36+
handler: async (argv) => {
37+
const { name, accounts: accountsNames } = argv;
38+
const accounts = await sdk.accounts.listAccounts({ name: accountsNames });
39+
const nonExistentAccounts = _.difference(accountsNames, accounts.map((a) => a.name));
40+
if (!_.isEmpty(nonExistentAccounts)) {
41+
throw new CFError({
42+
message: `Accounts do not exist: ${nonExistentAccounts}`,
43+
});
44+
}
45+
const accountIds = accounts.map((a) => a.id);
46+
await sdk.onPrem.runtimeEnvs.account.modify({ name }, { accounts: accountIds });
47+
console.log(`Successfully assigned "${name}" to accounts: ${accountsNames}`);
48+
},
49+
});
50+
51+
module.exports = command;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const CFError = require('cf-errors');
2+
const _ = require('lodash');
3+
const Command = require('../../Command');
4+
const unassignRoot = require('../root/unassign.cmd');
5+
const { crudFilenameOption } = require('../../helpers/general');
6+
const { sdk } = require('../../../../logic');
7+
8+
const command = new Command({
9+
command: 'system-runtime-environments <name> <accounts..>',
10+
aliases: ['sys-re', 'system-runtime-environment'],
11+
parent: unassignRoot,
12+
description: 'Unassign system-runtime-environments from accounts',
13+
onPremCommand: true,
14+
usage: 'Use unassign to remove runtime environments from accounts by their name or id',
15+
webDocs: {
16+
title: 'Unassign System Runtime-Environments',
17+
weight: 90,
18+
},
19+
builder: (yargs) => {
20+
crudFilenameOption(yargs);
21+
return yargs
22+
.positional('name', {
23+
describe: 'Runtime environment name',
24+
required: true,
25+
})
26+
.positional('accounts', {
27+
describe: 'Accounts names',
28+
type: Array,
29+
required: true,
30+
})
31+
.example(
32+
'codefresh unassign sys-re my-sys-re acc1 acc2',
33+
'Remove system runtime enviroment "my-sys-re" from accounts acc1 and acc2',
34+
);
35+
},
36+
handler: async (argv) => {
37+
const { name, accounts: accountsNames } = argv;
38+
const accounts = await sdk.accounts.listAccounts({ name: accountsNames });
39+
const nonExistentAccounts = _.difference(accountsNames, accounts.map((a) => a.name));
40+
if (!_.isEmpty(nonExistentAccounts)) {
41+
throw new CFError({
42+
message: `Accounts do not exist: ${nonExistentAccounts}`,
43+
});
44+
}
45+
const accountIds = accounts.map((a) => a.id);
46+
await sdk.onPrem.runtimeEnvs.account.delete({ name }, { accounts: accountIds });
47+
console.log(`Successfully removed "${name}" from accounts: ${accountsNames}`);
48+
},
49+
});
50+
51+
module.exports = command;

openapi.json

+116-2
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,30 @@
10361036
"description": "get all the accounts in the system",
10371037
"operationId": "accounts-list-accounts",
10381038
"summary": "List accounts",
1039-
"parameters": [],
1039+
"parameters": [
1040+
{
1041+
"name": "name",
1042+
"in": "query",
1043+
"required": false,
1044+
"schema": {
1045+
"type": "array",
1046+
"items": {
1047+
"type": "string"
1048+
}
1049+
}
1050+
},
1051+
{
1052+
"name": "_id",
1053+
"in": "query",
1054+
"required": false,
1055+
"schema": {
1056+
"type": "array",
1057+
"items": {
1058+
"type": "string"
1059+
}
1060+
}
1061+
}
1062+
],
10401063
"x-sdk-interface": "accounts.listAccounts"
10411064
},
10421065
"post": {
@@ -1318,6 +1341,82 @@
13181341
"x-sdk-interface": "onPrem.runtimeEnvs.account.list"
13191342
}
13201343
},
1344+
"/admin/runtime-environments/account/modify/{name}": {
1345+
"put": {
1346+
"x-action": "addRuntimeEnvToAccountByAdmin",
1347+
"x-entityId": {
1348+
"pathName": "params.name"
1349+
},
1350+
"requestBody": {
1351+
"content": {
1352+
"application/json": {
1353+
"schema": {
1354+
"type": "object"
1355+
}
1356+
}
1357+
}
1358+
},
1359+
"responses": {
1360+
"200": {
1361+
"$ref": "#/components/responses/Json"
1362+
}
1363+
},
1364+
"tags": [
1365+
"onPrem.runtimeEnvs.account"
1366+
],
1367+
"operationId": "onPrem-runtimeEnvs-account-modify",
1368+
"parameters": [
1369+
{
1370+
"in": "path",
1371+
"name": "name",
1372+
"schema": {
1373+
"type": "string"
1374+
},
1375+
"required": true,
1376+
"description": "Runtime environment name"
1377+
}
1378+
],
1379+
"summary": "Add to account",
1380+
"x-sdk-interface": "onPrem.runtimeEnvs.account.modify"
1381+
},
1382+
"delete": {
1383+
"x-action": "removeRuntimeEnvFromAccountByAdmin",
1384+
"x-entityId": {
1385+
"pathName": "params.name"
1386+
},
1387+
"requestBody": {
1388+
"content": {
1389+
"application/json": {
1390+
"schema": {
1391+
"type": "object"
1392+
}
1393+
}
1394+
}
1395+
},
1396+
"responses": {
1397+
"200": {
1398+
"$ref": "#/components/responses/Json"
1399+
}
1400+
},
1401+
"tags": [
1402+
"onPrem.runtimeEnvs.account"
1403+
],
1404+
"operationId": "onPrem-runtimeEnvs-account-delete",
1405+
"parameters": [
1406+
{
1407+
"in": "path",
1408+
"name": "name",
1409+
"schema": {
1410+
"type": "string"
1411+
},
1412+
"required": true,
1413+
"description": "Runtime environment name"
1414+
}
1415+
],
1416+
"summary": "Delete for account",
1417+
"x-sdk-interface": "onPrem.runtimeEnvs.account.delete"
1418+
}
1419+
},
13211420
"/admin/runtime-environments/default/{plan}/{name}": {
13221421
"put": {
13231422
"responses": {
@@ -8408,6 +8507,18 @@
84088507
}
84098508
},
84108509
"components": {
8510+
"responses": {
8511+
"Json": {
8512+
"content": {
8513+
"application/json": {
8514+
"schema": {
8515+
"$ref": "#/components/schemas/Json"
8516+
}
8517+
}
8518+
},
8519+
"description": "json"
8520+
}
8521+
},
84118522
"parameters": {
84128523
"accountName": {
84138524
"in": "path",
@@ -8523,6 +8634,9 @@
85238634
}
85248635
}
85258636
}
8637+
},
8638+
"Json": {
8639+
"type": "object"
85268640
}
85278641
},
85288642
"requestBodies": {
@@ -8669,7 +8783,7 @@
86698783
"content": {
86708784
"application/json": {
86718785
"schema": {
8672-
"$ref": "#/components/schemas/requestBody"
8786+
"$ref": "#/components/schemas/Json"
86738787
}
86748788
}
86758789
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.86.0",
3+
"version": "0.87.0",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,
@@ -115,4 +115,4 @@
115115
"./test-setup.js"
116116
]
117117
}
118-
}
118+
}

0 commit comments

Comments
 (0)