From b4bd8b8f0736a367417f6e3bccb2073af00ee734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Fern=C3=A1ndez?= Date: Tue, 5 Nov 2024 20:38:29 -0300 Subject: [PATCH 1/3] Adding zones property to Bastion Host resource and data source. Updating Basic Test to include zones. Updated docs. --- .../services/network/bastion_host_data_source.go | 4 ++++ .../network/bastion_host_data_source_test.go | 1 + internal/services/network/bastion_host_resource.go | 14 ++++++++++++++ .../services/network/bastion_host_resource_test.go | 3 +++ website/docs/d/bastion_host.html.markdown | 2 ++ website/docs/r/bastion_host.html.markdown | 2 ++ 6 files changed, 26 insertions(+) diff --git a/internal/services/network/bastion_host_data_source.go b/internal/services/network/bastion_host_data_source.go index 927f4aa14877..096bb378cd36 100644 --- a/internal/services/network/bastion_host_data_source.go +++ b/internal/services/network/bastion_host_data_source.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/go-azure-helpers/lang/response" "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" "github.com/hashicorp/go-azure-helpers/resourcemanager/tags" + "github.com/hashicorp/go-azure-helpers/resourcemanager/zones" "github.com/hashicorp/go-azure-sdk/resource-manager/network/2024-01-01/bastionhosts" "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" @@ -104,6 +105,8 @@ func dataSourceBastionHost() *pluginsdk.Resource { "resource_group_name": commonschema.ResourceGroupNameForDataSource(), "tags": commonschema.TagsDataSource(), + + "zones": commonschema.ZonesMultipleComputed(), }, } } @@ -133,6 +136,7 @@ func dataSourceBastionHostRead(d *pluginsdk.ResourceData, meta interface{}) erro skuName = string(*sku.Name) } d.Set("sku", skuName) + d.Set("zones", zones.FlattenUntyped(model.Zones)) if props := model.Properties; props != nil { d.Set("dns_name", props.DnsName) diff --git a/internal/services/network/bastion_host_data_source_test.go b/internal/services/network/bastion_host_data_source_test.go index c7ab367c939a..9f53975d2225 100644 --- a/internal/services/network/bastion_host_data_source_test.go +++ b/internal/services/network/bastion_host_data_source_test.go @@ -33,6 +33,7 @@ func TestAccBastionHostDataSource_basic(t *testing.T) { check.That(data.ResourceName).Key("ip_configuration.0.name").Exists(), check.That(data.ResourceName).Key("ip_configuration.0.subnet_id").Exists(), check.That(data.ResourceName).Key("ip_configuration.0.public_ip_address_id").Exists(), + check.That(data.ResourceName).Key("zones.#").HasValue("3"), ), }, }) diff --git a/internal/services/network/bastion_host_resource.go b/internal/services/network/bastion_host_resource.go index f4736ac10ea4..e53597785990 100644 --- a/internal/services/network/bastion_host_resource.go +++ b/internal/services/network/bastion_host_resource.go @@ -15,7 +15,9 @@ import ( "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" "github.com/hashicorp/go-azure-helpers/resourcemanager/location" "github.com/hashicorp/go-azure-helpers/resourcemanager/tags" + "github.com/hashicorp/go-azure-helpers/resourcemanager/zones" "github.com/hashicorp/go-azure-sdk/resource-manager/network/2024-01-01/bastionhosts" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate" @@ -160,6 +162,8 @@ func resourceBastionHost() *pluginsdk.Resource { }, "tags": commonschema.Tags(), + + "zones": commonschema.ZonesMultipleOptionalForceNew(), }, CustomizeDiff: pluginsdk.CustomDiffWithAll( @@ -272,6 +276,11 @@ func resourceBastionHostCreate(d *pluginsdk.ResourceData, meta interface{}) erro parameters.Properties.EnableSessionRecording = pointer.To(sessionRecordingEnabled) } + zones := zones.ExpandUntyped(d.Get("zones").(*schema.Set).List()) + if len(zones) > 0 { + parameters.Zones = pointer.To(zones) + } + if v, ok := d.GetOk("virtual_network_id"); ok { if sku != bastionhosts.BastionHostSkuNameDeveloper { return fmt.Errorf("`virtual_network_id` is only supported when `sku` is `Developer`") @@ -379,7 +388,10 @@ func resourceBastionHostUpdate(d *pluginsdk.ResourceData, meta interface{}) erro if d.HasChange("tags") { payload.Tags = tags.Expand(d.Get("tags").(map[string]interface{})) + } + if d.HasChange("zones") { + payload.Zones = pointer.To(zones.ExpandUntyped(d.Get("zones").(*schema.Set).List())) } if err := client.CreateOrUpdateThenPoll(ctx, *id, *payload); err != nil { @@ -421,6 +433,8 @@ func resourceBastionHostRead(d *pluginsdk.ResourceData, meta interface{}) error d.Set("sku", string(*sku.Name)) } + d.Set("zones", zones.FlattenUntyped(model.Zones)) + if props := model.Properties; props != nil { d.Set("dns_name", props.DnsName) d.Set("scale_units", props.ScaleUnits) diff --git a/internal/services/network/bastion_host_resource_test.go b/internal/services/network/bastion_host_resource_test.go index f0adc58f988f..cb958084387d 100644 --- a/internal/services/network/bastion_host_resource_test.go +++ b/internal/services/network/bastion_host_resource_test.go @@ -27,6 +27,7 @@ func TestAccBastionHost_basic(t *testing.T) { Config: r.basic(data), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("zones.#").HasValue("3"), ), }, }) @@ -198,12 +199,14 @@ resource "azurerm_public_ip" "test" { resource_group_name = azurerm_resource_group.test.name allocation_method = "Static" sku = "Standard" + zones = ["1", "2", "3"] } resource "azurerm_bastion_host" "test" { name = "acctestBastion%s" location = azurerm_resource_group.test.location resource_group_name = azurerm_resource_group.test.name + zones = azurerm_public_ip.test.zones ip_configuration { name = "ip-configuration" diff --git a/website/docs/d/bastion_host.html.markdown b/website/docs/d/bastion_host.html.markdown index 691be603e5a5..3b95db3445da 100644 --- a/website/docs/d/bastion_host.html.markdown +++ b/website/docs/d/bastion_host.html.markdown @@ -62,6 +62,8 @@ In addition to the Arguments listed above - the following Attributes are exporte * `tags` - A mapping of tags assigned to the Bastion Host. +* `zones` - A list of Availability Zones in which this Bastion Host is located. + --- A `ip_configuration` block supports the following: diff --git a/website/docs/r/bastion_host.html.markdown b/website/docs/r/bastion_host.html.markdown index 45e59ee889aa..d4075d357bcf 100644 --- a/website/docs/r/bastion_host.html.markdown +++ b/website/docs/r/bastion_host.html.markdown @@ -106,6 +106,8 @@ The following arguments are supported: * `tags` - (Optional) A mapping of tags to assign to the resource. +* `zones` - (Optional) Specifies a list of Availability Zones in which this Public Bastion Host should be located. Changing this forces a new resource to be created. + --- A `ip_configuration` block supports the following: From 7f1c47b874469cd1a1aeeee0245ff5af723ecb3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Fern=C3=A1ndez?= Date: Wed, 6 Nov 2024 09:35:01 -0300 Subject: [PATCH 2/3] Updated test. --- .../services/network/bastion_host_data_source_test.go | 11 +++++++---- .../services/network/bastion_host_resource_test.go | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/services/network/bastion_host_data_source_test.go b/internal/services/network/bastion_host_data_source_test.go index 9f53975d2225..3e9159d67aef 100644 --- a/internal/services/network/bastion_host_data_source_test.go +++ b/internal/services/network/bastion_host_data_source_test.go @@ -13,14 +13,15 @@ import ( type BastionHostDataSource struct{} -func TestAccBastionHostDataSource_basic(t *testing.T) { +func TestAccBastionHostDataSource_complete(t *testing.T) { data := acceptance.BuildTestData(t, "data.azurerm_bastion_host", "test") r := BastionHostDataSource{} data.DataSourceTest(t, []acceptance.TestStep{ { - Config: r.basic(data), + Config: r.complete(data), Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("copy_paste_enabled").Exists(), check.That(data.ResourceName).Key("id").Exists(), check.That(data.ResourceName).Key("location").Exists(), check.That(data.ResourceName).Key("sku").Exists(), @@ -33,13 +34,15 @@ func TestAccBastionHostDataSource_basic(t *testing.T) { check.That(data.ResourceName).Key("ip_configuration.0.name").Exists(), check.That(data.ResourceName).Key("ip_configuration.0.subnet_id").Exists(), check.That(data.ResourceName).Key("ip_configuration.0.public_ip_address_id").Exists(), + check.That(data.ResourceName).Key("tags.%").HasValue("1"), + check.That(data.ResourceName).Key("tags.environment").HasValue("production"), check.That(data.ResourceName).Key("zones.#").HasValue("3"), ), }, }) } -func (BastionHostDataSource) basic(data acceptance.TestData) string { +func (BastionHostDataSource) complete(data acceptance.TestData) string { return fmt.Sprintf(` %s @@ -47,5 +50,5 @@ data "azurerm_bastion_host" "test" { name = azurerm_bastion_host.test.name resource_group_name = azurerm_bastion_host.test.resource_group_name } -`, BastionHostResource{}.basic(data)) +`, BastionHostResource{}.complete(data)) } diff --git a/internal/services/network/bastion_host_resource_test.go b/internal/services/network/bastion_host_resource_test.go index cb958084387d..93285a92d50c 100644 --- a/internal/services/network/bastion_host_resource_test.go +++ b/internal/services/network/bastion_host_resource_test.go @@ -199,14 +199,12 @@ resource "azurerm_public_ip" "test" { resource_group_name = azurerm_resource_group.test.name allocation_method = "Static" sku = "Standard" - zones = ["1", "2", "3"] } resource "azurerm_bastion_host" "test" { name = "acctestBastion%s" location = azurerm_resource_group.test.location resource_group_name = azurerm_resource_group.test.name - zones = azurerm_public_ip.test.zones ip_configuration { name = "ip-configuration" @@ -301,6 +299,7 @@ resource "azurerm_public_ip" "test" { resource_group_name = azurerm_resource_group.test.name allocation_method = "Static" sku = "Standard" + zones = ["1", "2", "3"] } resource "azurerm_bastion_host" "test" { @@ -308,6 +307,7 @@ resource "azurerm_bastion_host" "test" { location = azurerm_resource_group.test.location resource_group_name = azurerm_resource_group.test.name copy_paste_enabled = false + zones = azurerm_public_ip.test.zones ip_configuration { name = "ip-configuration" From 305966356b7794899f06d91cbffb68fd233832f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Fern=C3=A1ndez?= Date: Thu, 7 Nov 2024 14:10:04 -0300 Subject: [PATCH 3/3] Moving check from Basic to Complete. --- internal/services/network/bastion_host_resource_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/network/bastion_host_resource_test.go b/internal/services/network/bastion_host_resource_test.go index 93285a92d50c..a67d89dd00f6 100644 --- a/internal/services/network/bastion_host_resource_test.go +++ b/internal/services/network/bastion_host_resource_test.go @@ -27,7 +27,6 @@ func TestAccBastionHost_basic(t *testing.T) { Config: r.basic(data), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), - check.That(data.ResourceName).Key("zones.#").HasValue("3"), ), }, }) @@ -58,6 +57,7 @@ func TestAccBastionHost_complete(t *testing.T) { check.That(data.ResourceName).ExistsInAzure(r), check.That(data.ResourceName).Key("tags.%").HasValue("1"), check.That(data.ResourceName).Key("tags.environment").HasValue("production"), + check.That(data.ResourceName).Key("zones.#").HasValue("3"), ), }, data.ImportStep(),