Skip to content

Commit

Permalink
patch - fixing flatten of json_content field bug (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrNovo authored Feb 12, 2024
1 parent c56d4d4 commit 99a5658
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,14 @@ New Features:
## Release 1.11.5
Bug fixing:
#### resource/coralogix_slo
* fixing log messages and flattening update-response into schema.
* fixing log messages and flattening update-response into schema.

## Release 1.11.6
Bug fixing:
#### resource/coralogix_dashboard
* fixing DataTableSpansAggregationModel parsing bug.

## Release 1.11.7
Bug fixing:
#### resource/coralogix_dashboard
* fixing flatten of `json_content` field bug.
12 changes: 7 additions & 5 deletions coralogix/resource_coralogix_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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/nsf/jsondiff"
"golang.org/x/exp/slices"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -2421,7 +2422,7 @@ func (c ContentJsonValidator) ValidateString(_ context.Context, request validato
}

func JSONStringsEqualPlanModifier(_ context.Context, plan planmodifier.StringRequest, req *stringplanmodifier.RequiresReplaceIfFuncResponse) {
if JSONStringsEqual(plan.PlanValue.ValueString(), plan.StateValue.ValueString()) {
if diffType, _ := jsondiff.Compare([]byte(plan.PlanValue.ValueString()), []byte(plan.StateValue.ValueString()), &jsondiff.Options{}); !(diffType == jsondiff.FullMatch || diffType == jsondiff.SupersetMatch) {
req.RequiresReplace = false
}
req.RequiresReplace = true
Expand Down Expand Up @@ -5521,17 +5522,18 @@ func expandDashboardIDs(id types.String) *wrapperspb.StringValue {
}

func flattenDashboard(ctx context.Context, plan DashboardResourceModel, dashboard *dashboards.Dashboard) (*DashboardResourceModel, diag.Diagnostics) {
if !plan.ContentJson.IsNull() {
if !(plan.ContentJson.IsNull() || plan.ContentJson.IsUnknown()) {
contentJson, err := protojson.Marshal(dashboard)
if err != nil {
return nil, diag.Diagnostics{diag.NewErrorDiagnostic("Error Flatten Dashboard", err.Error())}
}
if JSONStringsEqual(plan.ContentJson.ValueString(), string(contentJson)) {
contentJson = []byte(plan.ContentJson.ValueString())

if diffType, diffString := jsondiff.Compare([]byte(plan.ContentJson.ValueString()), contentJson, &jsondiff.Options{}); !(diffType == jsondiff.FullMatch || diffType == jsondiff.SupersetMatch) {
return nil, diag.Diagnostics{diag.NewErrorDiagnostic("Error Flatten Dashboard", fmt.Sprintf("ContentJson does not match the dashboard content: %s", diffString))}
}

return &DashboardResourceModel{
ContentJson: types.StringValue(string(contentJson)),
ContentJson: types.StringValue(plan.ContentJson.ValueString()),
ID: types.StringValue(dashboard.GetId().GetValue()),
Name: types.StringNull(),
Description: types.StringNull(),
Expand Down
3 changes: 2 additions & 1 deletion examples/dashboard/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ provider "coralogix" {
# env = "<add the environment you want to work at or add env variable CORALOGIX_ENV>"
}


resource "coralogix_dashboard" dashboard {
name = "portal monitoring"
description = "<insert description>"
Expand Down Expand Up @@ -603,7 +604,7 @@ resource "coralogix_dashboard" dashboard {
}

resource "coralogix_dashboards_folder" "example" {
name = "example_2"
name = "example"
}

resource "coralogix_dashboard" dashboard_from_json {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/hashicorp/terraform-plugin-mux v0.13.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.31.0
github.com/hashicorp/terraform-plugin-testing v1.6.0
github.com/nsf/jsondiff v0.0.0-20230430225905-43f6cf3098c1
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848
google.golang.org/grpc v1.60.0
google.golang.org/protobuf v1.31.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grafana/grafana-api-golang-client v0.27.0 h1:zIwMXcbCB4n588i3O2N6HfNcQogCNTd/vPkEXTr7zX8=
Expand Down Expand Up @@ -179,6 +177,8 @@ github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/nsf/jsondiff v0.0.0-20230430225905-43f6cf3098c1 h1:dOYG7LS/WK00RWZc8XGgcUTlTxpp3mKhdR2Q9z9HbXM=
github.com/nsf/jsondiff v0.0.0-20230430225905-43f6cf3098c1/go.mod h1:mpRZBD8SJ55OIICQ3iWH0Yz3cjzA61JdqMLoWXeB2+8=
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
Expand Down

0 comments on commit 99a5658

Please sign in to comment.