Skip to content

Commit

Permalink
Feature/azurerm virtual hub connection (#1)
Browse files Browse the repository at this point in the history
* Create virtual_hub_connection.html.markdown

* Update registration.go

* Create virtual_hub_connection_data_source.go

* Create virtual_hub_connection_data_source_test.go
  • Loading branch information
TechyMatt authored May 5, 2023
1 parent 6d4723c commit ffe704f
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/services/network/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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
157 changes: 157 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,157 @@
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.TypeInt,
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,
ValidateFunc: validate.HubRouteTableID,
},

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

"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.TypeSet,
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 Virtual Hub Connection.

* `resource_group_name` - The Name of the Resource Group where the Virtual Hub Connection exists.
* `virtual_hub_name` - The name which should be used for Virtual hub using the connection.

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

0 comments on commit ffe704f

Please sign in to comment.