Skip to content

Commit aff2fed

Browse files
Add runtime environment (#196)
1 parent 27ddaec commit aff2fed

File tree

11 files changed

+196
-5
lines changed

11 files changed

+196
-5
lines changed

lib/interface/cli/Command.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Command {
1414
this.requiresAuthentication = command.requiresAuthentication;
1515
}
1616
this.betaCommand = command.betaCommand || false;
17+
this.onPremCommand = command.onPremCommand || false;
1718

1819
this.handler = command.handler;
1920
this.command = command.command;
@@ -73,7 +74,14 @@ class Command {
7374
if (currentContext && currentContext.isBetaFeatEnabled()) {
7475
yargs.command(subCommand.toCommand());
7576
}
76-
} else {
77+
} else if (subCommand.isOnPremCommand()) {
78+
// load onPrem commands only if authentication exists and it is onPrem enabled
79+
const currentContext = authManager.getCurrentContext();
80+
if (currentContext && currentContext.isOnPremFeatEnabled()) {
81+
yargs.command(subCommand.toCommand());
82+
}
83+
}
84+
else {
7785
yargs.command(subCommand.toCommand());
7886
}
7987
});
@@ -210,7 +218,8 @@ class Command {
210218
description += choicesString;
211219
}
212220
const defaultValue = positional.value.default || '';
213-
res.positionals += `${key} | ${defaultValue} | ${description}\n`;
221+
const alias = positional.value.alias || '';
222+
res.positionals += `${key} | ${alias} | ${defaultValue} | ${description}\n`;
214223
});
215224

216225
res.options = _.get(parentCommandDocs, 'options', {});
@@ -253,6 +262,10 @@ class Command {
253262
return this.betaCommand;
254263
}
255264

265+
isOnPremCommand() {
266+
return this.onPremCommand;
267+
}
268+
256269
addBuilder(b) {
257270
this.builders.push(b);
258271
}

