Skip to content

Commit

Permalink
feat: tag assign region (#582)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wai-wong-edb authored Oct 8, 2024
1 parent 4fa10a2 commit 93b5be8
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 16 deletions.
10 changes: 10 additions & 0 deletions examples/resources/biganimal_region/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<ex-tag-name-2>"
},
]
}

output "region_status" {
Expand Down
19 changes: 17 additions & 2 deletions pkg/api/region_client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand All @@ -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 (
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
11 changes: 7 additions & 4 deletions pkg/models/region.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions pkg/provider/data_source_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
},
},
}
}
Expand Down
57 changes: 47 additions & 10 deletions pkg/provider/resource_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
},
},
},
}
}
Expand All @@ -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"`
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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(&region.Tags, read.Tags)

return state.Set(ctx, &region)
}

Expand Down

0 comments on commit 93b5be8

Please sign in to comment.