Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update metal ip resources to retrieve metro from facility if metro is null #265

Merged
merged 5 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/data-sources/equinix_metal_ip_block_ranges.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ output "out" {
The following arguments are supported:

* `project_id` - (Required) ID of the project from which to list the blocks.
* `facility` - (Optional) Facility code filtering the IP blocks. Global IPv4 blcoks will be listed
* `facility` - (Optional) Facility code filtering the IP blocks. Global IPv4 blocks will be listed
anyway. If you omit this and metro, all the block from the project will be listed.
* `metro` - (Optional) Metro code filtering the IP blocks. Global IPv4 blcoks will be listed
* `metro` - (Optional) Metro code filtering the IP blocks. Global IPv4 blocks will be listed
anyway. If you omit this and facility, all the block from the project will be listed.

## Attributes Reference
Expand Down
8 changes: 7 additions & 1 deletion docs/data-sources/equinix_metal_precreated_ip_block.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ subcategory: "Metal"

# equinix_metal_precreated_ip_block (Data Source)

Use this data source to get CIDR expression for precreated IPv6 and IPv4 blocks in Equinix Metal.
Use this data source to get CIDR expression for precreated (management) IPv6 and IPv4 blocks in Equinix Metal.
You can then use the cidrsubnet TF builtin function to derive subnets.

~> For backward compatibility, this data source will also return reserved (elastic) IP blocks.

-> Precreated (management) IP blocks for a metro will not be available until first device is created in that metro.

-> Public IPv4 blocks auto-assigned (management) to a device cannot be retrieved. If you need that information, consider using the [equinix_metal_device](equinix_metal_device.md) data source instead.

## Example Usage

```hcl
Expand Down
2 changes: 2 additions & 0 deletions docs/data-sources/equinix_metal_reserved_ip_block.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ subcategory: "Metal"
Use this data source to find IP address blocks in Equinix Metal. You can use IP address or a block
ID for lookup.

~> For backward compatibility, this data source can be also used for precreated (management) IP blocks.

~> VRF features are not generally available. The interfaces related to VRF resources may change ahead of general availability.

## Example Usage
Expand Down
21 changes: 18 additions & 3 deletions equinix/data_source_metal_ip_block_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func dataSourceMetalIPBlockRanges() *schema.Resource {
},
"facility": {
Type: schema.TypeString,
Description: "Facility code filtering the IP blocks. Global IPv4 blcoks will be listed anyway. If you omit this and metro, all the block from the project will be listed",
Description: "Facility code filtering the IP blocks. Global IPv4 blocks will be listed anyway. If you omit this and metro, all the block from the project will be listed",
Optional: true,
},
"metro": {
Type: schema.TypeString,
Description: "Metro code filtering the IP blocks. Global IPv4 blcoks will be listed anyway. If you omit this and facility, all the block from the project will be listed",
Description: "Metro code filtering the IP blocks. Global IPv4 blocks will be listed anyway. If you omit this and facility, all the block from the project will be listed",
Optional: true,
StateFunc: toLower,
},
Expand Down Expand Up @@ -76,6 +76,16 @@ func metroMatch(ref string, metro *packngo.Metro) bool {
return false
}

func metroOffacilityMatch(ref string, facility *packngo.Facility) bool {
if ref == "" {
return true
}
if facility != nil && facility.Metro != nil && ref == facility.Metro.Code {
return true
}
return false
}

func dataSourceMetalIPBlockRangesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal
projectID := d.Get("project_id").(string)
Expand Down Expand Up @@ -109,7 +119,12 @@ func dataSourceMetalIPBlockRangesRead(d *schema.ResourceData, meta interface{})
} else {
targetSlice = &theIPv6s
}
if targetSlice != nil && facilityMatch(facility, ip.Facility) && metroMatch(metro, ip.Metro) {
if targetSlice != nil {
if !(facilityMatch(facility, ip.Facility) && metroMatch(metro, ip.Metro)) {
if !metroOffacilityMatch(metro, ip.Facility) {
continue
}
}
*targetSlice = append(*targetSlice, cnStr)
}
}
Expand Down
19 changes: 11 additions & 8 deletions equinix/data_source_metal_precreated_ip_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/packethost/packngo"
)

Expand All @@ -26,25 +27,23 @@ func dataSourceMetalPreCreatedIPBlock() *schema.Resource {
Required: true,
Description: "Whether to look for public or private block.",
}

s["facility"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Facility of the searched block. (for non-global blocks).",
ConflictsWith: []string{"metro"},
}

s["metro"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Metro of the searched block (for non-global blocks).",
ConflictsWith: []string{"facility"},
}

s["address_family"] = &schema.Schema{
Type: schema.TypeInt,
Required: true,
Description: "4 or 6, depending on which block you are looking for.",
Type: schema.TypeInt,
Required: true,
Description: "4 or 6, depending on which block you are looking for.",
ValidateFunc: validation.IntInSlice([]int{4, 6}),
}
s["cidr_notation"] = &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -127,10 +126,14 @@ func dataSourceMetalPreCreatedIPBlockRead(d *schema.ResourceData, meta interface
// lookup of block specified with metro
metro := mval.(string)
for _, ip := range ips {
ipMetro := ip.Metro
if ip.Metro == nil {
continue
if ip.Facility.Metro == nil {
continue
}
ipMetro = ip.Facility.Metro
}
if ip.Public == public && ip.AddressFamily == ipv && metro == ip.Metro.Code {
if ip.Public == public && ip.AddressFamily == ipv && metro == ipMetro.Code {
return loadBlock(d, &ip)
}
}
Expand Down
9 changes: 0 additions & 9 deletions equinix/data_source_metal_precreated_ip_block_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ func TestAccDataSourceMetalPreCreatedIPBlock_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.equinix_metal_precreated_ip_block.test_fac_pubv6", "cidr_notation"),
resource.TestCheckResourceAttrSet(
"data.equinix_metal_precreated_ip_block.test_metro_pubv4", "cidr_notation"),
resource.TestCheckResourceAttrSet(
"data.equinix_metal_precreated_ip_block.test_metro_priv4", "cidr_notation"),
resource.TestCheckResourceAttrPair(
Expand Down Expand Up @@ -70,13 +68,6 @@ data "equinix_metal_precreated_ip_block" "test_fac_pubv6" {
public = true
}

data "equinix_metal_precreated_ip_block" "test_metro_pubv4" {
metro = equinix_metal_device.test.metro
project_id = equinix_metal_device.test.project_id
address_family = 4
public = true
}

data "equinix_metal_precreated_ip_block" "test_metro_priv4" {
metro = equinix_metal_device.test.metro
project_id = equinix_metal_device.test.project_id
Expand Down
2 changes: 2 additions & 0 deletions equinix/data_source_metal_reserved_ip_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ func dataSourceMetalReservedIPBlock() *schema.Resource {
Computed: true,
Description: "ID of the project where the searched block should be",
ConflictsWith: []string{"id"},
RequiredWith: []string{"ip_address"},
},
"ip_address": {
Type: schema.TypeString,
Optional: true,
Description: "Find block containing this IP address in given project",
ConflictsWith: []string{"id"},
RequiredWith: []string{"project_id"},
},

"global": {
Expand Down
3 changes: 3 additions & 0 deletions equinix/resource_metal_reserved_ip_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ func loadBlock(d *schema.ResourceData, reservedBlock *packngo.IPAddressReservati
},
"metro": func(d *schema.ResourceData, k string) error {
if reservedBlock.Metro == nil {
if reservedBlock.Facility != nil && reservedBlock.Facility.Metro != nil {
return d.Set(k, strings.ToLower(reservedBlock.Facility.Metro.Code))
}
return nil
}
return d.Set(k, strings.ToLower(reservedBlock.Metro.Code))
Expand Down