/
data_source_ibm_dl_locations.go
153 lines (144 loc) · 4.92 KB
/
data_source_ibm_dl_locations.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package ibm
import (
"fmt"
"github.com/IBM/networking-go-sdk/directlinkv1"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"time"
)
const (
dlBillingLocation = "billing_location"
dlLocDisplayName = "display_name"
dlLocationType = "location_type"
dlMarket = "market"
dlMarketGeography = "market_geography"
dlMzr = "mzr"
dlLocShortName = "name"
dlBuildingColocationOwner = "building_colocation_owner"
dlVpcRegion = "vpc_region"
dlLocations = "locations"
)
func dataSourceIBMDLLocations() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMDLOfferingLocationsRead,
Schema: map[string]*schema.Schema{
dlOfferingType: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAllowedStringValue([]string{"dedicated", "connect"}),
Description: "The Direct Link offering type. Current supported values (dedicated and connect).",
},
dlLocations: {
Type: schema.TypeList,
Description: "Collection of valid locations for the specified Direct Link offering.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
dlOfferingType: {
Type: schema.TypeString,
Computed: true,
Description: "The Direct Link offering type. Current supported values (dedicated and connect).",
},
dlBillingLocation: {
Type: schema.TypeString,
Computed: true,
Description: "Billing location. Only present for locations where provisioning is enabled.",
},
dlBuildingColocationOwner: {
Type: schema.TypeString,
Computed: true,
Description: "Building colocation owner. Only present for offering_type=dedicated locations where provisioning is enabled.",
},
dlLocationType: {
Type: schema.TypeString,
Computed: true,
Description: "Location type",
},
dlLocShortName: {
Type: schema.TypeString,
Computed: true,
Description: "Location short name",
}, dlLocDisplayName: {
Type: schema.TypeString,
Computed: true,
Description: "Location long name",
},
dlMarket: {
Type: schema.TypeString,
Computed: true,
Description: "Location market",
},
dlMarketGeography: {
Type: schema.TypeString,
Computed: true,
Description: "Location geography. Only present for locations where provisioning is enabled.",
},
dlMzr: {
Type: schema.TypeBool,
Computed: true,
Description: "Is location a multi-zone region (MZR). Only present for locations where provisioning is enabled.",
},
dlVpcRegion: {
Type: schema.TypeString,
Computed: true,
Description: "Location's VPC region. Only present for locations where provisioning is enabled.",
},
},
},
},
},
}
}
func dataSourceIBMDLOfferingLocationsRead(d *schema.ResourceData, meta interface{}) error {
directLink, err := meta.(ClientSession).DirectlinkV1API()
if err != nil {
return err
}
listOfferingTypeLocationsOptions := &directlinkv1.ListOfferingTypeLocationsOptions{}
listOfferingTypeLocationsOptions.SetOfferingType(d.Get(dlOfferingType).(string))
listLocations, response, err := directLink.ListOfferingTypeLocations(listOfferingTypeLocationsOptions)
if err != nil {
return fmt.Errorf("Error while listing directlink gateway's locations %s\n%s", err, response)
}
locations := make([]map[string]interface{}, 0)
for _, instance := range listLocations.Locations {
location := map[string]interface{}{}
if instance.BuildingColocationOwner != nil {
location[dlBuildingColocationOwner] = *instance.BuildingColocationOwner
}
if instance.DisplayName != nil {
location[dlLocDisplayName] = *instance.DisplayName
}
if instance.Name != nil {
location[dlLocShortName] = *instance.Name
}
if instance.LocationType != nil {
location[dlLocationType] = *instance.LocationType
}
if instance.OfferingType != nil {
location[dlOfferingType] = *instance.OfferingType
}
if instance.Market != nil {
location[dlMarket] = *instance.Market
}
if instance.MarketGeography != nil {
location[dlMarketGeography] = *instance.MarketGeography
}
if instance.Mzr != nil {
location[dlMzr] = *instance.Mzr
}
if instance.VpcRegion != nil {
location[dlVpcRegion] = *instance.VpcRegion
}
if instance.BillingLocation != nil {
location[dlBillingLocation] = *instance.BillingLocation
}
locations = append(locations, location)
}
d.SetId(dataSourceIBMDLLocationsID(d))
d.Set(dlLocations, locations)
return nil
}
// dataSourceIBMDLLocationsID returns a reasonable ID for a direct link offering locations list.
func dataSourceIBMDLLocationsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}