Skip to content

Commit ae24132

Browse files
authored
feat(vpc): add vpc datasource (#1963)
1 parent 22a6223 commit ae24132

7 files changed

+1376
-1
lines changed

docs/data-sources/vpc.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
subcategory: "VPC"
3+
page_title: "Scaleway: scaleway_vpc"
4+
---
5+
6+
# scaleway_vpc
7+
8+
Gets information about a Scaleway Virtual Private Cloud.
9+
10+
## Example Usage
11+
12+
```hcl
13+
# Get info by name
14+
data "scaleway_vpc" "by_name" {
15+
name = "foobar"
16+
}
17+
18+
# Get info by ID
19+
data "scaleway_vpc" "by_id" {
20+
vpc_id = "11111111-1111-1111-1111-111111111111"
21+
}
22+
23+
# Get default VPC info
24+
data "scaleway_vpc" "default" {
25+
is_default = true
26+
}
27+
```
28+
29+
## Argument Reference
30+
31+
* `name` - (Optional) Name of the VPC. One of `name` and `vpc_id` should be specified.
32+
* `vpc_id` - (Optional) ID of the VPC. One of `name` and `vpc_id` should be specified.
33+
* `is_default` - (Optional) To get default VPC's information.
34+
* `organization_id` - The ID of the organization the VPC is associated with.
35+
* `project_id` - (Optional. Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the VPC is associated with.
36+
37+
## Attributes Reference
38+
39+
Exported attributes are the ones from `scaleway_vpc` [resource](../resources/vpc.md)
40+

scaleway/data_source_vpc.go

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/scaleway/scaleway-sdk-go/api/vpc/v2"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
11+
)
12+
13+
func dataSourceScalewayVPC() *schema.Resource {
14+
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayVPC().Schema)
15+
16+
addOptionalFieldsToSchema(dsSchema, "name", "is_default", "region")
17+
18+
dsSchema["name"].ConflictsWith = []string{"vpc_id"}
19+
dsSchema["vpc_id"] = &schema.Schema{
20+
Type: schema.TypeString,
21+
Optional: true,
22+
Description: "The ID of the VPC",
23+
ValidateFunc: validationUUIDorUUIDWithLocality(),
24+
ConflictsWith: []string{"name"},
25+
}
26+
dsSchema["organization_id"] = organizationIDOptionalSchema()
27+
dsSchema["project_id"] = &schema.Schema{
28+
Type: schema.TypeString,
29+
Optional: true,
30+
Description: "The project ID the resource is associated to",
31+
ValidateFunc: validationUUID(),
32+
}
33+
34+
return &schema.Resource{
35+
Schema: dsSchema,
36+
ReadContext: dataSourceScalewayVPCRead,
37+
}
38+
}
39+
40+
func dataSourceScalewayVPCRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
41+
vpcAPI, region, err := vpcAPIWithRegion(d, meta)
42+
if err != nil {
43+
return diag.FromErr(err)
44+
}
45+
46+
var vpcID interface{}
47+
var ok bool
48+
49+
if d.Get("is_default").(bool) {
50+
request := &vpc.ListVPCsRequest{
51+
IsDefault: expandBoolPtr(d.Get("is_default").(bool)),
52+
Region: region,
53+
}
54+
55+
res, err := vpcAPI.ListVPCs(request, scw.WithContext(ctx))
56+
if err != nil {
57+
return diag.FromErr(err)
58+
}
59+
60+
vpcID = newRegionalIDString(region, res.Vpcs[0].ID)
61+
} else {
62+
vpcID, ok = d.GetOk("vpc_id")
63+
if !ok {
64+
request := &vpc.ListVPCsRequest{
65+
Name: expandStringPtr(d.Get("name").(string)),
66+
Region: region,
67+
ProjectID: expandStringPtr(d.Get("project_id")),
68+
OrganizationID: expandStringPtr(d.Get("organization_id")),
69+
}
70+
71+
res, err := vpcAPI.ListVPCs(request, scw.WithContext(ctx))
72+
if err != nil {
73+
return diag.FromErr(err)
74+
}
75+
76+
for _, v := range res.Vpcs {
77+
if v.Name == d.Get("name").(string) {
78+
if vpcID != "" {
79+
return diag.FromErr(fmt.Errorf("more than 1 VPC found with the same name %s", d.Get("name")))
80+
}
81+
vpcID = newRegionalIDString(region, v.ID)
82+
}
83+
}
84+
if res.TotalCount == 0 {
85+
return diag.FromErr(fmt.Errorf("no VPC found with the name %s", d.Get("name")))
86+
}
87+
}
88+
}
89+
90+
regionalID := datasourceNewRegionalizedID(vpcID, region)
91+
d.SetId(regionalID)
92+
err = d.Set("vpc_id", regionalID)
93+
if err != nil {
94+
return diag.FromErr(err)
95+
}
96+
97+
diags := resourceScalewayVPCRead(ctx, d, meta)
98+
if diags != nil {
99+
return append(diags, diag.Errorf("failed to read VPC")...)
100+
}
101+
102+
if d.Id() == "" {
103+
return diag.Errorf("VPC (%s) not found", regionalID)
104+
}
105+
106+
return nil
107+
}

