Skip to content

Commit

Permalink
fix: restore the metal_project datasource name filter (#554)
Browse files Browse the repository at this point in the history
Closes #549

When the Metal project data source was migrated to `equinix-sdk-go`,
finding a project by name was broken in some cases. To ensure that the
data source can always find the named project (assuming it really
exists), we have to use the `ExecuteWithPagination()` function to load
all pages of projects instead of `Execute()` which only loads one page.

As a minor optimization, this uses the `Name()` filter to reduce the
number of results that are returned by the API.

I've also taken the opportunity to move the Metal project resource and
data source to a separate package.
  • Loading branch information
ctreatma authored Feb 7, 2024
1 parent 2c09c52 commit 4f54401
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 97 deletions.
61 changes: 0 additions & 61 deletions equinix/data_source_metal_project_acc_test.go

This file was deleted.

5 changes: 3 additions & 2 deletions equinix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/equinix/terraform-provider-equinix/internal/resources/metal/metal_connection"
metal_project "github.com/equinix/terraform-provider-equinix/internal/resources/metal/project"

"github.com/equinix/ecx-go/v2"
"github.com/equinix/terraform-provider-equinix/internal/config"
Expand Down Expand Up @@ -107,7 +108,7 @@ func Provider() *schema.Provider {
"equinix_metal_device_bgp_neighbors": dataSourceMetalDeviceBGPNeighbors(),
"equinix_metal_plans": dataSourceMetalPlans(),
"equinix_metal_port": dataSourceMetalPort(),
"equinix_metal_project": dataSourceMetalProject(),
"equinix_metal_project": metal_project.DataSource(),
"equinix_metal_reserved_ip_block": dataSourceMetalReservedIPBlock(),
"equinix_metal_spot_market_request": dataSourceMetalSpotMarketRequest(),
"equinix_metal_virtual_circuit": dataSourceMetalVirtualCircuit(),
Expand Down Expand Up @@ -136,7 +137,7 @@ func Provider() *schema.Provider {
"equinix_metal_device_network_type": resourceMetalDeviceNetworkType(),
"equinix_metal_organization_member": resourceMetalOrganizationMember(),
"equinix_metal_port": resourceMetalPort(),
"equinix_metal_project": resourceMetalProject(),
"equinix_metal_project": metal_project.Resource(),
"equinix_metal_organization": resourceMetalOrganization(),
"equinix_metal_reserved_ip_block": resourceMetalReservedIPBlock(),
"equinix_metal_ip_attachment": resourceMetalIPAttachment(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package equinix
package project

import (
"context"
Expand All @@ -14,7 +14,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceMetalProject() *schema.Resource {
func DataSource() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceMetalProjectRead,
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -121,12 +121,12 @@ func dataSourceMetalProjectRead(ctx context.Context, d *schema.ResourceData, met
if nameOK {
name := nameRaw.(string)

os, _, err := client.ProjectsApi.FindProjects(ctx).Execute()
projects, err := client.ProjectsApi.FindProjects(ctx).Name(name).ExecuteWithPagination()
if err != nil {
return diag.FromErr(err)
}

project, err = findProjectByName(os, name)
project, err = findProjectByName(projects, name)
if err != nil {
return diag.FromErr(err)
}
Expand Down
113 changes: 113 additions & 0 deletions internal/resources/metal/project/datasource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package project_test

import (
"fmt"
"testing"

"github.com/equinix/terraform-provider-equinix/internal/acceptance"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/packethost/packngo"
)

func TestAccDataSourceMetalProject_byId(t *testing.T) {
var project packngo.Project
rn := acctest.RandStringFromCharSet(12, "abcdef0123456789")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ExternalProviders: acceptance.TestExternalProviders,
Providers: acceptance.TestAccProviders,
CheckDestroy: testAccMetalProjectCheckDestroyed,
Steps: []resource.TestStep{
{
Config: testAccDataSourceMetalProject_byId(rn),
Check: resource.ComposeTestCheckFunc(
testAccMetalProjectExists("equinix_metal_project.foobar", &project),
resource.TestCheckResourceAttr(
"equinix_metal_project.foobar", "name", fmt.Sprintf("tfacc-project-%s", rn)),
resource.TestCheckResourceAttr(
"equinix_metal_project.foobar", "bgp_config.0.md5",
"2SFsdfsg43"),
resource.TestCheckResourceAttrPair(
"equinix_metal_project.foobar", "id",
"data.equinix_metal_project.test", "id"),
),
},
},
})
}

func testAccDataSourceMetalProject_byId(r string) string {
return fmt.Sprintf(`
terraform {
provider_meta "equinix" {
module_name = "test"
}
}
resource "equinix_metal_project" "foobar" {
name = "tfacc-project-%s"
bgp_config {
deployment_type = "local"
md5 = "2SFsdfsg43"
asn = 65000
}
}
data equinix_metal_project "test" {
project_id = equinix_metal_project.foobar.id
}
`, r)
}

func TestAccDataSourceMetalProject_byName(t *testing.T) {
var project packngo.Project
rn := acctest.RandStringFromCharSet(12, "abcdef0123456789")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ExternalProviders: acceptance.TestExternalProviders,
Providers: acceptance.TestAccProviders,
CheckDestroy: testAccMetalProjectCheckDestroyed,
Steps: []resource.TestStep{
{
Config: testAccDataSourceMetalProject_byName(rn),
Check: resource.ComposeTestCheckFunc(
testAccMetalProjectExists("equinix_metal_project.foobar", &project),
resource.TestCheckResourceAttr(
"equinix_metal_project.foobar", "name", fmt.Sprintf("tfacc-project-%s", rn)),
resource.TestCheckResourceAttr(
"equinix_metal_project.foobar", "bgp_config.0.md5",
"2SFsdfsg43"),
resource.TestCheckResourceAttrPair(
"equinix_metal_project.foobar", "id",
"data.equinix_metal_project.test", "id"),
),
},
},
})
}

func testAccDataSourceMetalProject_byName(r string) string {
return fmt.Sprintf(`
terraform {
provider_meta "equinix" {
module_name = "test"
}
}
resource "equinix_metal_project" "foobar" {
name = "tfacc-project-%s"
bgp_config {
deployment_type = "local"
md5 = "2SFsdfsg43"
asn = 65000
}
}
data equinix_metal_project "test" {
name = equinix_metal_project.foobar.name
}
`, r)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package equinix
package project

import (
"context"
Expand All @@ -17,7 +17,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func resourceMetalProject() *schema.Resource {
func Resource() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceMetalProjectCreate,
ReadWithoutTimeout: resourceMetalProjectRead,
Expand Down
Loading

0 comments on commit 4f54401

Please sign in to comment.