Skip to content

feat(vpc): add support for ResourceIdentity #3143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
55 changes: 54 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,57 @@
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 65 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 70 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
66 changes: 65 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,57 @@
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 69 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 74 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 +318,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
55 changes: 54 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,57 @@
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 66 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 71 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
66 changes: 65 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,57 @@
UpdateContext: ResourceVPCUpdate,
DeleteContext: ResourceVPCDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: func(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a way to make a global function, since it'll basically be the same across all resources, right?

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/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 70 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 +183,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