From c6c9b0f40e6bcc21e9c6ff30e2b98b6f7914c71b Mon Sep 17 00:00:00 2001 From: YanniHu1996 Date: Fri, 7 Jul 2023 10:46:44 +0800 Subject: [PATCH] fix: Removed data_source_regions --- docs/data-sources/region.md | 10 -- docs/data-sources/regions.md | 64 -------- pkg/provider/data_source_region.go | 136 +++++++++++++++-- ...ons_test.go => data_source_region_test.go} | 0 pkg/provider/data_source_regions.go | 142 ------------------ pkg/provider/provider.go | 1 - .../{regions.md.tmpl => region.md.tmpl} | 0 7 files changed, 126 insertions(+), 227 deletions(-) delete mode 100644 docs/data-sources/regions.md rename pkg/provider/{data_source_regions_test.go => data_source_region_test.go} (100%) delete mode 100644 pkg/provider/data_source_regions.go rename templates/data-sources/{regions.md.tmpl => region.md.tmpl} (100%) diff --git a/docs/data-sources/region.md b/docs/data-sources/region.md index 946438f0..d02de644 100644 --- a/docs/data-sources/region.md +++ b/docs/data-sources/region.md @@ -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 diff --git a/docs/data-sources/regions.md b/docs/data-sources/regions.md deleted file mode 100644 index 141ae738..00000000 --- a/docs/data-sources/regions.md +++ /dev/null @@ -1,64 +0,0 @@ -# biganimal_regions (Data Source) - - -## Example Usage -```terraform -variable "cloud_provider" { - type = string - description = "Cloud Provider" - - validation { - condition = contains(["aws", "azure", "bah:aws"], var.cloud_provider) - error_message = "Please select one of the supported regions: aws, azure and bah:aws." - } -} - -variable "project_id" { - type = string - description = "BigAnimal Project ID" - -} - -data "biganimal_regions" "this" { - cloud_provider = var.cloud_provider - project_id = var.project_id - // region_id = "us-west-1" //optional - // query = "eu" // optional -} - -output "regions" { - value = data.biganimal_regions.this.regions -} - -output "cloud_provider_id" { - value = data.biganimal_regions.this.cloud_provider -} -``` - - -## Schema - -### Required - -- `cloud_provider` (String) Cloud provider to list the regions. For example, "aws", "azure" or "bah:aws". -- `project_id` (String) BigAnimal Project ID. - -### Optional - -- `query` (String) Query to filter region list. -- `region_id` (String) Unique region ID. For example, "germanywestcentral" in the Azure cloud provider, "eu-west-1" in the AWS cloud provider. - -### Read-Only - -- `id` (String) Datasource ID. -- `regions` (Attributes List) Region information. (see [below for nested schema](#nestedatt--regions)) - - -### Nested Schema for `regions` - -Read-Only: - -- `continent` (String) Continent that region belongs to. -- `name` (String) Region name of the region. -- `region_id` (String) Region ID of the region. -- `status` (String) Region status of the region. diff --git a/pkg/provider/data_source_region.go b/pkg/provider/data_source_region.go index 7966cf32..8a58a55e 100644 --- a/pkg/provider/data_source_region.go +++ b/pkg/provider/data_source_region.go @@ -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 ®ionDataSource{} +var _ datasource.DataSourceWithConfigure = ®ionsDataSource{} + +// NewRegionsDataSource is a helper function to simplify the provider implementation. +func NewRegionsDataSource() datasource.DataSource { + return ®ionsDataSource{} +} + +// 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)...) } diff --git a/pkg/provider/data_source_regions_test.go b/pkg/provider/data_source_region_test.go similarity index 100% rename from pkg/provider/data_source_regions_test.go rename to pkg/provider/data_source_region_test.go diff --git a/pkg/provider/data_source_regions.go b/pkg/provider/data_source_regions.go deleted file mode 100644 index 503d0bdf..00000000 --- a/pkg/provider/data_source_regions.go +++ /dev/null @@ -1,142 +0,0 @@ -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" -) - -var _ datasource.DataSourceWithConfigure = ®ionsDataSource{} - -// NewRegionsDataSource is a helper function to simplify the provider implementation. -func NewRegionsDataSource() datasource.DataSource { - return ®ionsDataSource{} -} - -// regionsDataSource is the data source implementation. -type regionsDataSource struct { - client *api.RegionClient -} - -// 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 *regionsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { - resp.TypeName = req.ProviderTypeName + "_regions" -} - -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)...) -} diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go index 2cfd5554..176c7766 100644 --- a/pkg/provider/provider.go +++ b/pkg/provider/provider.go @@ -178,7 +178,6 @@ func (b bigAnimalProvider) DataSources(ctx context.Context) []func() datasource. NewProjectsDataSource, NewPgdDataSource, NewRegionsDataSource, - NewRegionDataSource, } } diff --git a/templates/data-sources/regions.md.tmpl b/templates/data-sources/region.md.tmpl similarity index 100% rename from templates/data-sources/regions.md.tmpl rename to templates/data-sources/region.md.tmpl