-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_is_lb_profiles.go
133 lines (122 loc) · 3.7 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
// 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"
)
func DataSourceIBMISLbProfiles() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISLbProfilesRead,
Schema: map[string]*schema.Schema{
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]",
},
},
},
},
},
}
}
func dataSourceIBMISLbProfilesRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
start := ""
allrecs := []vpcv1.LoadBalancerProfile{}
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.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()
}