Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tag assign region #582

Merged
merged 30 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fc1e115
feat: resource tag create
wai-wong-edb Mar 22, 2024
b06819a
fix: lint errors
wai-wong-edb Mar 22, 2024
bddbab4
fix: lint errors
wai-wong-edb Mar 22, 2024
07ba079
refactor: refactor tag client
wai-wong-edb Mar 28, 2024
714a365
feat: tag update
wai-wong-edb Mar 28, 2024
bdf1710
refactor: refactor tag client
wai-wong-edb Apr 2, 2024
086aaa7
Merge branch 'ww-tags-create' into ww-tags-update
wai-wong-edb Apr 2, 2024
cd08fe8
feat: changed tag description
wai-wong-edb Apr 2, 2024
c07f3bc
Merge branch 'ww-tags-create' into ww-tags-update
wai-wong-edb Apr 2, 2024
6a0235e
feat: tag import
wai-wong-edb Apr 2, 2024
67263ab
feat: lint error
wai-wong-edb Apr 2, 2024
e5a74f0
feat: assign tags to resources
wai-wong-edb Apr 5, 2024
7931b8b
fix: faraway replica assign tag
wai-wong-edb Apr 8, 2024
b04fa41
feat: tag in cluster examples
wai-wong-edb Apr 9, 2024
29ab369
fix: plan modifier for cluster to merge plan with state so it can dea…
wai-wong-edb Apr 9, 2024
7abe3a4
fix: cluster assign tags fix
wai-wong-edb Apr 11, 2024
496b51d
feat: pgd assign tags
wai-wong-edb Apr 11, 2024
cb47561
fix: lint fix
wai-wong-edb Apr 15, 2024
7027740
fix: examples
wai-wong-edb Apr 15, 2024
e4e58c8
feat: project tags assign
wai-wong-edb Apr 15, 2024
0364ce9
fix: planmodifier to use state so it can assign and remove correctly
wai-wong-edb Apr 15, 2024
b7dc43f
Merge branch 'main' into ww-tag-assign-pgd-project-region
wai-wong-edb Aug 14, 2024
bd4ae33
Merge branch 'main' into ww-tag-assign-pgd-project-region
wai-wong-edb Aug 19, 2024
4b97360
Merge branch 'ww-tag-assign-pgd-project-region' into ww-tag-assign-pr…
wai-wong-edb Aug 19, 2024
c165509
fix: custom plan modifier
wai-wong-edb Aug 19, 2024
9952cb5
feat: tags for region
wai-wong-edb Aug 21, 2024
13c129d
feat: region tags examples and datasource
wai-wong-edb Aug 22, 2024
0218d20
fix: lint errors
wai-wong-edb Aug 22, 2024
c6327b8
fix: datasource tags description
wai-wong-edb Sep 9, 2024
c20373d
Merge branch 'main' into ww-tag-assign-region
wai-wong-edb Oct 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
wai-wong-edb marked this conversation as resolved.
Show resolved Hide resolved
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
Loading