Skip to content

Commit 7179dfc

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

12 files changed

+308
-220
lines changed

.cds/terraform-provider-ovh.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,17 @@ workflow:
568568
pipeline: terraform-provider-ovh-testacc
569569
when:
570570
- success
571+
Tests_TestAccCloudProjectInstance:
572+
application: terraform-provider-ovh
573+
depends_on:
574+
- terraform-provider-ovh-pre-sweepers
575+
environment: acctests
576+
one_at_a_time: true
577+
parameters:
578+
testargs: -run CloudProjectInstance
579+
pipeline: terraform-provider-ovh-testacc
580+
when:
581+
- success
571582
Tests_TestAccCloudProjectWorkflowBackup:
572583
application: terraform-provider-ovh
573584
depends_on:
@@ -807,6 +818,7 @@ workflow:
807818
- Tests_CloudProjectUser
808819
- Tests_CloudProjectLoadBalancer
809820
- Tests_CloudProjectVolume
821+
- Tests_TestAccCloudProjectInstance
810822
- Tests_DbaasLogs
811823
- Tests_DedicatedCeph
812824
- Tests_TestAccDedicatedInstallationTemplate

ovh/data_cloud_project_instance.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@ func dataSourceCloudProjectInstance() *schema.Resource {
2323
Type: schema.TypeString,
2424
Description: "Instance region",
2525
Required: true,
26-
ForceNew: true,
2726
},
2827
"instance_id": {
2928
Type: schema.TypeString,
3029
Description: "Instance id",
3130
Required: true,
32-
ForceNew: true,
3331
},
3432
// computed
3533
"addresses": {
36-
Type: schema.TypeList,
34+
Type: schema.TypeSet,
3735
Computed: true,
3836
Description: "Instance IP addresses",
3937
Elem: &schema.Resource{
@@ -52,12 +50,12 @@ func dataSourceCloudProjectInstance() *schema.Resource {
5250
},
5351
},
5452
"attached_volumes": {
55-
Type: schema.TypeList,
53+
Type: schema.TypeSet,
5654
Computed: true,
57-
Description: " Volumes attached to the instance",
55+
Description: "Volumes attached to the instance",
5856
Elem: &schema.Resource{
5957
Schema: map[string]*schema.Schema{
60-
"ip": {
58+
"id": {
6159
Type: schema.TypeString,
6260
Description: "Volume Id",
6361
Computed: true,
@@ -109,7 +107,7 @@ func dataSourceCloudProjectInstanceRead(d *schema.ResourceData, meta interface{}
109107
serviceName := d.Get("service_name").(string)
110108
region := d.Get("region").(string)
111109
instanceId := d.Get("instance_id").(string)
112-
log.Printf("[DEBUG] SCROUTCH")
110+
113111
endpoint := fmt.Sprintf("/cloud/project/%s/region/%s/instance/%s",
114112
url.PathEscape(serviceName),
115113
url.PathEscape(region),
@@ -121,15 +119,32 @@ func dataSourceCloudProjectInstanceRead(d *schema.ResourceData, meta interface{}
121119
if err := config.OVHClient.Get(endpoint, &res); err != nil {
122120
return helpers.CheckDeleted(d, err, endpoint)
123121
}
122+
log.Printf("[DEBUG] Read instance: %+v", res)
124123

125-
for k, v := range res.ToMap() {
126-
if k != "id" {
127-
d.Set(k, v)
128-
} else {
129-
d.SetId(fmt.Sprint(v))
130-
}
124+
addresses := make([]map[string]interface{}, 0)
125+
for i := range res.Addresses {
126+
address := make(map[string]interface{})
127+
address["ip"] = res.Addresses[i].Ip
128+
address["version"] = res.Addresses[i].Version
129+
addresses = append(addresses, address)
131130
}
132131

133-
log.Printf("[DEBUG] Read instance: %+v", res)
132+
attachedVolumes := make([]map[string]interface{}, 0)
133+
for i := range res.AttachedVolumes {
134+
attachedVolume := make(map[string]interface{})
135+
attachedVolume["id"] = res.AttachedVolumes[i].Id
136+
attachedVolumes = append(attachedVolumes, attachedVolume)
137+
}
138+
139+
d.Set("addresses", addresses)
140+
d.Set("flavor_id", res.FlavorId)
141+
d.Set("flavor_name", res.FlavorName)
142+
d.SetId(res.Id)
143+
d.Set("image_id", res.ImageId)
144+
d.Set("instance_id", res.Id)
145+
d.Set("name", res.Name)
146+
d.Set("ssh_key", res.SshKey)
147+
d.Set("task_state", res.TaskState)
148+
134149
return nil
135150
}

ovh/data_cloud_project_instance_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ import (
88
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
99
)
1010

11-
func TestAccDataSourceCloudProjecInstance_basic(t *testing.T) {
12-
13-
config := fmt.Sprintf(
14-
testAccDataSourceCloudProjectInstance,
11+
func TestAccDataSourceCloudProjectInstance_basic(t *testing.T) {
12+
config := fmt.Sprintf(`
13+
data "ovh_cloud_project_instance" "test" {
14+
service_name = "%s"
15+
region = "%s"
16+
instance_id = "%s"
17+
}
18+
`,
1519
os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST"),
1620
os.Getenv("OVH_CLOUD_PROJECT_REGION_TEST"),
17-
os.Getenv("OVH_CLOUD_PROJECT_INSTANCE_TEST"),
21+
os.Getenv("OVH_CLOUD_PROJECT_INSTANCE_ID_TEST"),
1822
)
1923

2024
resource.Test(t, resource.TestCase{
@@ -29,18 +33,14 @@ func TestAccDataSourceCloudProjecInstance_basic(t *testing.T) {
2933
Config: config,
3034
Check: resource.ComposeTestCheckFunc(
3135
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "flavor_name"),
36+
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "flavor_id"),
3237
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "id"),
3338
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "image_id"),
39+
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "name"),
40+
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "ssh_key"),
41+
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "region"),
3442
),
3543
},
3644
},
3745
})
3846
}
39-
40-
var testAccDataSourceCloudProjectInstance = `
41-
data "ovh_cloud_project_instance" "test" {
42-
service_name = "%s"
43-
region = "%s"
44-
instance_id = "%s"
45-
}
46-
`

ovh/data_cloud_project_instances.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"net/url"
7+
"sort"
78

89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
@@ -24,17 +25,16 @@ func dataSourceCloudProjectInstances() *schema.Resource {
2425
Type: schema.TypeString,
2526
Description: "Instance region",
2627
Required: true,
27-
ForceNew: true,
2828
},
2929
// computed
3030
"instances": {
31-
Type: schema.TypeList,
31+
Type: schema.TypeSet,
3232
Description: "List of instances",
3333
Computed: true,
3434
Elem: &schema.Resource{
3535
Schema: map[string]*schema.Schema{
3636
"addresses": {
37-
Type: schema.TypeList,
37+
Type: schema.TypeSet,
3838
Computed: true,
3939
Description: "Instance IP addresses",
4040
Elem: &schema.Resource{
@@ -58,7 +58,7 @@ func dataSourceCloudProjectInstances() *schema.Resource {
5858
Description: " Volumes attached to the instance",
5959
Elem: &schema.Resource{
6060
Schema: map[string]*schema.Schema{
61-
"ip": {
61+
"id": {
6262
Type: schema.TypeString,
6363
Description: "Volume Id",
6464
Computed: true,
@@ -123,17 +123,18 @@ func dataSourceCloudProjectInstancesRead(d *schema.ResourceData, meta interface{
123123
if err := config.OVHClient.Get(endpoint, &res); err != nil {
124124
return helpers.CheckDeleted(d, err, endpoint)
125125
}
126-
instances := make([]map[string]interface{}, len(res))
127-
ids := make([]string, len(instances))
128126

127+
instances := make([]map[string]interface{}, len(res))
128+
ids := make([]string, len(res))
129129
for i, instance := range res {
130130
instances[i] = instance.ToMap()
131131
ids = append(ids, instance.Id)
132132
}
133+
sort.Strings(ids)
133134

134135
d.SetId(hashcode.Strings(ids))
135136
d.Set("instances", instances)
136137

137-
log.Printf("[DEBUG] Read instances: %+v", res)
138+
log.Printf("[DEBUG] Read instances: %s", ids)
138139
return nil
139140
}

ovh/data_cloud_project_instances_test.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,15 @@ import (
88
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
99
)
1010

11-
const testAccDataSourceCloudProjecInstancesConfig_basic = `
12-
data "ovh_cloud_project_instances" "instances" {
13-
service_name = "%s"
14-
region = "%s"
15-
}
16-
`
17-
18-
func TestAccDataSourceCloudProjecInstances_basic(t *testing.T) {
19-
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
20-
region := os.Getenv("OVH_CLOUD_PROJECT_REGION_TEST")
21-
22-
config := fmt.Sprintf(
23-
testAccDataSourceCloudProjecInstancesConfig_basic,
24-
serviceName,
25-
region,
11+
func TestAccDataSourceCloudProjectInstances_basic(t *testing.T) {
12+
config := fmt.Sprintf(`
13+
data "ovh_cloud_project_instances" "instances" {
14+
service_name = "%s"
15+
region = "%s"
16+
}
17+
`,
18+
os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST"),
19+
os.Getenv("OVH_CLOUD_PROJECT_REGION_TEST"),
2620
)
2721

2822
resource.Test(t, resource.TestCase{
@@ -38,7 +32,23 @@ func TestAccDataSourceCloudProjecInstances_basic(t *testing.T) {
3832
Check: resource.ComposeTestCheckFunc(
3933
resource.TestCheckResourceAttrSet(
4034
"data.ovh_cloud_project_instances.instances",
41-
"instances.#",
35+
"instances.0.flavor_id",
36+
),
37+
resource.TestCheckResourceAttrSet(
38+
"data.ovh_cloud_project_instances.instances",
39+
"instances.0.flavor_name",
40+
),
41+
resource.TestCheckResourceAttrSet(
42+
"data.ovh_cloud_project_instances.instances",
43+
"instances.0.id",
44+
),
45+
resource.TestCheckResourceAttrSet(
46+
"data.ovh_cloud_project_instances.instances",
47+
"instances.0.image_id",
48+
),
49+
resource.TestCheckResourceAttrSet(
50+
"data.ovh_cloud_project_instances.instances",
51+
"instances.0.ssh_key",
4252
),
4353
),
4454
},

0 commit comments

Comments
 (0)