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

New Datasource: azurerm_virtual_hub_connection #21681

Merged
merged 10 commits into from
May 17, 2023
1 change: 1 addition & 0 deletions internal/services/network/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
"azurerm_network_service_tags": dataSourceNetworkServiceTags(),
"azurerm_subnet": dataSourceSubnet(),
"azurerm_virtual_hub": dataSourceVirtualHub(),
"azurerm_virtual_hub_connection": dataSourceVirtualHubConnection(),
"azurerm_virtual_hub_route_table": dataSourceVirtualHubRouteTable(),
"azurerm_virtual_network_gateway": dataSourceVirtualNetworkGateway(),
"azurerm_virtual_network_gateway_connection": dataSourceVirtualNetworkGatewayConnection(),
Expand Down
159 changes: 159 additions & 0 deletions internal/services/network/virtual_hub_connection_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package network

import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"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/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceVirtualHubConnection() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceVirtualHubConnectionRead,

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

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

"resource_group_name": commonschema.ResourceGroupNameForDataSource(),

"virtual_hub_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.VirtualHubName,
},

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

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

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

"routing": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"associated_route_table_id": {
Type: pluginsdk.TypeString,
Computed: true,
},

"propagated_route_table": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"labels": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"route_table_ids": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},

//lintignore:XS003
"static_vnet_route": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},

"address_prefixes": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

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

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

id := parse.NewHubVirtualNetworkConnectionID(subscriptionId, d.Get("resource_group_name").(string), d.Get("virtual_hub_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id.ResourceGroup, id.VirtualHubName, 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)
}

d.SetId(id.ID())

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("virtual_hub_name", id.VirtualHubName)
d.Set("virtual_hub_id", parse.NewVirtualHubID(id.SubscriptionId, id.ResourceGroup, id.VirtualHubName).ID())

if props := resp.HubVirtualNetworkConnectionProperties; props != nil {
d.Set("internet_security_enabled", props.EnableInternetSecurity)
remoteVirtualNetworkId := ""
if props.RemoteVirtualNetwork != nil && props.RemoteVirtualNetwork.ID != nil {
remoteVirtualNetworkId = *props.RemoteVirtualNetwork.ID
}
d.Set("remote_virtual_network_id", remoteVirtualNetworkId)

if err := d.Set("routing", flattenVirtualHubConnectionRouting(props.RoutingConfiguration)); err != nil {
return fmt.Errorf("setting `routing`: %+v", err)
}
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package network_test

import (
"fmt"
"testing"

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

type VirtualHubConnectionDataSource struct{}

func TestAccDataSourceAzureRMVirtualHubConnection_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_virtual_hub_connection", "test")
r := VirtualHubConnectionDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("virtual_hub_id").Exists(),
check.That(data.ResourceName).Key("remote_virtual_network_id").Exists(),
),
},
})
}

func (VirtualHubConnectionDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_virtual_hub_connection" "test" {
name = azurerm_virtual_hub_connection.test.name
resource_group_name = azurerm_virtual_hub.test.resource_group_name
virtual_hub_name = azurerm_virtual_hub.test.name
}
`, VirtualHubRouteTableResource{}.basic(data))
}
82 changes: 82 additions & 0 deletions website/docs/d/virtual_hub_connection.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
subcategory: "Network"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_virtual_hub_connection"
description: |-
Gets information about an existing Virtual Hub Connection
---

# Data Source: azurerm_virtual_hub_connection

Uses this data source to access information about an existing Virtual Hub Connection.

## Virtual Hub Connection Usage

```hcl
data "azurerm_virtual_hub_connection" "example" {
name = "example-connection"
resource_group_name = "example-resources"
virtual_hub_name = "example-hub-name"
}

output "virtual_hub_connection_id" {
value = data.azurerm_virtual_hub_connection.example.id
}
```

## Argument Reference

The following arguments are supported:

* `name` - The name of the Connection which should be retrieved.

* `resource_group_name` - The Name of the Resource Group where the Virtual Hub Connection exists.
* `virtual_hub_name` - The name of the Virtual Hub where this Connection exists.
katbyte marked this conversation as resolved.
Show resolved Hide resolved

## Attributes Reference

The following attributes are exported:

* `id` - The ID of the Virtual Hub Connection.

* `virtual_hub_id` - The ID of the Virtual Hub within which this connection is created

* `remote_virtual_network_id` - The ID of the Virtual Network which the Virtual Hub is connected

* `internet_security_enabled` - Whether Internet Security is enabled to secure internet traffic on this connection

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

---

An `routing` block exports the following:

* `associated_route_table_id` - The ID of the route table associated with this Virtual Hub connection.

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

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

---

A `propagated_route_table` block supports the following:

* `labels` - The list of labels assigned to this route table.

* `route_table_ids` - A list of Route Table IDs associated with this Virtual Hub Connection.

---

A `static_vnet_route` block supports the following:

* `name` - The name which is used for this Static Route.

* `address_prefixes` - A list of CIDR Ranges which is used as Address Prefixes.

* `next_hop_ip_address` - The IP Address which is used for the Next Hop.

## 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 Virtual Hub.