From 93b5be857acbec207b1dc672daa7563b12038593 Mon Sep 17 00:00:00 2001 From: wai-wong-edb <119956756+wai-wong-edb@users.noreply.github.com> Date: Tue, 8 Oct 2024 15:38:39 +0100 Subject: [PATCH] feat: tag assign region (#582) * feat: resource tag create * fix: lint errors * fix: lint errors * refactor: refactor tag client * feat: tag update * refactor: refactor tag client * feat: changed tag description * feat: tag import * feat: lint error * feat: assign tags to resources * fix: faraway replica assign tag * feat: tag in cluster examples * fix: plan modifier for cluster to merge plan with state so it can deal with existing tags when planning * fix: cluster assign tags fix * feat: pgd assign tags * fix: lint fix * fix: examples * feat: project tags assign * fix: planmodifier to use state so it can assign and remove correctly * fix: custom plan modifier * feat: tags for region * feat: region tags examples and datasource * fix: lint errors * fix: datasource tags description --- .../resources/biganimal_region/resource.tf | 10 ++++ pkg/api/region_client.go | 19 ++++++- pkg/models/region.go | 11 ++-- pkg/provider/data_source_region.go | 17 ++++++ pkg/provider/resource_region.go | 57 +++++++++++++++---- 5 files changed, 98 insertions(+), 16 deletions(-) diff --git a/examples/resources/biganimal_region/resource.tf b/examples/resources/biganimal_region/resource.tf index a7abfcc6..8d7a51fe 100644 --- a/examples/resources/biganimal_region/resource.tf +++ b/examples/resources/biganimal_region/resource.tf @@ -16,6 +16,16 @@ resource "biganimal_region" "this" { cloud_provider = "aws" region_id = "eu-west-1" project_id = var.project_id + + tags = [ + { + tag_name = "test" + color = "blue" + }, + { + tag_name = "" + }, + ] } output "region_status" { diff --git a/pkg/api/region_client.go b/pkg/api/region_client.go index 6f3a763a..9ed535ca 100644 --- a/pkg/api/region_client.go +++ b/pkg/api/region_client.go @@ -1,6 +1,7 @@ package api import ( + "bytes" "context" "encoding/json" "errors" @@ -9,6 +10,7 @@ import ( "time" "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models" + commonApi "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/common/api" ) const ( @@ -68,7 +70,7 @@ func (c RegionClient) List(ctx context.Context, project_id, csp_id, query string return response.Data, err } -func (c RegionClient) Update(ctx context.Context, action, project_id, csp_id, region_id string) error { +func (c RegionClient) Update(ctx context.Context, action, project_id, csp_id, region_id string, tags []commonApi.Tag) error { url := fmt.Sprintf("projects/%s/cloud-providers/%s/regions/%s", project_id, csp_id, region_id) switch action { @@ -82,6 +84,19 @@ func (c RegionClient) Update(ctx context.Context, action, project_id, csp_id, re return errors.New("unknown region action") } - _, err := c.doRequest(ctx, http.MethodPost, url, nil) + regionPatchModel := map[string]interface{}{ + "tags": tags, + } + + b, err := json.Marshal(regionPatchModel) + if err != nil { + return err + } + + _, err = c.doRequest(ctx, http.MethodPost, url, bytes.NewBuffer(b)) + if err != nil { + return err + } + return err } diff --git a/pkg/models/region.go b/pkg/models/region.go index 14ce0c81..78749384 100644 --- a/pkg/models/region.go +++ b/pkg/models/region.go @@ -1,10 +1,13 @@ package models +import commonApi "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/common/api" + type Region struct { - Id string `json:"regionId,omitempty" tfsdk:"region_id"` - Name string `json:"regionName,omitempty" tfsdk:"name"` - Status string `json:"status,omitempty" tfsdk:"status"` - Continent string `json:"continent,omitempty" tfsdk:"continent"` + Id string `json:"regionId,omitempty" tfsdk:"region_id"` + Name string `json:"regionName,omitempty" tfsdk:"name"` + Status string `json:"status,omitempty" tfsdk:"status"` + Continent string `json:"continent,omitempty" tfsdk:"continent"` + Tags []commonApi.Tag `json:"tags,omitempty"` } func (r Region) String() string { diff --git a/pkg/provider/data_source_region.go b/pkg/provider/data_source_region.go index 8a58a55e..b247ac58 100644 --- a/pkg/provider/data_source_region.go +++ b/pkg/provider/data_source_region.go @@ -90,6 +90,23 @@ func (r *regionsDataSource) Schema(ctx context.Context, req datasource.SchemaReq Description: "Unique region ID. For example, \"germanywestcentral\" in the Azure cloud provider, \"eu-west-1\" in the AWS cloud provider.", Optional: true, }, + "tags": schema.SetNestedAttribute{ + Description: "show tags associated with this resource", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "tag_id": schema.StringAttribute{ + Computed: true, + }, + "tag_name": schema.StringAttribute{ + Computed: true, + }, + "color": schema.StringAttribute{ + Computed: true, + }, + }, + }, + }, }, } } diff --git a/pkg/provider/resource_region.go b/pkg/provider/resource_region.go index 8cfe87d7..8154c09d 100644 --- a/pkg/provider/resource_region.go +++ b/pkg/provider/resource_region.go @@ -4,13 +4,16 @@ import ( "context" "errors" "fmt" + "strings" + "time" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/tfsdk" - "strings" - "time" "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/api" + commonTerraform "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/common/terraform" + "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/plan_modifier" "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" frameworkdiag "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/path" @@ -83,6 +86,36 @@ func (r regionResource) Schema(ctx context.Context, req resource.SchemaRequest, stringplanmodifier.UseStateForUnknown(), }, }, + "tags": schema.SetNestedAttribute{ + Description: "Assign existing tags or create tags to assign to this resource", + Optional: true, + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "tag_id": schema.StringAttribute{ + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "tag_name": schema.StringAttribute{ + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "color": schema.StringAttribute{ + Optional: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + }, + }, + PlanModifiers: []planmodifier.Set{ + plan_modifier.CustomAssignTags(), + }, + }, }, } } @@ -96,13 +129,14 @@ func (r *regionResource) Configure(_ context.Context, req resource.ConfigureRequ } type Region struct { - ProjectID *string `tfsdk:"project_id"` - CloudProvider *string `tfsdk:"cloud_provider"` - RegionID *string `tfsdk:"region_id"` - ID *string `tfsdk:"id"` - Name *string `tfsdk:"name"` - Continent *string `tfsdk:"continent"` - Status *string `tfsdk:"status"` + ProjectID *string `tfsdk:"project_id"` + CloudProvider *string `tfsdk:"cloud_provider"` + RegionID *string `tfsdk:"region_id"` + ID *string `tfsdk:"id"` + Name *string `tfsdk:"name"` + Continent *string `tfsdk:"continent"` + Status *string `tfsdk:"status"` + Tags []commonTerraform.Tag `tfsdk:"tags"` Timeouts timeouts.Value `tfsdk:"timeouts"` } @@ -169,7 +203,7 @@ func (r *regionResource) ensureStatueUpdated(ctx context.Context, region Region) } diags := frameworkdiag.Diagnostics{} - if err := r.client.Update(ctx, *region.Status, *region.ProjectID, *region.CloudProvider, *region.RegionID); err != nil { + if err := r.client.Update(ctx, *region.Status, *region.ProjectID, *region.CloudProvider, *region.RegionID, buildAPIReqAssignTags(region.Tags)); err != nil { if appendDiagFromBAErr(err, &diags) { return diags } @@ -210,6 +244,9 @@ func (r *regionResource) writeState(ctx context.Context, region Region, state *t region.Name = &read.Name region.Status = &read.Status region.Continent = &read.Continent + + buildTFRsrcAssignTagsAs(®ion.Tags, read.Tags) + return state.Set(ctx, ®ion) }