-
Notifications
You must be signed in to change notification settings - Fork 222
/
resource_user.go
160 lines (147 loc) · 4.18 KB
/
resource_user.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package grafana
import (
"context"
"log"
"strconv"
"strings"
gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceUser() *schema.Resource {
return &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/grafana/latest/administration/user-management/server-user-management/)
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/user/)
This resource represents an instance-scoped resource and uses Grafana's admin APIs.
It does not work with API tokens or service accounts which are org-scoped.
You must use basic auth.
`,
CreateContext: CreateUser,
ReadContext: ReadUser,
UpdateContext: UpdateUser,
DeleteContext: DeleteUser,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"user_id": {
Type: schema.TypeInt,
Computed: true,
Description: "The numerical ID of the Grafana user.",
},
"email": {
Type: schema.TypeString,
Required: true,
Description: "The email address of the Grafana user.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The display name for the Grafana user.",
},
"login": {
Type: schema.TypeString,
Optional: true,
Description: "The username for the Grafana user.",
},
"password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
Description: "The password for the Grafana user.",
},
"is_admin": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether to make user an admin.",
},
},
}
}
func CreateUser(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaAPI
user := gapi.User{
Email: d.Get("email").(string),
Name: d.Get("name").(string),
Login: d.Get("login").(string),
Password: d.Get("password").(string),
}
id, err := client.CreateUser(user)
if err != nil {
return diag.FromErr(err)
}
if d.HasChange("is_admin") {
err = client.UpdateUserPermissions(id, d.Get("is_admin").(bool))
if err != nil {
return diag.FromErr(err)
}
}
d.SetId(strconv.FormatInt(id, 10))
return ReadUser(ctx, d, meta)
}
func ReadUser(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaAPI
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return diag.FromErr(err)
}
user, err := client.User(id)
if err != nil {
if strings.Contains(err.Error(), "404") {
d.SetId("")
log.Printf("[WARN] removing user %s from state because it no longer exists in grafana", d.Id())
return nil
}
return diag.FromErr(err)
}
d.Set("user_id", user.ID)
d.Set("email", user.Email)
d.Set("name", user.Name)
d.Set("login", user.Login)
d.Set("is_admin", user.IsAdmin)
return nil
}
func UpdateUser(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaAPI
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return diag.FromErr(err)
}
u := gapi.User{
ID: id,
Email: d.Get("email").(string),
Name: d.Get("name").(string),
Login: d.Get("login").(string),
}
err = client.UserUpdate(u)
if err != nil {
return diag.FromErr(err)
}
if d.HasChange("password") {
err = client.UpdateUserPassword(id, d.Get("password").(string))
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("is_admin") {
err = client.UpdateUserPermissions(id, d.Get("is_admin").(bool))
if err != nil {
return diag.FromErr(err)
}
}
return ReadUser(ctx, d, meta)
}
func DeleteUser(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaAPI
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return diag.FromErr(err)
}
if err = client.DeleteUser(id); err != nil {
return diag.FromErr(err)
}
return diag.Diagnostics{}
}