Skip to content

Commit

Permalink
New Data Source : azurerm_bastion_host (#20062)
Browse files Browse the repository at this point in the history
  • Loading branch information
pchanvallon authored Jan 17, 2023
1 parent 5ecee83 commit db2860e
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 0 deletions.
150 changes: 150 additions & 0 deletions internal/services/network/bastion_host_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package network

import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceBastionHost() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceBastionHostRead,

Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.BastionHostName,
},

"copy_paste_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"file_copy_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"ip_configuration": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"subnet_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"public_ip_address_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},

"ip_connect_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"scale_units": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"shareable_link_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"sku": {
Type: pluginsdk.TypeString,
Computed: true,
},

"tunneling_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"dns_name": {
Type: pluginsdk.TypeString,
Computed: true,
},

"location": commonschema.LocationComputed(),

"resource_group_name": commonschema.ResourceGroupNameForDataSource(),

"tags": commonschema.TagsDataSource(),
},
}
}

func dataSourceBastionHostRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.BastionHostsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewBastionHostID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}

if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

if sku := resp.Sku; sku != nil {
d.Set("sku", sku.Name)
}

if props := resp.BastionHostPropertiesFormat; props != nil {
d.Set("dns_name", props.DNSName)
d.Set("scale_units", props.ScaleUnits)
d.Set("file_copy_enabled", props.EnableFileCopy)
d.Set("ip_connect_enabled", props.EnableIPConnect)
d.Set("shareable_link_enabled", props.EnableShareableLink)
d.Set("tunneling_enabled", props.EnableTunneling)

copyPasteEnabled := true
if props.DisableCopyPaste != nil {
copyPasteEnabled = !*props.DisableCopyPaste
}
d.Set("copy_paste_enabled", copyPasteEnabled)

if ipConfigs := props.IPConfigurations; ipConfigs != nil {
if err := d.Set("ip_configuration", flattenBastionHostIPConfiguration(ipConfigs)); err != nil {
return fmt.Errorf("flattening `ip_configuration`: %+v", err)
}
}
}

d.SetId(id.ID())

return tags.FlattenAndSet(d, resp.Tags)
}
46 changes: 46 additions & 0 deletions internal/services/network/bastion_host_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package network_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type BastionHostDataSource struct{}

func TestAccBastionHostDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_bastion_host", "test")
r := BastionHostDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("id").Exists(),
check.That(data.ResourceName).Key("location").Exists(),
check.That(data.ResourceName).Key("sku").Exists(),
check.That(data.ResourceName).Key("dns_name").Exists(),
check.That(data.ResourceName).Key("scale_units").Exists(),
check.That(data.ResourceName).Key("file_copy_enabled").Exists(),
check.That(data.ResourceName).Key("ip_connect_enabled").Exists(),
check.That(data.ResourceName).Key("shareable_link_enabled").Exists(),
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(),
),
},
})
}

func (BastionHostDataSource) basic(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))
}
1 change: 1 addition & 0 deletions internal/services/network/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
return map[string]*pluginsdk.Resource{
"azurerm_application_gateway": dataSourceApplicationGateway(),
"azurerm_application_security_group": dataSourceApplicationSecurityGroup(),
"azurerm_bastion_host": dataSourceBastionHost(),
"azurerm_express_route_circuit": dataSourceExpressRouteCircuit(),
"azurerm_ip_group": dataSourceIpGroup(),
"azurerm_nat_gateway": dataSourceNatGateway(),
Expand Down
77 changes: 77 additions & 0 deletions website/docs/d/bastion_host.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
subcategory: "Network"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_bastion_host"
description: |-
Gets information about an existing Bastion Host.
---

# Data Source: azurerm_bastion_host

Use this data source to access information about an existing Bastion Host.

## Example Usage

```hcl
data "azurerm_bastion_host" "example" {
name = "existing-bastion"
resource_group_name = "existing-resources"
}
output "id" {
value = data.azurerm_bastion_host.example.id
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the Bastion Host.

* `resource_group_name` - (Required) The name of the Resource Group where the Bastion Host exists.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:

* `id` - The ID of the Bastion Host.

* `location` - The Azure Region where the Bastion Host exists.

* `copy_paste_enabled` - Is Copy/Paste feature enabled for the Bastion Host.

* `file_copy_enabled` - Is File Copy feature enabled for the Bastion Host.

* `sku` - The SKU of the Bastion Host.

* `ip_configuration` - A `ip_configuration` block as defined below.

* `ip_connect_enabled` - Is IP Connect feature enabled for the Bastion Host.

* `scale_units` - The number of scale units provisioned for the Bastion Host.

* `shareable_link_enabled` - Is Shareable Link feature enabled for the Bastion Host.

* `tunneling_enabled` - Is Tunneling feature enabled for the Bastion Host.

* `dns_name` - The FQDN for the Bastion Host.

* `tags` - A mapping of tags assigned to the Bastion Host.

---

A `ip_configuration` block supports the following:

* `name` - The name of the IP configuration.

* `subnet_id` - Reference to the subnet in which this Bastion Host has been created.

* `public_ip_address_id` - Reference to a Public IP Address associated to this Bastion Host.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the Bastion Host.

0 comments on commit db2860e

Please sign in to comment.