Skip to content

Commit

Permalink
fix resource and datasource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushrangwala committed Feb 23, 2024
1 parent f01d77b commit 1fd3477
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 100 deletions.
4 changes: 4 additions & 0 deletions internal/resources/metal/vlans/datasource_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
func dataSourceSchema() schema.Schema {
return schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "The unique identifier for this Metal Vlan",
Computed: true,
},
"project_id": schema.StringAttribute{
Description: "ID of parent project of the VLAN. Use together with vxlan and metro or facility",
Computed: true,
Expand Down
47 changes: 0 additions & 47 deletions internal/resources/metal/vlans/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,6 @@ import (
"github.com/packethost/packngo"
)

func TestAccDataSourceMetalVlan_byVxlanFacility(t *testing.T) {
rs := acctest.RandString(10)
fac := "sv15"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheckMetal(t) },
ExternalProviders: acceptance.TestExternalProviders,
ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories,
CheckDestroy: testAccMetalDatasourceVlanCheckDestroyed,
Steps: []resource.TestStep{
{
Config: testAccDataSourceMetalVlanConfig_byVxlanFacility(rs, fac, "tfacc-vlan"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(
"equinix_metal_vlan.foovlan", "vxlan",
"data.equinix_metal_vlan.dsvlan", "vxlan",
),
resource.TestCheckResourceAttrPair(
"equinix_metal_vlan.foovlan", "id",
"data.equinix_metal_vlan.dsvlan", "id",
),
),
},
},
})
}

func testAccDataSourceMetalVlanConfig_byVxlanFacility(projSuffix, fac, desc string) string {
return fmt.Sprintf(`
resource "equinix_metal_project" "foobar" {
name = "tfacc-vlan-%s"
}
resource "equinix_metal_vlan" "foovlan" {
project_id = equinix_metal_project.foobar.id
facility = "%s"
description = "%s"
}
data "equinix_metal_vlan" "dsvlan" {
facility = equinix_metal_vlan.foovlan.facility
project_id = equinix_metal_vlan.foovlan.project_id
vxlan = equinix_metal_vlan.foovlan.vxlan
}
`, projSuffix, fac, desc)
}

