/
data_source_ibm_dl_ports.go
118 lines (104 loc) · 3.06 KB
/
data_source_ibm_dl_ports.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
package ibm
import (
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
const (
dlPorts = "ports"
dlPortID = "port_id"
dlCount = "direct_link_count"
dlLabel = "label"
// dlLocationDisplayName = "location_display_name"
// dlLocationName = "location_name"
dlSupportedLinkSpeeds = "supported_link_speeds"
dlProviderName = "provider_name"
)
func dataSourceIBMDirectLinkPorts() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMDirectLinkPortsRead,
Schema: map[string]*schema.Schema{
dlPorts: {
Type: schema.TypeList,
Description: "Collection of direct link ports",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
dlPortID: {
Type: schema.TypeString,
Computed: true,
Description: "Port ID",
},
dlCount: {
Type: schema.TypeInt,
Computed: true,
Description: "Count of existing Direct Link gateways in this account on this port",
},
dlLabel: {
Type: schema.TypeString,
Computed: true,
Description: "Port Label",
},
dlLocationDisplayName: {
Type: schema.TypeString,
Computed: true,
Description: "Port location long name",
},
dlLocationName: {
Type: schema.TypeString,
Computed: true,
Description: "Port location name identifier",
},
dlProviderName: {
Type: schema.TypeString,
Computed: true,
Description: "Port's provider name",
},
dlSupportedLinkSpeeds: {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Computed: true,
Description: "Port's supported speeds in megabits per second",
},
},
},
},
},
}
}
func dataSourceIBMDirectLinkPortsRead(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(ClientSession).DirectlinkV1API()
if err != nil {
return err
}
listPortsOptions := sess.NewListPortsOptions()
response, resp, err := sess.ListPorts(listPortsOptions)
if err != nil {
log.Println("[WARN] Error listing dl ports", resp, err)
return err
}
portCollections := make([]map[string]interface{}, 0)
for _, port := range response.Ports {
portCollection := map[string]interface{}{}
portCollection[dlPortID] = *port.ID
portCollection[dlCount] = *port.DirectLinkCount
portCollection[dlLabel] = *port.Label
portCollection[dlLocationDisplayName] = *port.LocationDisplayName
portCollection[dlLocationName] = *port.LocationName
portCollection[dlProviderName] = *port.ProviderName
speed := make([]interface{}, 0)
for _, s := range port.SupportedLinkSpeeds {
speed = append(speed, s)
}
portCollection[dlSupportedLinkSpeeds] = speed
portCollections = append(portCollections, portCollection)
}
d.SetId(dataSourceIBMDirectLinkPortsReadID(d))
d.Set(dlPorts, portCollections)
return nil
}
func dataSourceIBMDirectLinkPortsReadID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}