/
resource_openstack_sharedfilesystem_sharenetwork_v2.go
277 lines (231 loc) · 8.91 KB
/
resource_openstack_sharedfilesystem_sharenetwork_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
package openstack
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/securityservices"
"github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/sharenetworks"
)
func resourceSharedFilesystemShareNetworkV2() *schema.Resource {
return &schema.Resource{
Create: resourceSharedFilesystemShareNetworkV2Create,
Read: resourceSharedFilesystemShareNetworkV2Read,
Update: resourceSharedFilesystemShareNetworkV2Update,
Delete: resourceSharedFilesystemShareNetworkV2Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
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{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"project_id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"neutron_net_id": {
Type: schema.TypeString,
Required: true,
},
"neutron_subnet_id": {
Type: schema.TypeString,
Required: true,
},
"security_service_ids": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"network_type": {
Type: schema.TypeString,
Computed: true,
},
"segmentation_id": {
Type: schema.TypeInt,
Computed: true,
},
"cidr": {
Type: schema.TypeString,
Computed: true,
},
"ip_version": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func resourceSharedFilesystemShareNetworkV2Create(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)
}
createOpts := sharenetworks.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
NeutronNetID: d.Get("neutron_net_id").(string),
NeutronSubnetID: d.Get("neutron_subnet_id").(string),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
log.Printf("[DEBUG] Attempting to create sharenetwork")
sharenetwork, err := sharenetworks.Create(sfsClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating sharenetwork: %s", err)
}
d.SetId(sharenetwork.ID)
securityServiceIDs := resourceSharedFilesystemShareNetworkV2SecSvcToArray(d.Get("security_service_ids").(*schema.Set))
for _, securityServiceID := range securityServiceIDs {
log.Printf("[DEBUG] Adding %s security service to sharenetwork %s", securityServiceID, sharenetwork.ID)
securityServiceOpts := sharenetworks.AddSecurityServiceOpts{SecurityServiceID: securityServiceID}
_, err = sharenetworks.AddSecurityService(sfsClient, sharenetwork.ID, securityServiceOpts).Extract()
if err != nil {
return fmt.Errorf("Error adding %s security service to sharenetwork: %s", securityServiceID, err)
}
}
return resourceSharedFilesystemShareNetworkV2Read(d, meta)
}
func resourceSharedFilesystemShareNetworkV2Read(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)
}
sharenetwork, err := sharenetworks.Get(sfsClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "sharenetwork")
}
log.Printf("[DEBUG] Retrieved sharenetwork %s: %#v", d.Id(), sharenetwork)
securityServiceIDs, err := resourceSharedFilesystemShareNetworkV2GetSvcByShareNetID(sfsClient, d.Id())
if err != nil {
return err
}
d.Set("security_service_ids", securityServiceIDs)
d.Set("name", sharenetwork.Name)
d.Set("description", sharenetwork.Description)
d.Set("neutron_net_id", sharenetwork.NeutronNetID)
d.Set("neutron_subnet_id", sharenetwork.NeutronSubnetID)
// Computed
d.Set("project_id", sharenetwork.ProjectID)
d.Set("region", GetRegion(d, config))
d.Set("network_type", sharenetwork.NetworkType)
d.Set("segmentation_id", sharenetwork.SegmentationID)
d.Set("cidr", sharenetwork.CIDR)
d.Set("ip_version", sharenetwork.IPVersion)
return nil
}
func resourceSharedFilesystemShareNetworkV2Update(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)
}
var updateOpts sharenetworks.UpdateOpts
if d.HasChange("name") {
name := d.Get("name").(string)
updateOpts.Name = &name
}
if d.HasChange("description") {
description := d.Get("description").(string)
updateOpts.Description = &description
}
if d.HasChange("neutron_net_id") {
updateOpts.NeutronNetID = d.Get("neutron_net_id").(string)
}
if d.HasChange("neutron_subnet_id") {
updateOpts.NeutronSubnetID = d.Get("neutron_subnet_id").(string)
}
if updateOpts != (sharenetworks.UpdateOpts{}) {
log.Printf("[DEBUG] Updating sharenetwork %s with options: %#v", d.Id(), updateOpts)
_, err = sharenetworks.Update(sfsClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Unable to update sharenetwork %s: %s", d.Id(), err)
}
}
if d.HasChange("security_service_ids") {
old, new := d.GetChange("security_service_ids")
oldList, newList := old.(*schema.Set), new.(*schema.Set)
newSecurityServiceIDs := newList.Difference(oldList)
oldSecurityServiceIDs := oldList.Difference(newList)
for _, newSecurityServiceID := range newSecurityServiceIDs.List() {
id := newSecurityServiceID.(string)
log.Printf("[DEBUG] Adding new %s security service to sharenetwork %s", id, d.Id())
securityServiceOpts := sharenetworks.AddSecurityServiceOpts{SecurityServiceID: id}
_, err = sharenetworks.AddSecurityService(sfsClient, d.Id(), securityServiceOpts).Extract()
if err != nil {
return fmt.Errorf("Error adding new %s security service to sharenetwork: %s", id, err)
}
}
for _, oldSecurityServiceID := range oldSecurityServiceIDs.List() {
id := oldSecurityServiceID.(string)
log.Printf("[DEBUG] Removing old %s security service from sharenetwork %s", id, d.Id())
securityServiceOpts := sharenetworks.RemoveSecurityServiceOpts{SecurityServiceID: id}
_, err = sharenetworks.RemoveSecurityService(sfsClient, d.Id(), securityServiceOpts).Extract()
if err != nil {
return fmt.Errorf("Error removing old %s security service from sharenetwork: %s", id, err)
}
}
}
return resourceSharedFilesystemShareNetworkV2Read(d, meta)
}
func resourceSharedFilesystemShareNetworkV2Delete(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)
}
log.Printf("[DEBUG] Attempting to delete sharenetwork %s", d.Id())
err = sharenetworks.Delete(sfsClient, d.Id()).ExtractErr()
if err != nil {
return CheckDeleted(d, err, "Error deleting sharenetwork")
}
return nil
}
func resourceSharedFilesystemShareNetworkV2GetSvcByShareNetID(sfsClient *gophercloud.ServiceClient, shareNetworkID string) ([]string, error) {
securityServiceListOpts := securityservices.ListOpts{ShareNetworkID: shareNetworkID}
securityServicePages, err := securityservices.List(sfsClient, securityServiceListOpts).AllPages()
if err != nil {
return nil, fmt.Errorf("Unable to list security services for sharenetwork %s: %s", shareNetworkID, err)
}
securityServiceList, err := securityservices.ExtractSecurityServices(securityServicePages)
if err != nil {
return nil, fmt.Errorf("Unable to extract security services for sharenetwork %s: %s", shareNetworkID, err)
}
log.Printf("[DEBUG] Retrieved security services for sharenetwork %s: %#v", shareNetworkID, securityServiceList)
return resourceSharedFilesystemShareNetworkV2SecSvcToArray(&securityServiceList), nil
}
func resourceSharedFilesystemShareNetworkV2SecSvcToArray(v interface{}) []string {
var securityServicesIDs []string
switch t := v.(type) {
case *schema.Set:
for _, securityService := range (*v.(*schema.Set)).List() {
securityServicesIDs = append(securityServicesIDs, securityService.(string))
}
case *[]securityservices.SecurityService:
for _, securityService := range *v.(*[]securityservices.SecurityService) {
securityServicesIDs = append(securityServicesIDs, securityService.ID)
}
default:
log.Printf("[DEBUG] Invalid type provided to get the list of security service IDs: %s", t)
}
return securityServicesIDs
}