-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_instance_group_memberships.go
192 lines (172 loc) · 7 KB
/
data_source_ibm_is_instance_group_memberships.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
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
const (
isInstanceGroupMemberships = "memberships"
)
func DataSourceIBMISInstanceGroupMemberships() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISInstanceGroupMembershipsRead,
Schema: map[string]*schema.Schema{
isInstanceGroup: {
Type: schema.TypeString,
Required: true,
Description: "The instance group identifier.",
},
isInstanceGroupMemberships: {
Type: schema.TypeList,
Computed: true,
Description: "Collection of instance group memberships.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isInstanceGroupMemershipDeleteInstanceOnMembershipDelete: {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, when deleting the membership the instance will also be deleted.",
},
isInstanceGroupMembership: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this instance group membership.",
},
isInstanceGroupMemershipInstance: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isInstanceGroupMembershipCrn: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this virtual server instance.",
},
isInstanceGroupMembershipVirtualServerInstance: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this virtual server instance.",
},
isInstanceGroupMemershipInstanceName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this virtual server instance (and default system hostname).",
},
},
},
},
isInstanceGroupMemershipInstanceTemplate: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isInstanceGroupMembershipCrn: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this instance template.",
},
isInstanceGroupMemershipInstanceTemplate: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this instance template.",
},
isInstanceGroupMemershipInstanceTemplateName: {
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this instance template.",
},
},
},
},
isInstanceGroupMembershipName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this instance group membership. Names must be unique within the instance group.",
},
isInstanceGroupMembershipLoadBalancerPoolMember: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this load balancer pool member.",
},
isInstanceGroupMembershipStatus: {
Type: schema.TypeString,
Computed: true,
Description: "The status of the instance group membership- `deleting`: Membership is deleting dependent resources- `failed`: Membership was unable to maintain dependent resources- `healthy`: Membership is active and serving in the group- `pending`: Membership is waiting for dependent resources- `unhealthy`: Membership has unhealthy dependent resources.",
},
},
},
},
},
}
}
func dataSourceIBMISInstanceGroupMembershipsRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
instanceGroupID := d.Get(isInstanceGroup).(string)
// Support for pagination
start := ""
allrecs := []vpcv1.InstanceGroupMembership{}
for {
listInstanceGroupMembershipsOptions := vpcv1.ListInstanceGroupMembershipsOptions{
InstanceGroupID: &instanceGroupID,
}
if start != "" {
listInstanceGroupMembershipsOptions.Start = &start
}
instanceGroupMembershipCollection, response, err := sess.ListInstanceGroupMemberships(&listInstanceGroupMembershipsOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Getting InstanceGroup Membership Collection %s\n%s", err, response)
}
start = flex.GetNext(instanceGroupMembershipCollection.Next)
allrecs = append(allrecs, instanceGroupMembershipCollection.Memberships...)
if start == "" {
break
}
}
memberships := make([]map[string]interface{}, 0)
for _, instanceGroupMembership := range allrecs {
membership := map[string]interface{}{
isInstanceGroupMemershipDeleteInstanceOnMembershipDelete: *instanceGroupMembership.DeleteInstanceOnMembershipDelete,
isInstanceGroupMembership: *instanceGroupMembership.ID,
isInstanceGroupMembershipName: *instanceGroupMembership.Name,
isInstanceGroupMembershipStatus: *instanceGroupMembership.Status,
}
instances := make([]map[string]interface{}, 0)
if instanceGroupMembership.Instance != nil {
instance := map[string]interface{}{
isInstanceGroupMembershipCrn: *instanceGroupMembership.Instance.CRN,
isInstanceGroupMembershipVirtualServerInstance: *instanceGroupMembership.Instance.ID,
isInstanceGroupMemershipInstanceName: *instanceGroupMembership.Instance.Name,
}
instances = append(instances, instance)
}
membership[isInstanceGroupMemershipInstance] = instances
instance_templates := make([]map[string]interface{}, 0)
if instanceGroupMembership.InstanceTemplate != nil {
instance_template := map[string]interface{}{
isInstanceGroupMembershipCrn: *instanceGroupMembership.InstanceTemplate.CRN,
isInstanceGroupMemershipInstanceTemplate: *instanceGroupMembership.InstanceTemplate.ID,
isInstanceGroupMemershipInstanceTemplateName: *instanceGroupMembership.InstanceTemplate.Name,
}
instance_templates = append(instance_templates, instance_template)
}
membership[isInstanceGroupMemershipInstanceTemplate] = instance_templates
if instanceGroupMembership.PoolMember != nil && instanceGroupMembership.PoolMember.ID != nil {
membership[isInstanceGroupMembershipLoadBalancerPoolMember] = *instanceGroupMembership.PoolMember.ID
}
memberships = append(memberships, membership)
}
d.Set(isInstanceGroupMemberships, memberships)
d.SetId(dataSourceIbmIsInstanceGroupMembershipsID(d))
return nil
}
// dataSourceIbmIsInstanceGroupMembershipsID returns a reasonable ID for the list.
func dataSourceIbmIsInstanceGroupMembershipsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}