From 7fb3813acdce6e818e8201949985a325076fb830 Mon Sep 17 00:00:00 2001 From: MegaportPhilipBrowne Date: Thu, 29 Aug 2024 13:09:06 -0400 Subject: [PATCH 1/6] fix: plan modifier for ordered vlan --- internal/provider/vxc_resource.go | 88 +++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 15 deletions(-) diff --git a/internal/provider/vxc_resource.go b/internal/provider/vxc_resource.go index 92edc45..b809f12 100644 --- a/internal/provider/vxc_resource.go +++ b/internal/provider/vxc_resource.go @@ -486,8 +486,6 @@ func (orm *vxcResourceModel) fromAPIVXC(ctx context.Context, v *megaport.VXC) di } if aEndOrderedVLAN != nil { aEndModel.OrderedVLAN = types.Int64Value(*aEndOrderedVLAN) - } else { - aEndModel.OrderedVLAN = types.Int64PointerValue(nil) } if v.AEndConfiguration.InnerVLAN == 0 { aEndModel.InnerVLAN = types.Int64PointerValue(nil) @@ -522,8 +520,6 @@ func (orm *vxcResourceModel) fromAPIVXC(ctx context.Context, v *megaport.VXC) di } if bEndOrderedVLAN != nil { bEndModel.OrderedVLAN = types.Int64Value(*bEndOrderedVLAN) - } else { - bEndModel.OrderedVLAN = types.Int64PointerValue(nil) } if v.BEndConfiguration.InnerVLAN == 0 { bEndModel.InnerVLAN = types.Int64PointerValue(nil) @@ -3229,9 +3225,13 @@ func (r *vxcResource) Read(ctx context.Context, req resource.ReadRequest, resp * // Set refreshed state diags = resp.State.Set(ctx, &state) resp.Diagnostics.Append(diags...) - if resp.Diagnostics.HasError() { - return - } + + aEndConfig := &vxcEndConfigurationModel{} + bEndConfig := &vxcEndConfigurationModel{} + aEndConfigDiags := state.AEndConfiguration.As(ctx, aEndConfig, basetypes.ObjectAsOptions{}) + bEndConfigDiags := state.BEndConfiguration.As(ctx, bEndConfig, basetypes.ObjectAsOptions{}) + resp.Diagnostics.Append(aEndConfigDiags...) + resp.Diagnostics.Append(bEndConfigDiags...) } func (r *vxcResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { @@ -3278,24 +3278,32 @@ func (r *vxcResource) Update(ctx context.Context, req resource.UpdateRequest, re } // If Ordered VLAN is different from actual VLAN, attempt to change it to the ordered VLAN value. - if !aEndPlan.OrderedVLAN.IsUnknown() && !aEndPlan.OrderedVLAN.IsNull() && !aEndPlan.OrderedVLAN.Equal(aEndState.VLAN) { - updateReq.AEndVLAN = megaport.PtrTo(int(aEndPlan.OrderedVLAN.ValueInt64())) + if !aEndPlan.OrderedVLAN.IsUnknown() && !aEndPlan.OrderedVLAN.IsNull() { + if !aEndPlan.OrderedVLAN.Equal(aEndState.VLAN) { + updateReq.AEndVLAN = megaport.PtrTo(int(aEndPlan.OrderedVLAN.ValueInt64())) + } aEndState.OrderedVLAN = aEndPlan.OrderedVLAN } - if !aEndPlan.InnerVLAN.IsUnknown() && !aEndPlan.InnerVLAN.IsNull() && !aEndPlan.InnerVLAN.Equal(aEndState.InnerVLAN) { - updateReq.AEndInnerVLAN = megaport.PtrTo(int(aEndPlan.InnerVLAN.ValueInt64())) + if !aEndPlan.InnerVLAN.IsUnknown() && !aEndPlan.InnerVLAN.IsNull() { + if !aEndPlan.InnerVLAN.Equal(aEndState.InnerVLAN) { + updateReq.AEndInnerVLAN = megaport.PtrTo(int(aEndPlan.InnerVLAN.ValueInt64())) + } aEndState.InnerVLAN = aEndPlan.InnerVLAN } // If Ordered VLAN is different from actual VLAN, attempt to change it to the ordered VLAN value. - if !bEndPlan.OrderedVLAN.IsUnknown() && !bEndPlan.OrderedVLAN.IsNull() && !bEndPlan.OrderedVLAN.Equal(bEndState.VLAN) { - updateReq.BEndVLAN = megaport.PtrTo(int(bEndPlan.OrderedVLAN.ValueInt64())) + if !bEndPlan.OrderedVLAN.IsUnknown() && !bEndPlan.OrderedVLAN.IsNull() { + if !bEndPlan.OrderedVLAN.Equal(bEndState.VLAN) { + updateReq.BEndVLAN = megaport.PtrTo(int(bEndPlan.OrderedVLAN.ValueInt64())) + } bEndState.OrderedVLAN = bEndPlan.OrderedVLAN } - if !bEndPlan.InnerVLAN.IsUnknown() && !bEndPlan.InnerVLAN.IsNull() && !bEndPlan.InnerVLAN.Equal(bEndState.InnerVLAN) { - updateReq.BEndInnerVLAN = megaport.PtrTo(int(bEndPlan.InnerVLAN.ValueInt64())) + if !bEndPlan.InnerVLAN.IsUnknown() && !bEndPlan.InnerVLAN.IsNull() { + if !bEndPlan.InnerVLAN.Equal(bEndState.InnerVLAN) { + updateReq.BEndInnerVLAN = megaport.PtrTo(int(bEndPlan.InnerVLAN.ValueInt64())) + } bEndState.InnerVLAN = bEndPlan.InnerVLAN } @@ -3531,3 +3539,53 @@ func fromAPICSPConnection(ctx context.Context, c megaport.CSPConnectionConfig) ( apiDiags.AddError("Error creating CSP Connection", "Could not create CSP Connection, unknown type") return types.ObjectNull(cspConnectionFullAttrs), apiDiags } + +func (r *vxcResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { + // Get current state + var plan, state vxcResourceModel + diags := diag.Diagnostics{} + stateDiags := req.State.Get(ctx, &state) + diags.Append(stateDiags...) + planDiags := req.Plan.Get(ctx, &plan) + diags.Append(planDiags...) + + // If VXC is not yet created, return + if !state.UID.IsNull() { + aEndStateObj := state.AEndConfiguration + bEndStateObj := state.BEndConfiguration + aEndStateConfig := &vxcEndConfigurationModel{} + bEndStateConfig := &vxcEndConfigurationModel{} + aEndDiags := aEndStateObj.As(ctx, aEndStateConfig, basetypes.ObjectAsOptions{}) + bEndDiags := bEndStateObj.As(ctx, bEndStateConfig, basetypes.ObjectAsOptions{}) + diags = append(diags, aEndDiags...) + diags = append(diags, bEndDiags...) + aEndPlanObj := plan.AEndConfiguration + bEndPlanObj := plan.BEndConfiguration + aEndPlanConfig := &vxcEndConfigurationModel{} + bEndPlanConfig := &vxcEndConfigurationModel{} + aEndDiags = aEndPlanObj.As(ctx, aEndPlanConfig, basetypes.ObjectAsOptions{}) + bEndDiags = bEndPlanObj.As(ctx, bEndPlanConfig, basetypes.ObjectAsOptions{}) + diags = append(diags, aEndDiags...) + diags = append(diags, bEndDiags...) + if aEndStateConfig.OrderedVLAN.IsNull() { + aEndPlanConfig.OrderedVLAN = aEndStateConfig.VLAN + } + if bEndStateConfig.OrderedVLAN.IsNull() { + bEndPlanConfig.OrderedVLAN = bEndStateConfig.VLAN + } + newPlanAEndObj, aEndDiags := types.ObjectValueFrom(ctx, vxcEndConfigurationAttrs, aEndPlanConfig) + newPlanBEndObj, bEndDiags := types.ObjectValueFrom(ctx, vxcEndConfigurationAttrs, bEndPlanConfig) + diags = append(diags, aEndDiags...) + diags = append(diags, bEndDiags...) + plan.AEndConfiguration = newPlanAEndObj + plan.BEndConfiguration = newPlanBEndObj + req.Plan.Set(ctx, &plan) + req.State.Set(ctx, &plan) + resp.Plan.Set(ctx, &plan) + } + + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } +} From 7acc333eb3b5135e647140bb4c2a36d314528a85 Mon Sep 17 00:00:00 2001 From: MegaportPhilipBrowne Date: Tue, 8 Oct 2024 13:09:20 -0400 Subject: [PATCH 2/6] chore: bump mpgo version --- go.mod | 2 +- go.sum | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index e99cebe..6ead287 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 github.com/hashicorp/terraform-plugin-go v0.22.1 github.com/hashicorp/terraform-plugin-log v0.9.0 - github.com/megaport/megaportgo v1.2.2 + github.com/megaport/megaportgo v1.2.3 ) require ( diff --git a/go.sum b/go.sum index 8c161e3..6cb69a8 100644 --- a/go.sum +++ b/go.sum @@ -150,10 +150,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -github.com/megaport/megaportgo v1.2.0 h1:LLI+3OaK4BzUMazuQm1TAptRaQbGrTp+wDxEjkARCNE= -github.com/megaport/megaportgo v1.2.0/go.mod h1:I+8jJioFFsF+55sxFYCgcKNUENaOpqzwlsIu6NGA7qk= -github.com/megaport/megaportgo v1.2.2 h1:exF0cLFk5Bug9SAThvJDBmwpVf7TCc0XFv7UjRS6x5Y= -github.com/megaport/megaportgo v1.2.2/go.mod h1:I+8jJioFFsF+55sxFYCgcKNUENaOpqzwlsIu6NGA7qk= +github.com/megaport/megaportgo v1.2.3 h1:1yBEFiHsvcPWl7vppmfMx1+MfAbxhZwpZAFnqvBnzJc= +github.com/megaport/megaportgo v1.2.3/go.mod h1:I+8jJioFFsF+55sxFYCgcKNUENaOpqzwlsIu6NGA7qk= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= From bb3fb13b63a10166482255b18390c039a851ae7b Mon Sep 17 00:00:00 2001 From: MegaportPhilipBrowne Date: Mon, 14 Oct 2024 16:38:27 -0400 Subject: [PATCH 3/6] chore: bump megaportgo version --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 6ead287..2592e69 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 github.com/hashicorp/terraform-plugin-go v0.22.1 github.com/hashicorp/terraform-plugin-log v0.9.0 - github.com/megaport/megaportgo v1.2.3 + github.com/megaport/megaportgo v1.2.4 ) require ( diff --git a/go.sum b/go.sum index 6cb69a8..0a17103 100644 --- a/go.sum +++ b/go.sum @@ -150,8 +150,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -github.com/megaport/megaportgo v1.2.3 h1:1yBEFiHsvcPWl7vppmfMx1+MfAbxhZwpZAFnqvBnzJc= -github.com/megaport/megaportgo v1.2.3/go.mod h1:I+8jJioFFsF+55sxFYCgcKNUENaOpqzwlsIu6NGA7qk= +github.com/megaport/megaportgo v1.2.4 h1:O1LbknD74ktMDMoV75WUGb1A/s/vLfhwFVGU9871Dmg= +github.com/megaport/megaportgo v1.2.4/go.mod h1:I+8jJioFFsF+55sxFYCgcKNUENaOpqzwlsIu6NGA7qk= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= From 6a9efb63d56eef7a58de180d3145a1853dcdcd99 Mon Sep 17 00:00:00 2001 From: MegaportPhilipBrowne Date: Wed, 16 Oct 2024 11:26:43 -0400 Subject: [PATCH 4/6] chore: bump megaportgo version --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 2592e69..2d4737f 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 github.com/hashicorp/terraform-plugin-go v0.22.1 github.com/hashicorp/terraform-plugin-log v0.9.0 - github.com/megaport/megaportgo v1.2.4 + github.com/megaport/megaportgo v1.2.5 ) require ( diff --git a/go.sum b/go.sum index 0a17103..3015943 100644 --- a/go.sum +++ b/go.sum @@ -150,8 +150,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -github.com/megaport/megaportgo v1.2.4 h1:O1LbknD74ktMDMoV75WUGb1A/s/vLfhwFVGU9871Dmg= -github.com/megaport/megaportgo v1.2.4/go.mod h1:I+8jJioFFsF+55sxFYCgcKNUENaOpqzwlsIu6NGA7qk= +github.com/megaport/megaportgo v1.2.5 h1:87IdC1X1zivNv3j0xOtjAJ7oQNAAXhuposfOtom8mis= +github.com/megaport/megaportgo v1.2.5/go.mod h1:I+8jJioFFsF+55sxFYCgcKNUENaOpqzwlsIu6NGA7qk= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= From 11d015c05394faa5535303435eac5356fb628606 Mon Sep 17 00:00:00 2001 From: MegaportPhilipBrowne Date: Fri, 1 Nov 2024 09:37:02 -0400 Subject: [PATCH 5/6] fix: address import of partner config issue --- internal/provider/vxc_resource.go | 48 +++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/internal/provider/vxc_resource.go b/internal/provider/vxc_resource.go index b809f12..f6f70c1 100644 --- a/internal/provider/vxc_resource.go +++ b/internal/provider/vxc_resource.go @@ -3244,6 +3244,14 @@ func (r *vxcResource) Update(ctx context.Context, req resource.UpdateRequest, re return } + // If Imported, AEndPartnerConfig will be null. Set the partner config to the existing one in the plan. + if state.AEndPartnerConfig.IsNull() { + state.AEndPartnerConfig = plan.AEndPartnerConfig + } + if state.BEndPartnerConfig.IsNull() { + state.BEndPartnerConfig = plan.BEndPartnerConfig + } + var aEndPlan, bEndPlan, aEndState, bEndState *vxcEndConfigurationModel aEndPlanDiags := plan.AEndConfiguration.As(ctx, &aEndPlan, basetypes.ObjectAsOptions{}) @@ -3417,6 +3425,7 @@ func (r *vxcResource) Configure(_ context.Context, req resource.ConfigureRequest func (r *vxcResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { // Retrieve import ID and save to id attribute resource.ImportStatePassthroughID(ctx, path.Root("product_uid"), req, resp) + } func fromAPICSPConnection(ctx context.Context, c megaport.CSPConnectionConfig) (types.Object, diag.Diagnostics) { @@ -3544,10 +3553,16 @@ func (r *vxcResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq // Get current state var plan, state vxcResourceModel diags := diag.Diagnostics{} - stateDiags := req.State.Get(ctx, &state) - diags.Append(stateDiags...) + planDiags := req.Plan.Get(ctx, &plan) - diags.Append(planDiags...) + resp.Diagnostics.Append(planDiags...) + if !req.State.Raw.IsNull() { + stateDiags := req.State.Get(ctx, &state) + resp.Diagnostics.Append(stateDiags...) + } + if resp.Diagnostics.HasError() { + return + } // If VXC is not yet created, return if !state.UID.IsNull() { @@ -3567,12 +3582,32 @@ func (r *vxcResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq bEndDiags = bEndPlanObj.As(ctx, bEndPlanConfig, basetypes.ObjectAsOptions{}) diags = append(diags, aEndDiags...) diags = append(diags, bEndDiags...) - if aEndStateConfig.OrderedVLAN.IsNull() { + if aEndStateConfig.OrderedVLAN.IsUnknown() { aEndPlanConfig.OrderedVLAN = aEndStateConfig.VLAN } - if bEndStateConfig.OrderedVLAN.IsNull() { + if bEndStateConfig.OrderedVLAN.IsUnknown() { bEndPlanConfig.OrderedVLAN = bEndStateConfig.VLAN } + if aEndStateConfig.RequestedProductUID.IsUnknown() { + aEndPlanConfig.RequestedProductUID = aEndStateConfig.CurrentProductUID + } + if bEndStateConfig.RequestedProductUID.IsUnknown() { + bEndPlanConfig.RequestedProductUID = bEndStateConfig.CurrentProductUID + } + if state.AEndPartnerConfig.IsNull() { + state.AEndPartnerConfig = plan.AEndPartnerConfig + } else { + if !plan.AEndPartnerConfig.Equal(state.AEndPartnerConfig) { + resp.RequiresReplace = append(resp.RequiresReplace, path.Root("a_end_partner_config")) + } + } + if state.BEndPartnerConfig.IsNull() { + state.BEndPartnerConfig = plan.BEndPartnerConfig + } else { + if !plan.BEndPartnerConfig.Equal(state.BEndPartnerConfig) { + resp.RequiresReplace = append(resp.RequiresReplace, path.Root("b_end_partner_config")) + } + } newPlanAEndObj, aEndDiags := types.ObjectValueFrom(ctx, vxcEndConfigurationAttrs, aEndPlanConfig) newPlanBEndObj, bEndDiags := types.ObjectValueFrom(ctx, vxcEndConfigurationAttrs, bEndPlanConfig) diags = append(diags, aEndDiags...) @@ -3580,8 +3615,9 @@ func (r *vxcResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq plan.AEndConfiguration = newPlanAEndObj plan.BEndConfiguration = newPlanBEndObj req.Plan.Set(ctx, &plan) - req.State.Set(ctx, &plan) resp.Plan.Set(ctx, &plan) + stateDiags := req.State.Set(ctx, &state) + diags = append(diags, stateDiags...) } resp.Diagnostics.Append(diags...) From d7c4214641ebe1ecab7aaba0fc103d4b56789d47 Mon Sep 17 00:00:00 2001 From: MegaportPhilipBrowne Date: Fri, 1 Nov 2024 09:56:10 -0400 Subject: [PATCH 6/6] fix: do not change requested product uid upon import --- internal/provider/vxc_resource.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/internal/provider/vxc_resource.go b/internal/provider/vxc_resource.go index f6f70c1..e56fc7b 100644 --- a/internal/provider/vxc_resource.go +++ b/internal/provider/vxc_resource.go @@ -3588,12 +3588,6 @@ func (r *vxcResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq if bEndStateConfig.OrderedVLAN.IsUnknown() { bEndPlanConfig.OrderedVLAN = bEndStateConfig.VLAN } - if aEndStateConfig.RequestedProductUID.IsUnknown() { - aEndPlanConfig.RequestedProductUID = aEndStateConfig.CurrentProductUID - } - if bEndStateConfig.RequestedProductUID.IsUnknown() { - bEndPlanConfig.RequestedProductUID = bEndStateConfig.CurrentProductUID - } if state.AEndPartnerConfig.IsNull() { state.AEndPartnerConfig = plan.AEndPartnerConfig } else {