Skip to content

Commit

Permalink
feat: tags assign to pgd (#530)
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
  • Loading branch information
wai-wong-edb authored Oct 3, 2024
1 parent 4cdad31 commit dbfbf6a
Show file tree
Hide file tree
Showing 18 changed files with 273 additions and 97 deletions.
4 changes: 2 additions & 2 deletions examples/resources/biganimal_cluster/ha/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ resource "biganimal_cluster" "ha_cluster" {

#tags = [
# {
# tag_name = "test-tag-1"
# tag_name = "ex-tag-name-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# tag_name = "ex-tag-name-2"
# },
#]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ resource "biganimal_cluster" "single_node_cluster" {

#tags = [
# {
# tag_name = "test-tag-1"
# tag_name = "ex-tag-name-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# tag_name = "ex-tag-name-2"
# },
#]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ resource "biganimal_cluster" "single_node_cluster" {

#tags = [
# {
# tag_name = "test-tag-1"
# tag_name = "ex-tag-name-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# tag_name = "ex-tag-name-2"
# },
#]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ resource "biganimal_cluster" "single_node_cluster" {

#tags = [
# {
# tag_name = "test-tag-1"
# tag_name = "ex-tag-name-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# tag_name = "ex-tag-name-2"
# },
#]

Expand Down
16 changes: 7 additions & 9 deletions examples/resources/biganimal_faraway_replica/aws/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ resource "biganimal_faraway_replica" "faraway_replica" {
private_networking = false
region = "ap-south-1"

#tags = [
# {
# tag_name = "test-tag-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# },
#]
#tags {
# tag_name = "ex-tag-name-1"
# color = "blue"
#}
#tags {
# tag_name = "ex-tag-name-2"
#}

# pe_allowed_principal_ids = [
# <example_value> # ex: 123456789012
Expand Down
16 changes: 7 additions & 9 deletions examples/resources/biganimal_faraway_replica/azure/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ resource "biganimal_faraway_replica" "faraway_replica" {
private_networking = false
region = "australiaeast"

#tags = [
# {
# tag_name = "test-tag-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# },
#]
#tags {
# tag_name = "ex-tag-name-1"
# color = "blue"
#}
#tags {
# tag_name = "ex-tag-name-2"
#}

# pe_allowed_principal_ids = [
# <example_value> # ex: "9334e5e6-7f47-aE61-5A4F-ee067daeEf4A"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,13 @@ resource "biganimal_faraway_replica" "faraway_replica" {
private_networking = false
region = "centralindia"

#tags = [
# {
# tag_name = "test-tag-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# },
#]
#tags {
# tag_name = "ex-tag-name-1"
# color = "blue"
#}
#tags {
# tag_name = "ex-tag-name-2"
#}

# transparent_data_encryption = {
# key_id = <example_value>
Expand Down
16 changes: 7 additions & 9 deletions examples/resources/biganimal_faraway_replica/gcp/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ resource "biganimal_faraway_replica" "faraway_replica" {
private_networking = false
region = "us-east1"

#tags = [
# {
# tag_name = "test-tag-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# },
#]
#tags {
# tag_name = "ex-tag-name-1"
# color = "blue"
#}
#tags {
# tag_name = "ex-tag-name-2"
#}

# pe_allowed_principal_ids = [
# <example_value> # ex: "development-data-123456"
Expand Down
9 changes: 9 additions & 0 deletions examples/resources/biganimal_pgd/aws/data_group/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ resource "biganimal_pgd" "pgd_cluster" {
project_id = var.project_id
password = resource.random_password.password.result
pause = false
#tags = [
# {
# tag_name = "ex-tag-name-1"
# color = "blue"
# },
# {
# tag_name = "ex-tag-name-2"
# },
#]
data_groups = [
{
allowed_ip_ranges = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ resource "biganimal_pgd" "pgd_cluster" {
project_id = var.project_id
password = resource.random_password.password.result
pause = false
#tags = [
# {
# tag_name = "ex-tag-name-1"
# color = "blue"
# },
# {
# tag_name = "ex-tag-name-2"
# },
#]
data_groups = [
{
allowed_ip_ranges = [
Expand Down
9 changes: 9 additions & 0 deletions examples/resources/biganimal_pgd/azure/data_group/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ resource "biganimal_pgd" "pgd_cluster" {
project_id = var.project_id
password = resource.random_password.password.result
pause = false
#tags = [
# {
# tag_name = "ex-tag-name-1"
# color = "blue"
# },
# {
# tag_name = "ex-tag-name-2"
# },
#]
data_groups = [
{
allowed_ip_ranges = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ resource "biganimal_pgd" "pgd_cluster" {
project_id = var.project_id
password = resource.random_password.password.result
pause = false
#tags = [
# {
# tag_name = "ex-tag-name-1"
# color = "blue"
# },
# {
# tag_name = "ex-tag-name-2"
# },
#]
data_groups = [
{
allowed_ip_ranges = [
Expand Down
9 changes: 9 additions & 0 deletions examples/resources/biganimal_pgd/gcp/data_group/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ resource "biganimal_pgd" "pgd_cluster" {
project_id = var.project_id
password = resource.random_password.password.result
pause = false
#tags = [
# {
# tag_name = "ex-tag-name-1"
# color = "blue"
# },
# {
# tag_name = "ex-tag-name-2"
# },
#]
data_groups = [
{
allowed_ip_ranges = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ resource "biganimal_pgd" "pgd_cluster" {
project_id = var.project_id
password = resource.random_password.password.result
pause = false
#tags = [
# {
# tag_name = "ex-tag-name-1"
# color = "blue"
# },
# {
# tag_name = "ex-tag-name-2"
# },
#]
data_groups = [
{
allowed_ip_ranges = [
Expand Down
73 changes: 73 additions & 0 deletions pkg/plan_modifier/assign_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package plan_modifier

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

func CustomAssignTags() planmodifier.Set {
return assignTagsModifier{}
}

// assignTagsModifier implements the plan modifier.
type assignTagsModifier struct{}

// Description returns a human-readable description of the plan modifier.
func (m assignTagsModifier) Description(_ context.Context) string {
return "Once set, the value of this attribute in state will not change."
}

// MarkdownDescription returns a markdown description of the plan modifier.
func (m assignTagsModifier) MarkdownDescription(_ context.Context) string {
return "Once set, the value of this attribute in state will not change."
}

// PlanModifySet implements the plan modification logic.
func (m assignTagsModifier) PlanModifySet(ctx context.Context, req planmodifier.SetRequest, resp *planmodifier.SetResponse) {
state := req.StateValue
plan := resp.PlanValue

// This is on creation.
// Do nothing if there is no state value.
if req.StateValue.IsNull() {
return
}

// This is on update and tags are not set in config so just plan for state
if req.PlanValue.IsUnknown() {
resp.PlanValue = basetypes.NewSetValueMust(req.ConfigValue.ElementType(ctx), state.Elements())
return
}

// This is for anything else ie update with tags set in config.

// merge plan into newPlan (plan is from config) and merge state in newPlan (state is from read)
newPlan := state.Elements()

for _, planTag := range plan.Elements() {
existing := false
for _, newPlanElem := range newPlan {
if planTag.Equal(newPlanElem) {
existing = true
continue
}
}
if !existing {
newPlan = append(newPlan, planTag)
}
}

resp.PlanValue = basetypes.NewSetValueMust(req.ConfigValue.ElementType(ctx), newPlan)
}

func ContainsTag(s []attr.Value, e attr.Value) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
36 changes: 27 additions & 9 deletions pkg/provider/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ func (c *clusterResource) Schema(ctx context.Context, req resource.SchemaRequest
},
},
PlanModifiers: []planmodifier.Set{
setplanmodifier.UseStateForUnknown(),
plan_modifier.CustomAssignTags(),
},
},
"transparent_data_encryption": schema.SingleNestedAttribute{
Expand Down Expand Up @@ -963,14 +963,7 @@ func readCluster(ctx context.Context, client *api.ClusterClient, tfClusterResour
}
}

tfClusterResource.Tags = []commonTerraform.Tag{}
for _, v := range responseCluster.Tags {
tfClusterResource.Tags = append(tfClusterResource.Tags, commonTerraform.Tag{
TagId: types.StringValue(v.TagId),
TagName: types.StringValue(v.TagName),
Color: basetypes.NewStringPointerValue(v.Color),
})
}
buildTFRsrcAssignTagsAs(&tfClusterResource.Tags, &responseCluster.Tags)

if responseCluster.EncryptionKeyResp != nil {
tfClusterResource.TransparentDataEncryption = &TransparentDataEncryptionModel{}
Expand Down Expand Up @@ -1178,6 +1171,8 @@ func (c *clusterResource) generateGenericClusterModel(ctx context.Context, clust
cluster.ServiceAccountIds = svAccIds
cluster.PeAllowedPrincipalIds = principalIds

cluster.Tags = buildAPIReqAssignTags(clusterResource.Tags)

if clusterResource.TransparentDataEncryption != nil {
if !clusterResource.TransparentDataEncryption.KeyId.IsNull() {
cluster.EncryptionKeyIdReq = clusterResource.TransparentDataEncryption.KeyId.ValueStringPointer()
Expand Down Expand Up @@ -1226,3 +1221,26 @@ func StringSliceToSet(items *[]string) types.Set {

return types.SetValueMust(types.StringType, eles)
}

func buildTFRsrcAssignTagsAs(tfRsrcTags *[]commonTerraform.Tag, apiRespTags *[]commonApi.Tag) {
*tfRsrcTags = []commonTerraform.Tag{}
for _, v := range *apiRespTags {
*tfRsrcTags = append(*tfRsrcTags, commonTerraform.Tag{
TagId: types.StringValue(v.TagId),
TagName: types.StringValue(v.TagName),
Color: basetypes.NewStringPointerValue(v.Color),
})
}
}

func buildAPIReqAssignTags(tfRsrcTags []commonTerraform.Tag) []commonApi.Tag {
tags := []commonApi.Tag{}
for _, tag := range tfRsrcTags {
tags = append(tags, commonApi.Tag{
Color: tag.Color.ValueStringPointer(),
TagId: tag.TagId.ValueString(),
TagName: tag.TagName.ValueString(),
})
}
return tags
}
2 changes: 1 addition & 1 deletion pkg/provider/resource_fareplica.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func (r *FAReplicaResource) Schema(ctx context.Context, req resource.SchemaReque
},
},
PlanModifiers: []planmodifier.Set{
setplanmodifier.UseStateForUnknown(),
plan_modifier.CustomAssignTags(),
},
},
},
Expand Down
Loading

0 comments on commit dbfbf6a

Please sign in to comment.