scaleway/data_source_vpc_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package scaleway
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccScalewayDataSourceVPC_Basic(t *testing.T) {
11+
tt := NewTestTools(t)
12+
defer tt.Cleanup()
13+
vpcName := "DataSourceVPC_Basic"
14+
resource.ParallelTest(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
ProviderFactories: tt.ProviderFactories,
17+
CheckDestroy: testAccCheckScalewayVPCDestroy(tt),
18+
Steps: []resource.TestStep{
19+
{
20+
Config: fmt.Sprintf(`
21+
resource "scaleway_vpc" "vpc01" {
22+
name = "%s"
23+
}`, vpcName),
24+
},
25+
{
26+
Config: fmt.Sprintf(`
27+
resource "scaleway_vpc" "vpc01" {
28+
name = "%s"
29+
}
30+
31+
data "scaleway_vpc" "by_name" {
32+
name = "${scaleway_vpc.vpc01.name}"
33+
}
34+
35+
data "scaleway_vpc" "by_id" {
36+
vpc_id = "${scaleway_vpc.vpc01.id}"
37+
}
38+
`, vpcName),
39+
Check: resource.ComposeTestCheckFunc(
40+
testAccCheckScalewayVPCExists(tt, "scaleway_vpc.vpc01"),
41+
resource.TestCheckResourceAttrPair("data.scaleway_vpc.by_name", "vpc_id", "scaleway_vpc.vpc01", "id"),
42+
resource.TestCheckResourceAttrPair("data.scaleway_vpc.by_id", "name", "scaleway_vpc.vpc01", "name"),
43+
),
44+
},
45+
},
46+
})
47+
}
48+
49+
func TestAccScalewayDataSourceVPC_Default(t *testing.T) {
50+
tt := NewTestTools(t)
51+
defer tt.Cleanup()
52+
resource.ParallelTest(t, resource.TestCase{
53+
PreCheck: func() { testAccPreCheck(t) },
54+
ProviderFactories: tt.ProviderFactories,
55+
Steps: []resource.TestStep{
56+
{
57+
Config: `
58+
data "scaleway_vpc" "default" {
59+
is_default = true
60+
}
61+
`,
62+
Check: resource.ComposeTestCheckFunc(
63+
resource.TestCheckResourceAttrSet("data.scaleway_vpc.default", "id"),
64+
resource.TestCheckResourceAttr("data.scaleway_vpc.default", "name", "default"),
65+
resource.TestCheckResourceAttr("data.scaleway_vpc.default", "is_default", "true"),
66+
resource.TestCheckResourceAttr("data.scaleway_vpc.default", "tags.0", "default"),
67+
),
68+
},
69+
},
70+
})
71+
}

scaleway/helpers_vpc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func vpcAPIWithRegion(d *schema.ResourceData, m interface{}) (*v2.API, scw.Regio
4545
return vpcAPI, region, err
4646
}
4747

48-
// vpcAPIWithRegionAndID
48+
// vpcAPIWithRegionAndID returns a new VPC API with locality and ID extracted from the state
4949
func vpcAPIWithRegionAndID(m interface{}, id string) (*v2.API, scw.Region, string, error) {
5050
meta := m.(*Meta)
5151
vpcAPI := v2.NewAPI(meta.scwClient)

scaleway/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
226226
"scaleway_secret": dataSourceScalewaySecret(),
227227
"scaleway_secret_version": dataSourceScalewaySecretVersion(),
228228
"scaleway_registry_image": dataSourceScalewayRegistryImage(),
229+
"scaleway_vpc": dataSourceScalewayVPC(),
229230
"scaleway_vpc_public_gateway": dataSourceScalewayVPCPublicGateway(),
230231
"scaleway_vpc_gateway_network": dataSourceScalewayVPCGatewayNetwork(),
231232
"scaleway_vpc_public_gateway_dhcp": dataSourceScalewayVPCPublicGatewayDHCP(),

0 commit comments

Comments
 (0)