func TestAccDataSourceMetalVlan_byVxlanMetro(t *testing.T) {
rs := acctest.RandString(10)
metro := "sv"
Expand Down
24 changes: 16 additions & 8 deletions internal/resources/metal/vlans/models.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package vlans

import (
"context"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/packethost/packngo"
"strings"
)

type DataSourceModel struct {
ID types.String `tfsdk:"id"`
ProjectID types.String `tfsdk:"project_id"`
VlanID types.String `tfsdk:"vlan_id"`
Vxlan types.Int64 `tfsdk:"vxlan"`
Facility types.String `tfsdk:"facility"`
Metro types.String `tfsdk:"metro"`
Description types.String `tfsdk:"description"`
ID types.String `tfsdk:"id"`
ProjectID types.String `tfsdk:"project_id"`
VlanID types.String `tfsdk:"vlan_id"`
Vxlan types.Int64 `tfsdk:"vxlan"`
Facility types.String `tfsdk:"facility"`
Metro types.String `tfsdk:"metro"`
Description types.String `tfsdk:"description"`
AssignedDevicesIds types.List `tfsdk:"assigned_devices_ids"`
}

func (m *DataSourceModel) parse(vlan *packngo.VirtualNetwork) diag.Diagnostics {
Expand All @@ -40,7 +42,13 @@ func (m *DataSourceModel) parse(vlan *packngo.VirtualNetwork) diag.Diagnostics {
if !strings.EqualFold(m.Metro.ValueString(), vlan.MetroCode) {
m.Metro = types.StringValue(vlan.MetroCode)
}
return nil

deviceIds := make([]types.String, 0, len(vlan.Instances))
for _, device := range vlan.Instances {
deviceIds = append(deviceIds, types.StringValue(device.ID))
}

return m.AssignedDevicesIds.ElementsAs(context.Background(), &deviceIds, false)
}

type ResourceModel struct {
Expand Down
8 changes: 4 additions & 4 deletions internal/resources/metal/vlans/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ func (r *Resource) Create(ctx context.Context, request resource.CreateRequest, r
}

if data.Facility.IsNull() && data.Metro.IsNull() {
response.Diagnostics.AddError("Invalid inout params",
response.Diagnostics.AddError("Invalid input params",
equinix_errors.FriendlyError(errors.New("one of facility or metro must be configured")).Error())
return
}
if !(data.Facility.IsNull() && data.Vxlan.IsNull()) {
response.Diagnostics.AddError("Invalid inout params",
if !data.Facility.IsNull() && !data.Vxlan.IsNull() {
response.Diagnostics.AddError("Invalid input params",
equinix_errors.FriendlyError(errors.New("you can set vxlan only for metro vlans")).Error())
return
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func (r *Resource) Delete(ctx context.Context, request resource.DeleteRequest, r
}
}

if equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(client.ProjectVirtualNetworks.Delete(vlan.ID)) != nil {
if err := equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(client.ProjectVirtualNetworks.Delete(vlan.ID)); err != nil {
response.Diagnostics.AddError("Error deleting Vlan",
equinix_errors.FriendlyError(err).Error())
return
Expand Down
45 changes: 4 additions & 41 deletions internal/resources/metal/vlans/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ resource "equinix_metal_vlan" "foovlan" {
}

func TestAccMetalVlan_metro(t *testing.T) {
var vlan packngo.VirtualNetwork
rs := acctest.RandString(10)
metro := "sv"

Expand All @@ -90,36 +91,12 @@ func TestAccMetalVlan_metro(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccCheckMetalVlanConfig_metro(rs, metro, "tfacc-vlan"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"equinix_metal_vlan.foovlan", "metro", metro),
resource.TestCheckResourceAttr(
"equinix_metal_vlan.foovlan", "facility", ""),
),
},
},
})
}

func TestAccMetalVlan_basic(t *testing.T) {
var vlan packngo.VirtualNetwork
rs := acctest.RandString(10)
fac := "ny5"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheckMetal(t) },
ExternalProviders: acceptance.TestExternalProviders,
ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories,
CheckDestroy: testAccMetalVlanCheckDestroyed,
Steps: []resource.TestStep{
{
Config: testAccMetalVlanConfig_var(rs, fac, "tfacc-vlan"),
Check: resource.ComposeTestCheckFunc(
testAccCheckMetalVlanExists("equinix_metal_vlan.foovlan", &vlan),
resource.TestCheckResourceAttr(
"equinix_metal_vlan.foovlan", "description", "tfacc-vlan"),
resource.TestCheckResourceAttr(
"equinix_metal_vlan.foovlan", "facility", fac),
"equinix_metal_vlan.foovlan", "metro", metro),
),
},
},
Expand Down Expand Up @@ -167,23 +144,9 @@ func testAccMetalVlanCheckDestroyed(s *terraform.State) error {
return nil
}

func testAccMetalVlanConfig_var(projSuffix, facility, desc string) string {
return fmt.Sprintf(`
resource "equinix_metal_project" "foobar" {
name = "tfacc-vlan-%s"
}
resource "equinix_metal_vlan" "foovlan" {
project_id = "${equinix_metal_project.foobar.id}"
facility = "%s"
description = "%s"
}
`, projSuffix, facility, desc)
}

func TestAccMetalVlan_importBasic(t *testing.T) {
rs := acctest.RandString(10)
fac := "ny5"
metro := "sv"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheckMetal(t) },
Expand All @@ -192,7 +155,7 @@ func TestAccMetalVlan_importBasic(t *testing.T) {
CheckDestroy: testAccMetalVlanCheckDestroyed,
Steps: []resource.TestStep{
{
Config: testAccMetalVlanConfig_var(rs, fac, "tfacc-vlan"),
Config: testAccCheckMetalVlanConfig_metro(rs, metro, "tfacc-vlan"),
},
{
ResourceName: "equinix_metal_vlan.foovlan",
Expand Down

0 comments on commit 1fd3477

Please sign in to comment.