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

Commit 447b6dc

Browse files
author
yacut
committed
refactor
1 parent 81cf640 commit 447b6dc

File tree

1 file changed

+72
-58
lines changed

1 file changed

+72
-58
lines changed

index.ts

Lines changed: 72 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class GrafanaSync {
125125
this.client = client;
126126
this.service = google.admin("directory_v1");
127127
} catch (e) {
128-
this.logger.error(e);
128+
this.logger.error({ error: this.formatError(e) });
129129
}
130130
}
131131

@@ -172,7 +172,7 @@ class GrafanaSync {
172172
}
173173
return response.id;
174174
} catch (e) {
175-
this.logger.error({ name }, e);
175+
this.logger.error({ name, error: this.formatError(e) });
176176
}
177177
}
178178

@@ -196,7 +196,7 @@ class GrafanaSync {
196196
.filter((m) => m.role && m.role === role)
197197
.map((m) => m.email);
198198
} catch (e) {
199-
this.logger.error({ orgId }, e);
199+
this.logger.error({ orgId, error: this.formatError(e) });
200200
}
201201
}
202202

@@ -217,7 +217,7 @@ class GrafanaSync {
217217
}
218218
return response.id;
219219
} catch (e) {
220-
this.logger.error({ email }, e);
220+
this.logger.error({ email, error: this.formatError(e) });
221221
}
222222
}
223223

@@ -243,7 +243,7 @@ class GrafanaSync {
243243
this.logger.debug({ userId, email, role }, "Got grafana user role.");
244244
return role;
245245
} catch (e) {
246-
this.logger.error({ userId }, e);
246+
this.logger.error({ userId, error: this.formatError(e) });
247247
}
248248
}
249249

@@ -267,7 +267,7 @@ class GrafanaSync {
267267
this.logger.debug({ orgId, email, role, response }, "Created grafana organisation user.");
268268
return response;
269269
} catch (e) {
270-
this.logger.debug({ orgId, email, role }, e);
270+
this.logger.debug({ orgId, email, role, error: this.formatError(e) });
271271
}
272272
}
273273

@@ -289,7 +289,7 @@ class GrafanaSync {
289289
this.logger.debug({ orgId, userId, role, response }, "Updated grafana user.");
290290
return response;
291291
} catch (e) {
292-
this.logger.error({ orgId, userId, role }, e);
292+
this.logger.error({ orgId, userId, role, error: this.formatError(e) });
293293
}
294294
}
295295

@@ -312,21 +312,35 @@ class GrafanaSync {
312312
this.logger.debug({ orgId, userId, response }, "Delete grafana user.");
313313
return response;
314314
} catch (e) {
315-
this.logger.error({ orgId, userId }, e);
315+
this.logger.error({ orgId, userId, error: this.formatError(e) });
316316
}
317317
}
318318

