This repository has been archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrafana.go
108 lines (86 loc) · 2.6 KB
/
grafana.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"math/rand"
"net/url"
"os"
apex "github.com/apex/log"
sdk "github.com/grafana/grafana-api-golang-client"
)
var passwordBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890%#"
func getGrafanaClient(logs *apex.Entry) (*sdk.Client, error) {
// Connect to Grafana API as 'admin'
grafanaURL := os.Getenv("GRAFANA-ENDPOINT")
password := os.Getenv("GRAFANA-ADMIN-PASSWORD")
client, err := sdk.New(grafanaURL, sdk.Config{BasicAuth: url.UserPassword("admin", password), NumRetries: 2})
if err != nil {
logs.Error("Error connecting to Grafana API server: " + err.Error())
return nil, err
}
return client, nil
}
// Check if the user is already present in Grafana
// If present, get the user ID
// If not present, add the user and get the user ID
func getGrafanaUserId(client *sdk.Client, loginId string, logs *apex.Entry) (int64, error) {
var userId int64
user, err := client.UserByEmail(loginId)
if err != nil {
logs.Error("User not found: " + err.Error())
// Generating random password - https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go
password := make([]byte, 10)
randomInt := rand.Int63()
for i := 0; i < 10; {
idx := int(randomInt & 63)
password[i] = passwordBytes[idx]
randomInt >>= 6
i++
}
var userDetails = sdk.User{Email: loginId, Password: string(password)}
user, err := client.CreateUser(userDetails)
if err != nil {
logs.Error("Couldn't create user: " + err.Error())
return -1, err
} else {
logs.Info("Created new User")
userId = user
}
} else {
userId = user.ID
}
logs.Info("User ID: " + fmt.Sprintf("%d", userId))
return userId, nil
}
// Update the user roles in Grafana
func updateUserPermission(loginId string, orgRoles map[string]string, logs *apex.Entry) error {
client, err := getGrafanaClient(logs)
if err != nil {
return err
}
userId, err := getGrafanaUserId(client, loginId, logs)
if err != nil {
return err
}
// Update the org-roles for the user
for org, role := range orgRoles {
currOrg, err := client.OrgByName(org)
if err != nil {
logs.Info("Invalid organization - " + org)
} else {
if role == "delete-user" {
err = client.RemoveOrgUser(currOrg.ID, userId)
} else {
err = client.UpdateOrgUser(currOrg.ID, userId, role)
// If UpdateOrgUser fails, the user is new the org
// Add the user to the org with their respective roles
if err != nil {
err = client.AddOrgUser(currOrg.ID, loginId, role)
if err != nil {
logs.Info("Couldn't add user to org - " + org)
}
}
}
}
}
return nil
}