Skip to content

Commit

Permalink
azurerm_bastion_host: Adding zones property to resource and data so…
Browse files Browse the repository at this point in the history
…urce. (#27909)

* Adding zones property to Bastion Host resource and data source.
Updating Basic Test to include zones.
Updated docs.

* Updated test.

* Moving check from Basic to Complete.
  • Loading branch information
CorrenSoft authored Nov 8, 2024
1 parent cef24df commit e85a546
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
4 changes: 4 additions & 0 deletions internal/services/network/bastion_host_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -104,6 +105,8 @@ func dataSourceBastionHost() *pluginsdk.Resource {
"resource_group_name": commonschema.ResourceGroupNameForDataSource(),

"tags": commonschema.TagsDataSource(),

"zones": commonschema.ZonesMultipleComputed(),
},
}
}
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions internal/services/network/bastion_host_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -33,18 +34,21 @@ 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
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))
}
14 changes: 14 additions & 0 deletions internal/services/network/bastion_host_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -160,6 +162,8 @@ func resourceBastionHost() *pluginsdk.Resource {
},

"tags": commonschema.Tags(),

"zones": commonschema.ZonesMultipleOptionalForceNew(),
},

CustomizeDiff: pluginsdk.CustomDiffWithAll(
Expand Down Expand Up @@ -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`")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions internal/services/network/bastion_host_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,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(),
Expand Down Expand Up @@ -298,13 +299,15 @@ 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
copy_paste_enabled = false
zones = azurerm_public_ip.test.zones
ip_configuration {
name = "ip-configuration"
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/bastion_host.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/bastion_host.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit e85a546

Please sign in to comment.