Skip to content

Commit 0de7674

Browse files
authored
feat(vpc): add vpcs datasource (#2055)
1 parent 476c467 commit 0de7674

File tree

5 files changed

+1188
-0
lines changed

5 files changed

+1188
-0
lines changed

docs/data-sources/vpcs.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
subcategory: "VPC"
3+
page_title: "Scaleway: scaleway_vpcs"
4+
---
5+
6+
# scaleway_vpcs
7+
8+
Gets information about multiple Virtual Private Clouds.
9+
10+
## Example Usage
11+
12+
```hcl
13+
# Find VPCs that share the same tags
14+
data "scaleway_vpcs" "my_key" {
15+
tags = ["tag"]
16+
}
17+
18+
# Find VPCs by name and region
19+
data "scaleway_vpcs" "my_key" {
20+
name = "tf-vpc-datasource"
21+
region = "nl-ams"
22+
}
23+
```
24+
25+
## Argument Reference
26+
27+
- `name` - (Optional) The VPC name used as filter. VPCs with a name like it are listed.
28+
29+
- `tags` - (Optional) List of tags used as filter. VPCs with these exact tags are listed.
30+
31+
- `region` - (Defaults to [provider](../index.md#region) `region`). The [region](../guides/regions_and_zones.md#regions) in which vpcs exist.
32+
33+
## Attributes Reference
34+
35+
In addition to all arguments above, the following attributes are exported:
36+
37+
- `vpcs` - List of found vpcs
38+
- `id` - The associated VPC ID.
39+
~> **Important:** VPCs' IDs are [regional](../guides/regions_and_zones.md#resource-ids), which means they are of the form `{region}/{id}`, e.g. `fr-par/11111111-1111-1111-1111-111111111111
40+
- `is_default` - Defines whether the VPC is the default one for its Project.
41+
- `created_at` - Date and time of VPC's creation (RFC 3339 format).
42+
- `updated_at` - Date and time of VPC's last update (RFC 3339 format).
43+
- `organization_id` - The organization ID the VPC is associated with.
44+
- `project_id` - The ID of the project the VPC is associated with.

scaleway/data_source_vpcs.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/scaleway/scaleway-sdk-go/api/vpc/v2"
9+
"github.com/scaleway/scaleway-sdk-go/scw"
10+
)
11+
12+
func dataSourceScalewayVPCs() *schema.Resource {
13+
return &schema.Resource{
14+
ReadContext: dataSourceScalewayVPCsRead,
15+
Schema: map[string]*schema.Schema{
16+
"name": {
17+
Type: schema.TypeString,
18+
Optional: true,
19+
Description: "VPCs with a name like it are listed.",
20+
},
21+
"tags": {
22+
Type: schema.TypeList,
23+
Elem: &schema.Schema{
24+
Type: schema.TypeString,
25+
},
26+
Optional: true,
27+
Description: "VPCs with these exact tags are listed.",
28+
},
29+
"vpcs": {
30+
Type: schema.TypeList,
31+
Computed: true,
32+
Elem: &schema.Resource{
33+
Schema: map[string]*schema.Schema{
34+
"id": {
35+
Computed: true,
36+
Type: schema.TypeString,
37+
},
38+
"name": {
39+
Computed: true,
40+
Type: schema.TypeString,
41+
},
42+
"tags": {
43+
Computed: true,
44+
Type: schema.TypeList,
45+
Elem: &schema.Schema{
46+
Type: schema.TypeString,
47+
},
48+
},
49+
"created_at": {
50+
Computed: true,
51+
Type: schema.TypeString,
52+
},
53+
"update_at": {
54+
Computed: true,
55+
Type: schema.TypeString,
56+
},
57+
"is_default": {
58+
Computed: true,
59+
Type: schema.TypeBool,
60+
},
61+
"region": regionSchema(),
62+
"organization_id": organizationIDSchema(),
63+
"project_id": projectIDSchema(),
64+
},
65+
},
66+
},
67+
"region": regionSchema(),
68+
"organization_id": organizationIDSchema(),
69+
"project_id": projectIDSchema(),
70+
},
71+
}
72+
}
73+
74+
func dataSourceScalewayVPCsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
75+
vpcAPI, region, err := vpcAPIWithRegion(d, meta)
76+
if err != nil {
77+
return diag.FromErr(err)
78+
}
79+
80+
res, err := vpcAPI.ListVPCs(&vpc.ListVPCsRequest{
81+
Region: region,
82+
Tags: expandStrings(d.Get("tags")),
83+
Name: expandStringPtr(d.Get("name")),
84+
ProjectID: expandStringPtr(d.Get("project_id")),
85+
}, scw.WithContext(ctx))
86+
if err != nil {
87+
return diag.FromErr(err)
88+
}
89+
90+
vpcs := []interface{}(nil)
91+
for _, virtualPrivateCloud := range res.Vpcs {
92+
rawVpc := make(map[string]interface{})
93+
rawVpc["id"] = newRegionalIDString(region, virtualPrivateCloud.ID)
94+
rawVpc["name"] = virtualPrivateCloud.Name
95+
rawVpc["created_at"] = flattenTime(virtualPrivateCloud.CreatedAt)
96+
rawVpc["update_at"] = flattenTime(virtualPrivateCloud.UpdatedAt)
97+
rawVpc["is_default"] = virtualPrivateCloud.IsDefault
98+
if len(virtualPrivateCloud.Tags) > 0 {
99+
rawVpc["tags"] = virtualPrivateCloud.Tags
100+
}
101+
rawVpc["region"] = region.String()
102+
rawVpc["organization_id"] = virtualPrivateCloud.OrganizationID
103+
rawVpc["project_id"] = virtualPrivateCloud.ProjectID
104+
105+
vpcs = append(vpcs, rawVpc)
106+
}
107+
108+
d.SetId(region.String())
109+
_ = d.Set("vpcs", vpcs)
110+
111+
return nil
112+
}

scaleway/data_source_vpcs_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package scaleway
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccScalewayDataSourceVPCs_Basic(t *testing.T) {
10+
tt := NewTestTools(t)
11+
defer tt.Cleanup()
12+
resource.ParallelTest(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
ProviderFactories: tt.ProviderFactories,
15+
CheckDestroy: testAccCheckScalewayInstanceServerDestroy(tt),
16+
Steps: []resource.TestStep{
17+
{
18+
Config: `
19+
resource scaleway_vpc vpc01 {
20+
name = "tf-vpc-datasource0"
21+
tags = [ "terraform-test", "data_scaleway_vpcs", "basic" ]
22+
}`,
23+
},
24+
{
25+
Config: `
26+
resource scaleway_vpc vpc01 {
27+
name = "tf-vpc-datasource0"
28+
tags = [ "terraform-test", "data_scaleway_vpcs", "basic" ]
29+
}
30+
31+
resource scaleway_vpc vpc02 {
32+
name = "tf-vpc-datasource1"
33+
tags = [ "terraform-test", "data_scaleway_vpcs", "basic" ]
34+
}`,
35+
},
36+
{
37+
Config: `
38+
resource scaleway_vpc vpc01 {
39+
name = "tf-vpc-datasource0"
40+
tags = [ "terraform-test", "data_scaleway_vpcs", "basic" ]
41+
}
42+
43+
resource scaleway_vpc vpc02 {
44+
name = "tf-vpc-datasource1"
45+
tags = [ "terraform-test", "data_scaleway_vpcs", "basic" ]
46+
}
47+
data scaleway_vpcs vpcs_by_name {
48+
name = "tf-vpc-datasource"
49+
}
50+
51+
data scaleway_vpcs vpcs_by_tag {
52+
tags = ["data_scaleway_vpcs", "terraform-test"]
53+
}
54+
55+
data scaleway_vpcs vpcs_by_name_other_region {
56+
name = "tf-vpc-datasource"
57+
region = "nl-ams"
58+
}`,
59+
Check: resource.ComposeTestCheckFunc(
60+
resource.TestCheckResourceAttrSet("data.scaleway_vpcs.vpcs_by_tag", "vpcs.0.id"),
61+
resource.TestCheckResourceAttrSet("data.scaleway_vpcs.vpcs_by_tag", "vpcs.1.id"),
62+
63+
resource.TestCheckResourceAttrSet("data.scaleway_vpcs.vpcs_by_name", "vpcs.0.id"),
64+
resource.TestCheckResourceAttrSet("data.scaleway_vpcs.vpcs_by_name", "vpcs.1.id"),
65+
66+
resource.TestCheckNoResourceAttr("data.scaleway_vpcs.vpcs_by_name_other_region", "vpcs.0.id"),
67+
),
68+
},
69+
},
70+
})
71+
}

scaleway/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
228228
"scaleway_secret_version": dataSourceScalewaySecretVersion(),
229229
"scaleway_registry_image": dataSourceScalewayRegistryImage(),
230230
"scaleway_vpc": dataSourceScalewayVPC(),
231+
"scaleway_vpcs": dataSourceScalewayVPCs(),
231232
"scaleway_vpc_public_gateway": dataSourceScalewayVPCPublicGateway(),
232233
"scaleway_vpc_gateway_network": dataSourceScalewayVPCGatewayNetwork(),
233234
"scaleway_vpc_public_gateway_dhcp": dataSourceScalewayVPCPublicGatewayDHCP(),

0 commit comments

Comments
 (0)