-
Notifications
You must be signed in to change notification settings - Fork 657
/
data_source_ibm_is_lb_pool_members.go
242 lines (217 loc) · 8.11 KB
/
data_source_ibm_is_lb_pool_members.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
func DataSourceIBMISLBPoolMembers() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsLbPoolMembersRead,
Schema: map[string]*schema.Schema{
"lb": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The load balancer identifier.",
},
"pool": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The pool identifier.",
},
"members": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Collection of members.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that this member was created.",
},
"health": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Health of the server member in the pool.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The member's canonical URL.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this load balancer pool member.",
},
"port": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The port number of the application running in the server member.",
},
"provisioning_status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The provisioning status of this member.",
},
"target": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The pool member target. Load balancers in the `network` family support virtual serverinstances. Load balancers in the `application` family support IP addresses.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this virtual server instance.",
},
"deleted": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this virtual server instance.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this virtual server instance.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this virtual server instance (and default system hostname).",
},
"address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The IP address.This property may add support for IPv6 addresses in the future. When processing a value in this property, verify that the address is in an expected format. If it is not, log an error. Optionally halt processing and surface the error, or bypass the resource on which the unexpected IP address format was encountered.",
},
},
},
},
"weight": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "Weight of the server member. Applicable only if the pool algorithm is`weighted_round_robin`.",
},
},
},
},
},
}
}
func dataSourceIBMIsLbPoolMembersRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
listLoadBalancerPoolMembersOptions := &vpcv1.ListLoadBalancerPoolMembersOptions{}
listLoadBalancerPoolMembersOptions.SetLoadBalancerID(d.Get("lb").(string))
listLoadBalancerPoolMembersOptions.SetPoolID(d.Get("pool").(string))
loadBalancerPoolMemberCollection, response, err := sess.ListLoadBalancerPoolMembersWithContext(context, listLoadBalancerPoolMembersOptions)
if err != nil {
log.Printf("[DEBUG] ListLoadBalancerPoolMembersWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ListLoadBalancerPoolMembersWithContext failed %s\n%s", err, response))
}
d.SetId(dataSourceIBMIsLbPoolMembersID(d))
if loadBalancerPoolMemberCollection.Members != nil {
err = d.Set("members", dataSourceLoadBalancerPoolMemberCollectionFlattenMembers(loadBalancerPoolMemberCollection.Members))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting members %s", err))
}
}
return nil
}
// dataSourceIBMIsLbPoolMembersID returns a reasonable ID for the list.
func dataSourceIBMIsLbPoolMembersID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceLoadBalancerPoolMemberCollectionFlattenMembers(result []vpcv1.LoadBalancerPoolMember) (members []map[string]interface{}) {
for _, membersItem := range result {
members = append(members, dataSourceLoadBalancerPoolMemberCollectionMembersToMap(membersItem))
}
return members
}
func dataSourceLoadBalancerPoolMemberCollectionMembersToMap(membersItem vpcv1.LoadBalancerPoolMember) (membersMap map[string]interface{}) {
membersMap = map[string]interface{}{}
if membersItem.CreatedAt != nil {
membersMap["created_at"] = membersItem.CreatedAt.String()
}
if membersItem.Health != nil {
membersMap["health"] = membersItem.Health
}
if membersItem.Href != nil {
membersMap["href"] = membersItem.Href
}
if membersItem.ID != nil {
membersMap["id"] = membersItem.ID
}
if membersItem.Port != nil {
membersMap["port"] = membersItem.Port
}
if membersItem.ProvisioningStatus != nil {
membersMap["provisioning_status"] = membersItem.ProvisioningStatus
}
if membersItem.Target != nil {
targetList := []map[string]interface{}{}
target := membersItem.Target.(*vpcv1.LoadBalancerPoolMemberTarget)
targetMap := dataSourceLoadBalancerPoolMemberCollectionMembersTargetToMap(*target)
targetList = append(targetList, targetMap)
membersMap["target"] = targetList
}
if membersItem.Weight != nil {
membersMap["weight"] = membersItem.Weight
}
return membersMap
}
func dataSourceLoadBalancerPoolMemberCollectionMembersTargetToMap(targetItem vpcv1.LoadBalancerPoolMemberTarget) (targetMap map[string]interface{}) {
targetMap = map[string]interface{}{}
if targetItem.CRN != nil {
targetMap["crn"] = targetItem.CRN
}
if targetItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceLoadBalancerPoolMemberCollectionTargetDeletedToMap(*targetItem.Deleted)
deletedList = append(deletedList, deletedMap)
targetMap["deleted"] = deletedList
}
if targetItem.Href != nil {
targetMap["href"] = targetItem.Href
}
if targetItem.ID != nil {
targetMap["id"] = targetItem.ID
}
if targetItem.Name != nil {
targetMap["name"] = targetItem.Name
}
if targetItem.Address != nil {
targetMap["address"] = targetItem.Address
}
return targetMap
}
func dataSourceLoadBalancerPoolMemberCollectionTargetDeletedToMap(deletedItem vpcv1.InstanceReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}