-
Notifications
You must be signed in to change notification settings - Fork 133
/
resource_cloud_project_network_private.go
302 lines (248 loc) · 8.82 KB
/
resource_cloud_project_network_private.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package ovh
import (
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
"github.com/ovh/go-ovh/ovh"
)
func resourceOvhCloudProjectNetworkPrivateImportState(
d *schema.ResourceData,
meta interface{}) ([]*schema.ResourceData, error) {
givenId := d.Id()
splitId := strings.SplitN(givenId, "/", 2)
if len(splitId) != 2 {
return nil, fmt.Errorf("Import Id is not OVH_CLOUD_PROJECT/network_id formatted")
}
d.SetId(splitId[1])
d.Set("service_name", splitId[0])
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceCloudProjectNetworkPrivate() *schema.Resource {
return &schema.Resource{
Create: resourceCloudProjectNetworkPrivateCreate,
Read: resourceCloudProjectNetworkPrivateRead,
Update: resourceCloudProjectNetworkPrivateUpdate,
Delete: resourceCloudProjectNetworkPrivateDelete,
Importer: &schema.ResourceImporter{
State: resourceOvhCloudProjectNetworkPrivateImportState,
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
Description: "Service name of the resource representing the id of the cloud project.",
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"vlan_id": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Default: 0,
},
"regions": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"regions_status": {
Type: schema.TypeSet,
Computed: true,
Deprecated: "use the regions_attributes field instead",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": {
Type: schema.TypeString,
Required: true,
},
"region": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"regions_attributes": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": {
Type: schema.TypeString,
Required: true,
},
"region": {
Type: schema.TypeString,
Computed: true,
},
"openstackid": {
Type: schema.TypeString,
Computed: true,
},
},
},
Set: RegionAttributesHash,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceCloudProjectNetworkPrivateCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
regions, _ := helpers.StringsFromSchema(d, "regions")
params := &CloudProjectNetworkPrivateCreateOpts{
ServiceName: serviceName,
VlanId: d.Get("vlan_id").(int),
Name: d.Get("name").(string),
Regions: regions,
}
r := &CloudProjectNetworkPrivateResponse{}
log.Printf("[DEBUG] Will create public cloud private network: %s", params)
endpoint := fmt.Sprintf("/cloud/project/%s/network/private", params.ServiceName)
if err := config.OVHClient.Post(endpoint, params, r); err != nil {
return fmt.Errorf("calling %s with params %s:\n\t %q", endpoint, params, err)
}
log.Printf("[DEBUG] Waiting for Private Network %s:", r)
stateConf := &resource.StateChangeConf{
Pending: []string{"BUILDING"},
Target: []string{"ACTIVE"},
Refresh: waitForCloudProjectNetworkPrivateActive(config.OVHClient, serviceName, r.Id),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("waiting for private network (%s): %s", params, err)
}
log.Printf("[DEBUG] Created Private Network %s", r)
//set id
d.SetId(r.Id)
return resourceCloudProjectNetworkPrivateRead(d, meta)
}
func resourceCloudProjectNetworkPrivateRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
r := &CloudProjectNetworkPrivateResponse{}
log.Printf("[DEBUG] Will read public cloud private network for project: %s, id: %s", serviceName, d.Id())
endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s", serviceName, d.Id())
if err := config.OVHClient.Get(endpoint, r); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
d.Set("name", r.Name)
d.Set("status", r.Status)
d.Set("type", r.Type)
d.Set("vlan_id", r.Vlanid)
regions_status := make([]map[string]interface{}, 0)
regions_attributes := make([]map[string]interface{}, 0)
regions := make([]string, 0)
for i := range r.Regions {
region_attributes := make(map[string]interface{})
region_attributes["region"] = r.Regions[i].Region
region_attributes["status"] = r.Regions[i].Status
region_attributes["openstackid"] = r.Regions[i].OpenStackId
regions_attributes = append(regions_attributes, region_attributes)
region_status := make(map[string]interface{})
region_status["region"] = r.Regions[i].Region
region_status["status"] = r.Regions[i].Status
regions_status = append(regions_status, region_status)
regions = append(regions, fmt.Sprintf(r.Regions[i].Region))
}
d.Set("regions_attributes", regions_attributes)
d.Set("regions_status", regions_status)
d.Set("regions", regions)
d.SetId(r.Id)
d.Set("service_name", serviceName)
log.Printf("[DEBUG] Read Public Cloud Private Network %s", r)
return nil
}
func resourceCloudProjectNetworkPrivateUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
params := &CloudProjectNetworkPrivateUpdateOpts{
Name: d.Get("name").(string),
}
log.Printf("[DEBUG] Will update public cloud private network: %s", params)
endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s", serviceName, d.Id())
if err := config.OVHClient.Put(endpoint, params, nil); err != nil {
return fmt.Errorf("calling %s with params %s:\n\t %q", endpoint, params, err)
}
log.Printf("[DEBUG] Updated Public cloud %s Private Network %s:", serviceName, d.Id())
return resourceCloudProjectNetworkPrivateRead(d, meta)
}
func resourceCloudProjectNetworkPrivateDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
id := d.Id()
log.Printf("[DEBUG] Will delete public cloud private network for project: %s, id: %s", serviceName, id)
endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s", serviceName, id)
if err := config.OVHClient.Delete(endpoint, nil); err != nil {
return fmt.Errorf("calling %s:\n\t %q", endpoint, err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{"DELETING"},
Target: []string{"DELETED"},
Refresh: waitForCloudProjectNetworkPrivateDelete(config.OVHClient, serviceName, id),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("deleting for private network (%s): %s", id, err)
}
d.SetId("")
log.Printf("[DEBUG] Deleted Public Cloud %s Private Network %s", serviceName, id)
return nil
}
// AttachmentStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// an Attachment Task.
func waitForCloudProjectNetworkPrivateActive(c *ovh.Client, serviceName, CloudProjectNetworkPrivateId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
r := &CloudProjectNetworkPrivateResponse{}
endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s", serviceName, CloudProjectNetworkPrivateId)
if err := c.Get(endpoint, r); err != nil {
return r, "", err
}
log.Printf("[DEBUG] Pending Private Network: %s", r)
return r, r.Status, nil
}
}
// AttachmentStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// an Attachment Task.
func waitForCloudProjectNetworkPrivateDelete(c *ovh.Client, serviceName, CloudProjectNetworkPrivateId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
r := &CloudProjectNetworkPrivateResponse{}
endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s", serviceName, CloudProjectNetworkPrivateId)
if err := c.Get(endpoint, r); err != nil {
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
log.Printf("[DEBUG] private network id %s on project %s deleted", CloudProjectNetworkPrivateId, serviceName)
return r, "DELETED", nil
} else {
return r, "", err
}
}
log.Printf("[DEBUG] Pending Private Network: %s", r)
return r, r.Status, nil
}
}