Skip to content

Commit

Permalink
fix: address import of partner config issue
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaportPhilipBrowne committed Nov 1, 2024
1 parent 6a9efb6 commit 11d015c
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions internal/provider/vxc_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand All @@ -3567,21 +3582,42 @@ 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...)
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)
stateDiags := req.State.Set(ctx, &state)
diags = append(diags, stateDiags...)
}

resp.Diagnostics.Append(diags...)
Expand Down

0 comments on commit 11d015c

Please sign in to comment.