From f9a0089d438e229787b4d509d63c38b1a58b12e0 Mon Sep 17 00:00:00 2001 From: "PC-2NR0VQ3\\wai.wong" Date: Mon, 19 Aug 2024 20:36:16 +0100 Subject: [PATCH] feat: csp tag datasource --- pkg/provider/data_source_cluster.go | 24 ----- pkg/provider/data_source_csp_tag.go | 138 ++++++++++++++++++++++++++++ pkg/provider/provider.go | 1 + pkg/provider/resource_csp_tag.go | 2 +- 4 files changed, 140 insertions(+), 25 deletions(-) create mode 100644 pkg/provider/data_source_csp_tag.go diff --git a/pkg/provider/data_source_cluster.go b/pkg/provider/data_source_cluster.go index 3b5a9bef..e8fd5e8f 100644 --- a/pkg/provider/data_source_cluster.go +++ b/pkg/provider/data_source_cluster.go @@ -18,34 +18,10 @@ var ( _ datasource.DataSourceWithConfigure = &clusterDataSource{} ) -type PgConfigDatasourceModel struct { - Value types.String `tfsdk:"value"` - Name types.String `tfsdk:"name"` -} - -type StorageDatasourceModel struct { - Throughput types.String `tfsdk:"throughput"` - VolumeProperties types.String `tfsdk:"volume_properties"` - VolumeType types.String `tfsdk:"volume_type"` - Iops types.String `tfsdk:"iops"` - Size types.String `tfsdk:"size"` -} - -type ClusterArchitectureDatasourceModel struct { - Nodes types.Int64 `tfsdk:"nodes"` - Id types.String `tfsdk:"id"` - Name types.String `tfsdk:"name"` -} - type clusterDatasourceModel struct { ClusterResourceModel } -type AllowedIpRangesDatasourceModel struct { - CidrBlock types.String `tfsdk:"cidr_block"` - Description types.String `tfsdk:"description"` -} - type clusterDataSource struct { client *api.ClusterClient } diff --git a/pkg/provider/data_source_csp_tag.go b/pkg/provider/data_source_csp_tag.go new file mode 100644 index 00000000..951172d6 --- /dev/null +++ b/pkg/provider/data_source_csp_tag.go @@ -0,0 +1,138 @@ +package provider + +import ( + "context" + + "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/api" + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" +) + +var ( + _ datasource.DataSource = &cSPTagDataSource{} + _ datasource.DataSourceWithConfigure = &cSPTagDataSource{} +) + +type cSPTagDatasourceModel struct { + CSPTagResourceModel +} + +type cSPTagDataSource struct { + client *api.CSPTagClient +} + +func (c *cSPTagDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_csp_tag" +} + +// Configure adds the provider configured client to the data source. +func (c *cSPTagDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + c.client = req.ProviderData.(*api.API).CSPTagClient() +} + +func (c *cSPTagDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + MarkdownDescription: "CSP Tags will enable users to categorize and organize resources across types and improve the efficiency of resource retrieval", + // using Blocks for backward compatible + Blocks: map[string]schema.Block{ + "timeouts": timeouts.Block(ctx, + timeouts.Opts{Create: true, Delete: true, Update: true}, + ), + }, + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + }, + "project_id": schema.StringAttribute{ + Required: true, + }, + "cloud_provider_id": schema.StringAttribute{ + Required: true, + }, + "add_tags": schema.ListNestedAttribute{ + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "csp_tag_key": schema.StringAttribute{ + Computed: true, + }, + "csp_tag_value": schema.StringAttribute{ + Computed: true, + }, + }, + }, + }, + "delete_tags": schema.ListAttribute{ + Computed: true, + ElementType: types.StringType, + }, + "edit_tags": schema.ListNestedAttribute{ + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "csp_tag_id": schema.StringAttribute{ + Computed: true, + }, + "csp_tag_key": schema.StringAttribute{ + Computed: true, + }, + "csp_tag_value": schema.StringAttribute{ + Computed: true, + }, + "status": schema.StringAttribute{ + Computed: true, + }, + }, + }, + }, + "csp_tags": schema.ListNestedAttribute{ + Description: "CSP Tags on cluster", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "csp_tag_id": schema.StringAttribute{ + Computed: true, + }, + "csp_tag_key": schema.StringAttribute{ + Computed: true, + }, + "csp_tag_value": schema.StringAttribute{ + Computed: true, + }, + "status": schema.StringAttribute{ + Computed: true, + }, + }, + }, + }, + }, + } +} + +func (c *cSPTagDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data cSPTagDatasourceModel + diags := req.Config.Get(ctx, &data.CSPTagResourceModel) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + if err := readCSPTag(ctx, c.client, &data.CSPTagResourceModel); err != nil { + if !appendDiagFromBAErr(err, &resp.Diagnostics) { + resp.Diagnostics.AddError("Error reading cluster", err.Error()) + } + return + } + + resp.Diagnostics.Append(resp.State.Set(ctx, data.CSPTagResourceModel)...) +} + +func NewCSPTagDataSource() datasource.DataSource { + return &cSPTagDataSource{} +} diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go index 2a628cb7..a7ce26ec 100644 --- a/pkg/provider/provider.go +++ b/pkg/provider/provider.go @@ -193,6 +193,7 @@ func (b bigAnimalProvider) DataSources(ctx context.Context) []func() datasource. NewRegionsDataSource, NewAnalyticsClusterDataSource, NewTagDataSource, + NewCSPTagDataSource, } } diff --git a/pkg/provider/resource_csp_tag.go b/pkg/provider/resource_csp_tag.go index 2f77bbf1..19ea3076 100644 --- a/pkg/provider/resource_csp_tag.go +++ b/pkg/provider/resource_csp_tag.go @@ -66,7 +66,7 @@ func (tr *cSPTagResource) Metadata(ctx context.Context, req resource.MetadataReq func (tf *cSPTagResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = schema.Schema{ - MarkdownDescription: "Tags will enable users to categorize and organize resources across types and improve the efficiency of resource retrieval", + MarkdownDescription: "CSP Tags will enable users to categorize and organize resources across types and improve the efficiency of resource retrieval", Blocks: map[string]schema.Block{ "timeouts": timeouts.Block(ctx, timeouts.Opts{Create: true, Delete: true, Update: true}),