@@ -323,6 +323,7 @@ const sync = async () => {
323
323
const role = uniqueId . split ( ":" ) [ 1 ] ;
324
324
await Promise . all ( emails . map ( async ( email ) => {
325
325
try {
326
+ logger . info ( { email, orgId, role } , "Sync gsuite rule" ) ;
326
327
const userId = await getGrafanaUserId ( email ) ;
327
328
if ( userId ) {
328
329
if ( ! grafanaMembers [ uniqueId ] . includes ( email ) ) {
@@ -341,24 +342,42 @@ const sync = async () => {
341
342
} ) ) ;
342
343
} ) ) ;
343
344
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
+
344
362
// create or update static users
345
363
await Promise . all ( staticRules . map ( async ( rule ) => {
346
- const userEmail = rule . split ( ":" ) [ 0 ] ;
364
+ const email = rule . split ( ":" ) [ 0 ] ;
347
365
const orgName = rule . split ( ":" ) [ 1 ] ;
348
366
const role = rule . split ( ":" ) [ 2 ] ;
349
- if ( ! userEmail || ! orgName || ! role ) {
367
+ if ( ! email || ! orgName || ! role ) {
350
368
throw new Error ( "Email or organization name or role missing." ) ;
351
369
}
352
370
const orgId = await getGrafanaOrgId ( orgName ) ;
353
371
if ( ! orgId ) {
354
372
throw new Error ( "Could not get grafana organisation" ) ;
355
373
}
374
+ logger . info ( { email, orgId, role } , "Sync static rule" ) ;
356
375
const uniqueId = `${ orgId } :${ role } ` ;
357
376
try {
358
- const userId = await getGrafanaUserId ( userEmail ) ;
377
+ const userId = await getGrafanaUserId ( email ) ;
359
378
if ( userId ) {
360
379
try {
361
- await createGrafanaUser ( orgId , userEmail , role ) ;
380
+ await createGrafanaUser ( orgId , email , role ) ;
362
381
} catch ( e ) {
363
382
await updateGrafanaUser ( orgId , userId , role ) ;
364
383
}
@@ -368,28 +387,12 @@ const sync = async () => {
368
387
}
369
388
finally {
370
389
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 ) ;
373
392
}
374
393
}
375
394
} ) ) ;
376
395
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
- }
393
396
logger . info ( "End sync process" ) ;
394
397
updateRunning = false ;
395
398
} catch ( e ) {
0 commit comments