Skip to content

Commit

Permalink
fix: ignore whitespace when comparing ssh keys
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalo-rodrigues committed Aug 31, 2022
1 parent 6800d2c commit 66979bd
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
2 changes: 1 addition & 1 deletion multy/resource_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (r ResourceVirtualMachineType) GetSchema(_ context.Context) (tfsdk.Schema,
Type: types.StringType,
Description: "Public SSH Key of Virtual Machine",
Optional: true,
PlanModifiers: []tfsdk.AttributePlanModifier{resource.RequiresReplace()},
PlanModifiers: []tfsdk.AttributePlanModifier{resource.RequiresReplace(), validators.IgnoringWhitespace},
},
"public_ip_id": {
Type: types.StringType,
Expand Down
1 change: 0 additions & 1 deletion multy/resource_virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ var virtualNetworkGcpOutputs = map[string]attr.Type{
}

func (r ResourceVirtualNetworkType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {

return tfsdk.Schema{
MarkdownDescription: "Provides Multy Virtual Network resource",
Attributes: map[string]tfsdk.Attribute{
Expand Down
73 changes: 73 additions & 0 deletions multy/validators/diff_suppress_func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package validators

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-log/tflog"
"strings"
)

type DiffSuppressFunc[T any] struct {
isEqual func(T, T) bool
}

var IgnoringWhitespace = NewDiffSuppressFunc(func(val1 string, val2 string) bool {
return strings.TrimSpace(val1) == strings.TrimSpace(val2)
})

func NewDiffSuppressFunc[T any](isEqual func(T, T) bool) DiffSuppressFunc[T] {
return DiffSuppressFunc[T]{isEqual: isEqual}
}

func (m DiffSuppressFunc[T]) Description(_ context.Context) string {
return fmt.Sprintf("Ignores diff based on a custom function.")
}

func (m DiffSuppressFunc[T]) MarkdownDescription(c context.Context) string {
return m.Description(c)
}

func (m DiffSuppressFunc[T]) Modify(ctx context.Context, req tfsdk.ModifyAttributePlanRequest, resp *tfsdk.ModifyAttributePlanResponse) {
if req.AttributeConfig == nil || req.AttributeConfig.IsNull() || req.AttributeConfig.IsUnknown() {
return
}
if req.AttributeState == nil || req.AttributeState.IsNull() || req.AttributeState.IsUnknown() {
return
}

if req.AttributeConfig.Equal(req.AttributeState) {
return
}

stateVal, err := req.AttributeState.ToTerraformValue(ctx)
if err != nil {
tflog.Error(ctx, fmt.Sprintf("DiffSupressFunc: Unable to convert state value to tf value, %s", err))
return
}

configVal, err := req.AttributeConfig.ToTerraformValue(ctx)
if err != nil {
tflog.Error(ctx, fmt.Sprintf("DiffSupressFunc: Unable to convert config value to tf value, %s", err))
return
}

var c, s T

err = stateVal.As(&s)
if err != nil {
tflog.Error(ctx, fmt.Sprintf("DiffSupressFunc: Unable to convert state value, %s", err))
return
}

err = configVal.As(&c)
if err != nil {
tflog.Error(ctx, fmt.Sprintf("DiffSupressFunc: Unable to convert config value, %s", err))
return
}

if m.isEqual(c, s) {
tflog.Info(ctx, fmt.Sprintf("DiffSupressFunc: Diff has been suprressed."))
resp.AttributePlan = req.AttributeState
}
}
2 changes: 1 addition & 1 deletion tests/resources/virtual_machine/virtual_machine/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ resource multy_virtual_machine vm {
cloud = var.cloud
location = var.location
network_security_group_ids = [multy_network_security_group.nsg.id]
public_ssh_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQJRMv5ix5QPfs3+t6mLcXvtBuMdxHc5aitbWE4myzVf33UttLI4td1Q2WZ+kfn5SwiF7b9YowqWlM0kiIFGoJhcBwN0Mq4TOcGmn5Hidl94Rf8xzk88+W0LJEd+JEKY4czZNNgdWhhsxuZgt9P4NdzONqFC2XL5AggLau7SdDVV9JCHHI+dw1C0FLq1Y/5Ga7rJN2Zm7vmT4I/tCPEheEDYN2MH2ClKgQf4Ni2KoiHLxvrbBmcXOuknn+/yjN+dpiAncQFnMjykV5lKMnXFm6u43KlMLpr/XKKmdaLDZWBVaNRdPeqt2FiWisGFowAuUsXWUhSBcwXArZbWRbc0rp+ASh9is2fC9BvkOcCdMZTqJmoXFib23KaSKi1wZhGOPB93YJvZ/Gnr6n0GHWryqPJ6QvijmN20zYJq1j4ZUrNsxLgIMkGNMtCId+pbJ1amn1DM7X2fT99k+rcycTIuI83OEASs49hJfty0AM1ChSxvkm0ZAZQcFsdUnuAAyhXgU= [email protected]"
public_ssh_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQJRMv5ix5QPfs3+t6mLcXvtBuMdxHc5aitbWE4myzVf33UttLI4td1Q2WZ+kfn5SwiF7b9YowqWlM0kiIFGoJhcBwN0Mq4TOcGmn5Hidl94Rf8xzk88+W0LJEd+JEKY4czZNNgdWhhsxuZgt9P4NdzONqFC2XL5AggLau7SdDVV9JCHHI+dw1C0FLq1Y/5Ga7rJN2Zm7vmT4I/tCPEheEDYN2MH2ClKgQf4Ni2KoiHLxvrbBmcXOuknn+/yjN+dpiAncQFnMjykV5lKMnXFm6u43KlMLpr/XKKmdaLDZWBVaNRdPeqt2FiWisGFowAuUsXWUhSBcwXArZbWRbc0rp+ASh9is2fC9BvkOcCdMZTqJmoXFib23KaSKi1wZhGOPB93YJvZ/Gnr6n0GHWryqPJ6QvijmN20zYJq1j4ZUrNsxLgIMkGNMtCId+pbJ1amn1DM7X2fT99k+rcycTIuI83OEASs49hJfty0AM1ChSxvkm0ZAZQcFsdUnuAAyhXgU= [email protected]\n"
depends_on = [multy_route_table_association.rta1]
}
4 changes: 2 additions & 2 deletions tests/resources/virtual_network/main.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
variable "cloud" {
type = string
default = "gcp"
default = "aws"
}

resource "multy_virtual_network" "vn" {
name = "vn-test2"
name = "vn-test"
cidr_block = "10.0.0.0/16"
location = "eu_west_1"
cloud = var.cloud
Expand Down

0 comments on commit 66979bd

Please sign in to comment.