Skip to content
This repository was archived by the owner on Feb 4, 2020. It is now read-only.

Commit 834f975

Browse files
author
yacut
committed
sync static rule after cleanup
1 parent bfe7004 commit 834f975

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

src/index.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ const sync = async () => {
323323
const role = uniqueId.split(":")[1];
324324
await Promise.all(emails.map(async (email) => {
325325
try {
326+
logger.info({ email, orgId, role }, "Sync gsuite rule");
326327
const userId = await getGrafanaUserId(email);
327328
if (userId) {
328329
if (!grafanaMembers[uniqueId].includes(email)) {
@@ -341,24 +342,42 @@ const sync = async () => {
341342
}));
342343
}));
343344

345+
// delete users which are not in google groups
346+
if (mode === "sync") {
347+
await Promise.all(Object.keys(grafanaMembers).map(async (uniqueId) => {
348+
const emails = grafanaMembers[uniqueId];
349+
const orgId = uniqueId.split(":")[0];
350+
await Promise.all(emails.map(async (email) => {
351+
const userId = await getGrafanaUserId(email);
352+
if (userId) {
353+
const userRole = await getGrafanaUserRole(userId, orgId);
354+
if (excludeRole !== userRole) {
355+
await deleteGrafanaUser(orgId, userId);
356+
}
357+
}
358+
}));
359+
}));
360+
}
361+
344362
// create or update static users
345363
await Promise.all(staticRules.map(async (rule) => {
346-
const userEmail = rule.split(":")[0];
364+
const email = rule.split(":")[0];
347365
const orgName = rule.split(":")[1];
348366
const role = rule.split(":")[2];
349-
if (!userEmail || !orgName || !role) {
367+
if (!email || !orgName || !role) {
350368
throw new Error("Email or organization name or role missing.");
351369
}
352370
const orgId = await getGrafanaOrgId(orgName);
353371
if (!orgId) {
354372
throw new Error("Could not get grafana organisation");
355373
}
374+
logger.info({ email, orgId, role }, "Sync static rule");
356375
const uniqueId = `${orgId}:${role}`;
357376
try {
358-
const userId = await getGrafanaUserId(userEmail);
377+
const userId = await getGrafanaUserId(email);
359378
if (userId) {
360379
try {
361-
await createGrafanaUser(orgId, userEmail, role);
380+
await createGrafanaUser(orgId, email, role);
362381
}catch(e){
363382
await updateGrafanaUser(orgId, userId, role);
364383
}
@@ -368,28 +387,12 @@ const sync = async () => {
368387
}
369388
finally {
370389
if (grafanaMembers[uniqueId]) {
371-
logger.debug(`Remove user ${userEmail} from sync map.`);
372-
grafanaMembers[uniqueId] = grafanaMembers[uniqueId].filter(e => e !== userEmail);
390+
logger.debug(`Remove user ${email} from sync map.`);
391+
grafanaMembers[uniqueId] = grafanaMembers[uniqueId].filter(e => e !== email);
373392
}
374393
}
375394
}));
376395

377-
// delete users which are not in google groups nor static rules
378-
if (mode === "sync") {
379-
await Promise.all(Object.keys(grafanaMembers).map(async (uniqueId) => {
380-
const emails = grafanaMembers[uniqueId];
381-
const orgId = uniqueId.split(":")[0];
382-
await Promise.all(emails.map(async (email) => {
383-
const userId = await getGrafanaUserId(email);
384-
if (userId) {
385-
const userRole = await getGrafanaUserRole(userId, orgId);
386-
if (excludeRole !== userRole) {
387-
await deleteGrafanaUser(orgId, userId);
388-
}
389-
}
390-
}));
391-
}));
392-
}
393396
logger.info("End sync process");
394397
updateRunning = false;
395398
} catch (e) {

0 commit comments

Comments
 (0)