forked from aiven/terraform-provider-aiven
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_service_user.go
More file actions
118 lines (103 loc) · 2.51 KB
/
Copy pathresource_service_user.go
File metadata and controls
118 lines (103 loc) · 2.51 KB
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
package main
import (
"errors"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jelmersnoeck/aiven"
)
func resourceServiceUser() *schema.Resource {
return &schema.Resource{
Create: resourceServiceUserCreate,
Read: resourceServiceUserRead,
Delete: resourceServiceUserDelete,
Schema: map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Required: true,
Description: "Project to link the user to",
ForceNew: true,
},
"service_name": {
Type: schema.TypeString,
Required: true,
Description: "Service to link the user to",
ForceNew: true,
},
"username": {
Type: schema.TypeString,
Required: true,
Description: "Service username",
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"password": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
},
"access_cert": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
},
"access_key": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
},
},
}
}
func resourceServiceUserCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*aiven.Client)
user, err := client.ServiceUsers.Create(
d.Get("project").(string),
d.Get("service_name").(string),
aiven.CreateServiceUserRequest{
Username: d.Get("username").(string),
},
)
if err != nil {
d.SetId("")
return err
}
d.SetId(user.Username + "!")
d.Set("username", user.Username)
d.Set("password", user.Password)
d.Set("type", user.Type)
d.Set("access_cert", user.AccessCert)
d.Set("access_key", user.AccessKey)
return nil
}
func resourceServiceUserRead(d *schema.ResourceData, m interface{}) error {
client := m.(*aiven.Client)
service, err := client.Services.Get(
d.Get("project").(string),
d.Get("service_name").(string),
)
if err != nil {
return err
}
username := d.Get("username").(string)
for _, user := range service.Users {
if user.Username == username {
d.Set("username", user.Username)
d.Set("password", user.Password)
d.Set("type", user.Type)
d.Set("access_cert", user.AccessCert)
d.Set("access_key", user.AccessKey)
return nil
}
}
return errors.New("User not found")
}
func resourceServiceUserDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*aiven.Client)
return client.ServiceUsers.Delete(
d.Get("project").(string),
d.Get("service_name").(string),
d.Get("username").(string),
)
}