This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
resource_openstack_sharedfilesystem_share_access_v2.go
286 lines (234 loc) · 7.97 KB
/
resource_openstack_sharedfilesystem_share_access_v2.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package openstack
import (
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/errors"
"github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/shares"
)
func resourceSharedFilesystemShareAccessV2() *schema.Resource {
return &schema.Resource{
Create: resourceSharedFilesystemShareAccessV2Create,
Read: resourceSharedFilesystemShareAccessV2Read,
Delete: resourceSharedFilesystemShareAccessV2Delete,
Importer: &schema.ResourceImporter{
resourceSharedFilesystemShareAccessV2Import,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"share_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"access_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"ip", "user", "cert",
}, true),
},
"access_to": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"access_level": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"rw", "ro",
}, true),
},
},
}
}
func resourceSharedFilesystemShareAccessV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
sfsClient, err := config.sharedfilesystemV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack sharedfilesystem client: %s", err)
}
sfsClient.Microversion = minManilaMicroversion
shareID := d.Get("share_id").(string)
grantOpts := shares.GrantAccessOpts{
AccessType: d.Get("access_type").(string),
AccessTo: d.Get("access_to").(string),
AccessLevel: d.Get("access_level").(string),
}
log.Printf("[DEBUG] Create Options: %#v", grantOpts)
timeout := d.Timeout(schema.TimeoutCreate)
log.Printf("[DEBUG] Attempting to grant access")
var access *shares.AccessRight
err = resource.Retry(timeout, func() *resource.RetryError {
access, err = shares.GrantAccess(sfsClient, shareID, grantOpts).Extract()
if err != nil {
return checkForRetryableError(err)
}
return nil
})
if err != nil {
detailedErr := errors.ErrorDetails{}
e := errors.ExtractErrorInto(err, &detailedErr)
if e != nil {
return fmt.Errorf("Error granting access: %s: %s", err, e)
}
for k, msg := range detailedErr {
return fmt.Errorf("Error granting access: %s (%d): %s", k, msg.Code, msg.Message)
}
}
d.SetId(access.ID)
pending := []string{"new", "queued_to_apply", "applying"}
// Wait for access to become active before continuing
err = waitForSFV2Access(sfsClient, shareID, access.ID, "active", pending, timeout)
if err != nil {
return err
}
return resourceSharedFilesystemShareAccessV2Read(d, meta)
}
func resourceSharedFilesystemShareAccessV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
sfsClient, err := config.sharedfilesystemV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack sharedfilesystem client: %s", err)
}
sfsClient.Microversion = minManilaMicroversion
shareID := d.Get("share_id").(string)
access, err := shares.ListAccessRights(sfsClient, shareID).Extract()
if err != nil {
return CheckDeleted(d, err, "share_access")
}
for _, v := range access {
if v.ID == d.Id() {
log.Printf("[DEBUG] Retrieved %s share ACL: %#v", d.Id(), v)
d.Set("access_type", v.AccessType)
d.Set("access_to", v.AccessTo)
d.Set("access_level", v.AccessLevel)
return nil
}
}
log.Printf("[DEBUG] Unable to find %s share access", d.Id())
d.SetId("")
return nil
}
func resourceSharedFilesystemShareAccessV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
sfsClient, err := config.sharedfilesystemV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack sharedfilesystem client: %s", err)
}
sfsClient.Microversion = minManilaMicroversion
shareID := d.Get("share_id").(string)
revokeOpts := shares.RevokeAccessOpts{AccessID: d.Id()}
timeout := d.Timeout(schema.TimeoutDelete)
log.Printf("[DEBUG] Attempting to revoke access %s", d.Id())
err = resource.Retry(timeout, func() *resource.RetryError {
err = shares.RevokeAccess(sfsClient, shareID, revokeOpts).ExtractErr()
if err != nil {
return checkForRetryableError(err)
}
return nil
})
if err != nil {
e := CheckDeleted(d, err, "")
if e == nil {
return nil
}
detailedErr := errors.ErrorDetails{}
e = errors.ExtractErrorInto(err, &detailedErr)
if e != nil {
return fmt.Errorf("Error waiting for OpenStack share ACL on %s to be removed: %s: %s", shareID, err, e)
}
for k, msg := range detailedErr {
return fmt.Errorf("Error waiting for OpenStack share ACL on %s to be removed: %s (%d): %s", shareID, k, msg.Code, msg.Message)
}
}
// Wait for access to become deleted before continuing
pending := []string{"active", "new", "queued_to_deny", "denying"}
err = waitForSFV2Access(sfsClient, shareID, d.Id(), "denied", pending, timeout)
if err != nil {
return err
}
return nil
}
func resourceSharedFilesystemShareAccessV2Import(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.SplitN(d.Id(), "/", 2)
if len(parts) != 2 {
err := fmt.Errorf("Invalid format specified for Openstack share ACL. Format must be <share id>/<ACL id>")
return nil, err
}
config := meta.(*Config)
sfsClient, err := config.sharedfilesystemV2Client(GetRegion(d, config))
if err != nil {
return nil, fmt.Errorf("Error creating OpenStack sharedfilesystem client: %s", err)
}
sfsClient.Microversion = minManilaMicroversion
shareID := parts[0]
accessID := parts[1]
access, err := shares.ListAccessRights(sfsClient, shareID).Extract()
if err != nil {
return nil, fmt.Errorf("Unable to get %s Openstack share and its ACL's: %s", shareID, err)
}
for _, v := range access {
if v.ID == accessID {
log.Printf("[DEBUG] Retrieved %s share ACL: %#v", accessID, v)
d.SetId(accessID)
d.Set("share_id", shareID)
d.Set("access_type", v.AccessType)
d.Set("access_to", v.AccessTo)
d.Set("access_level", v.AccessLevel)
return []*schema.ResourceData{d}, nil
}
}
return nil, fmt.Errorf("[DEBUG] Unable to find %s share access", accessID)
}
// Full list of the share access statuses: https://developer.openstack.org/api-ref/shared-file-system/?expanded=list-services-detail,list-access-rules-detail#list-access-rules
func waitForSFV2Access(sfsClient *gophercloud.ServiceClient, shareID string, id string, target string, pending []string, timeout time.Duration) error {
log.Printf("[DEBUG] Waiting for access %s to become %s.", id, target)
stateConf := &resource.StateChangeConf{
Target: []string{target},
Pending: pending,
Refresh: resourceSFV2AccessRefreshFunc(sfsClient, shareID, id),
Timeout: timeout,
Delay: 1 * time.Second,
MinTimeout: 1 * time.Second,
}
_, err := stateConf.WaitForState()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
switch target {
case "denied":
return nil
default:
return fmt.Errorf("Error: access %s not found: %s", id, err)
}
}
return fmt.Errorf("Error waiting for access %s to become %s: %s", id, target, err)
}
return nil
}
func resourceSFV2AccessRefreshFunc(sfsClient *gophercloud.ServiceClient, shareID string, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
access, err := shares.ListAccessRights(sfsClient, shareID).Extract()
if err != nil {
return nil, "", err
}
for _, v := range access {
if v.ID == id {
return v, v.State, nil
}
}
return nil, "", gophercloud.ErrDefault404{}
}
}