Skip to content

Commit ffb7603

Browse files
crud for team (#213)
* crud for team * bit-156 cli for crud teams, fix use default -f for load file, use only existing capability of cf-api * crud for team (add synchronise) * saas-156 menage teams, change version
1 parent 5680100 commit ffb7603

File tree

11 files changed

+309
-2
lines changed

11 files changed

+309
-2
lines changed

docs/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const categoriesOrder = {
2626
compositions : 110 ,
2727
'helm repos' : 111 ,
2828
'predefined pipelines': 120,
29-
more : 130 ,
29+
teams: 130,
30+
more : 140,
3031
};
3132

3233

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const Command = require('../../Command');
2+
3+
const annotate = new Command({
4+
root: true,
5+
command: 'synchronize',
6+
description: 'Synchronize a resource',
7+
webDocs: {
8+
title: 'synchronize',
9+
weight: 100,
10+
},
11+
builder: (yargs) => {
12+
return yargs
13+
.demandCommand(1, 'You need at least one command before moving on');
14+
},
15+
});
16+
17+
module.exports = annotate;
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 { team } = require('../../../../logic').api;
3+
const applyRoot = require('../root/apply.cmd');
4+
5+
6+
const command = new Command({
7+
command: 'teams',
8+
aliases: ['team', 'tm'],
9+
parent: applyRoot,
10+
description: 'Assign user to a team',
11+
usage: 'Use patch command and pass user id and team id to assign user to a team',
12+
webDocs: {
13+
category: 'Teams',
14+
title: 'Add user',
15+
},
16+
builder: (yargs) => {
17+
return yargs
18+
.option('team-id', {
19+
describe: 'Id team in which to add a user',
20+
alias: 't',
21+
required: true,
22+
})
23+
.option('user-id', {
24+
describe: 'Id of a user to add him in team',
25+
alias: 'u',
26+
required: true,
27+
})
28+
.example('codefresh patch tm -u [userID] -t [teamID]', 'Assign user to team');
29+
},
30+
handler: async (argv) => {
31+
await team.assignUserToTeam(argv['user-id'], argv['team-id']);
32+
console.log(`User: ${argv['user-id']} was assigned to team: ${argv['team-id']}`);
33+
},
34+
});
35+
36+
37+
module.exports = command;
38+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const Command = require('../../Command');
2+
const { team } = require('../../../../logic').api;
3+
const createRoot = require('../root/create.cmd');
4+
5+
6+
const command = new Command({
7+
command: 'teams <name>',
8+
aliases: ['team', 'tm'],
9+
parent: createRoot,
10+
description: 'Create a team',
11+
usage: 'You can create a new team specifying only name of team or specify path to json file for include extended options' +
12+
'\n Possible fields in file:' +
13+
'\n name - team name (specifying name in file will rewrite name specified as command argument) ' +
14+
'\n users - array of users id for that team ' +
15+
'\n tags - array of tag names',
16+
webDocs: {
17+
category: 'Teams',
18+
title: 'Create team',
19+
},
20+
builder: (yargs) => {
21+
return yargs
22+
.positional('name', {
23+
describe: 'Name of team',
24+
})
25+
.example('codefresh create team NAME', 'Creating a team specifying only team name')
26+
.example('codefresh create team NAME -f [pathToFile]', 'Creating a team using a file to specify additional options');
27+
},
28+
handler: async (argv) => {
29+
const reqBody = Object.assign({ name: argv.name }, argv.f);
30+
31+
await team.createTeam(reqBody);
32+
console.log(`Team: ${reqBody.name} created`);
33+
},
34+
});
35+
36+
37+
module.exports = command;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const Command = require('../../Command');
2+
const { team } = require('../../../../logic').api;
3+
const { specifyOutputForArray } = require('../../helpers/get');
4+
const getRoot = require('../root/get.cmd');
5+
6+
7+
const command = new Command({
8+
command: 'teams [id|name..]',
9+
aliases: ['team', 'tm'],
10+
parent: getRoot,
11+
description: 'Get an array of all current user teams, with specifying user-id get all teams for that user',
12+
webDocs: {
13+
category: 'Teams',
14+
title: 'Get Teams',
15+
},
16+
builder: (yargs) => {
17+
return yargs
18+
.option('user-id', {
19+
describe: 'User id',
20+
alias: 'u',
21+
required: false,
22+
})
23+
.example('codefresh get teams', 'Get all teams for current user')
24+
.example('codefresh get teams -u [userID]', 'Get all teams for specific user');
25+
},
26+
handler: async (argv) => {
27+
if (argv['user-id']) {
28+
specifyOutputForArray(argv.output, await team.getTeamsByUserId(argv.id));
29+
} else {
30+
specifyOutputForArray(argv.output, await team.getTeamsForCurrentUser());
31+
}
32+
},
33+
});
34+
35+
36+
module.exports = command;
37+
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 { team } = require('../../../../logic').api;
3+
const deleteRoot = require('../root/delete.cmd');
4+
5+
6+
const command = new Command({
7+
command: 'teams',
8+
aliases: ['team', 'tm'],
9+
parent: deleteRoot,
10+
description: 'Remove user from team',
11+
usage: 'Pass user id and team id to remove user from team',
12+
webDocs: {
13+
category: 'Teams',
14+
title: 'Remove user',
15+
},
16+
builder: (yargs) => {
17+
return yargs
18+
.option('team-id', {
19+
describe: 'Id team in which to remove a user',
20+
alias: 't',
21+
required: true,
22+
})
23+
.option('user-id', {
24+
describe: 'Id of a user to remove him in team',
25+
alias: 'u',
26+
required: true,
27+
})
28+
.example('codefresh delete tm -u [userID] -t [teamID]', 'Remove user from team');
29+
},
30+
handler: async (argv) => {
31+
await team.removeUserFromTeam(argv['team-id'], argv['user-id']);
32+
console.log(`User: ${argv['user-id']} was removed from team: ${argv['team-id']}`);
33+
},
34+
});
35+
36+
37+
module.exports = command;
38+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const Command = require('../../Command');
2+
const { team } = require('../../../../logic').api;
3+
const syncRoot = require('../root/synchronize.cmd');
4+
5+
const command = new Command({
6+
command: 'teams client-name',
7+
aliases: ['team', 'tm'],
8+
parent: syncRoot,
9+
description: 'Synchronize team with group',
10+
webDocs: {
11+
category: 'Teams',
12+
title: 'Synchronize Teams',
13+
},
14+
builder: (yargs) => {
15+
return yargs
16+
.option('client-type', {
17+
describe: 'Client type like github',
18+
alias: 't',
19+
required: true,
20+
})
21+
.example('codefresh synchronize teams [clientName] -t [clientType]', 'Synchronize team with group');
22+
},
23+
handler: async (argv) => {
24+
console.log(JSON.stringify(await team.synchronizeClientWithGroup(argv['client-name'], argv.t), null, 2));
25+
},
26+
});
27+
28+
29+
module.exports = command;
30+

lib/logic/api/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const log = require('./log');
1010
const version = require('./version');
1111
const helm = require('./helm');
1212
const runtimeEnvironments = require('./runtimeEnvironments');
13+
const team = require('./team');
1314

1415
module.exports = {
1516
user,
@@ -24,4 +25,5 @@ module.exports = {
2425
version,
2526
helm,
2627
runtimeEnvironments,
28+
team,
2729
};

lib/logic/api/team.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const _ = require('lodash');
2+
const { sendHttpRequest } = require('./helper');
3+
const Team = require('../entities/Team');
4+
5+
6+
const _extractFieldsForTeamEntity = team => ({
7+
id: team._id,
8+
name: team.name,
9+
type: team.type,
10+
account: team.account,
11+
tags: team.tags,
12+
users: team.users.map(({ id, userName }) => ({ id, userName }))
13+
});
14+
15+
16+
const createTeam = async (data) => {
17+
const options = {
18+
url: '/api/team/',
19+
method: 'POST',
20+
body: data,
21+
};
22+
23+
return sendHttpRequest(options);
24+
};
25+
26+
const assignUserToTeam = async (userId, teamId) => {
27+
const options = {
28+
url: `/api/team/${teamId}/${userId}/assignUserToTeam`,
29+
method: 'PUT',
30+
};
31+
32+
return sendHttpRequest(options);
33+
};
34+
35+
const getTeamsForCurrentUser = async () => {
36+
const userOptions = {
37+
url: '/api/team/',
38+
method: 'GET',
39+
};
40+
41+
const result = await sendHttpRequest(userOptions);
42+
const teams = [];
43+
let data = {};
44+
_.forEach(result, (team) => {
45+
data = _extractFieldsForTeamEntity(team);
46+
teams.push(new Team(data));
47+
});
48+
49+
return teams;
50+
};
51+
52+
const getTeamsByUserId = async (userId) => {
53+
const userOptions = {
54+
url: `/api/team/${userId}/findTeamsByUser`,
55+
method: 'GET',
56+
};
57+
58+
const result = await sendHttpRequest(userOptions);
59+
const teams = [];
60+
let data = {};
61+
_.forEach(result, (team) => {
62+
data = _extractFieldsForTeamEntity(team);
63+
teams.push(new Team(data));
64+
});
65+
66+
return teams;
67+
};
68+
69+
const removeUserFromTeam = async (teamId, userId) => {
70+
const options = {
71+
url: `/api/team/${teamId}/${userId}/deleteUserFromTeam`,
72+
method: 'PUT',
73+
};
74+
75+
return sendHttpRequest(options);
76+
};
77+
78+
const synchronizeClientWithGroup = async (clientName, clientType) => {
79+
const options = {
80+
url: `/api/team/group/synchronize/name/${clientName}/type/${clientType}`,
81+
method: 'GET',
82+
};
83+
84+
return sendHttpRequest(options);
85+
};
86+
87+
module.exports = {
88+
createTeam,
89+
assignUserToTeam,
90+
removeUserFromTeam,
91+
getTeamsForCurrentUser,
92+
getTeamsByUserId,
93+
synchronizeClientWithGroup,
94+
};

lib/logic/entities/Team.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const Entity = require('./Entity');
2+
3+
class Team extends Entity {
4+
constructor(data) {
5+
super();
6+
this.entityType = 'team';
7+
this.info = data;
8+
this.defaultColumns = ['id', 'name', 'type', 'account', 'tags', 'users'];
9+
this.wideColumns = this.defaultColumns.concat([]);
10+
}
11+
}
12+
13+
module.exports = Team;

0 commit comments

Comments
 (0)