Skip to content

Commit 657a2f0

Browse files
Thibaut Di PrimaArthur Amstutz
Thibaut Di Prima
authored and
Arthur Amstutz
committed
datasource & resource instance
1 parent 7179dfc commit 657a2f0

10 files changed

+148
-172
lines changed

.cds/terraform-provider-ovh.yml

+1
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ workflow:
576576
one_at_a_time: true
577577
parameters:
578578
testargs: -run CloudProjectInstance
579+
skipthispipeline: "{{.workflow.terraform-provider-ovh.pip.skipthistest.cloudproject}}"
579580
pipeline: terraform-provider-ovh-testacc
580581
when:
581582
- success

ovh/data_cloud_project_instance.go

+12-17
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func dataSourceCloudProjectInstance() *schema.Resource {
1717
Type: schema.TypeString,
1818
Required: true,
1919
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
20-
Description: "Service name of the resource representing the id of the cloud project.",
20+
Description: "Service name of the resource representing the id of the cloud project",
2121
},
2222
"region": {
2323
Type: schema.TypeString,
@@ -57,7 +57,7 @@ func dataSourceCloudProjectInstance() *schema.Resource {
5757
Schema: map[string]*schema.Schema{
5858
"id": {
5959
Type: schema.TypeString,
60-
Description: "Volume Id",
60+
Description: "Volume id",
6161
Computed: true,
6262
},
6363
},
@@ -75,12 +75,7 @@ func dataSourceCloudProjectInstance() *schema.Resource {
7575
},
7676
"name": {
7777
Type: schema.TypeString,
78-
Description: "Flavor name",
79-
Computed: true,
80-
},
81-
"id": {
82-
Type: schema.TypeString,
83-
Description: "Instance id",
78+
Description: "Instance name",
8479
Computed: true,
8580
},
8681
"image_id": {
@@ -90,7 +85,7 @@ func dataSourceCloudProjectInstance() *schema.Resource {
9085
},
9186
"ssh_key": {
9287
Type: schema.TypeString,
93-
Description: "Instance task state",
88+
Description: "SSH Key pair name",
9489
Computed: true,
9590
},
9691
"task_state": {
@@ -119,20 +114,19 @@ func dataSourceCloudProjectInstanceRead(d *schema.ResourceData, meta interface{}
119114
if err := config.OVHClient.Get(endpoint, &res); err != nil {
120115
return helpers.CheckDeleted(d, err, endpoint)
121116
}
122-
log.Printf("[DEBUG] Read instance: %+v", res)
123117

124-
addresses := make([]map[string]interface{}, 0)
125-
for i := range res.Addresses {
118+
addresses := make([]map[string]interface{}, 0, len(res.Addresses))
119+
for _, addr := range res.Addresses {
126120
address := make(map[string]interface{})
127-
address["ip"] = res.Addresses[i].Ip
128-
address["version"] = res.Addresses[i].Version
121+
address["ip"] = addr.Ip
122+
address["version"] = addr.Version
129123
addresses = append(addresses, address)
130124
}
131125

132-
attachedVolumes := make([]map[string]interface{}, 0)
133-
for i := range res.AttachedVolumes {
126+
attachedVolumes := make([]map[string]interface{}, 0, len(res.AttachedVolumes))
127+
for _, volume := range res.AttachedVolumes {
134128
attachedVolume := make(map[string]interface{})
135-
attachedVolume["id"] = res.AttachedVolumes[i].Id
129+
attachedVolume["id"] = volume.Id
136130
attachedVolumes = append(attachedVolumes, attachedVolume)
137131
}
138132

@@ -145,6 +139,7 @@ func dataSourceCloudProjectInstanceRead(d *schema.ResourceData, meta interface{}
145139
d.Set("name", res.Name)
146140
d.Set("ssh_key", res.SshKey)
147141
d.Set("task_state", res.TaskState)
142+
d.Set("attached_volumes", attachedVolumes)
148143

149144
return nil
150145
}

ovh/data_cloud_project_instances.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func dataSourceCloudProjectInstances() *schema.Resource {
1919
Type: schema.TypeString,
2020
Required: true,
2121
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
22-
Description: "Service name of the resource representing the id of the cloud project.",
22+
Description: "Service name of the resource representing the id of the cloud project",
2323
},
2424
"region": {
2525
Type: schema.TypeString,
@@ -55,12 +55,12 @@ func dataSourceCloudProjectInstances() *schema.Resource {
5555
"attached_volumes": {
5656
Type: schema.TypeList,
5757
Computed: true,
58-
Description: " Volumes attached to the instance",
58+
Description: "Volumes attached to the instance",
5959
Elem: &schema.Resource{
6060
Schema: map[string]*schema.Schema{
6161
"id": {
6262
Type: schema.TypeString,
63-
Description: "Volume Id",
63+
Description: "Volume id",
6464
Computed: true,
6565
},
6666
},
@@ -78,7 +78,7 @@ func dataSourceCloudProjectInstances() *schema.Resource {
7878
},
7979
"name": {
8080
Type: schema.TypeString,
81-
Description: "Flavor name",
81+
Description: "Instance name",
8282
Computed: true,
8383
},
8484
"id": {
@@ -93,7 +93,7 @@ func dataSourceCloudProjectInstances() *schema.Resource {
9393
},
9494
"ssh_key": {
9595
Type: schema.TypeString,
96-
Description: "Instance task state",
96+
Description: "SSH Key pair name",
9797
Computed: true,
9898
},
9999
"task_state": {
@@ -124,10 +124,10 @@ func dataSourceCloudProjectInstancesRead(d *schema.ResourceData, meta interface{
124124
return helpers.CheckDeleted(d, err, endpoint)
125125
}
126126

127-
instances := make([]map[string]interface{}, len(res))
128-
ids := make([]string, len(res))
129-
for i, instance := range res {
130-
instances[i] = instance.ToMap()
127+
instances := make([]map[string]interface{}, 0, len(res))
128+
ids := make([]string, 0, len(res))
129+
for _, instance := range res {
130+
instances = append(instances, instance.ToMap())
131131
ids = append(ids, instance.Id)
132132
}
133133
sort.Strings(ids)

ovh/resource_cloud_project_instance.go

+26-71
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ import (
55
"fmt"
66
"log"
77
"net/url"
8-
"time"
98

109
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1210
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13-
"github.com/ovh/go-ovh/ovh"
1411
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
1512
)
1613

@@ -25,7 +22,7 @@ func resourceCloudProjectInstance() *schema.Resource {
2522
Type: schema.TypeString,
2623
Required: true,
2724
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
28-
Description: "Service name of the resource representing the id of the cloud project.",
25+
Description: "Service name of the resource representing the id of the cloud project",
2926
ForceNew: true,
3027
},
3128
"region": {
@@ -134,12 +131,12 @@ func resourceCloudProjectInstance() *schema.Resource {
134131
Optional: true,
135132
ForceNew: true,
136133
MaxItems: 1,
137-
Description: "Existing SSH Keypair",
134+
Description: "Existing SSH Key pair",
138135
Elem: &schema.Resource{
139136
Schema: map[string]*schema.Schema{
140137
"name": {
141138
Type: schema.TypeString,
142-
Description: "SSH Keypair name",
139+
Description: "SSH Key pair name",
143140
Required: true,
144141
},
145142
},
@@ -150,17 +147,17 @@ func resourceCloudProjectInstance() *schema.Resource {
150147
Optional: true,
151148
ForceNew: true,
152149
MaxItems: 1,
153-
Description: "Creatting SSH Keypair",
150+
Description: "Add existing SSH Key pair into your Public Cloud project and link it to the instance",
154151
Elem: &schema.Resource{
155152
Schema: map[string]*schema.Schema{
156153
"name": {
157154
Type: schema.TypeString,
158-
Description: "SSH Keypair name",
155+
Description: "SSH Key pair name",
159156
Required: true,
160157
},
161158
"public_key": {
162159
Type: schema.TypeString,
163-
Description: "Group id",
160+
Description: "SSH Public Key",
164161
Required: true,
165162
},
166163
},
@@ -211,12 +208,12 @@ func resourceCloudProjectInstance() *schema.Resource {
211208
"attached_volumes": {
212209
Type: schema.TypeSet,
213210
Computed: true,
214-
Description: " Volumes attached to the instance",
211+
Description: "Volumes attached to the instance",
215212
Elem: &schema.Resource{
216213
Schema: map[string]*schema.Schema{
217214
"id": {
218215
Type: schema.TypeString,
219-
Description: "Volume Id",
216+
Description: "Volume id",
220217
Computed: true,
221218
},
222219
},
@@ -258,107 +255,65 @@ func resourceCloudProjectInstanceCreate(ctx context.Context, d *schema.ResourceD
258255
params := new(CloudProjectInstanceCreateOpts)
259256
params.FromResource(d)
260257

261-
r := &CloudProjectOperation{}
262-
263258
endpoint := fmt.Sprintf("/cloud/project/%s/region/%s/instance",
264259
url.PathEscape(serviceName),
265260
url.PathEscape(region),
266261
)
267262

263+
r := &CloudProjectOperationResponse{}
268264
if err := config.OVHClient.Post(endpoint, params, r); err != nil {
269265
return diag.Errorf("calling %s with params %v:\n\t %q", endpoint, params, err)
270266
}
271267

272-
err := waitForInstanceCreation(ctx, config.OVHClient, serviceName, r.Id)
268+
instanceID, err := waitForCloudProjectOperation(ctx, config.OVHClient, serviceName, r.Id)
273269
if err != nil {
274270
return diag.Errorf("timeout instance creation: %s", err)
275271
}
276272

277-
endpointInstance := fmt.Sprintf("/cloud/project/%s/operation/%s",
278-
url.PathEscape(serviceName),
279-
url.PathEscape(r.Id),
280-
)
281-
282-
err = config.OVHClient.GetWithContext(ctx, endpointInstance, r)
283-
if err != nil {
284-
return diag.Errorf("failed to get instance id: %s", err)
285-
}
286-
287-
d.SetId(r.SubOperations[0].ResourceId)
273+
d.SetId(instanceID)
288274
d.Set("region", region)
289275

290276
return resourceCloudProjectInstanceRead(ctx, d, meta)
291277
}
292278

293-
func waitForInstanceCreation(ctx context.Context, client *ovh.Client, serviceName, operationId string) error {
294-
stateConf := &retry.StateChangeConf{
295-
Pending: []string{"null", "in-progress", "created", ""},
296-
Target: []string{"completed"},
297-
Refresh: func() (interface{}, string, error) {
298-
res := &CloudProjectOperation{}
299-
endpoint := fmt.Sprintf("/cloud/project/%s/operation/%s",
300-
url.PathEscape(serviceName),
301-
url.PathEscape(operationId),
302-
)
303-
err := client.GetWithContext(ctx, endpoint, res)
304-
if err != nil {
305-
return res, "", err
306-
}
307-
return res, res.Status, nil
308-
},
309-
Timeout: 360 * time.Second,
310-
Delay: 10 * time.Second,
311-
MinTimeout: 3 * time.Second,
312-
}
313-
314-
_, err := stateConf.WaitForStateContext(ctx)
315-
return err
316-
}
317-
318279
func resourceCloudProjectInstanceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
319280
config := meta.(*Config)
320281
id := d.Id()
321282
region := d.Get("region").(string)
322283
serviceName := d.Get("service_name").(string)
323284

324-
r := &CloudProjectInstanceResponse{}
325-
326285
endpoint := fmt.Sprintf("/cloud/project/%s/region/%s/instance/%s",
327286
url.PathEscape(serviceName),
328287
url.PathEscape(region),
329288
url.PathEscape(id),
330289
)
331290

291+
r := &CloudProjectInstanceResponse{}
332292
if err := config.OVHClient.Get(endpoint, r); err != nil {
333-
return diag.Errorf("Error calling post %s:\n\t %q", endpoint, err)
293+
return diag.Errorf("Error calling Get %s:\n\t %q", endpoint, err)
334294
}
335295

336296
d.Set("flavor_id", r.FlavorId)
337297
d.Set("flavor_name", r.FlavorName)
338298
d.Set("image_id", r.ImageId)
339299
d.Set("region", r.Region)
340300
d.Set("task_state", r.TaskState)
341-
d.Set("name", d.Get("name").(string))
342301
d.Set("id", r.Id)
343302

344-
addresses := make([]map[string]interface{}, 0)
345-
if r.Addresses != nil {
346-
for _, add := range r.Addresses {
347-
address := make(map[string]interface{})
348-
address["ip"] = add.Ip
349-
address["version"] = add.Version
350-
addresses = append(addresses, address)
351-
}
303+
addresses := make([]map[string]interface{}, 0, len(r.Addresses))
304+
for _, add := range r.Addresses {
305+
address := make(map[string]interface{})
306+
address["ip"] = add.Ip
307+
address["version"] = add.Version
308+
addresses = append(addresses, address)
352309
}
353310
d.Set("addresses", addresses)
354311

355-
attachedVolumes := make([]map[string]interface{}, 0)
356-
if r.AttachedVolumes != nil {
357-
for _, att := range r.AttachedVolumes {
358-
attachedVolume := make(map[string]interface{})
359-
attachedVolume["id"] = att.Id
360-
attachedVolumes = append(attachedVolumes, attachedVolume)
361-
}
312+
attachedVolumes := make([]map[string]interface{}, 0, len(r.AttachedVolumes))
313+
for _, att := range r.AttachedVolumes {
314+
attachedVolume := make(map[string]interface{})
315+
attachedVolume["id"] = att.Id
316+
attachedVolumes = append(attachedVolumes, attachedVolume)
362317
}
363318
d.Set("attached_volumes", attachedVolumes)
364319

@@ -380,11 +335,11 @@ func resourceCloudProjectInstanceDelete(ctx context.Context, d *schema.ResourceD
380335
)
381336

382337
if err := config.OVHClient.Delete(endpoint, nil); err != nil {
383-
return diag.Errorf("Error calling post %s:\n\t %q", endpoint, err)
338+
return diag.Errorf("Error calling Delete %s:\n\t %q", endpoint, err)
384339
}
385340

386341
d.SetId("")
387342

388-
log.Printf("[DEBUG] Deleted Public Cloud %s Gateway %s", serviceName, id)
343+
log.Printf("[DEBUG] Deleted Public Cloud %s Instance %s", serviceName, id)
389344
return nil
390345
}

0 commit comments

Comments
 (0)