Skip to content

CR-19944 -- assign and unassign sys re #841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/interface/cli/commands/root/assign.cmd.js
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 18 additions & 0 deletions lib/interface/cli/commands/root/unassign.cmd.js
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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) => {
Expand All @@ -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');
}
}
}
}
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <name> <accounts..>',
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;
Original file line number Diff line number Diff line change
@@ -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 <name> <accounts..>',
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;
107 changes: 105 additions & 2 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -8408,6 +8496,18 @@
}
},
"components": {
"responses": {
"Json": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Json"
}
}
},
"description": "json"
}
},
"parameters": {
"accountName": {
"in": "path",
Expand Down Expand Up @@ -8523,6 +8623,9 @@
}
}
}
},
"Json": {
"type": "object"
}
},
"requestBodies": {
Expand Down Expand Up @@ -8669,7 +8772,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/requestBody"
"$ref": "#/components/schemas/Json"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codefresh",
"version": "0.86.0",
"version": "0.87.0",
"description": "Codefresh command line utility",
"main": "index.js",
"preferGlobal": true,
Expand Down Expand Up @@ -115,4 +115,4 @@
"./test-setup.js"
]
}
}
}