Skip to content

Commit

Permalink
cleanup: rename plan to state in update mcr, remove ordered_product_u…
Browse files Browse the repository at this point in the history
…id 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
  • Loading branch information
MegaportPhilipBrowne committed May 22, 2024
1 parent 28afd26 commit 9df6de9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 90 deletions.
6 changes: 3 additions & 3 deletions internal/provider/mcr_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
80 changes: 27 additions & 53 deletions internal/provider/vxc_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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"`

Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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)),
Expand All @@ -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)),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()),
Expand All @@ -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())
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 9df6de9

Please sign in to comment.