-
Notifications
You must be signed in to change notification settings - Fork 132
/
resource_fastly_user_v1.go
110 lines (87 loc) · 2.49 KB
/
resource_fastly_user_v1.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
package fastly
import (
gofastly "github.com/fastly/go-fastly/v3/fastly"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceUserV1() *schema.Resource {
return &schema.Resource{
Create: resourceUserV1Create,
Read: resourceUserV1Read,
Update: resourceUserV1Update,
Delete: resourceUserV1Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"login": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The email address, which is the login name, of the User",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The real life name of the user",
},
"role": {
Type: schema.TypeString,
Optional: true,
Default: "user",
Description: "The role of this user. Can be `user` (the default), `billing`, `engineer`, or `superuser`. For detailed information on the abilities granted to each role, see [Fastly's Documentation on User roles](https://docs.fastly.com/en/guides/configuring-user-roles-and-permissions#user-roles-and-what-they-can-do)",
ValidateFunc: validateUserRole(),
},
},
}
}
func resourceUserV1Create(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*FastlyClient).conn
u, err := conn.CreateUser(&gofastly.CreateUserInput{
Login: d.Get("login").(string),
Name: d.Get("name").(string),
Role: d.Get("role").(string),
})
if err != nil {
return err
}
d.SetId(u.ID)
return nil
}
func resourceUserV1Read(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*FastlyClient).conn
u, err := conn.GetUser(&gofastly.GetUserInput{
ID: d.Id(),
})
if err != nil {
return err
}
d.Set("login", u.Login)
d.Set("name", u.Name)
d.Set("role", u.Role)
return nil
}
func resourceUserV1Update(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*FastlyClient).conn
// Update Name and/or Role.
if d.HasChange("name") || d.HasChange("role") {
_, err := conn.UpdateUser(&gofastly.UpdateUserInput{
ID: d.Id(),
Name: gofastly.String(d.Get("name").(string)),
Role: gofastly.String(d.Get("role").(string)),
})
if err != nil {
return err
}
}
return resourceUserV1Read(d, meta)
}
func resourceUserV1Delete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*FastlyClient).conn
err := conn.DeleteUser(&gofastly.DeleteUserInput{
ID: d.Id(),
})
if err != nil {
return err
}
return nil
}