Skip to content

Commit

Permalink
New Resource & Data Source: azurerm_virtual_hub (#4919)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Ye authored and mbfrahry committed Nov 20, 2019
1 parent 857e3d4 commit 44df08d
Show file tree
Hide file tree
Showing 12 changed files with 1,146 additions and 3 deletions.
162 changes: 162 additions & 0 deletions azurerm/data_source_virtual_hub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
aznetwork "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmVirtualHub() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmVirtualHubRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: aznetwork.ValidateVirtualHubName,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"location": azure.SchemaLocationForDataSource(),

"address_prefix": {
Type: schema.TypeString,
Computed: true,
},

"virtual_wan_id": {
Type: schema.TypeString,
Computed: true,
},

"s2s_vpn_gateway_id": {
Type: schema.TypeString,
Computed: true,
},

"p2s_vpn_gateway_id": {
Type: schema.TypeString,
Computed: true,
},

"express_route_gateway_id": {
Type: schema.TypeString,
Computed: true,
},

"virtual_network_connection": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"remote_virtual_network_id": {
Type: schema.TypeString,
Computed: true,
},
"allow_hub_to_remote_vnet_transit": {
Type: schema.TypeBool,
Computed: true,
},
"allow_remote_vnet_to_use_hub_vnet_gateways": {
Type: schema.TypeBool,
Computed: true,
},
"enable_internet_security": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},

"route": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address_prefixes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"next_hop_ip_address": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"tags": tags.SchemaDataSource(),
},
}
}

func dataSourceArmVirtualHubRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Network.VirtualHubClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Virtual Hub %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("Error reading Virtual Hub %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if props := resp.VirtualHubProperties; props != nil {
d.Set("address_prefix", props.AddressPrefix)
if props.VirtualWan != nil {
if err := d.Set("virtual_wan_id", props.VirtualWan.ID); err != nil {
return fmt.Errorf("Error setting `virtual_wan_id`: %+v", err)
}
}
if props.VpnGateway != nil {
if err := d.Set("s2s_vpn_gateway_id", props.VpnGateway.ID); err != nil {
return fmt.Errorf("Error setting `s2s_vpn_gateway_id`: %+v", err)
}
}
if props.P2SVpnGateway != nil {
if err := d.Set("p2s_vpn_gateway_id", props.P2SVpnGateway.ID); err != nil {
return fmt.Errorf("Error setting `p2s_vpn_gateway_id`: %+v", err)
}
}
if props.ExpressRouteGateway != nil {
if err := d.Set("express_route_gateway_id", props.ExpressRouteGateway.ID); err != nil {
return fmt.Errorf("Error setting `express_route_gateway_id`: %+v", err)
}
}
if err := d.Set("virtual_network_connection", flattenArmVirtualHubVirtualNetworkConnection(props.VirtualNetworkConnections)); err != nil {
return fmt.Errorf("Error setting `virtual_network_connection`: %+v", err)
}
if props.RouteTable != nil {
if err := d.Set("route", flattenArmVirtualHubRoute(props.RouteTable.Routes)); err != nil {
return fmt.Errorf("Error setting `route`: %+v", err)
}
}
}

return tags.FlattenAndSet(d, resp.Tags)
}
41 changes: 41 additions & 0 deletions azurerm/data_source_virtual_hub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
)

func TestAccDataSourceAzureRMVirtualHub_basic(t *testing.T) {
dataSourceName := "data.azurerm_virtual_hub.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVirtualHub_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "address_prefix"),
resource.TestCheckResourceAttrSet(dataSourceName, "virtual_wan_id"),
),
},
},
})
}

func testAccDataSourceVirtualHub_basic(rInt int, location string) string {
config := testAccAzureRMVirtualHub_basic(rInt, location)
return fmt.Sprintf(`
%s
data "azurerm_virtual_hub" "test" {
name = "${azurerm_virtual_hub.test.name}"
resource_group_name = "${azurerm_virtual_hub.test.resource_group_name}"
}
`, config)
}
5 changes: 5 additions & 0 deletions azurerm/internal/services/network/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Client struct {
VnetClient *network.VirtualNetworksClient
VnetPeeringsClient *network.VirtualNetworkPeeringsClient
VirtualWanClient *network.VirtualWansClient
VirtualHubClient *network.VirtualHubsClient
WatcherClient *network.WatchersClient
WebApplicationFirewallPoliciesClient *network.WebApplicationFirewallPoliciesClient
}
Expand Down Expand Up @@ -115,6 +116,9 @@ func BuildClient(o *common.ClientOptions) *Client {
VirtualWanClient := network.NewVirtualWansClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&VirtualWanClient.Client, o.ResourceManagerAuthorizer)

VirtualHubClient := network.NewVirtualHubsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&VirtualHubClient.Client, o.ResourceManagerAuthorizer)

WatcherClient := network.NewWatchersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&WatcherClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -148,6 +152,7 @@ func BuildClient(o *common.ClientOptions) *Client {
VnetClient: &VnetClient,
VnetPeeringsClient: &VnetPeeringsClient,
VirtualWanClient: &VirtualWanClient,
VirtualHubClient: &VirtualHubClient,
WatcherClient: &WatcherClient,
WebApplicationFirewallPoliciesClient: &WebApplicationFirewallPoliciesClient,
}
Expand Down
16 changes: 16 additions & 0 deletions azurerm/internal/services/network/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package network

import (
"fmt"
"regexp"
)

func ValidateVirtualHubName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

if !regexp.MustCompile(`^.{1,256}$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%q must be between 1 and 256 characters in length.", k))
}

return warnings, errors
}
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_subscriptions": dataSourceArmSubscriptions(),
"azurerm_traffic_manager_geographical_location": dataSourceArmTrafficManagerGeographicalLocation(),
"azurerm_user_assigned_identity": dataSourceArmUserAssignedIdentity(),
"azurerm_virtual_hub": dataSourceArmVirtualHub(),
"azurerm_virtual_machine": dataSourceArmVirtualMachine(),
"azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(),
"azurerm_virtual_network_gateway_connection": dataSourceArmVirtualNetworkGatewayConnection(),
Expand Down Expand Up @@ -476,6 +477,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_virtual_network_peering": resourceArmVirtualNetworkPeering(),
"azurerm_virtual_network": resourceArmVirtualNetwork(),
"azurerm_virtual_wan": resourceArmVirtualWan(),
"azurerm_virtual_hub": resourceArmVirtualHub(),
"azurerm_web_application_firewall_policy": resourceArmWebApplicationFirewallPolicy(),
}

Expand Down
Loading

0 comments on commit 44df08d

Please sign in to comment.