-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_is_lb_profiles.go
188 lines (178 loc) · 5.55 KB
/
data_source_ibm_is_lb_profiles.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"reflect"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isLbsProfiles = "lb_profiles"
isLbsProfileName = "name"
)
func DataSourceIBMISLbProfiles() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISLbProfilesRead,
Schema: map[string]*schema.Schema{
isLbsProfileName: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The load balancer profile name.",
},
isLbsProfiles: {
Type: schema.TypeList,
Description: "Collection of load balancer profile collectors",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name for this load balancer profile",
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this load balancer profile",
},
"family": {
Type: schema.TypeString,
Computed: true,
Description: "The product family this load balancer profile belongs to",
},
"route_mode_supported": {
Type: schema.TypeBool,
Computed: true,
Description: "The route mode support for a load balancer with this profile depends on its configuration",
},
"route_mode_type": {
Type: schema.TypeString,
Computed: true,
Description: "The route mode type for this load balancer profile, one of [fixed, dependent]",
},
"udp_supported": {
Type: schema.TypeBool,
Computed: true,
Description: "The UDP support for a load balancer with this profile",
},
"udp_supported_type": {
Type: schema.TypeString,
Computed: true,
Description: "The UDP support type for a load balancer with this profile",
},
},
},
},
},
}
}
func dataSourceIBMISLbProfilesRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
start := ""
allrecs := []vpcv1.LoadBalancerProfile{}
if lbprofilenameok, ok := d.GetOk(isLbsProfileName); ok {
lbprofilename := lbprofilenameok.(string)
getLoadBalancerProfileOptions := &vpcv1.GetLoadBalancerProfileOptions{
Name: &lbprofilename,
}
lbProfile, response, err := sess.GetLoadBalancerProfile(getLoadBalancerProfileOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching Load Balancer Profile(%s) for VPC %s\n%s", lbprofilename, err, response)
}
allrecs = append(allrecs, *lbProfile)
} else {
for {
listOptions := &vpcv1.ListLoadBalancerProfilesOptions{}
if start != "" {
listOptions.Start = &start
}
profileCollectors, response, err := sess.ListLoadBalancerProfiles(listOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching Load Balancer Profiles for VPC %s\n%s", err, response)
}
start = flex.GetNext(profileCollectors.Next)
allrecs = append(allrecs, profileCollectors.Profiles...)
if start == "" {
break
}
}
}
lbprofilesInfo := make([]map[string]interface{}, 0)
for _, profileCollector := range allrecs {
l := map[string]interface{}{
"name": *profileCollector.Name,
"href": *profileCollector.Href,
"family": *profileCollector.Family,
}
if profileCollector.UDPSupported != nil {
udpSupport := profileCollector.UDPSupported
switch reflect.TypeOf(udpSupport).String() {
case "*vpcv1.LoadBalancerProfileUDPSupportedFixed":
{
udp := udpSupport.(*vpcv1.LoadBalancerProfileUDPSupportedFixed)
l["udp_supported"] = udp.Value
l["udp_supported_type"] = udp.Type
}
case "*vpcv1.LoadBalancerProfileUDPSupportedDependent":
{
udp := udpSupport.(*vpcv1.LoadBalancerProfileUDPSupportedDependent)
if udp.Type != nil {
l["udp_supported_type"] = *udp.Type
}
}
case "*vpcv1.LoadBalancerProfileUDPSupported":
{
udp := udpSupport.(*vpcv1.LoadBalancerProfileUDPSupported)
if udp.Type != nil {
l["udp_supported_type"] = *udp.Type
}
if udp.Value != nil {
l["udp_supported"] = *udp.Value
}
}
}
}
if profileCollector.RouteModeSupported != nil {
routeMode := profileCollector.RouteModeSupported
switch reflect.TypeOf(routeMode).String() {
case "*vpcv1.LoadBalancerProfileRouteModeSupportedFixed":
{
rms := routeMode.(*vpcv1.LoadBalancerProfileRouteModeSupportedFixed)
l["route_mode_supported"] = rms.Value
l["route_mode_type"] = rms.Type
}
case "*vpcv1.LoadBalancerProfileRouteModeSupportedDependent":
{
rms := routeMode.(*vpcv1.LoadBalancerProfileRouteModeSupportedDependent)
if rms.Type != nil {
l["route_mode_type"] = *rms.Type
}
}
case "*vpcv1.LoadBalancerProfileRouteModeSupported":
{
rms := routeMode.(*vpcv1.LoadBalancerProfileRouteModeSupported)
if rms.Type != nil {
l["route_mode_type"] = *rms.Type
}
if rms.Value != nil {
l["route_mode_supported"] = *rms.Value
}
}
}
}
lbprofilesInfo = append(lbprofilesInfo, l)
}
d.SetId(dataSourceIBMISLbProfilesID(d))
d.Set(isLbsProfiles, lbprofilesInfo)
return nil
}
// dataSourceIBMISLbProfilesID returns a reasonable ID for a profileCollector list.
func dataSourceIBMISLbProfilesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}