forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_permissions.go
205 lines (159 loc) · 4.54 KB
/
resource_permissions.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package rabbitmq
import (
"fmt"
"log"
"strings"
"github.com/michaelklishin/rabbit-hole"
"github.com/hashicorp/terraform/helper/schema"
)
func resourcePermissions() *schema.Resource {
return &schema.Resource{
Create: CreatePermissions,
Update: UpdatePermissions,
Read: ReadPermissions,
Delete: DeletePermissions,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"user": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vhost": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "/",
ForceNew: true,
},
"permissions": &schema.Schema{
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"configure": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"write": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"read": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
func CreatePermissions(d *schema.ResourceData, meta interface{}) error {
rmqc := meta.(*rabbithole.Client)
user := d.Get("user").(string)
vhost := d.Get("vhost").(string)
permsList := d.Get("permissions").([]interface{})
permsMap, ok := permsList[0].(map[string]interface{})
if !ok {
return fmt.Errorf("Unable to parse permissions")
}
if err := setPermissionsIn(rmqc, vhost, user, permsMap); err != nil {
return err
}
id := fmt.Sprintf("%s@%s", user, vhost)
d.SetId(id)
return ReadPermissions(d, meta)
}
func ReadPermissions(d *schema.ResourceData, meta interface{}) error {
rmqc := meta.(*rabbithole.Client)
permissionId := strings.Split(d.Id(), "@")
if len(permissionId) < 2 {
return fmt.Errorf("Unable to determine Permission ID")
}
user := permissionId[0]
vhost := permissionId[1]
userPerms, err := rmqc.GetPermissionsIn(vhost, user)
if err != nil {
return checkDeleted(d, err)
}
log.Printf("[DEBUG] RabbitMQ: Permission retrieved for %s: %#v", d.Id(), userPerms)
d.Set("user", userPerms.User)
d.Set("vhost", userPerms.Vhost)
perms := make([]map[string]interface{}, 1)
p := make(map[string]interface{})
p["configure"] = userPerms.Configure
p["write"] = userPerms.Write
p["read"] = userPerms.Read
perms[0] = p
d.Set("permissions", perms)
return nil
}
func UpdatePermissions(d *schema.ResourceData, meta interface{}) error {
rmqc := meta.(*rabbithole.Client)
permissionId := strings.Split(d.Id(), "@")
if len(permissionId) < 2 {
return fmt.Errorf("Unable to determine Permission ID")
}
user := permissionId[0]
vhost := permissionId[1]
if d.HasChange("permissions") {
_, newPerms := d.GetChange("permissions")
newPermsList := newPerms.([]interface{})
permsMap, ok := newPermsList[0].(map[string]interface{})
if !ok {
return fmt.Errorf("Unable to parse permissions")
}
if err := setPermissionsIn(rmqc, vhost, user, permsMap); err != nil {
return err
}
}
return ReadPermissions(d, meta)
}
func DeletePermissions(d *schema.ResourceData, meta interface{}) error {
rmqc := meta.(*rabbithole.Client)
permissionId := strings.Split(d.Id(), "@")
if len(permissionId) < 2 {
return fmt.Errorf("Unable to determine Permission ID")
}
user := permissionId[0]
vhost := permissionId[1]
log.Printf("[DEBUG] RabbitMQ: Attempting to delete permission for %s", d.Id())
resp, err := rmqc.ClearPermissionsIn(vhost, user)
log.Printf("[DEBUG] RabbitMQ: Permission delete response: %#v", resp)
if err != nil {
return err
}
if resp.StatusCode == 404 {
// The permissions were already deleted
return nil
}
if resp.StatusCode >= 400 {
return fmt.Errorf("Error deleting RabbitMQ permission: %s", resp.Status)
}
return nil
}
func setPermissionsIn(rmqc *rabbithole.Client, vhost string, user string, permsMap map[string]interface{}) error {
perms := rabbithole.Permissions{}
if v, ok := permsMap["configure"].(string); ok {
perms.Configure = v
}
if v, ok := permsMap["write"].(string); ok {
perms.Write = v
}
if v, ok := permsMap["read"].(string); ok {
perms.Read = v
}
log.Printf("[DEBUG] RabbitMQ: Attempting to set permissions for %s@%s: %#v", user, vhost, perms)
resp, err := rmqc.UpdatePermissionsIn(vhost, user, perms)
log.Printf("[DEBUG] RabbitMQ: Permission response: %#v", resp)
if err != nil {
return err
}
if resp.StatusCode >= 400 {
return fmt.Errorf("Error setting permissions: %s", resp.Status)
}
return nil
}