From 9df6de9b9fc4cd5c5384dcfde0031a2c0fae3321 Mon Sep 17 00:00:00 2001 From: Phil Browne Date: Wed, 22 May 2024 18:11:51 +0000 Subject: [PATCH] cleanup: rename plan to state in update mcr, remove ordered_product_uid from end configs in vxc, remove port_uid from vxc --- note that the megaport API will break provider on aws vxcs because it sometimes will change the product uid after the user has specified one --- internal/provider/mcr_resource.go | 6 +- internal/provider/vxc_resource.go | 80 +++++++++----------------- internal/provider/vxc_resource_test.go | 68 +++++++++++----------- 3 files changed, 64 insertions(+), 90 deletions(-) diff --git a/internal/provider/mcr_resource.go b/internal/provider/mcr_resource.go index a806635..8da053b 100644 --- a/internal/provider/mcr_resource.go +++ b/internal/provider/mcr_resource.go @@ -645,13 +645,13 @@ func (r *mcrResource) Update(ctx context.Context, req resource.UpdateRequest, re return } - apiDiags := plan.fromAPIMCR(ctx, mcr) + apiDiags := state.fromAPIMCR(ctx, mcr) resp.Diagnostics.Append(apiDiags...) // Update the state with the new values - plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850)) + state.LastUpdated = types.StringValue(time.Now().Format(time.RFC850)) - diags := resp.State.Set(ctx, &plan) + diags := resp.State.Set(ctx, &state) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return diff --git a/internal/provider/vxc_resource.go b/internal/provider/vxc_resource.go index 3804eb6..a0b1434 100644 --- a/internal/provider/vxc_resource.go +++ b/internal/provider/vxc_resource.go @@ -28,17 +28,16 @@ var ( _ resource.ResourceWithImportState = &vxcResource{} vxcEndConfigurationAttrs = map[string]attr.Type{ - "owner_uid": types.StringType, - "ordered_product_uid": types.StringType, - "product_uid": types.StringType, - "product_name": types.StringType, - "location_id": types.Int64Type, - "location": types.StringType, - "ordered_vlan": types.Int64Type, - "vlan": types.Int64Type, - "inner_vlan": types.Int64Type, - "vnic_index": types.Int64Type, - "secondary_name": types.StringType, + "owner_uid": types.StringType, + "product_uid": types.StringType, + "product_name": types.StringType, + "location_id": types.Int64Type, + "location": types.StringType, + "ordered_vlan": types.Int64Type, + "vlan": types.Int64Type, + "inner_vlan": types.Int64Type, + "vnic_index": types.Int64Type, + "secondary_name": types.StringType, } cspConnectionFullAttrs = map[string]attr.Type{ @@ -210,7 +209,6 @@ type vxcResourceModel struct { ContractEndDate types.String `tfsdk:"contract_end_date"` Shutdown types.Bool `tfsdk:"shutdown"` - PortUID types.String `tfsdk:"port_uid"` AEndConfiguration types.Object `tfsdk:"a_end"` BEndConfiguration types.Object `tfsdk:"b_end"` @@ -290,7 +288,6 @@ type vxcApprovalModel struct { // vxcEndConfigurationModel maps the end configuration schema data. type vxcEndConfigurationModel struct { OwnerUID types.String `tfsdk:"owner_uid"` - OrderedProductUID types.String `tfsdk:"ordered_product_uid"` ProductUID types.String `tfsdk:"product_uid"` Name types.String `tfsdk:"product_name"` LocationID types.Int64 `tfsdk:"location_id"` @@ -441,19 +438,16 @@ func (orm *vxcResourceModel) fromAPIVXC(ctx context.Context, v *megaport.VXC) di orm.ContractEndDate = types.StringNull() } - var aEndOrderedProductUID, bEndOrderedProductUID string var aEndOrderedVLAN, bEndOrderedVLAN int64 if !orm.AEndConfiguration.IsNull() { existingAEnd := &vxcEndConfigurationModel{} aEndDiags := orm.AEndConfiguration.As(ctx, existingAEnd, basetypes.ObjectAsOptions{}) apiDiags = append(apiDiags, aEndDiags...) - aEndOrderedProductUID = existingAEnd.OrderedProductUID.ValueString() aEndOrderedVLAN = existingAEnd.OrderedVLAN.ValueInt64() } aEndModel := &vxcEndConfigurationModel{ OwnerUID: types.StringValue(v.AEndConfiguration.OwnerUID), - OrderedProductUID: types.StringValue(aEndOrderedProductUID), ProductUID: types.StringValue(v.AEndConfiguration.UID), Name: types.StringValue(v.AEndConfiguration.Name), LocationID: types.Int64Value(int64(v.AEndConfiguration.LocationID)), @@ -472,13 +466,11 @@ func (orm *vxcResourceModel) fromAPIVXC(ctx context.Context, v *megaport.VXC) di existingBEnd := &vxcEndConfigurationModel{} bEndDiags := orm.BEndConfiguration.As(ctx, existingBEnd, basetypes.ObjectAsOptions{}) apiDiags = append(apiDiags, bEndDiags...) - bEndOrderedProductUID = existingBEnd.OrderedProductUID.ValueString() bEndOrderedVLAN = existingBEnd.OrderedVLAN.ValueInt64() } bEndModel := &vxcEndConfigurationModel{ OwnerUID: types.StringValue(v.BEndConfiguration.OwnerUID), - OrderedProductUID: types.StringValue(bEndOrderedProductUID), ProductUID: types.StringValue(v.BEndConfiguration.UID), Name: types.StringValue(v.BEndConfiguration.Name), LocationID: types.Int64Value(int64(v.BEndConfiguration.LocationID)), @@ -625,10 +617,6 @@ func (r *vxcResource) Schema(_ context.Context, _ resource.SchemaRequest, resp * Description: "The rate limit of the product.", Required: true, }, - "port_uid": schema.StringAttribute{ - Description: "The UID of the port the VXC is connected to.", - Required: true, - }, "product_type": schema.StringAttribute{ Description: "The type of the product.", Computed: true, @@ -1022,14 +1010,9 @@ func (r *vxcResource) Schema(_ context.Context, _ resource.SchemaRequest, resp * Description: "The owner UID of the A-End configuration.", Computed: true, }, - "ordered_product_uid": schema.StringAttribute{ - Description: "The initial ordered product UID of the A-End configuration.", - Optional: true, - Computed: true, - }, "product_uid": schema.StringAttribute{ - Description: "The current product UID of the A-End configuration.", - Computed: true, + Description: "The product UID of the A-End configuration.", + Required: true, }, "product_name": schema.StringAttribute{ Description: "The product name of the A-End configuration.", @@ -1077,13 +1060,9 @@ func (r *vxcResource) Schema(_ context.Context, _ resource.SchemaRequest, resp * Description: "The owner UID of the B-End configuration.", Computed: true, }, - "ordered_product_uid": schema.StringAttribute{ - Description: "The initial ordered product UID of the B-End configuration.", - Optional: true, - Computed: true, - }, "product_uid": schema.StringAttribute{ Description: "The current product UID of the B-End configuration.", + Optional: true, Computed: true, }, "product_name": schema.StringAttribute{ @@ -1589,7 +1568,6 @@ func (r *vxcResource) Create(ctx context.Context, req resource.CreateRequest, re } buyReq := &megaport.BuyVXCRequest{ - PortUID: plan.PortUID.ValueString(), VXCName: plan.Name.ValueString(), Term: int(plan.ContractTermMonths.ValueInt64()), RateLimit: int(plan.RateLimit.ValueInt64()), @@ -1612,9 +1590,10 @@ func (r *vxcResource) Create(ctx context.Context, req resource.CreateRequest, re return } aEndConfig := &megaport.VXCOrderEndpointConfiguration{ - ProductUID: a.OrderedProductUID.ValueString(), + ProductUID: a.ProductUID.ValueString(), VLAN: int(a.VLAN.ValueInt64()), } + buyReq.PortUID = a.ProductUID.ValueString() if !a.OrderedVLAN.IsNull() { aEndConfig.VLAN = int(a.OrderedVLAN.ValueInt64()) @@ -1687,9 +1666,7 @@ func (r *vxcResource) Create(ctx context.Context, req resource.CreateRequest, re PortSpeed: int(plan.RateLimit.ValueInt64()), Partner: "AZURE", } - if !a.OrderedProductUID.IsNull() { - partnerPortReq.ProductID = a.OrderedProductUID.ValueString() - } + partnerPortReq.ProductID = a.ProductUID.ValueString() partnerPortRes, err := r.client.VXCService.LookupPartnerPorts(ctx, partnerPortReq) if err != nil { resp.Diagnostics.AddError( @@ -1750,9 +1727,7 @@ func (r *vxcResource) Create(ctx context.Context, req resource.CreateRequest, re PortSpeed: int(plan.RateLimit.ValueInt64()), Partner: "GOOGLE", } - if !a.OrderedProductUID.IsNull() { - partnerPortReq.ProductID = a.OrderedProductUID.ValueString() - } + partnerPortReq.ProductID = a.ProductUID.ValueString() partnerPortRes, err := r.client.VXCService.LookupPartnerPorts(ctx, partnerPortReq) if err != nil { resp.Diagnostics.AddError( @@ -1805,9 +1780,8 @@ func (r *vxcResource) Create(ctx context.Context, req resource.CreateRequest, re PortSpeed: int(plan.RateLimit.ValueInt64()), Partner: "ORACLE", } - if !a.OrderedProductUID.IsNull() { - partnerPortReq.ProductID = a.OrderedProductUID.ValueString() - } + partnerPortReq.ProductID = a.ProductUID.ValueString() + partnerPortRes, err := r.client.VXCService.LookupPartnerPorts(ctx, partnerPortReq) if err != nil { @@ -1854,7 +1828,7 @@ func (r *vxcResource) Create(ctx context.Context, req resource.CreateRequest, re resp.Diagnostics.Append(aEndDiags...) return } - prefixFilterListRes, err := r.client.MCRService.GetMCRPrefixFilterLists(ctx, plan.PortUID.ValueString()) + prefixFilterListRes, err := r.client.MCRService.GetMCRPrefixFilterLists(ctx, a.ProductUID.ValueString()) if err != nil { resp.Diagnostics.AddError( "Error creating VXC", @@ -2002,7 +1976,7 @@ func (r *vxcResource) Create(ctx context.Context, req resource.CreateRequest, re return } bEndConfig := &megaport.VXCOrderEndpointConfiguration{ - ProductUID: b.OrderedProductUID.ValueString(), + ProductUID: b.ProductUID.ValueString(), VLAN: int(b.VLAN.ValueInt64()), } if !b.OrderedVLAN.IsNull() { @@ -2094,8 +2068,8 @@ func (r *vxcResource) Create(ctx context.Context, req resource.CreateRequest, re PortSpeed: int(plan.RateLimit.ValueInt64()), Partner: "AZURE", } - if !b.OrderedProductUID.IsNull() { - partnerPortReq.ProductID = b.OrderedProductUID.ValueString() + if !b.ProductUID.IsNull() { + partnerPortReq.ProductID = b.ProductUID.ValueString() } partnerPortRes, err := r.client.VXCService.LookupPartnerPorts(ctx, partnerPortReq) if err != nil { @@ -2150,8 +2124,8 @@ func (r *vxcResource) Create(ctx context.Context, req resource.CreateRequest, re PortSpeed: int(plan.RateLimit.ValueInt64()), Partner: "GOOGLE", } - if !b.OrderedProductUID.IsNull() { - partnerPortReq.ProductID = b.OrderedProductUID.ValueString() + if !b.ProductUID.IsNull() { + partnerPortReq.ProductID = b.ProductUID.ValueString() } partnerPortRes, err := r.client.VXCService.LookupPartnerPorts(ctx, partnerPortReq) if err != nil { @@ -2204,8 +2178,8 @@ func (r *vxcResource) Create(ctx context.Context, req resource.CreateRequest, re PortSpeed: int(plan.RateLimit.ValueInt64()), Partner: "ORACLE", } - if !b.OrderedProductUID.IsNull() { - partnerPortReq.ProductID = b.OrderedProductUID.ValueString() + if !b.ProductUID.IsNull() { + partnerPortReq.ProductID = b.ProductUID.ValueString() } partnerPortRes, err := r.client.VXCService.LookupPartnerPorts(ctx, partnerPortReq) if err != nil { diff --git a/internal/provider/vxc_resource_test.go b/internal/provider/vxc_resource_test.go index b27c1b9..e34fa2e 100644 --- a/internal/provider/vxc_resource_test.go +++ b/internal/provider/vxc_resource_test.go @@ -41,13 +41,13 @@ func TestAccMegaportVXC_Basic(t *testing.T) { product_name = "%s" rate_limit = 1000 contract_term_months = 12 - port_uid = megaport_port.port_1.product_uid a_end = { + product_uid = megaport_port.port_1.product_uid } b_end = { - ordered_product_uid = megaport_port.port_2.product_uid + product_uid = megaport_port.port_2.product_uid } } `, portName1, portName2, vxcName), @@ -86,7 +86,7 @@ func TestAccMegaportVXC_Basic(t *testing.T) { } return rawState["product_uid"], nil }, - ImportStateVerifyIgnore: []string{"last_updated", "port_uid", "a_end_partner_config", "b_end_partner_config", "a_end", "b_end", "contract_start_date", "contract_end_date", "live_date", "resources", "provisioning_status"}, + ImportStateVerifyIgnore: []string{"last_updated", "a_end_partner_config", "b_end_partner_config", "a_end", "b_end", "contract_start_date", "contract_end_date", "live_date", "resources", "provisioning_status"}, }, // Update Tests { @@ -95,36 +95,36 @@ func TestAccMegaportVXC_Basic(t *testing.T) { name = "NextDC B1" } resource "megaport_port" "port_1" { - product_name = "%s" - port_speed = 1000 - location_id = data.megaport_location.loc.id - contract_term_months = 12 + product_name = "%s" + port_speed = 1000 + location_id = data.megaport_location.loc.id + contract_term_months = 12 cost_centre = "test" marketplace_visibility = false - } - resource "megaport_port" "port_2" { - product_name = "%s" - port_speed = 1000 - location_id = data.megaport_location.loc.id - contract_term_months = 12 + } + resource "megaport_port" "port_2" { + product_name = "%s" + port_speed = 1000 + location_id = data.megaport_location.loc.id + contract_term_months = 12 cost_centre = "test" marketplace_visibility = false - } - resource "megaport_vxc" "vxc" { - product_name = "%s" + } + resource "megaport_vxc" "vxc" { + product_name = "%s" cost_centre = "%s" - rate_limit = 500 - port_uid = megaport_port.port_1.product_uid + rate_limit = 500 contract_term_months = 12 - a_end = { - } + a_end = { + product_uid = megaport_port.port_1.product_uid + } - b_end = { - ordered_product_uid = megaport_port.port_2.product_uid - } - } - `, portName1, portName2, vxcNameNew, costCentreNew), + b_end = { + product_uid = megaport_port.port_2.product_uid + } + } + `, portName1, portName2, vxcNameNew, costCentreNew), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("megaport_port.port_1", "product_name", portName1), resource.TestCheckResourceAttr("megaport_port.port_1", "port_speed", "1000"), @@ -184,15 +184,15 @@ func TestAccMegaportMCRVXCWithCSPs_Basic(t *testing.T) { resource "megaport_vxc" "aws_vxc" { product_name = "%s" rate_limit = 1000 - port_uid = megaport_mcr.mcr.product_uid contract_term_months = 1 a_end = { + product_uid = megaport_mcr.mcr.product_uid ordered_vlan = 2191 } b_end = { - ordered_product_uid = data.megaport_partner.aws_port.product_uid + product_uid = data.megaport_partner.aws_port.product_uid } b_end_partner_config = { @@ -212,9 +212,9 @@ func TestAccMegaportMCRVXCWithCSPs_Basic(t *testing.T) { product_name = "%s" rate_limit = 1000 contract_term_months = 1 - port_uid = megaport_mcr.mcr.product_uid a_end = { + product_uid = megaport_mcr.mcr.product_uid ordered_vlan = 182 } @@ -232,9 +232,9 @@ func TestAccMegaportMCRVXCWithCSPs_Basic(t *testing.T) { product_name = "%s" rate_limit = 200 contract_term_months = 1 - port_uid = megaport_mcr.mcr.product_uid a_end = { + product_uid = megaport_mcr.mcr.product_uid ordered_vlan = 0 } @@ -270,15 +270,15 @@ func TestAccMegaportMCRVXCWithBGP_Basic(t *testing.T) { name = "NextDC B1" } - data "megaport_location" "syd_gs" { - name = "Global Switch Sydney West" + data "megaport_location" "syd_ndc" { + name = "NextDC C1" } data "megaport_partner" "aws_port" { connect_type = "AWS" company_name = "AWS" product_name = "Asia Pacific (Sydney) (ap-southeast-2)" - location_id = data.megaport_location.syd_gs.id + location_id = data.megaport_location.syd_ndc.id } resource "megaport_mcr" "mcr" { @@ -312,10 +312,10 @@ func TestAccMegaportMCRVXCWithBGP_Basic(t *testing.T) { resource "megaport_vxc" "aws_vxc" { product_name = "%s" rate_limit = 1000 - port_uid = megaport_mcr.mcr.product_uid contract_term_months = 1 a_end = { + product_uid = megaport_mcr.mcr.product_uid ordered_vlan = 0 } @@ -351,7 +351,7 @@ func TestAccMegaportMCRVXCWithBGP_Basic(t *testing.T) { } b_end = { - ordered_product_uid = data.megaport_partner.aws_port.product_uid + product_uid = aws_port.product_uid } b_end_partner_config = {