Skip to content

Commit

Permalink
Merge pull request #987 from terraform-providers/data-source-traffic-…
Browse files Browse the repository at this point in the history
…manager-geographical

New Data Source: `azurerm_traffic_manager_geographical_location`
  • Loading branch information
tombuildsstuff authored Mar 16, 2018
2 parents e8bb072 + 45028bd commit 08c96ea
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 26 deletions.
9 changes: 7 additions & 2 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,9 @@ type ArmClient struct {
storageUsageClient storage.UsageClient

// Traffic Manager
trafficManagerProfilesClient trafficmanager.ProfilesClient
trafficManagerEndpointsClient trafficmanager.EndpointsClient
trafficManagerGeographialHierarchiesClient trafficmanager.GeographicHierarchiesClient
trafficManagerProfilesClient trafficmanager.ProfilesClient
trafficManagerEndpointsClient trafficmanager.EndpointsClient

// Web
appServicePlansClient web.AppServicePlansClient
Expand Down Expand Up @@ -844,6 +845,10 @@ func (c *ArmClient) registerTrafficManagerClients(endpoint, subscriptionId strin
c.configureClient(&endpointsClient.Client, auth)
c.trafficManagerEndpointsClient = endpointsClient

geographicalHierarchiesClient := trafficmanager.NewGeographicHierarchiesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&geographicalHierarchiesClient.Client, auth)
c.trafficManagerGeographialHierarchiesClient = geographicalHierarchiesClient

profilesClient := trafficmanager.NewProfilesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&profilesClient.Client, auth)
c.trafficManagerProfilesClient = profilesClient
Expand Down
71 changes: 71 additions & 0 deletions azurerm/data_source_traffic_manager_geographical_location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package azurerm

import (
"fmt"
"strings"

"github.com/Azure/azure-sdk-for-go/services/trafficmanager/mgmt/2017-05-01/trafficmanager"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceArmTrafficManagerGeographicalLocation() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmTrafficManagerGeographicalLocationRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
},
}
}

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

results, err := client.GetDefault(ctx)
if err != nil {
return fmt.Errorf("Error loading Traffic Manager Geographical Hierarchies: %+v", err)
}

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

var result *trafficmanager.Region
if props := results.GeographicHierarchyProperties; props != nil {
if topLevelRegion := props.GeographicHierarchy; topLevelRegion != nil {
result = topLevelRegion
if !geographicalRegionIsMatch(topLevelRegion, name) {
result = filterGeographicalRegions(topLevelRegion.Regions, name)
}
}
}

if result == nil {
return fmt.Errorf("Couldn't find a Traffic Manager Geographic Location with the name %q", name)
}

d.SetId(*result.Code)
return nil
}

func filterGeographicalRegions(input *[]trafficmanager.Region, name string) *trafficmanager.Region {
if regions := input; regions != nil {
for _, region := range *regions {
if geographicalRegionIsMatch(&region, name) {
return &region
}

result := filterGeographicalRegions(region.Regions, name)
if result != nil {
return result
}
}
}

return nil
}

func geographicalRegionIsMatch(input *trafficmanager.Region, name string) bool {
return strings.EqualFold(*input.Name, name)
}
133 changes: 133 additions & 0 deletions azurerm/data_source_traffic_manager_geographical_location_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAzureRMDataSourceTrafficManagerGeographicalLocation_europe(t *testing.T) {
dataSourceName := "data.azurerm_traffic_manager_geographical_location.test"
config := testAccAzureRMDataSourceTrafficManagerGeographicalLocation_europe()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(*terraform.State) error {
// nothing to do
return nil
},
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "id", "GEO-EU"),
resource.TestCheckResourceAttr(dataSourceName, "name", "Europe"),
),
},
},
})
}

func TestAccAzureRMDataSourceTrafficManagerGeographicalLocation_germany(t *testing.T) {
dataSourceName := "data.azurerm_traffic_manager_geographical_location.test"
config := testAccAzureRMDataSourceTrafficManagerGeographicalLocation_germany()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(*terraform.State) error {
// nothing to do
return nil
},
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "id", "DE"),
resource.TestCheckResourceAttr(dataSourceName, "name", "Germany"),
),
},
},
})
}

func TestAccAzureRMDataSourceTrafficManagerGeographicalLocation_unitedKingdom(t *testing.T) {
dataSourceName := "data.azurerm_traffic_manager_geographical_location.test"
config := testAccAzureRMDataSourceTrafficManagerGeographicalLocation_unitedKingdom()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(*terraform.State) error {
// nothing to do
return nil
},
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "id", "GB"),
resource.TestCheckResourceAttr(dataSourceName, "name", "United Kingdom"),
),
},
},
})
}

func TestAccAzureRMDataSourceTrafficManagerGeographicalLocation_world(t *testing.T) {
dataSourceName := "data.azurerm_traffic_manager_geographical_location.test"
config := testAccAzureRMDataSourceTrafficManagerGeographicalLocation_world()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(*terraform.State) error {
// nothing to do
return nil
},
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "id", "WORLD"),
resource.TestCheckResourceAttr(dataSourceName, "name", "World"),
),
},
},
})
}

func testAccAzureRMDataSourceTrafficManagerGeographicalLocation_europe() string {
return fmt.Sprintf(`
data "azurerm_traffic_manager_geographical_location" "test" {
name = "Europe"
}
`)
}

