Skip to content

Commit

Permalink
CLI can add and remove users from entities (#7236)
Browse files Browse the repository at this point in the history
Signed-off-by: ClementBouvierN <[email protected]>
  • Loading branch information
ClementBouvierN authored and freddidierRTE committed Sep 30, 2024
1 parent 3a8c006 commit a531260
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 76 deletions.
143 changes: 73 additions & 70 deletions cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,87 @@
* This file is part of the OperatorFabric project.
*/

const omelette = require('omelette'); // For completion
const fs = require('fs');
const omelette = require('omelette');

const commands = require('./commands.js');
const config = require('./configCommands.js');
const args = process.argv.slice(2);

const levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];

omelette('opfab').tree({
bundle: ['load', 'delete'],
businessdata: ['load','delete'],
card: ['send', 'delete','resetratelimiter'],
commands : [],
config: ['set', 'get', 'list'],
connectedusers: {
sendmessage: ['RELOAD', 'BUSINESS_CONFIG_CHANGE', 'USER_CONFIG_CHANGE']
},
entities: ['load'],
groups: ['load'],
help: [
'bundle',
'businessdata',
'card',
'config',
'commands',
'connectedusers',
'entities',
'groups',
'login',
'logout',
'monitoringconfig',
'perimeters',
'processgroups',
'processmonitoring',
'realtimescreen',
'service',
'status',
'users'
],
login: [],
logout: [],
monitoringconfig: ['load', 'delete'],
perimeters: ['create', 'addtogroup', 'delete'],
processgroups: ['load','clear'],
processmonitoring: ['load'],
realtimescreen: ['load'],
service: {
'get-log-level': [
'users',
'businessconfig',
'cards-consultation',
'cards-publication',
'external-devices',
'supervisor',
'cards-external-diffusion',
'cards-reminder'
// Configuration of autocompletion in the CLI
omelette('opfab')
.tree({
bundle: ['load', 'delete'],
businessdata: ['load', 'delete'],
card: ['send', 'delete', 'resetratelimiter'],
commands: [],
config: ['set', 'get', 'list'],
connectedusers: {
sendmessage: ['RELOAD', 'BUSINESS_CONFIG_CHANGE', 'USER_CONFIG_CHANGE']
},
entities: ['load'],
groups: ['load'],
help: [
'bundle',
'businessdata',
'card',
'config',
'commands',
'connectedusers',
'entities',
'groups',
'login',
'logout',
'monitoringconfig',
'perimeters',
'processgroups',
'processmonitoring',
'realtimescreen',
'service',
'status',
'users'
],
'set-log-level': {
users: levels,
businessconfig: levels,
'cards-consultation': levels,
'cards-publication': levels,
'external-devices': levels,
supervisor: levels,
'cards-external-diffusion': levels,
'cards-reminder': levels
}
},
status: [],
users: [
'set-notified',
'set-not-notified',
'set-notified-mail',
'set-not-notified-mail'
]
}).init();
login: [],
logout: [],
monitoringconfig: ['load', 'delete'],
perimeters: ['create', 'addtogroup', 'delete'],
processgroups: ['load', 'clear'],
processmonitoring: ['load'],
realtimescreen: ['load'],
service: {
'get-log-level': [
'users',
'businessconfig',
'cards-consultation',
'cards-publication',
'external-devices',
'supervisor',
'cards-external-diffusion',
'cards-reminder'
],
'set-log-level': {
users: levels,
businessconfig: levels,
'cards-consultation': levels,
'cards-publication': levels,
'external-devices': levels,
supervisor: levels,
'cards-external-diffusion': levels,
'cards-reminder': levels
}
},
status: [],
users: [
'addtoentity',
'removefromentity',
'set-notified',
'set-not-notified',
'set-notified-mail',
'set-not-notified-mail'
]
})
.init();

(async () => {
if (args[0] === undefined) {
Expand All @@ -95,4 +99,3 @@ omelette('opfab').tree({
commands.processCommand(args);
}
})();

2 changes: 1 addition & 1 deletion cli/src/realtimescreenCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const realtimescreenCommands = {
Command list :
load load realtimescreen from a file : opfab realtimescreen load <realtimescreenFileName>
load load realtimescreen from a file : opfab realtimescreen load <realtimescreenFileName>
`);
}
Expand Down
81 changes: 76 additions & 5 deletions cli/src/usersCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

const config = require('./configCommands.js');
const prompts = require('prompts');
const utils = require('./utils.js');

const usersCommands = {
async processUsersCommand(args) {
Expand All @@ -20,6 +21,8 @@ const usersCommands = {
name: 'value',
message: 'Users action',
choices: [
{title: 'Add to entity', value: 'addtoentity'},
{title: 'Remove from entity', value: 'removefromentity'},
{title: 'Set notified', value: 'set-notified'},
{title: 'Set not notified', value: 'set-not-notified'},
{title: 'Set notified by mail', value: 'set-notified-mail'},
Expand All @@ -34,6 +37,12 @@ const usersCommands = {
}

switch (action) {
case 'addtoentity':
await this.addUserTo('Entity', 'entities', args[1], args[2]);
break;
case 'removefromentity':
await this.removeUserFrom('Entity', 'entities', args[1], args[2]);
break;
case 'set-notified':
await this.configureNotification(args[1], args[2], 'POST', 'processstatenotified');
break;
Expand All @@ -54,6 +63,59 @@ const usersCommands = {
}
},

async missingPrompt(object, objectId, user) {
if (!objectId) {
objectId = (
await prompts({
type: 'text',
name: 'value',
message: `${object} `
})
).value;
if (!objectId) {
console.log(`${object} is required`);
return;
}
}
if (!user) {
user = (
await prompts({
type: 'text',
name: 'value',
message: `user `
})
).value;
if (!user) {
console.log(`user is required`);
return;
}
}
return {objectId, user};
},

async addUserTo(object, objectUrl, objectId, user) {
const {objectId: resolvedObjectId, user: resolvedUser} = await this.missingPrompt(object, objectId, user);
await utils.sendRequest(
`users/${objectUrl}/${resolvedObjectId}/users`,
'PATCH',
`["${resolvedUser}"]`,
`User ${resolvedUser} has been added to ${resolvedObjectId}`,
``,
`${object} or user not found`
);
},

async removeUserFrom(object, objectUrl, objectId, user) {
const {objectId: resolvedObjectId, user: resolvedUser} = await this.missingPrompt(object, objectId, user);
await utils.sendRequest(
`users/${objectUrl}/${resolvedObjectId}/users/${resolvedUser}`,
'DELETE',
undefined,
`User ${resolvedUser} has been removed from ${resolvedObjectId}`,
`${object} or user not found`
);
},

async configureNotification(process, state, method, path) {
if (!process) {
process = (
Expand Down Expand Up @@ -82,38 +144,47 @@ const usersCommands = {
}
}

const url = `${config.getConfig('url')}:${config.getConfig('port')}/` + 'users/notificationconfiguration/' + path + '/' + process + '/' + state;
const url =
`${config.getConfig('url')}:${config.getConfig('port')}/` +
'users/notificationconfiguration/' +
path +
'/' +
process +
'/' +
state;
const token = config.getConfig('access_token');
const options = {
method: method,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
Authorization: `Bearer ${token}`
}
};

let response;
try {
response = await fetch(url, options);
} catch (error) {
console.error('Failed to configure process/state notification for ' + process + ' / ' +state);
console.error('Failed to configure process/state notification for ' + process + ' / ' + state);
console.error('Error:', error);
return;
}

if (response.ok) {
console.error('Success');
} else {
console.error('Failed to configure process/state notification for ' + process + '/' +state);
console.error('Failed to configure process/state notification for ' + process + '/' + state);
console.error('Response:', response);
}
},

async printHelp() {
console.log(`Usage: opfab users set-notif <process> <state>
console.log(`Usage: opfab users <command>
Commands list :
addtoentity Add a <user> to an <entity> : opfab users <entityId> <user>
removefromentity Remove a <user> from an <entity> : opfab users <entityId> <user>
set-notified Configure <process>/<state> as to be notified for all users
set-not-notified Configure <process>/<state> as not to be notified for all users
set-notified-mail Configure <process>/<state> as to be notified by email for all users
Expand Down

0 comments on commit a531260

Please sign in to comment.