Skip to content

Commit

Permalink
fix: vxc update, cost centre in products/mcr/ports, update marketplac…
Browse files Browse the repository at this point in the history
…e_visibility, fix modifying port bug, make market computed across provider in all resources
  • Loading branch information
MegaportPhilipBrowne committed May 16, 2024
1 parent e7d3d02 commit 009ddd6
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 99 deletions.
21 changes: 14 additions & 7 deletions internal/provider/lag_port_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,7 @@ func (r *lagPortResource) Schema(_ context.Context, _ resource.SchemaRequest, re
},
"market": schema.StringAttribute{
Description: "The market the product is in.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Computed: true,
},
"location_id": schema.Int64Attribute{
Description: "The numeric location ID of the product.",
Expand Down Expand Up @@ -484,10 +481,10 @@ func (r *lagPortResource) Create(ctx context.Context, req resource.CreateRequest
Term: int(plan.ContractTermMonths.ValueInt64()),
PortSpeed: int(plan.PortSpeed.ValueInt64()),
LocationId: int(plan.LocationID.ValueInt64()),
Market: plan.Market.ValueString(),
LagCount: int(plan.LagCount.ValueInt64()),
MarketPlaceVisibility: plan.MarketplaceVisibility.ValueBool(),
DiversityZone: plan.DiversityZone.ValueString(),
CostCentre: plan.CostCentre.ValueString(),
WaitForProvision: true,
WaitForTime: 5 * time.Minute,
})
Expand Down Expand Up @@ -583,17 +580,27 @@ func (r *lagPortResource) Update(ctx context.Context, req resource.UpdateRequest

// Check on changes
var name, costCentre string
var marketplaceVisibility bool
if !plan.Name.Equal(state.Name) {
name = plan.Name.ValueString()
} else {
name = state.Name.ValueString()
}
if !plan.CostCentre.Equal(state.CostCentre) {
costCentre = plan.Name.ValueString()
costCentre = plan.CostCentre.ValueString()
} else {
costCentre = state.CostCentre.ValueString()
}
if !plan.MarketplaceVisibility.Equal(state.MarketplaceVisibility) {
marketplaceVisibility = plan.MarketplaceVisibility.ValueBool()
} else {
marketplaceVisibility = state.MarketplaceVisibility.ValueBool()
}

r.client.PortService.ModifyPort(ctx, &megaport.ModifyPortRequest{
PortID: plan.UID.ValueString(),
Name: name,
MarketplaceVisibility: plan.MarketplaceVisibility.ValueBool(),
MarketplaceVisibility: marketplaceVisibility,
CostCentre: costCentre,
WaitForUpdate: true,
})
Expand Down
48 changes: 41 additions & 7 deletions internal/provider/lag_port_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (

func TestAccMegaportLAGPort_Basic(t *testing.T) {
portName := RandomTestName()
costCentreName := RandomTestName()
portNameNew := RandomTestName()
costCentreNameNew := RandomTestName()
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
Expand All @@ -20,20 +23,20 @@ func TestAccMegaportLAGPort_Basic(t *testing.T) {
}
resource "megaport_lag_port" "lag_port" {
product_name = "%s"
cost_centre = "%s"
port_speed = 10000
location_id = data.megaport_location.bne_nxt1.id
contract_term_months = 1
market = "AU"
marketplace_visibility = false
contract_term_months = 12
marketplace_visibility = true
lag_count = 3
}`, portName),
}`, portName, costCentreName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "product_name", portName),
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "port_speed", "10000"),
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "contract_term_months", "1"),
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "market", "AU"),
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "marketplace_visibility", "false"),
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "contract_term_months", "12"),
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "marketplace_visibility", "true"),
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "lag_count", "3"),
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "cost_centre", costCentreName),
resource.TestCheckResourceAttrSet("megaport_lag_port.lag_port", "product_uid"),
resource.TestCheckResourceAttrSet("megaport_lag_port.lag_port", "product_id"),
resource.TestCheckResourceAttrSet("megaport_lag_port.lag_port", "provisioning_status"),
Expand Down Expand Up @@ -63,6 +66,37 @@ func TestAccMegaportLAGPort_Basic(t *testing.T) {
},
ImportStateVerifyIgnore: []string{"last_updated", "lag_count", "lag_port_uids", "contract_start_date", "contract_end_date", "live_date", "resources", "provisioning_status"},
},
// Update Testing
{
Config: providerConfig + fmt.Sprintf(`
data "megaport_location" "bne_nxt1" {
name = "NextDC B1"
}
resource "megaport_lag_port" "lag_port" {
product_name = "%s"
cost_centre = "%s"
port_speed = 10000
location_id = data.megaport_location.bne_nxt1.id
contract_term_months = 12
marketplace_visibility = false
lag_count = 3
}`, portNameNew, costCentreNameNew),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "product_name", portNameNew),
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "port_speed", "10000"),
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "contract_term_months", "12"),
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "marketplace_visibility", "false"),
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "lag_count", "3"),
resource.TestCheckResourceAttr("megaport_lag_port.lag_port", "cost_centre", costCentreNameNew),
resource.TestCheckResourceAttrSet("megaport_lag_port.lag_port", "product_uid"),
resource.TestCheckResourceAttrSet("megaport_lag_port.lag_port", "product_id"),
resource.TestCheckResourceAttrSet("megaport_lag_port.lag_port", "provisioning_status"),
resource.TestCheckResourceAttrSet("megaport_lag_port.lag_port", "create_date"),
resource.TestCheckResourceAttrSet("megaport_lag_port.lag_port", "created_by"),
resource.TestCheckResourceAttrSet("megaport_lag_port.lag_port", "location_id"),
resource.TestCheckResourceAttrSet("megaport_lag_port.lag_port", "company_uid"),
),
},
},
})
}
30 changes: 19 additions & 11 deletions internal/provider/mcr_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,7 @@ func (r *mcrResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *
},
"market": schema.StringAttribute{
Description: "Market the product is in.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Computed: true,
},
"location_id": schema.Int64Attribute{
Description: "Location ID of the product.",
Expand Down Expand Up @@ -489,6 +486,7 @@ func (r *mcrResource) Create(ctx context.Context, req resource.CreateRequest, re
Term: int(plan.ContractTermMonths.ValueInt64()),
PortSpeed: int(plan.PortSpeed.ValueInt64()),
LocationID: int(plan.LocationID.ValueInt64()),
CostCentre: plan.CostCentre.ValueString(),
WaitForProvision: true,
WaitForTime: 10 * time.Minute,
}
Expand Down Expand Up @@ -613,17 +611,27 @@ func (r *mcrResource) Update(ctx context.Context, req resource.UpdateRequest, re

// Check on changes
var name, costCentre string
if !plan.Name.Equal(state.Name) {
var marketplaceVisibility bool
if !plan.Name.IsNull() && !plan.Name.Equal(state.Name) {
name = plan.Name.ValueString()
} else {
name = state.Name.ValueString()
}
if !plan.CostCentre.Equal(state.CostCentre) {
costCentre = plan.Name.ValueString()
if !plan.CostCentre.IsNull() && !plan.CostCentre.Equal(state.CostCentre) {
costCentre = plan.CostCentre.ValueString()
} else {
costCentre = state.CostCentre.ValueString()
}
if !plan.MarketplaceVisibility.IsNull() && !plan.MarketplaceVisibility.Equal(state.MarketplaceVisibility) {
marketplaceVisibility = plan.MarketplaceVisibility.ValueBool()
} else {
marketplaceVisibility = state.MarketplaceVisibility.ValueBool()
}

_, err := r.client.MCRService.ModifyMCR(ctx, &megaport.ModifyMCRRequest{
MCRID: plan.UID.ValueString(),
Name: name,
MarketplaceVisibility: plan.MarketplaceVisibility.ValueBool(),
MarketplaceVisibility: marketplaceVisibility,
CostCentre: costCentre,
WaitForUpdate: true,
})
Expand All @@ -646,13 +654,13 @@ func (r *mcrResource) Update(ctx context.Context, req resource.UpdateRequest, re
return
}

apiDiags := state.fromAPIMCR(ctx, mcr)
apiDiags := plan.fromAPIMCR(ctx, mcr)
resp.Diagnostics.Append(apiDiags...)

// Update the state with the new values
state.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))

diags := resp.State.Set(ctx, state)
diags := resp.State.Set(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand Down
63 changes: 31 additions & 32 deletions internal/provider/mcr_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,46 @@ import (
func TestAccMegaportMCR_Basic(t *testing.T) {
mcrName := RandomTestName()
prefixFilterName := RandomTestName()
costCentreName := RandomTestName()
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: providerConfig + fmt.Sprintf(`
data "megaport_location" "bne_nxt1" {
name = "NextDC B1"
}
resource "megaport_mcr" "mcr" {
product_name = "%s"
port_speed = 1000
location_id = data.megaport_location.bne_nxt1.id
contract_term_months = 1
market = "AU"
marketplace_visibility = false
resource "megaport_mcr" "mcr" {
product_name = "%s"
port_speed = 1000
location_id = 5
contract_term_months = 12
cost_centre = "%s"
marketplace_visibility = true
prefix_filter_list = {
description = "%s"
address_family = "IPv4"
entries = [
{
action = "permit"
prefix = "10.0.1.0/24"
ge = 24
le = 24
},
{
action = "deny"
prefix = "10.0.2.0/24"
ge = 24
le = 24
}
]
}
}`, mcrName, prefixFilterName),
description = "%s"
address_family = "IPv4"
entries = [
{
action = "permit"
prefix = "10.0.1.0/24"
ge = 24
le = 24
},
{
action = "deny"
prefix = "10.0.2.0/24"
ge = 24
le = 24
}
]
}
}
`, mcrName, costCentreName, prefixFilterName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("megaport_mcr.mcr", "product_name", mcrName),
resource.TestCheckResourceAttr("megaport_mcr.mcr", "port_speed", "1000"),
resource.TestCheckResourceAttr("megaport_mcr.mcr", "contract_term_months", "1"),
resource.TestCheckResourceAttr("megaport_mcr.mcr", "market", "AU"),
resource.TestCheckResourceAttr("megaport_mcr.mcr", "marketplace_visibility", "false"),
resource.TestCheckResourceAttr("megaport_mcr.mcr", "contract_term_months", "12"),
resource.TestCheckResourceAttr("megaport_mcr.mcr", "marketplace_visibility", "true"),
resource.TestCheckResourceAttr("megaport_mcr.mcr", "cost_centre", costCentreName),
resource.TestCheckResourceAttrSet("megaport_mcr.mcr", "product_uid"),
resource.TestCheckResourceAttrSet("megaport_mcr.mcr", "product_id"),
resource.TestCheckResourceAttrSet("megaport_mcr.mcr", "provisioning_status"),
Expand Down
11 changes: 5 additions & 6 deletions internal/provider/mve_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,6 @@ func (r *mveResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *
"market": schema.StringAttribute{
Description: "The market the MVE is in.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"location_id": schema.Int64Attribute{
Description: "The location ID of the MVE.",
Expand Down Expand Up @@ -1015,14 +1012,16 @@ func (r *mveResource) Update(ctx context.Context, req resource.UpdateRequest, re
}

// Check on changes
var name types.String
var name string
if !plan.Name.Equal(state.Name) {
name = plan.Name
name = plan.Name.ValueString()
} else {
name = state.Name.ValueString()
}

_, err := r.client.MVEService.ModifyMVE(ctx, &megaport.ModifyMVERequest{
MVEID: state.UID.ValueString(),
Name: name.String(),
Name: name,
WaitForUpdate: true,
})

Expand Down
21 changes: 14 additions & 7 deletions internal/provider/single_port_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,7 @@ func (r *portResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
},
"market": schema.StringAttribute{
Description: "The market the product is in.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Computed: true,
},
"location_id": schema.Int64Attribute{
Description: "The numeric location ID of the product.",
Expand Down Expand Up @@ -551,9 +548,9 @@ func (r *portResource) Create(ctx context.Context, req resource.CreateRequest, r
Term: int(plan.ContractTermMonths.ValueInt64()),
PortSpeed: int(plan.PortSpeed.ValueInt64()),
LocationId: int(plan.LocationID.ValueInt64()),
Market: plan.Market.ValueString(),
MarketPlaceVisibility: plan.MarketplaceVisibility.ValueBool(),
DiversityZone: plan.DiversityZone.ValueString(),
CostCentre: plan.CostCentre.ValueString(),
WaitForProvision: true,
WaitForTime: 10 * time.Minute,
})
Expand Down Expand Up @@ -641,17 +638,27 @@ func (r *portResource) Update(ctx context.Context, req resource.UpdateRequest, r

// Check on changes
var name, costCentre string
var marketplaceVisibility bool
if !plan.Name.Equal(state.Name) {
name = plan.Name.ValueString()
} else {
name = state.Name.ValueString()
}
if !plan.CostCentre.Equal(state.CostCentre) {
costCentre = plan.Name.ValueString()
costCentre = plan.CostCentre.ValueString()
} else {
costCentre = state.CostCentre.ValueString()
}
if !plan.MarketplaceVisibility.Equal(state.MarketplaceVisibility) {
marketplaceVisibility = plan.MarketplaceVisibility.ValueBool()
} else {
marketplaceVisibility = state.MarketplaceVisibility.ValueBool()
}

r.client.PortService.ModifyPort(ctx, &megaport.ModifyPortRequest{
PortID: plan.UID.ValueString(),
Name: name,
MarketplaceVisibility: plan.MarketplaceVisibility.ValueBool(),
MarketplaceVisibility: marketplaceVisibility,
CostCentre: costCentre,
WaitForUpdate: true,
})
Expand Down
Loading

0 comments on commit 009ddd6

Please sign in to comment.