-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_tg_location.go
102 lines (91 loc) · 3.42 KB
/
data_source_ibm_tg_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
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
// 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 (
tgLocalConnectionLocations = "local_connection_locations"
tgLocationsDisplayName = "display_name"
)
func DataSourceIBMTransitGatewaysLocation() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMTransitGatewaysLocationRead,
Schema: map[string]*schema.Schema{
tgLocationsName: {
Type: schema.TypeString,
Required: 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.",
},
tgLocalConnectionLocations: {
Type: schema.TypeList,
Computed: true,
Description: "The set of network locations that are considered local for this Transit Gateway location.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
tgLocationsName: {
Type: schema.TypeString,
Computed: true,
Description: "Name of the Location.",
},
tgLocationsDisplayName: {
Type: schema.TypeString,
Computed: true,
Description: "A descriptive display name for 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.",
},
},
},
},
},
}
}
func dataSourceIBMTransitGatewaysLocationRead(d *schema.ResourceData, meta interface{}) error {
client, err := transitgatewayClient(meta)
if err != nil {
return err
}
detailGatewayLocationOptionsModel := &transitgatewayapisv1.GetGatewayLocationOptions{}
locName := d.Get(tgLocationsName).(string)
detailGatewayLocationOptionsModel.Name = &locName
detailTransitGatewayLocation, response, err := client.GetGatewayLocation(detailGatewayLocationOptionsModel)
if err != nil {
return fmt.Errorf("[ERROR] Error while fetching transit gateway detailed location: %s\n%s", err, response)
}
if detailTransitGatewayLocation != nil {
d.SetId(dataSourceIBMTransitGatewaysLocationID(d))
d.Set(tgLocationsType, *detailTransitGatewayLocation.Type)
d.Set(tgLocationsBillingLoc, *detailTransitGatewayLocation.BillingLocation)
tgConnLocationsCol := make([]map[string]interface{}, 0)
for _, instance := range detailTransitGatewayLocation.LocalConnectionLocations {
tgConnLocation := map[string]interface{}{}
tgConnLocation[tgLocationsName] = *instance.Name
tgConnLocation[tgLocationsDisplayName] = *instance.DisplayName
tgConnLocation[tgLocationsType] = *instance.Type
tgConnLocationsCol = append(tgConnLocationsCol, tgConnLocation)
}
d.Set(tgLocalConnectionLocations, tgConnLocationsCol)
}
return nil
}
// dataSourceIBMTransitGatewaysLocationID returns a reasonable ID for a transit gateways location list.
func dataSourceIBMTransitGatewaysLocationID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}