forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_traffic_manager_geographical_location.go
71 lines (58 loc) · 1.83 KB
/
data_source_traffic_manager_geographical_location.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)
}