Skip to content

Commit

Permalink
fix: Removed data_source_regions
Browse files Browse the repository at this point in the history
  • Loading branch information
YanniHu1996 committed Jul 7, 2023
1 parent bd8b5d1 commit c6c9b0f
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 227 deletions.
10 changes: 0 additions & 10 deletions docs/data-sources/region.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "biganimal_region Data Source - terraform-provider-biganimal"
subcategory: ""
description: |-
---

# biganimal_region (Data Source)



## Example Usage

```terraform
variable "cloud_provider" {
type = string
Expand Down
64 changes: 0 additions & 64 deletions docs/data-sources/regions.md

This file was deleted.

136 changes: 126 additions & 10 deletions pkg/provider/data_source_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,141 @@ package provider

import (
"context"
"fmt"
"strconv"
"time"

"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/api"
"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// NewRegionDataSource is a helper function to simplify the provider implementation.
func NewRegionDataSource() datasource.DataSource {
return &regionDataSource{}
var _ datasource.DataSourceWithConfigure = &regionsDataSource{}

// NewRegionsDataSource is a helper function to simplify the provider implementation.
func NewRegionsDataSource() datasource.DataSource {
return &regionsDataSource{}
}

// regionsDataSource is the data source implementation.
type regionsDataSource struct {
client *api.RegionClient
}

// regionDataSource is the data source implementation.
type regionDataSource struct {
regionsDataSource
// Configure adds the provider configured client to the data source.
func (r *regionsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

r.client = req.ProviderData.(*api.API).RegionClient()
}

func (r *regionDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
func (r *regionsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_region"
}

func (r *regionDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
r.regionsDataSource.Schema(ctx, req, resp)
resp.Schema.DeprecationMessage = "The datasource 'region' is deprecated and will be removed in the next major version. Please use 'regions' instead."
func (r *regionsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Datasource ID.",
Computed: true,
},
"regions": schema.ListNestedAttribute{
Description: "Region information.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"region_id": schema.StringAttribute{
Description: "Region ID of the region.",
Computed: true,
},
"name": schema.StringAttribute{
Description: "Region name of the region.",
Computed: true,
},
"status": schema.StringAttribute{
Description: "Region status of the region.",
Computed: true,
},
"continent": schema.StringAttribute{
Description: "Continent that region belongs to.",
Computed: true,
},
},
},
},

"cloud_provider": schema.StringAttribute{
Description: "Cloud provider to list the regions. For example, \"aws\", \"azure\" or \"bah:aws\".",
Required: true,
},
"project_id": schema.StringAttribute{
Description: "BigAnimal Project ID.",
Required: true,
Validators: []validator.String{
ProjectIdValidator(),
},
},
"query": schema.StringAttribute{
Description: "Query to filter region list.",
Optional: true,
},
"region_id": schema.StringAttribute{
Description: "Unique region ID. For example, \"germanywestcentral\" in the Azure cloud provider, \"eu-west-1\" in the AWS cloud provider.",
Optional: true,
},
},
}
}

type regionsDataSourceModel struct {
ID *string `tfsdk:"id"`
ProjectId *string `tfsdk:"project_id"`
CloudProvider *string `tfsdk:"cloud_provider"`
RegionId *string `tfsdk:"region_id"`
Query types.String `tfsdk:"query"`
Regions []*models.Region `tfsdk:"regions"`
}

func (r *regionsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var cfg regionsDataSourceModel
diags := req.Config.Get(ctx, &cfg)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

regions := []*models.Region{}
if cfg.RegionId != nil {
region, err := r.client.Read(ctx, *cfg.ProjectId, *cfg.CloudProvider, *cfg.RegionId)
if err != nil {
if appendDiagFromBAErr(err, &diags) {
return
}
diags.AddError(fmt.Sprintf("Error reading region by id: %q", *cfg.RegionId), err.Error())
return
}
regions = append(regions, region)

} else {
respRegions, err := r.client.List(ctx, *cfg.ProjectId, *cfg.CloudProvider, cfg.Query.ValueString())
if err != nil {
if appendDiagFromBAErr(err, &diags) {
return
}
diags.AddError(fmt.Sprintf("Error reading region by query: %q", cfg.Query.ValueString()), err.Error())
return
}
regions = respRegions
}

cfg.Regions = append(cfg.Regions, regions...)
resourceID := strconv.FormatInt(time.Now().Unix(), 10)
cfg.ID = &resourceID
resp.Diagnostics.Append(resp.State.Set(ctx, &cfg)...)
}
File renamed without changes.
142 changes: 0 additions & 142 deletions pkg/provider/data_source_regions.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ func (b bigAnimalProvider) DataSources(ctx context.Context) []func() datasource.
NewProjectsDataSource,
NewPgdDataSource,
NewRegionsDataSource,
NewRegionDataSource,
}
}

Expand Down
File renamed without changes.

0 comments on commit c6c9b0f

Please sign in to comment.