Skip to content

Commit

Permalink
chore: migrate datasource to its own package (#2456)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone authored Mar 13, 2024
1 parent b3b0802 commit 4f33d1d
Show file tree
Hide file tree
Showing 65 changed files with 246 additions and 177 deletions.
35 changes: 20 additions & 15 deletions scaleway/data_source_helpers.go → internal/datasource/schemas.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package scaleway
package datasource

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -7,7 +7,7 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
)

func datasourceNewZonedID(idI interface{}, fallBackZone scw.Zone) string {
func NewZonedID(idI interface{}, fallBackZone scw.Zone) string {
zone, id, err := zonal.ParseID(idI.(string))
if err != nil {
id = idI.(string)
Expand All @@ -17,7 +17,7 @@ func datasourceNewZonedID(idI interface{}, fallBackZone scw.Zone) string {
return zonal.NewIDString(zone, id)
}

func datasourceNewRegionalID(idI interface{}, fallBackRegion scw.Region) string {
func NewRegionalID(idI interface{}, fallBackRegion scw.Region) string {
region, id, err := regional.ParseID(idI.(string))
if err != nil {
id = idI.(string)
Expand All @@ -27,18 +27,16 @@ func datasourceNewRegionalID(idI interface{}, fallBackRegion scw.Region) string
return regional.NewIDString(region, id)
}

////
// The below methods are imported from Google's terraform provider.
// source: https://github.com/terraform-providers/terraform-provider-google/blob/master/google/datasource_helpers.go
////

// datasourceSchemaFromResourceSchema is a recursive func that
// SchemaFromResourceSchema is a recursive func that
// converts an existing Resource schema to a Datasource schema.
// All schema elements are copied, but certain attributes are ignored or changed:
// - all attributes have Computed = true
// - all attributes have ForceNew, Required = false
// - Validation funcs and attributes (e.g. MaxItems) are not copied
func datasourceSchemaFromResourceSchema(rs map[string]*schema.Schema) map[string]*schema.Schema {
//
// code imported from Google's terraform provider.
// source: https://github.com/hashicorp/terraform-provider-google/blob/main/google/tpgresource/datasource_helpers.go
func SchemaFromResourceSchema(rs map[string]*schema.Schema) map[string]*schema.Schema {
ds := make(map[string]*schema.Schema, len(rs))
for k, v := range rs {
dv := &schema.Schema{
Expand All @@ -59,7 +57,7 @@ func datasourceSchemaFromResourceSchema(rs map[string]*schema.Schema) map[string
if elem, ok := v.Elem.(*schema.Resource); ok {
// handle the case where the Element is a sub-resource
dv.Elem = &schema.Resource{
Schema: datasourceSchemaFromResourceSchema(elem.Schema),
Schema: SchemaFromResourceSchema(elem.Schema),
}
} else {
// handle simple primitive case
Expand All @@ -75,20 +73,27 @@ func datasourceSchemaFromResourceSchema(rs map[string]*schema.Schema) map[string
return ds
}

// fixDatasourceSchemaFlags is a convenience func that toggles the Computed,
// FixDatasourceSchemaFlags is a convenience func that toggles the Computed,
// Optional + Required flags on a schema element. This is useful when the schema
// has been generated (using `datasourceSchemaFromResourceSchema` above for
// example) and therefore the attribute flags were not set appropriately when
// first added to the schema definition. Currently only supports top-level
// schema elements.
func fixDatasourceSchemaFlags(schema map[string]*schema.Schema, required bool, keys ...string) {
//
// code imported from Google's terraform provider.
// source: https://github.com/hashicorp/terraform-provider-google/blob/main/google/tpgresource/datasource_helpers.go
func FixDatasourceSchemaFlags(schema map[string]*schema.Schema, required bool, keys ...string) {
for _, v := range keys {
schema[v].Computed = false
schema[v].Optional = !required
schema[v].Required = required
}
}

func addOptionalFieldsToSchema(schema map[string]*schema.Schema, keys ...string) {
fixDatasourceSchemaFlags(schema, false, keys...)
// AddOptionalFieldsToSchema
//
// code imported from Google's terraform provider.
// source: https://github.com/hashicorp/terraform-provider-google/blob/main/google/tpgresource/datasource_helpers.go
func AddOptionalFieldsToSchema(schema map[string]*schema.Schema, keys ...string) {
FixDatasourceSchemaFlags(schema, false, keys...)
}
5 changes: 3 additions & 2 deletions scaleway/data_source_account_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
accountV3 "github.com/scaleway/scaleway-sdk-go/api/account/v3"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func dataSourceScalewayAccountProject() *schema.Resource {
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayAccountProject().Schema)
addOptionalFieldsToSchema(dsSchema, "name", "organization_id")
dsSchema := datasource.SchemaFromResourceSchema(resourceScalewayAccountProject().Schema)
datasource.AddOptionalFieldsToSchema(dsSchema, "name", "organization_id")

dsSchema["name"].ConflictsWith = []string{"project_id"}
dsSchema["project_id"] = &schema.Schema{
Expand Down
5 changes: 3 additions & 2 deletions scaleway/data_source_baremetal_offer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
)

Expand Down Expand Up @@ -150,7 +151,7 @@ func dataSourceScalewayBaremetalOfferRead(ctx context.Context, d *schema.Resourc
return diag.FromErr(err)
}

zone, offerID, _ := zonal.ParseID(datasourceNewZonedID(d.Get("offer_id"), fallBackZone))
zone, offerID, _ := zonal.ParseID(datasource.NewZonedID(d.Get("offer_id"), fallBackZone))

var offer *baremetal.Offer

Expand Down Expand Up @@ -203,7 +204,7 @@ func dataSourceScalewayBaremetalOfferRead(ctx context.Context, d *schema.Resourc
offer = matches[0]
}

zonedID := datasourceNewZonedID(offer.ID, zone)
zonedID := datasource.NewZonedID(offer.ID, zone)
d.SetId(zonedID)
_ = d.Set("offer_id", zonedID)
_ = d.Set("zone", zone)
Expand Down
3 changes: 2 additions & 1 deletion scaleway/data_source_baremetal_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)
Expand Down Expand Up @@ -77,7 +78,7 @@ func dataSourceScalewayBaremetalOptionRead(ctx context.Context, d *schema.Resour
}
}

zoneID := datasourceNewZonedID(optionID, zone)
zoneID := datasource.NewZonedID(optionID, zone)
d.SetId(zoneID)

_ = d.Set("option_id", zoneID)
Expand Down
3 changes: 2 additions & 1 deletion scaleway/data_source_baremetal_os.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)
Expand Down Expand Up @@ -79,7 +80,7 @@ func dataSourceScalewayBaremetalOsRead(ctx context.Context, d *schema.ResourceDa
}
}

zoneID := datasourceNewZonedID(osID, zone)
zoneID := datasource.NewZonedID(osID, zone)
d.SetId(zoneID)

_ = d.Set("os_id", zoneID)
Expand Down
7 changes: 4 additions & 3 deletions scaleway/data_source_baremetal_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func dataSourceScalewayBaremetalServer() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayBaremetalServer().Schema)
dsSchema := datasource.SchemaFromResourceSchema(resourceScalewayBaremetalServer().Schema)

// Set 'Optional' schema elements
addOptionalFieldsToSchema(dsSchema, "name", "zone", "project_id")
datasource.AddOptionalFieldsToSchema(dsSchema, "name", "zone", "project_id")

dsSchema["name"].ConflictsWith = []string{"server_id"}
dsSchema["server_id"] = &schema.Schema{
Expand Down Expand Up @@ -63,7 +64,7 @@ func dataSourceScalewayBaremetalServerRead(ctx context.Context, d *schema.Resour
serverID = foundServer.ID
}

zoneID := datasourceNewZonedID(serverID, zone)
zoneID := datasource.NewZonedID(serverID, zone)
d.SetId(zoneID)
err = d.Set("server_id", zoneID)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions scaleway/data_source_block_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func dataSourceScalewayBlockSnapshot() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayBlockSnapshot().Schema)
dsSchema := datasource.SchemaFromResourceSchema(resourceScalewayBlockSnapshot().Schema)

addOptionalFieldsToSchema(dsSchema, "name", "zone", "volume_id", "project_id")
datasource.AddOptionalFieldsToSchema(dsSchema, "name", "zone", "volume_id", "project_id")

dsSchema["snapshot_id"] = &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -60,7 +61,7 @@ func dataSourceScalewayBlockSnapshotRead(ctx context.Context, d *schema.Resource
}
}

zoneID := datasourceNewZonedID(snapshotID, zone)
zoneID := datasource.NewZonedID(snapshotID, zone)
d.SetId(zoneID)
err = d.Set("snapshot_id", zoneID)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions scaleway/data_source_block_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func dataSourceScalewayBlockVolume() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayBlockVolume().Schema)
dsSchema := datasource.SchemaFromResourceSchema(resourceScalewayBlockVolume().Schema)

addOptionalFieldsToSchema(dsSchema, "name", "zone", "project_id")
datasource.AddOptionalFieldsToSchema(dsSchema, "name", "zone", "project_id")

dsSchema["volume_id"] = &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -59,7 +60,7 @@ func dataSourceScalewayBlockVolumeRead(ctx context.Context, d *schema.ResourceDa
}
}

zoneID := datasourceNewZonedID(volumeID, zone)
zoneID := datasource.NewZonedID(volumeID, zone)
d.SetId(zoneID)
err = d.Set("volume_id", zoneID)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion scaleway/data_source_cockpit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func dataSourceScalewayCockpit() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayCockpit().Schema)
dsSchema := datasource.SchemaFromResourceSchema(resourceScalewayCockpit().Schema)

dsSchema["project_id"] = &schema.Schema{
Type: schema.TypeString,
Expand Down
7 changes: 4 additions & 3 deletions scaleway/data_source_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func dataSourceScalewayContainer() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayContainer().Schema)
dsSchema := datasource.SchemaFromResourceSchema(resourceScalewayContainer().Schema)

addOptionalFieldsToSchema(dsSchema, "name", "region")
datasource.AddOptionalFieldsToSchema(dsSchema, "name", "region")

dsSchema["name"].ConflictsWith = []string{"container_id"}
dsSchema["container_id"] = &schema.Schema{
Expand Down Expand Up @@ -77,7 +78,7 @@ func dataSourceScalewayContainerRead(ctx context.Context, d *schema.ResourceData
containerID = foundContainer.ID
}

regionalID := datasourceNewRegionalID(containerID, region)
regionalID := datasource.NewRegionalID(containerID, region)
d.SetId(regionalID)
_ = d.Set("container_id", regionalID)

Expand Down
7 changes: 4 additions & 3 deletions scaleway/data_source_container_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func dataSourceScalewayContainerNamespace() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayContainerNamespace().Schema)
dsSchema := datasource.SchemaFromResourceSchema(resourceScalewayContainerNamespace().Schema)

addOptionalFieldsToSchema(dsSchema, "name", "region", "project_id")
datasource.AddOptionalFieldsToSchema(dsSchema, "name", "region", "project_id")

dsSchema["name"].ConflictsWith = []string{"namespace_id"}
dsSchema["namespace_id"] = &schema.Schema{
Expand Down Expand Up @@ -62,7 +63,7 @@ func dataSourceScalewayContainerNamespaceRead(ctx context.Context, d *schema.Res
namespaceID = foundNamespace.ID
}

regionalID := datasourceNewRegionalID(namespaceID, region)
regionalID := datasource.NewRegionalID(namespaceID, region)
d.SetId(regionalID)
_ = d.Set("namespace_id", regionalID)

Expand Down
5 changes: 3 additions & 2 deletions scaleway/data_source_document_db_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
)

func dataSourceScalewayDocumentDBDatabase() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayDocumentDBDatabase().Schema)
dsSchema := datasource.SchemaFromResourceSchema(resourceScalewayDocumentDBDatabase().Schema)

addOptionalFieldsToSchema(dsSchema, "name", "region")
datasource.AddOptionalFieldsToSchema(dsSchema, "name", "region")

dsSchema["instance_id"].Required = true
dsSchema["instance_id"].Computed = false
Expand Down
7 changes: 4 additions & 3 deletions scaleway/data_source_document_db_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
documentdb "github.com/scaleway/scaleway-sdk-go/api/documentdb/v1beta1"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func dataSourceScalewayDocumentDBInstance() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayDocumentDBInstance().Schema)
dsSchema := datasource.SchemaFromResourceSchema(resourceScalewayDocumentDBInstance().Schema)

addOptionalFieldsToSchema(dsSchema, "name", "region", "project_id")
datasource.AddOptionalFieldsToSchema(dsSchema, "name", "region", "project_id")

dsSchema["instance_id"] = &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -60,7 +61,7 @@ func dataSourceScalewayDocumentDBInstanceRead(ctx context.Context, d *schema.Res
instanceID = foundRawInstance.ID
}

regionID := datasourceNewRegionalID(instanceID, region)
regionID := datasource.NewRegionalID(instanceID, region)
d.SetId(regionID)
err = d.Set("instance_id", regionID)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion scaleway/data_source_document_db_load_balancer_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
documentdb "github.com/scaleway/scaleway-sdk-go/api/documentdb/v1beta1"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"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/types"
Expand Down Expand Up @@ -102,7 +103,7 @@ func dataSourceScalewayDocumentDBLoadBalancerRead(ctx context.Context, d *schema
_ = d.Set("ip", types.FlattenIPPtr(lb.IP))
_ = d.Set("name", lb.Name)

d.SetId(datasourceNewRegionalID(lb.ID, region))
d.SetId(datasource.NewRegionalID(lb.ID, region))

return nil
}
Expand Down
Loading

0 comments on commit 4f33d1d

Please sign in to comment.