From 1fd3477f6f89ce3cac8eb8f088cdb831eeeca0f2 Mon Sep 17 00:00:00 2001 From: Ayush Rangwala Date: Sat, 24 Feb 2024 01:44:27 +0530 Subject: [PATCH] fix resource and datasource tests --- .../metal/vlans/datasource_schema.go | 4 ++ .../resources/metal/vlans/datasource_test.go | 47 ------------------- internal/resources/metal/vlans/models.go | 24 ++++++---- internal/resources/metal/vlans/resource.go | 8 ++-- .../resources/metal/vlans/resource_test.go | 45 ++---------------- 5 files changed, 28 insertions(+), 100 deletions(-) diff --git a/internal/resources/metal/vlans/datasource_schema.go b/internal/resources/metal/vlans/datasource_schema.go index d66024307..32498e840 100644 --- a/internal/resources/metal/vlans/datasource_schema.go +++ b/internal/resources/metal/vlans/datasource_schema.go @@ -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, diff --git a/internal/resources/metal/vlans/datasource_test.go b/internal/resources/metal/vlans/datasource_test.go index 98a7a7ef0..06ffa8909 100644 --- a/internal/resources/metal/vlans/datasource_test.go +++ b/internal/resources/metal/vlans/datasource_test.go @@ -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" diff --git a/internal/resources/metal/vlans/models.go b/internal/resources/metal/vlans/models.go index 197744d30..5c8077e82 100644 --- a/internal/resources/metal/vlans/models.go +++ b/internal/resources/metal/vlans/models.go @@ -1,6 +1,7 @@ package vlans import ( + "context" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/packethost/packngo" @@ -8,13 +9,14 @@ import ( ) 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 { @@ -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 { diff --git a/internal/resources/metal/vlans/resource.go b/internal/resources/metal/vlans/resource.go index 2da47b619..590bf1a8f 100644 --- a/internal/resources/metal/vlans/resource.go +++ b/internal/resources/metal/vlans/resource.go @@ -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 } @@ -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 diff --git a/internal/resources/metal/vlans/resource_test.go b/internal/resources/metal/vlans/resource_test.go index 35144ac17..8aa289064 100644 --- a/internal/resources/metal/vlans/resource_test.go +++ b/internal/resources/metal/vlans/resource_test.go @@ -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" @@ -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), ), }, }, @@ -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) }, @@ -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",