Skip to content

Commit

Permalink
support solution update (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
mawasile authored Jul 12, 2024
1 parent 2b320f4 commit 60000ad
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package powerplatform_modifiers

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"

powerplatform_helpers "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/helpers"
)

func SetBoolValueToUnknownIfChecksumsChangeModifier(firstAttributePair, secondAttributePair []string) planmodifier.Bool {
return &setBoolValueToUnknownIfChecksumsChangeModifier{
firstAttributePair: firstAttributePair,
secondAttributePair: secondAttributePair,
}
}

type setBoolValueToUnknownIfChecksumsChangeModifier struct {
firstAttributePair []string
secondAttributePair []string
}

func (d *setBoolValueToUnknownIfChecksumsChangeModifier) Description(ctx context.Context) string {
return "Ensures that the attribute value is set to unknown if the checksums change."
}

func (d *setBoolValueToUnknownIfChecksumsChangeModifier) MarkdownDescription(ctx context.Context) string {
return d.Description(ctx)
}

func (d *setBoolValueToUnknownIfChecksumsChangeModifier) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse) {

firstAttributeHasChanged := d.hasChecksumChanged(ctx, req, resp, d.firstAttributePair[0], d.firstAttributePair[1])
if resp.Diagnostics.HasError() {
return
}

secondAttributeHasChanged := d.hasChecksumChanged(ctx, req, resp, d.secondAttributePair[0], d.secondAttributePair[1])
if resp.Diagnostics.HasError() {
return
}

if firstAttributeHasChanged || secondAttributeHasChanged {
resp.PlanValue = types.BoolUnknown()
}
}

func (d *setBoolValueToUnknownIfChecksumsChangeModifier) hasChecksumChanged(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse, attributeName, checksumAttributeName string) bool {
var attribute types.String
diags := req.Plan.GetAttribute(ctx, path.Root(attributeName), &attribute)
resp.Diagnostics.Append(diags...)

var attributeChecksum types.String
diags = req.State.GetAttribute(ctx, path.Root(checksumAttributeName), &attributeChecksum)
resp.Diagnostics.Append(diags...)

value, err := powerplatform_helpers.CalculateMd5(attribute.ValueString())
if err != nil {
resp.Diagnostics.AddError(fmt.Sprintf("Error calculating MD5 checksum for %s", attribute), err.Error())
}

return value != attributeChecksum.ValueString()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package powerplatform_modifiers

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"

powerplatform_helpers "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/helpers"
)

func SetStringValueToUnknownIfChecksumsChangeModifier(firstAttributePair, secondAttributePair []string) planmodifier.String {
return &setStringValueToUnknownIfChecksumsChangeModifier{
firstAttributePair: firstAttributePair,
secondAttributePair: secondAttributePair,
}
}

type setStringValueToUnknownIfChecksumsChangeModifier struct {
firstAttributePair []string
secondAttributePair []string
}

func (d *setStringValueToUnknownIfChecksumsChangeModifier) Description(ctx context.Context) string {
return "Ensures that the attribute value is set to unknown if the checksums change."
}

func (d *setStringValueToUnknownIfChecksumsChangeModifier) MarkdownDescription(ctx context.Context) string {
return d.Description(ctx)
}

