Skip to content

Commit

Permalink
d/azurerm_public_ips: Deprecate attached for attachment_status (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aristosvo authored Oct 5, 2021
1 parent 809466e commit 7bf999d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
32 changes: 30 additions & 2 deletions internal/services/network/public_ips_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,27 @@ func dataSourcePublicIPs() *pluginsdk.Resource {
Optional: true,
},

// TODO - Remove in 3.0.
"attached": {
Type: pluginsdk.TypeBool,
Type: pluginsdk.TypeBool,
Optional: true,
Deprecated: "This property has been deprecated in favour of `attachment_status` to improve filtering",
ConflictsWith: []string{
"attachment_status",
},
},

"attachment_status": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"Attached",
"Unattached",
"All", // TODO - Remove "All" in 3.0.
}, false),
ConflictsWith: []string{
"attached",
},
},

"allocation_type": {
Expand Down Expand Up @@ -99,8 +117,18 @@ func dataSourcePublicIPsRead(d *pluginsdk.ResourceData, meta interface{}) error
}
}

attachmentStatus, attachmentStatusOk := d.GetOk("attachment_status")
if attachmentStatusOk && attachmentStatus.(string) == "Attached" && !nicIsAttached {
continue
}
if attachmentStatusOk && attachmentStatus.(string) == "Unattached" && nicIsAttached {
continue
}

// Deprecated for `attachment_status`, remove in 3.0
// Removal will also change behaviour for data sources without `attachment_status` set
attachedOnly := d.Get("attached").(bool)
if attachedOnly != nicIsAttached {
if !attachmentStatusOk && attachedOnly != nicIsAttached {
continue
}

Expand Down
24 changes: 24 additions & 0 deletions internal/services/network/public_ips_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ func TestAccDataSourcePublicIPs_assigned(t *testing.T) {
r := PublicIPsResource{}

attachedDataSourceName := "data.azurerm_public_ips.attached"
attachedDataSourceName2 := "data.azurerm_public_ips.attached2"
unattachedDataSourceName := "data.azurerm_public_ips.unattached"
unattachedDataSourceName2 := "data.azurerm_public_ips.unattached2"
unattachedAndAttachedDataSourceName := "data.azurerm_public_ips.unattached_and_attached"

data.DataSourceTest(t, []acceptance.TestStep{
{
Expand All @@ -45,8 +48,14 @@ func TestAccDataSourcePublicIPs_assigned(t *testing.T) {
Check: acceptance.ComposeTestCheckFunc(
acceptance.TestCheckResourceAttr(attachedDataSourceName, "public_ips.#", "3"),
acceptance.TestCheckResourceAttr(attachedDataSourceName, "public_ips.0.name", fmt.Sprintf("acctestpip%s-0", data.RandomString)),
acceptance.TestCheckResourceAttr(attachedDataSourceName2, "public_ips.#", "3"),
acceptance.TestCheckResourceAttr(attachedDataSourceName2, "public_ips.0.name", fmt.Sprintf("acctestpip%s-0", data.RandomString)),
acceptance.TestCheckResourceAttr(unattachedDataSourceName, "public_ips.#", "4"),
acceptance.TestCheckResourceAttr(unattachedDataSourceName, "public_ips.0.name", fmt.Sprintf("acctestpip%s-3", data.RandomString)),
acceptance.TestCheckResourceAttr(unattachedDataSourceName2, "public_ips.#", "4"),
acceptance.TestCheckResourceAttr(unattachedDataSourceName2, "public_ips.0.name", fmt.Sprintf("acctestpip%s-3", data.RandomString)),
acceptance.TestCheckResourceAttr(unattachedAndAttachedDataSourceName, "public_ips.#", "7"),
acceptance.TestCheckResourceAttr(unattachedAndAttachedDataSourceName, "public_ips.0.name", fmt.Sprintf("acctestpip%s-0", data.RandomString)),
),
},
})
Expand Down Expand Up @@ -118,11 +127,26 @@ func (r PublicIPsResource) attachedDataSource(data acceptance.TestData) string {
%s
data "azurerm_public_ips" "unattached" {
resource_group_name = azurerm_resource_group.test.name
attachment_status = "Unattached"
}
data "azurerm_public_ips" "unattached2" {
resource_group_name = azurerm_resource_group.test.name
attached = false
}
data "azurerm_public_ips" "unattached_and_attached" {
resource_group_name = azurerm_resource_group.test.name
attachment_status = "All"
}
data "azurerm_public_ips" "attached" {
resource_group_name = azurerm_resource_group.test.name
attachment_status = "Attached"
}
data "azurerm_public_ips" "attached2" {
resource_group_name = azurerm_resource_group.test.name
attached = true
}
Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/public_ips.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ data "azurerm_public_ips" "example" {
## Argument Reference

* `resource_group_name` - Specifies the name of the resource group.
* `attached` - (Optional) Filter to include IP Addresses which are attached to a device, such as a VM/LB (`true`) or unattached (`false`).
* `attachment_status` - (Optional) Filter to include IP Addresses which are attached to a device, such as a VM/LB (`Attached`) or unattached (`Unattached`). To allow for both, use `All`.
* `name_prefix` - (Optional) A prefix match used for the IP Addresses `name` field, case sensitive.
* `allocation_type` - (Optional) The Allocation Type for the Public IP Address. Possible values include `Static` or `Dynamic`.

Expand Down

0 comments on commit 7bf999d

Please sign in to comment.