Skip to content

Commit 3a23be4

Browse files
Saas 4802 (#388)
*Introduce agent crud
1 parent 8f2a737 commit 3a23be4

File tree

7 files changed

+429
-2
lines changed

7 files changed

+429
-2
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const Command = require('../../Command');
2+
const CFError = require('cf-errors');
3+
const { sdk } = require('../../../../logic');
4+
5+
const applyRoot = require('../root/apply.cmd');
6+
const { ignoreHttpError } = require('../../helpers/general');
7+
8+
const command = new Command({
9+
command: 'agent [id|name]',
10+
aliases: [],
11+
parent: applyRoot,
12+
description: 'Patch an agent',
13+
webDocs: {
14+
category: 'Agents',
15+
title: 'Patch Agent',
16+
},
17+
builder: (yargs) => {
18+
yargs
19+
.positional('id|name', {
20+
describe: 'agent id or name',
21+
})
22+
.option('newName', {
23+
describe: 'New name',
24+
alias: 'n',
25+
})
26+
.option('runtimes', {
27+
array: true,
28+
alias: 're',
29+
describe: 'Agent runtimes',
30+
})
31+
.example(
32+
'codefresh patch agent ID --name NEW_NAME',
33+
'Replace agent name. Specifying agent by ID',
34+
)
35+
.example(
36+
'codefresh patch agent NAME --re runtime1',
37+
'Replace agent runtimes. Specifying agent by NAME',
38+
);
39+
40+
return yargs;
41+
},
42+
handler: async (argv) => {
43+
const { id, name } = argv;
44+
45+
const {
46+
newName,
47+
runtimes,
48+
} = argv;
49+
50+
let agent = await sdk.agents.get({ agentId: id }).catch(ignoreHttpError);
51+
agent = agent || await sdk.agents.getByName({ name }).catch(ignoreHttpError);
52+
if (!agent) {
53+
throw new CFError(`No such agent: "${name || id}"`);
54+
}
55+
56+
await sdk.agents.replace({ agentId: agent.id }, { name: newName, runtimes });
57+
console.log(`Agent: "${name || id}" patched.`);
58+
},
59+
});
60+
61+
module.exports = command;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const Command = require('../../Command');
2+
const { sdk } = require('../../../../logic');
3+
const createRoot = require('../root/create.cmd');
4+
5+
6+
const command = new Command({
7+
command: 'agent <name>',
8+
parent: createRoot,
9+
description: 'Create an agent',
10+
usage: 'Create an agent used to behind firewall integration.',
11+
webDocs: {
12+
category: 'Agents',
13+
title: 'Create Agent',
14+
},
15+
builder: yargs => yargs
16+
.positional('name', {
17+
describe: 'Name of agent',
18+
})
19+
.option('runtimes', {
20+
array: true,
21+
alias: 'r',
22+
describe: 'Agent runtimes',
23+
default: [],
24+
})
25+
.example('codefresh create agent NAME', 'Create an agent')
26+
.example('codefresh create agent NAME -r runtime1 -r runtime2', 'Create an agent with runtimes: [ "runtime1", "runtime2"]'),
27+
handler: async (argv) => {
28+
const {
29+
name,
30+
runtimes,
31+
} = argv;
32+
33+
const agent = await sdk.agents.create({ name, runtimes });
34+
console.log(`Agent: "${agent.name}" created with token ${agent.token}`);
35+
},
36+
});
37+
38+
module.exports = command;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const Command = require('../../Command');
2+
const CFError = require('cf-errors');
3+
const { sdk } = require('../../../../logic');
4+
const deleteRoot = require('../root/delete.cmd');
5+
const { ignoreHttpError } = require('../../helpers/general');
6+
7+
const command = new Command({
8+
command: 'agent <id|name>',
9+
aliases: [],
10+
parent: deleteRoot,
11+
description: 'Delete an agent',
12+
webDocs: {
13+
category: 'Agemts',
14+
title: 'Delete Agent',
15+
},
16+
builder: (yargs) => {
17+
yargs
18+
.positional('id|name', {
19+
describe: 'agent id or name',
20+
})
21+
.example('codefresh delete agent NAME', 'Delete agent by name.')
22+
.example('codefresh delete agent ID', 'Delete agent by Id.');
23+
return yargs;
24+
},
25+
handler: async (argv) => {
26+
const { id, name } = argv;
27+
28+
let agent = await sdk.agents.get({ agentId: id }).catch(ignoreHttpError);
29+
agent = agent || await sdk.agents.getByName({ name }).catch(ignoreHttpError);
30+
if (!agent) {
31+
throw new CFError(`No such agent: "${name || id}"`);
32+
}
33+
34+
await sdk.agents.delete({ agentId: agent.id });
35+
console.log(`Agent '${name || id}' deleted.`);
36+
},
37+
});
38+
39+
module.exports = command;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const Command = require('../../Command');
2+
const { sdk } = require('../../../../logic');
3+
const Agent = require('../../../../logic/entities/Agent');
4+
const Output = require('../../../../output/Output');
5+
const _ = require('lodash');
6+
const { ignoreHttpError } = require('../../helpers/general');
7+
8+
const getRoot = require('../root/get.cmd');
9+
10+
const command = new Command({
11+
command: 'agents [id|name]',
12+
aliases: ['agent'],
13+
parent: getRoot,
14+
description: 'Get a specific agent or an array of agents',
15+
webDocs: {
16+
category: 'Agents',
17+
title: 'Get Agents',
18+
},
19+
builder: yargs => yargs
20+
.positional('id|name', {
21+
describe: 'Agent id or name to get one agent',
22+
})
23+
.option('name', {
24+
alias: 'n',
25+
describe: 'Agent name to filter by',
26+
}),
27+
handler: async (argv) => {
28+
const { id, name } = argv; // eslint-disable-line
29+
30+
let agents;
31+
if (id) {
32+
let agent = await sdk.agents.get({ agentId: id }).catch(ignoreHttpError);
33+
agent = agent || await sdk.agents.getByName({ name }).catch(ignoreHttpError);
34+
agents = agent ? [agent] : [];
35+
} else {
36+
agents = await sdk.agents.list({
37+
name,
38+
});
39+
}
40+
Output.print(_.map(agents, Agent.fromResponse));
41+
},
42+
});
43+
44+
module.exports = command;

lib/logic/entities/Agent.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const _ = require('lodash');
2+
const Entity = require('./Entity');
3+
4+
class Agent extends Entity {
5+
constructor(data) {
6+
super();
7+
this.entityType = 'agent';
8+
this.id = data.id;
9+
this.name = data.name;
10+
this.runtimes = data.runtimes;
11+
this.defaultColumns = ['name', 'runtimes'];
12+
}
13+
14+
static fromResponse(response) {
15+
return new Agent(_.pick(response, 'id', 'name', 'runtimes'));
16+
}
17+
}
18+
19+
module.exports = Agent;
20+

0 commit comments

Comments
 (0)