-
Notifications
You must be signed in to change notification settings - Fork 669
/
data_source_ibm_dl_ports.go
147 lines (130 loc) · 3.84 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
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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package directlink
import (
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
dl "github.com/IBM/networking-go-sdk/directlinkv1"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/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{
dlLocationName: {
Type: schema.TypeString,
Optional: true,
Description: "Direct Link location short name",
},
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.(conns.ClientSession).DirectlinkV1API()
if err != nil {
return err
}
start := ""
allrecs := []dl.Port{}
for {
listPortsOptions := sess.NewListPortsOptions()
if _, ok := d.GetOk(dlLocationName); ok {
dlLocationName := d.Get(dlLocationName).(string)
listPortsOptions.SetLocationName(dlLocationName)
}
if start != "" {
listPortsOptions.Start = &start
}
response, resp, err := sess.ListPorts(listPortsOptions)
if err != nil {
log.Println("[WARN] Error listing dl ports", resp, err)
return err
}
start = flex.GetNext(response.Next)
allrecs = append(allrecs, response.Ports...)
if start == "" {
break
}
}
portCollections := make([]map[string]interface{}, 0)
for _, port := range allrecs {
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()
}