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 Resource & Data Source: azurerm_virtual_hub #4919

Merged
merged 6 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions azurerm/data_source_virtual_hub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package azurerm

import (
"fmt"
"regexp"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"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: validation.StringMatch(
regexp.MustCompile(`^.{1,256}$`),
`The name must be between 1 and 256 characters in length.`,
),
},

"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
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,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 @@ -474,6 +475,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