Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion internal/services/vpc/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import (
"context"
"errors"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -10,6 +12,7 @@
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

Expand All @@ -20,7 +23,56 @@
UpdateContext: ResourceVPCACLUpdate,
DeleteContext: ResourceVPCACLDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: func(
ctx context.Context,
d *schema.ResourceData,
m interface{},
) ([]*schema.ResourceData, error) {
// If importing by ID (e.g. "fr-par/8cef…"), we just set the ID field to state, allowing the read to fill in the rest of the data
if d.Id() != "" {
return []*schema.ResourceData{d}, nil
}

// Otherwise, we're importing by identity “identity = { id = ..., region = ... }”
identity, err := d.Identity()
if err != nil {
return nil, fmt.Errorf("error retrieving identity: %w", err)
}

rawID := identity.Get("id").(string)

regionVal := identity.Get("region").(string)
if regionVal == "" {
region, err := meta.ExtractRegion(d, m)
if err != nil {
return nil, errors.New("identity.region was not set")
}
regionVal = region.String()
}

localizedID := fmt.Sprintf("%s/%s", regionVal, rawID)

d.SetId(localizedID)

return []*schema.ResourceData{d}, nil
},
},
Identity: &schema.ResourceIdentity{
Version: 0,
SchemaFunc: func() map[string]*schema.Schema {
return map[string]*schema.Schema{
"id": {

Check failure on line 64 in internal/services/vpc/acl.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

S013: schema should configure one of Computed, Optional, or Required
Type: schema.TypeString,
RequiredForImport: true,
Description: "The ACL ID (e.g. `11111111-1111-1111-1111-111111111111`)",
},
"region": {

Check failure on line 69 in internal/services/vpc/acl.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

S013: schema should configure one of Computed, Optional, or Required
Type: schema.TypeString,
OptionalForImport: true,
Description: "The region of the VPC. If omitted during import, defaults from provider",
},
}
},
},
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
Expand Down
65 changes: 64 additions & 1 deletion internal/services/vpc/private_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import (
"context"
"errors"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
Expand All @@ -25,7 +27,56 @@
UpdateContext: ResourceVPCPrivateNetworkUpdate,
DeleteContext: ResourceVPCPrivateNetworkDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: func(
ctx context.Context,
d *schema.ResourceData,
m interface{},
) ([]*schema.ResourceData, error) {
// If importing by ID (e.g. "fr-par/8cef…"), we just set the ID field to state, allowing the read to fill in the rest of the data
if d.Id() != "" {
return []*schema.ResourceData{d}, nil
}

// Otherwise, we're importing by identity “identity = { id = ..., region = ... }”
identity, err := d.Identity()
if err != nil {
return nil, fmt.Errorf("error retrieving identity: %w", err)
}

rawID := identity.Get("id").(string)

regionVal := identity.Get("region").(string)
if regionVal == "" {
region, err := meta.ExtractRegion(d, m)
if err != nil {
return nil, errors.New("identity.region was not set")
}
regionVal = region.String()
}

localizedID := fmt.Sprintf("%s/%s", regionVal, rawID)

d.SetId(localizedID)

return []*schema.ResourceData{d}, nil
},
},
Identity: &schema.ResourceIdentity{
Version: 0,
SchemaFunc: func() map[string]*schema.Schema {
return map[string]*schema.Schema{
"id": {

Check failure on line 68 in internal/services/vpc/private_network.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

S013: schema should configure one of Computed, Optional, or Required
Type: schema.TypeString,
RequiredForImport: true,
Description: "The Private Network ID (e.g. `11111111-1111-1111-1111-111111111111`)",
},
"region": {

Check failure on line 73 in internal/services/vpc/private_network.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

S013: schema should configure one of Computed, Optional, or Required
Type: schema.TypeString,
OptionalForImport: true,
Description: "The region of the VPC. If omitted during import, defaults from provider",
},
}
},
},
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
Expand Down Expand Up @@ -266,6 +317,18 @@
_ = d.Set("ipv4_subnet", ipv4Subnet)
_ = d.Set("ipv6_subnets", ipv6Subnets)

identity, err := d.Identity()
if err != nil {
return diag.FromErr(err)
}

if err = identity.Set("id", pn.ID); err != nil {
return diag.FromErr(err)
}
if err = identity.Set("region", region); err != nil {
return diag.FromErr(err)
}