319+
public formatError(err: any) {
320+
if (!err) {
321+
return "";
322+
}
323+
if (err && err.error) {
324+
return err.error;
325+
}
326+
if (err && err.message) {
327+
return err.message;
328+
}
329+
return "";
330+
}
331+
319332
public async sync() {
333+
const self = this;
320334
try {
321-
if (this.updateRunning) {
322-
this.logger.debug("Update is already running. Skipping...");
335+
if (self.updateRunning) {
336+
self.logger.debug("Update is already running. Skipping...");
323337
return;
324338
}
325-
this.logger.info("Start sync process");
326-
this.updateRunning = true;
339+
self.logger.info("Start sync process");
340+
self.updateRunning = true;
327341

328342
// Build grafana and google users cache
329-
await Promise.all(this.rules.map(async (rule) => {
343+
await Promise.all(self.rules.map(async (rule) => {
330344
try {
331345
const groupEmail = rule.split(":")[0];
332346
const orgName = rule.split(":")[1];
@@ -335,113 +349,113 @@ class GrafanaSync {
335349
throw new Error("Email or organization name or role missing.");
336350
}
337351

338-
const orgId = await this.getGrafanaOrgId(orgName);
352+
const orgId = await self.getGrafanaOrgId(orgName);
339353
if (!orgId) {
340354
throw new Error("Could not get grafana organisation");
341355
}
342356
const uniqueId = `${orgId}:${role}`;
343-
this.grafanaMembers.set(uniqueId, (this.grafanaMembers.get(uniqueId) || []).concat(await this.getGrafanaOrgUsers(orgId, role)));
357+
self.grafanaMembers.set(uniqueId, (self.grafanaMembers.get(uniqueId) || []).concat(await self.getGrafanaOrgUsers(orgId, role)));
344358

345-
await this.getGoogleApiClient();
346-
this.googleMembers.set(uniqueId, (this.googleMembers.get(uniqueId) || []).concat(await this.getGroupMembers(groupEmail)));
359+
await self.getGoogleApiClient();
360+
self.googleMembers.set(uniqueId, (self.googleMembers.get(uniqueId) || []).concat(await self.getGroupMembers(groupEmail)));
347361

348-
this.success.inc();
362+
self.success.inc();
349363

350364
} catch (e) {
351-
this.fail.inc();
352-
this.logger.error(e);
365+
self.fail.inc();
366+
self.logger.error(e);
353367
}
354368
}));
355369

356-
this.logger.debug(this.googleMembers, "Google members map before create/update");
357-
this.logger.debug(this.grafanaMembers, "Grafana members map before create/update");
370+
self.logger.debug(self.googleMembers, "Google members map before create/update");
371+
self.logger.debug(self.grafanaMembers, "Grafana members map before create/update");
358372

359373
// create or update all google users in grafana
360-
await Promise.all(Array.from(this.googleMembers.keys()).map(async (uniqueId) => {
361-
const emails = this.googleMembers.get(uniqueId);
374+
await Promise.all(Array.from(self.googleMembers.keys()).map(async (uniqueId) => {
375+
const emails = self.googleMembers.get(uniqueId);
362376
const orgId = uniqueId.split(":")[0];
363377
const role = uniqueId.split(":")[1];
364378
await Promise.all(emails.map(async (email) => {
365379
try {
366-
this.logger.info({ email, orgId, role }, "Sync gsuite rule");
367-
const userId = await this.getGrafanaUserId(email);
380+
self.logger.info({ email, orgId, role }, "Sync gsuite rule");
381+
const userId = await self.getGrafanaUserId(email);
368382
if (userId) {
369-
if (!this.grafanaMembers.get(uniqueId).find((e) => e === email)) {
370-
await this.createGrafanaUser(orgId, email, role);
383+
if (!self.grafanaMembers.get(uniqueId).find((e) => e === email)) {
384+
await self.createGrafanaUser(orgId, email, role);
371385
} else {
372-
await this.updateGrafanaUser(orgId, userId, role);
386+
await self.updateGrafanaUser(orgId, userId, role);
373387
}
374388
}
375389
} catch (e) {
376-
this.logger.error(e);
390+
self.logger.error(e);
377391
} finally {
378-
this.logger.debug(`Remove user ${email} from sync map.`);
379-
this.grafanaMembers.set(uniqueId, this.grafanaMembers.get(uniqueId).filter((e) => e !== email));
392+
self.logger.debug(`Remove user ${email} from sync map.`);
393+
self.grafanaMembers.set(uniqueId, self.grafanaMembers.get(uniqueId).filter((e) => e !== email));
380394
}
381395
}));
382396
}));
383397

384-
this.logger.debug(this.googleMembers, "Google members map before delete");
385-
this.logger.debug(this.grafanaMembers, "Grafana members map before delete");
398+
self.logger.debug(self.googleMembers, "Google members map before delete");
399+
self.logger.debug(self.grafanaMembers, "Grafana members map before delete");
386400

387401
// delete users which are not in google groups
388-
if (this.mode === "sync") {
389-
await Promise.all(Array.from(this.grafanaMembers.keys()).map(async (uniqueId) => {
390-
const emails = this.grafanaMembers.get(uniqueId);
402+
if (self.mode === "sync") {
403+
await Promise.all(Array.from(self.grafanaMembers.keys()).map(async (uniqueId) => {
404+
const emails = self.grafanaMembers.get(uniqueId);
391405
const orgId = uniqueId.split(":")[0];
392406
await Promise.all(emails.map(async (email) => {
393-
const userId = await this.getGrafanaUserId(email);
407+
const userId = await self.getGrafanaUserId(email);
394408
if (userId) {
395-
const userRole = await this.getGrafanaUserRole(userId, orgId, email);
396-
if (this.excludeRole !== userRole && !this.googleMembers.get(uniqueId).find((e) => e === email)) {
397-
await this.deleteGrafanaUser(orgId, userId, email);
409+
const userRole = await self.getGrafanaUserRole(userId, orgId, email);
410+
if (self.excludeRole !== userRole && !self.googleMembers.get(uniqueId).find((e) => e === email)) {
411+
await self.deleteGrafanaUser(orgId, userId, email);
398412
}
399413
}
400414
}));
401415
}));
402416
}
403417

404418
// create or update static users
405-
await Promise.all(this.staticRules.map(async (rule) => {
419+
await Promise.all(self.staticRules.map(async (rule) => {
406420
const email = rule.split(":")[0];
407421
const orgName = rule.split(":")[1];
408422
const role = rule.split(":")[2];
409423
if (!email || !orgName || !role) {
410424
throw new Error("Email or organization name or role missing.");
411425
}
412-
const orgId = await this.getGrafanaOrgId(orgName);
426+
const orgId = await self.getGrafanaOrgId(orgName);
413427
if (!orgId) {
414428
throw new Error("Could not get grafana organisation");
415429
}
416-
this.logger.info({ email, orgId, role }, "Sync static rule");
430+
self.logger.info({ email, orgId, role }, "Sync static rule");
417431
const uniqueId = `${orgId}:${role}`;
418432
try {
419-
const userId = await this.getGrafanaUserId(email);
433+
const userId = await self.getGrafanaUserId(email);
420434
if (userId) {
421435
try {
422-
await this.createGrafanaUser(orgId, email, role);
436+
await self.createGrafanaUser(orgId, email, role);
423437
} catch (e) {
424-
await this.updateGrafanaUser(orgId, userId, role);
438+
await self.updateGrafanaUser(orgId, userId, role);
425439
}
426440
}
427441
} catch (e) {
428-
this.logger.error(e);
442+
self.logger.error(e);
429443
} finally {
430-
if (this.grafanaMembers.get(uniqueId)) {
431-
this.logger.debug(`Remove user ${email} from sync map.`);
432-
this.grafanaMembers.set(uniqueId, this.grafanaMembers.get(uniqueId).filter((e) => e !== email));
444+
if (self.grafanaMembers.get(uniqueId)) {
445+
self.logger.debug(`Remove user ${email} from sync map.`);
446+
self.grafanaMembers.set(uniqueId, self.grafanaMembers.get(uniqueId).filter((e) => e !== email));
433447
}
434448
}
435449
}));
436450

437-
this.googleMembers.clear();
438-
this.grafanaMembers.clear();
439-
this.logger.info("End sync process");
440-
this.updateRunning = false;
451+
self.googleMembers.clear();
452+
self.grafanaMembers.clear();
453+
self.logger.info("End sync process");
454+
self.updateRunning = false;
441455
} catch (e) {
442-
this.fail.inc();
443-
this.logger.error(e);
444-
this.updateRunning = false;
456+
self.fail.inc();
457+
self.logger.error(e);
458+
self.updateRunning = false;
445459
}
446460
}
447461
}

0 commit comments

Comments
 (0)