-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_tg_locations.go
85 lines (72 loc) · 2.6 KB
/
data_source_ibm_tg_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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package transitgateway
import (
"fmt"
"time"
"github.com/IBM/networking-go-sdk/transitgatewayapisv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
tgLocations = "locations"
tgLocationsName = "name"
tgLocationsBillingLoc = "billing_location"
tgLocationsType = "type"
)
func DataSourceIBMTransitGatewaysLocations() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMTransitGatewaysLocationsRead,
Schema: map[string]*schema.Schema{
tgLocations: {
Type: schema.TypeList,
Description: "Collection of Transit Gateway locations",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
tgLocationsName: {
Type: schema.TypeString,
Computed: true,
Description: "Name of the Location.",
},
tgLocationsType: {
Type: schema.TypeString,
Computed: true,
Description: "The type of the location, determining is this a multi-zone region, a single data center, or a point of presence.",
},
tgLocationsBillingLoc: {
Type: schema.TypeString,
Computed: true,
Description: "The geographical location of this location, used for billing purposes.",
},
},
},
},
},
}
}
func dataSourceIBMTransitGatewaysLocationsRead(d *schema.ResourceData, meta interface{}) error {
client, err := transitgatewayClient(meta)
if err != nil {
return err
}
listTransitGatewayLocationsOptionsModel := &transitgatewayapisv1.ListGatewayLocationsOptions{}
listTransitGatewayLocations, response, err := client.ListGatewayLocations(listTransitGatewayLocationsOptionsModel)
if err != nil {
return fmt.Errorf("[ERROR] Error while fetching transit gateways locations: %s\n%s", err, response)
}
tgLocationsCol := make([]map[string]interface{}, 0)
for _, instance := range listTransitGatewayLocations.Locations {
transitgatewayLoc := map[string]interface{}{}
transitgatewayLoc[tgLocationsName] = instance.Name
transitgatewayLoc[tgLocationsType] = instance.Type
transitgatewayLoc[tgLocationsBillingLoc] = instance.BillingLocation
tgLocationsCol = append(tgLocationsCol, transitgatewayLoc)
}
d.Set(tgLocations, tgLocationsCol)
d.SetId(dataSourceIBMTransitGatewaysID(d))
return nil
}
// dataSourceIBMTransitGatewaysLocationsID returns a reasonable ID for a transit gateways locations list.
func dataSourceIBMTransitGatewaysLocationsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}