func testAccAzureRMDataSourceTrafficManagerGeographicalLocation_germany() string {
return fmt.Sprintf(`
data "azurerm_traffic_manager_geographical_location" "test" {
name = "Germany"
}
`)
}

func testAccAzureRMDataSourceTrafficManagerGeographicalLocation_unitedKingdom() string {
return fmt.Sprintf(`
data "azurerm_traffic_manager_geographical_location" "test" {
name = "United Kingdom"
}
`)
}

func testAccAzureRMDataSourceTrafficManagerGeographicalLocation_world() string {
return fmt.Sprintf(`
data "azurerm_traffic_manager_geographical_location" "test" {
name = "World"
}
`)
}
49 changes: 25 additions & 24 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,31 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"azurerm_application_security_group": dataSourceArmApplicationSecurityGroup(),
"azurerm_app_service_plan": dataSourceAppServicePlan(),
"azurerm_builtin_role_definition": dataSourceArmBuiltInRoleDefinition(),
"azurerm_client_config": dataSourceArmClientConfig(),
"azurerm_cdn_profile": dataSourceArmCdnProfile(),
"azurerm_dns_zone": dataSourceArmDnsZone(),
"azurerm_eventhub_namespace": dataSourceEventHubNamespace(),
"azurerm_image": dataSourceArmImage(),
"azurerm_key_vault_access_policy": dataSourceArmKeyVaultAccessPolicy(),
"azurerm_managed_disk": dataSourceArmManagedDisk(),
"azurerm_network_interface": dataSourceArmNetworkInterface(),
"azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(),
"azurerm_platform_image": dataSourceArmPlatformImage(),
"azurerm_public_ip": dataSourceArmPublicIP(),
"azurerm_public_ips": dataSourceArmPublicIPs(),
"azurerm_resource_group": dataSourceArmResourceGroup(),
"azurerm_role_definition": dataSourceArmRoleDefinition(),
"azurerm_storage_account": dataSourceArmStorageAccount(),
"azurerm_snapshot": dataSourceArmSnapshot(),
"azurerm_subnet": dataSourceArmSubnet(),
"azurerm_subscription": dataSourceArmSubscription(),
"azurerm_subscriptions": dataSourceArmSubscriptions(),
"azurerm_virtual_network": dataSourceArmVirtualNetwork(),
"azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(),
"azurerm_application_security_group": dataSourceArmApplicationSecurityGroup(),
"azurerm_app_service_plan": dataSourceAppServicePlan(),
"azurerm_builtin_role_definition": dataSourceArmBuiltInRoleDefinition(),
"azurerm_client_config": dataSourceArmClientConfig(),
"azurerm_cdn_profile": dataSourceArmCdnProfile(),
"azurerm_dns_zone": dataSourceArmDnsZone(),
"azurerm_eventhub_namespace": dataSourceEventHubNamespace(),
"azurerm_image": dataSourceArmImage(),
"azurerm_key_vault_access_policy": dataSourceArmKeyVaultAccessPolicy(),
"azurerm_managed_disk": dataSourceArmManagedDisk(),
"azurerm_network_interface": dataSourceArmNetworkInterface(),
"azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(),
"azurerm_platform_image": dataSourceArmPlatformImage(),
"azurerm_public_ip": dataSourceArmPublicIP(),
"azurerm_public_ips": dataSourceArmPublicIPs(),
"azurerm_resource_group": dataSourceArmResourceGroup(),
"azurerm_role_definition": dataSourceArmRoleDefinition(),
"azurerm_storage_account": dataSourceArmStorageAccount(),
"azurerm_snapshot": dataSourceArmSnapshot(),
"azurerm_subnet": dataSourceArmSubnet(),
"azurerm_subscription": dataSourceArmSubscription(),
"azurerm_subscriptions": dataSourceArmSubscriptions(),
"azurerm_traffic_manager_geographical_location": dataSourceArmTrafficManagerGeographicalLocation(),
"azurerm_virtual_network": dataSourceArmVirtualNetwork(),
"azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
<a href="/docs/providers/azurerm/d/subscriptions.html">azurerm_subscriptions</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-traffic-manager-geographical-location") %>>
<a href="/docs/providers/azurerm/d/traffic_manager_geographical_location.html">azurerm_traffic_manager_geographical_location</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-virtual-network-x") %>>
<a href="/docs/providers/azurerm/d/virtual_network.html">azurerm_virtual_network</a>
</li>
Expand Down
32 changes: 32 additions & 0 deletions website/docs/d/traffic_manager_geographical_location.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_traffic_manager_geographical_location"
sidebar_current: "docs-azurerm-datasource-traffic-manager-geographical-location"
description: |-
Gets information about a specified Traffic Manager Geographical Location within the Geographical Hierarchy.
---

# Data Source: azurerm_traffic_manager_geographical_location

Use this data source to access the ID of a specified Traffic Manager Geographical Location within the Geographical Hierarchy.

## Example Usage (World)

```hcl
data "azurerm_traffic_manager_geographical_location" "test" {
name = "World"
}
output "location_code" {
value = "${data.azurerm_traffic_manager_geographical_location.test.id}"
}
```

## Argument Reference

* `name` - (Required) Specifies the name of the Location, for example `World`, `Europe` or `Germany`.

## Attributes Reference

* `id` - The ID of this Location, also known as the `Code` of this Location.

0 comments on commit 08c96ea

Please sign in to comment.