lib/interface/cli/commands/auth/create-context.cmd.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ const command = new Command({
6060
},
6161
handler: async (argv) => {
6262
const authContext = await _loginWithToken(argv.url, argv['api-key']);
63+
const userData = await authContext.validate();
64+
const roles = _.get(userData,'roles');
6365

64-
await authContext.validate();
66+
if (roles.indexOf('Admin') > -1) {
67+
authContext.onPrem = true;
68+
}
6569

6670
if (argv.name) {
6771
authContext.setName(argv.name);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const debug = require('debug')('codefresh:cli:create:pipelines2');
2+
const Command = require('../../Command');
3+
const _ = require('lodash');
4+
const { runtimeEnvironments } = require('../../../../logic/index').api;
5+
6+
const getRoot = require('../root/get.cmd');
7+
8+
9+
const command = new Command({
10+
command: 'runtime-environments [name] [version]',
11+
aliases: ['re', 'runtime-environment'],
12+
parent: getRoot,
13+
description: 'Get a runtime environments configuration',
14+
onPremCommand: true,
15+
webDocs: {
16+
category: 'Runtime-Environments (On Prem)',
17+
title: 'Get Runtime-Environments',
18+
},
19+
builder: (yargs) => {
20+
return yargs
21+
.positional('name', {
22+
describe: 'Runtime environments name',
23+
default: 'default',
24+
})
25+
.positional('version', {
26+
describe: 'Runtime environments version',
27+
})
28+
.option('account', {
29+
describe: 'Return a specific account configuration from the current runtime environments by his name',
30+
})
31+
.option('history', {
32+
describe: 'Return all the history of the specific runtime environments',
33+
type: 'boolean',
34+
});
35+
},
36+
handler: async (argv) => {
37+
const { name, version, history, account } = argv;
38+
let currruntimeEnvironments;
39+
if (account) {
40+
currruntimeEnvironments = await runtimeEnvironments.getRuntimeEvironment(account);
41+
} else {
42+
currruntimeEnvironments = await runtimeEnvironments.getRuntimeEvironmentsByName({
43+
name,
44+
version,
45+
history,
46+
});
47+
}
48+
console.log(JSON.stringify(currruntimeEnvironments, null, '\t'));
49+
},
50+
});
51+
52+
module.exports = command;
53+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const debug = require('debug')('codefresh:cli:replace:context');
2+
const Command = require('../../Command');
3+
const CFError = require('cf-errors');
4+
const _ = require('lodash');
5+
const { runtimeEnvironments } = require('../../../../logic/index').api;
6+
const replaceRoot = require('../root/replace.cmd');
7+
8+
9+
const command = new Command({
10+
command: 'runtime-environments name',
11+
aliases: ['re','runtime-environment'],
12+
parent: replaceRoot,
13+
onPremCommand: true,
14+
description: 'Replace a runtime-environments resource',
15+
webDocs: {
16+
category: 'Runtime-Environments (On Prem)',
17+
title: 'Replace Runtime-Environments',
18+
},
19+
builder: (yargs) => {
20+
return yargs
21+
.positional('name', {
22+
default: 'default',
23+
describe: 'Runtime environments name',
24+
});
25+
},
26+
handler: async (argv) => {
27+
const data = argv.filename;
28+
const name = argv.name;
29+
30+
await runtimeEnvironments.replaceByName(name, data);
31+
32+
console.log(`Runtime-Environments: ${name} replaced`);
33+
},
34+
});
35+
36+
37+
module.exports = command;
38+

lib/interface/cli/commands/workflow/get.cmd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const command = new Command({
5353
})
5454
.example('codefresh get build ID', 'Get build ID')
5555
.example('codefresh get builds', 'Get all builds')
56-
.example('codefresh bet builds --pipeline-id ID', 'Get all builds that are executions of pipeline "ID"')
56+
.example('codefresh get builds --pipeline-id ID', 'Get all builds that are executions of pipeline "ID"')
5757
.example('codefresh get builds --pipeline-name NAME', 'Get all builds that are executions of pipelines that are named "NAME"')
5858
.example('codefresh get builds --status=error', 'Get all builds that their status is error');
5959

lib/logic/api/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const workflow = require('./workflow');
99
const log = require('./log');
1010
const version = require('./version');
1111
const helm = require('./helm');
12+
const runtimeEnvironments = require('./runtimeEnvironments');
1213

1314
module.exports = {
1415
user,
@@ -22,4 +23,5 @@ module.exports = {
2223
log,
2324
version,
2425
helm,
26+
runtimeEnvironments,
2527
};

lib/logic/api/runtimeEnvironments.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const _ = require('lodash');
2+
const { sendHttpRequest } = require('./helper');
3+
const RuntimeEnvironments = require('../entities/RuntimeEnvironments');
4+
5+
6+
const _extractFieldsForRuntimeEnvironmentsEntity = (runtimeEnvironments) => {
7+
return {
8+
id: runtimeEnvironments.id,
9+
name: runtimeEnvironments.name,
10+
version: runtimeEnvironments.version,
11+
userName : runtimeEnvironments.userName,
12+
runtimeEnvironments: runtimeEnvironments.runtimeEnvironments,
13+
history : runtimeEnvironments.history,
14+
};
15+
};
16+
17+
18+
const getRuntimeEvironmentsByName = async (option) => {
19+
const qs = {
20+
name: option.name,
21+
version: option.version,
22+
history: option.history,
23+
};
24+
const options = {
25+
url: '/api/admin/runtime-environment-manager/runtimeEnvironmentsName',
26+
method: 'GET',
27+
qs,
28+
};
29+
const runtimeEnvironments = await sendHttpRequest(options);
30+
return runtimeEnvironments;
31+
};
32+
33+
const getRuntimeEvironment = async (runtimeEnvironmentName) => {
34+
const options = {
35+
url: `/api/admin/runtime-environment-manager/runtimeEnvironment/${runtimeEnvironmentName}`,
36+
method: 'GET',
37+
};
38+
const runtimeEnvironment = await sendHttpRequest(options);
39+
return runtimeEnvironment;
40+
};
41+
42+
const replaceByName = async (runtimeEnvironmentName, data) => {
43+
const body = data;
44+
45+
const options = {
46+
url: `/api/admin/runtime-environment-manager/${runtimeEnvironmentName}`,
47+
method: 'POST',
48+
body,
49+
};
50+
51+
return sendHttpRequest(options);
52+
};
53+
54+
55+
module.exports = {
56+
getRuntimeEvironmentsByName,
57+
replaceByName,
58+
getRuntimeEvironment,
59+
};

lib/logic/auth/contexts/APIKeyContext.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class APIKeyContext extends Context {
1313
this.name = options.name || this.name;
1414
this.defaultColumns = ['current', 'name', 'url', 'account', 'status'];
1515
this.beta = options.beta || this.beta;
16+
this.onPrem = options.onPrem || this.onPrem;
1617
}
1718

1819
prepareHttpOptions() {
@@ -26,7 +27,7 @@ class APIKeyContext extends Context {
2627

2728
async validate() {
2829
try {
29-
await api.user.getByAuthContext(this);
30+
return await api.user.getByAuthContext(this);
3031
} catch (err) {
3132
// TODO catch 401 errors, etc...
3233
throw new CFError({
@@ -44,6 +45,7 @@ class APIKeyContext extends Context {
4445
const data = {
4546
token: this.token,
4647
beta: this.beta,
48+
onPrem: this.onPrem,
4749
};
4850

4951
return _.assignIn(super.serialize(), data);
@@ -55,6 +57,7 @@ class APIKeyContext extends Context {
5557
url: rawContext.url,
5658
token: rawContext.token,
5759
beta: rawContext.beta,
60+
onPrem: rawContext.onPrem,
5861
});
5962
}
6063

lib/logic/auth/contexts/Context.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ class Context {
77
this.name = options.name || uuidv4();
88
this.url = options.url;
99
this.beta = false;
10+
this.onPrem = false;
1011
}
1112

1213
isBetaFeatEnabled() {
1314
return this.beta;
1415
}
1516

17+
isOnPremFeatEnabled() {
18+
return this.onPrem;
19+
}
20+
1621
async addAccountInfo() {
1722
try {
1823
const { name, runtimeEnvironment } = await whoami(this);

lib/logic/auth/contexts/JWTContext.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class JWTContext extends Context {
2020
this.name = options.name || this.name;
2121
this.defaultColumns = ['current', 'name', 'url', 'account', 'status'];
2222
this.beta = options.beta || this.beta;
23+
this.onPrem = options.onPrem || this.onPrem;
2324
}
2425

2526
_reCalculateName() {
@@ -67,6 +68,7 @@ class JWTContext extends Context {
6768
'account-id': this.accountId,
6869
expires: this.expires.getTime() / 1000,
6970
beta: this.beta,
71+
onPrem: this.onPrem,
7072
};
7173

7274
if (this.userName) {
@@ -92,6 +94,7 @@ class JWTContext extends Context {
9294
accountName: rawContext['account-name'],
9395
expires: rawContext.expires,
9496
beta: rawContext.beta,
97+
onPrem : rawContext.onPrem,
9598
});
9699
}
97100

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const Entity = require('./Entity');
2+
3+
class RuntimeEnvironments extends Entity {
4+
constructor(data) {
5+
super();
6+
this.entityType = 'RuntimeEnvironments';
7+
this.info = data;
8+
}
9+
}
10+
11+
module.exports = RuntimeEnvironments;

0 commit comments

Comments
 (0)