func (d *setStringValueToUnknownIfChecksumsChangeModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {

firstAttributeHasChanged := d.hasChecksumChanged(ctx, req, resp, d.firstAttributePair[0], d.firstAttributePair[1])
if resp.Diagnostics.HasError() {
return
}

secondAttributeHasChanged := d.hasChecksumChanged(ctx, req, resp, d.secondAttributePair[0], d.secondAttributePair[1])
if resp.Diagnostics.HasError() {
return
}

if firstAttributeHasChanged || secondAttributeHasChanged {
resp.PlanValue = types.StringUnknown()
}
}

func (d *setStringValueToUnknownIfChecksumsChangeModifier) hasChecksumChanged(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse, attributeName, checksumAttributeName string) bool {
var attribute types.String
diags := req.Plan.GetAttribute(ctx, path.Root(attributeName), &attribute)
resp.Diagnostics.Append(diags...)

var attributeChecksum types.String
diags = req.State.GetAttribute(ctx, path.Root(checksumAttributeName), &attributeChecksum)
resp.Diagnostics.Append(diags...)

value, err := powerplatform_helpers.CalculateMd5(attribute.ValueString())
if err != nil {
resp.Diagnostics.AddError(fmt.Sprintf("Error calculating MD5 checksum for %s", attribute), err.Error())
}

return value != attributeChecksum.ValueString()
}
19 changes: 10 additions & 9 deletions internal/powerplatform/services/solution/resource_solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -114,22 +113,24 @@ func (r *SolutionResource) Schema(ctx context.Context, req resource.SchemaReques
MarkdownDescription: "Display name of the solution",
Description: "Display name of the solution",
Computed: true,
PlanModifiers: []planmodifier.String{
powerplatform_modifiers.SetStringValueToUnknownIfChecksumsChangeModifier([]string{"solution_file", "solution_file_checksum"}, []string{"settings_file", "settings_file_checksum"}),
},
},
"is_managed": schema.BoolAttribute{
MarkdownDescription: "Indicates whether the solution is managed or not",
Description: "Indicates whether the solution is managed or not",
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
powerplatform_modifiers.SetBoolValueToUnknownIfChecksumsChangeModifier([]string{"solution_file", "solution_file_checksum"}, []string{"settings_file", "settings_file_checksum"}),
},
},

"solution_version": schema.StringAttribute{
MarkdownDescription: "Version of the solution",
Description: "Version of the solution",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
powerplatform_modifiers.SetStringValueToUnknownIfChecksumsChangeModifier([]string{"solution_file", "solution_file_checksum"}, []string{"settings_file", "settings_file_checksum"}),
},
},
},
Expand Down Expand Up @@ -178,7 +179,7 @@ func (r *SolutionResource) Create(ctx context.Context, req resource.CreateReques
plan.DisplayName = types.StringValue(solution.DisplayName)
plan.Id = types.StringValue(fmt.Sprintf("%s_%s", plan.EnvironmentId.ValueString(), solution.Name))

plan.SettingsFileChecksum = types.StringUnknown()
plan.SettingsFileChecksum = types.StringNull()
if !plan.SettingsFile.IsNull() && !plan.SettingsFile.IsUnknown() {
value, err := powerplatform_helpers.CalculateMd5(plan.SettingsFile.ValueString())
if err != nil {
Expand All @@ -187,8 +188,6 @@ func (r *SolutionResource) Create(ctx context.Context, req resource.CreateReques
plan.SettingsFileChecksum = types.StringValue(value)
tflog.Warn(ctx, fmt.Sprintf("CREATE Calculated md5 hash of settings file: %s", value))
}
} else {
plan.SettingsFileChecksum = types.StringNull()
}

plan.SolutionFileChecksum = types.StringUnknown()
Expand Down Expand Up @@ -233,7 +232,6 @@ func (r *SolutionResource) Read(ctx context.Context, req resource.ReadRequest, r
if solution.Name == state.SolutionName.ValueString() {
state.Id = types.StringValue(fmt.Sprintf("%s_%s", state.EnvironmentId.ValueString(), solution.Name))
state.SolutionName = types.StringValue(solution.Name)
//TODO test a case when solution version changes
state.SolutionVersion = types.StringValue(solution.Version)
state.IsManaged = types.BoolValue(solution.IsManaged)
state.DisplayName = types.StringValue(solution.DisplayName)
Expand Down Expand Up @@ -316,6 +314,9 @@ func (r *SolutionResource) Update(ctx context.Context, req resource.UpdateReques
}

solution := r.importSolution(ctx, plan, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}

plan.Id = types.StringValue(fmt.Sprintf("%s_%s", plan.EnvironmentId.ValueString(), solution.Name))

Expand All @@ -324,7 +325,7 @@ func (r *SolutionResource) Update(ctx context.Context, req resource.UpdateReques
plan.IsManaged = types.BoolValue(solution.IsManaged)
plan.DisplayName = types.StringValue(solution.DisplayName)

plan.SettingsFileChecksum = types.StringUnknown()
plan.SettingsFileChecksum = types.StringNull()
if !plan.SettingsFile.IsNull() && !plan.SettingsFile.IsUnknown() {
value, err := powerplatform_helpers.CalculateMd5(plan.SettingsFile.ValueString())
if err != nil {
Expand Down

0 comments on commit 60000ad

Please sign in to comment.