return nil
}

Expand Down
54 changes: 53 additions & 1 deletion internal/services/vpc/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import (
"context"
"errors"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -11,6 +13,7 @@
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
)

Expand All @@ -21,7 +24,56 @@
UpdateContext: ResourceRouteUpdate,
DeleteContext: ResourceRouteDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: func(
ctx context.Context,
d *schema.ResourceData,
m interface{},
) ([]*schema.ResourceData, error) {
// If importing by ID (e.g. "fr-par/8cef…"), we just set the ID field to state, allowing the read to fill in the rest of the data
if d.Id() != "" {
return []*schema.ResourceData{d}, nil
}

// Otherwise, we're importing by identity “identity = { id = ..., region = ... }”
identity, err := d.Identity()
if err != nil {
return nil, fmt.Errorf("error retrieving identity: %w", err)
}

rawID := identity.Get("id").(string)

regionVal := identity.Get("region").(string)
if regionVal == "" {
region, err := meta.ExtractRegion(d, m)
if err != nil {
return nil, errors.New("identity.region was not set")
}
regionVal = region.String()
}

localizedID := fmt.Sprintf("%s/%s", regionVal, rawID)

d.SetId(localizedID)

return []*schema.ResourceData{d}, nil
},
},
Identity: &schema.ResourceIdentity{
Version: 0,
SchemaFunc: func() map[string]*schema.Schema {
return map[string]*schema.Schema{
"id": {

Check failure on line 65 in internal/services/vpc/route.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

S013: schema should configure one of Computed, Optional, or Required
Type: schema.TypeString,
RequiredForImport: true,
Description: "The Route ID (e.g. `11111111-1111-1111-1111-111111111111`)",
},
"region": {

Check failure on line 70 in internal/services/vpc/route.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

S013: schema should configure one of Computed, Optional, or Required
Type: schema.TypeString,
OptionalForImport: true,
Description: "The region of the VPC. If omitted during import, defaults from provider",
},
}
},
},
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
Expand Down
65 changes: 64 additions & 1 deletion internal/services/vpc/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import (
"context"
"errors"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/vpc/v2"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
)
Expand All @@ -21,7 +23,56 @@
UpdateContext: ResourceVPCUpdate,
DeleteContext: ResourceVPCDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: func(
ctx context.Context,
d *schema.ResourceData,
m interface{},
) ([]*schema.ResourceData, error) {
// If importing by ID (e.g. "fr-par/8cef…"), we just set the ID field to state, allowing the read to fill in the rest of the data
if d.Id() != "" {
return []*schema.ResourceData{d}, nil
}

// Otherwise, we're importing by identity “identity = { id = ..., region = ... }”
identity, err := d.Identity()
if err != nil {
return nil, fmt.Errorf("error retrieving identity: %w", err)
}

rawID := identity.Get("id").(string)

regionVal := identity.Get("region").(string)
if regionVal == "" {
region, err := meta.ExtractRegion(d, m)
if err != nil {
return nil, errors.New("identity.region was not set")
}
regionVal = region.String()
}

localizedID := fmt.Sprintf("%s/%s", regionVal, rawID)

d.SetId(localizedID)

return []*schema.ResourceData{d}, nil
},
},
Identity: &schema.ResourceIdentity{
Version: 0,
SchemaFunc: func() map[string]*schema.Schema {
return map[string]*schema.Schema{
"id": {

Check failure on line 64 in internal/services/vpc/vpc.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

S013: schema should configure one of Computed, Optional, or Required
Type: schema.TypeString,
RequiredForImport: true,
Description: "The VPC ID (e.g. `11111111-1111-1111-1111-111111111111`)",
},
"region": {

Check failure on line 69 in internal/services/vpc/vpc.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

S013: schema should configure one of Computed, Optional, or Required
Type: schema.TypeString,
OptionalForImport: true,
Description: "The region of the VPC. If omitted during import, defaults from provider",
},
}
},
},
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -131,6 +182,18 @@
_ = d.Set("tags", res.Tags)
}

identity, err := d.Identity()
if err != nil {
return diag.FromErr(err)
}

if err = identity.Set("id", res.ID); err != nil {
return diag.FromErr(err)
}
if err = identity.Set("region", region); err != nil {
return diag.FromErr(err)
}

return nil
}

Expand Down
Loading