Skip to content

Commit

Permalink
fix: modify plan tag color wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wai-wong-edb committed Jan 31, 2025
1 parent 2b5fd08 commit 2ddcd45
Showing 1 changed file with 47 additions and 18 deletions.
65 changes: 47 additions & 18 deletions pkg/provider/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/constants"
"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models"
commonApi "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/common/api"
"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/common/terraform"
commonTerraform "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/common/terraform"
"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/plan_modifier"
"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/utils"
Expand All @@ -31,6 +32,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
)

Expand Down Expand Up @@ -586,24 +588,51 @@ func (c *clusterResource) Schema(ctx context.Context, req resource.SchemaRequest
}
}

// func (c *clusterResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
// config := resp.Plan

// // Validate existing tag. Existing tag colors cannot be changed in a cluster create request and must be removed.
// // To change tag color, use tag request
// existingTags, err := c.client.TagClient().List(ctx)
// if err != nil {
// resp.Diagnostics.AddError("Error fetching existing tags", err.Error())
// }
// for _, configTag := range config.Elements() {
// tagName := configTag.(basetypes.ObjectValue).Attributes()["tag_name"].(basetypes.StringValue).ValueString()
// for _, existingTag := range existingTags {
// if existingTag.TagName == tagName {
// resp.Diagnostics.AddError("Existing tag color cannot be changed", fmt.Sprintf("Please remove existing tag color for tag %s", tagName))
// }
// }
// }
// }
func (c *clusterResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
var planObject map[string]tftypes.Value

err := req.Plan.Raw.As(&planObject)
if err != nil {
resp.Diagnostics.AddError("Mapping plan object in cluster modify plan error", err.Error())
return
}

set := new(types.Set)
req.Plan.GetAttribute(ctx, path.Root("tags"), set)

configTags := []terraform.Tag{}
diag := set.ElementsAs(ctx, &configTags, false)
if diag.ErrorsCount() > 0 {
resp.Diagnostics.Append(diag...)
return
}

// Validate existing tag. Existing tag colors cannot be changed in a cluster create request and must be removed.
// To change tag color, use tag request
existingTags, err := c.client.TagClient().List(ctx)
if err != nil {
resp.Diagnostics.AddError("Error fetching existing tags", err.Error())
}
for _, configTag := range configTags {
for _, existingTag := range existingTags {
// if config tag matches existing tag, then config tags color has to match existing tag color or
// config tag color should be set to nil, other throw validation error
if existingTag.TagName == configTag.TagName.ValueString() &&
(existingTag.Color != nil && *existingTag.Color != configTag.Color.ValueString() ||
configTag.Color.ValueStringPointer() != nil) {

existingColor := "nil"
if existingTag.Color != nil {
existingColor = *existingTag.Color
}

resp.Diagnostics.AddError("An existing tag's color cannot be changed",
fmt.Sprintf("Please remove the color field for tag: `%v` or set it to the existing tag's color: `%v`.\nTo change an existing tag's color please use resource `biganimal_tag`",
configTag.TagName.ValueString(), existingColor))
}
}
}
}

func (c *clusterResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
Expand Down

0 comments on commit 2ddcd45

Please sign in to comment.