diff --git a/azurerm/config.go b/azurerm/config.go index 120297fd583c..eaf69aecdae8 100644 --- a/azurerm/config.go +++ b/azurerm/config.go @@ -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 @@ -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 diff --git a/azurerm/data_source_traffic_manager_geographical_location.go b/azurerm/data_source_traffic_manager_geographical_location.go new file mode 100644 index 000000000000..6a2bca1cf4c9 --- /dev/null +++ b/azurerm/data_source_traffic_manager_geographical_location.go @@ -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) +} diff --git a/azurerm/data_source_traffic_manager_geographical_location_test.go b/azurerm/data_source_traffic_manager_geographical_location_test.go new file mode 100644 index 000000000000..88e44aaf8caf --- /dev/null +++ b/azurerm/data_source_traffic_manager_geographical_location_test.go @@ -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" +} +`) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index 7e4ef29e9be9..0330b7b0a76e 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -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{ diff --git a/website/azurerm.erb b/website/azurerm.erb index 7c8aaeeebd33..4d9ca33c17d5 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -111,6 +111,10 @@ azurerm_subscriptions +