-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #987 from terraform-providers/data-source-traffic-…
…manager-geographical New Data Source: `azurerm_traffic_manager_geographical_location`
- Loading branch information
Showing
6 changed files
with
272 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
azurerm/data_source_traffic_manager_geographical_location.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(®ion, name) { | ||
return ®ion | ||
} | ||
|
||
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
133
azurerm/data_source_traffic_manager_geographical_location_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
website/docs/d/traffic_manager_geographical_location.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |