-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_instance_group_managers.go
206 lines (175 loc) · 6.06 KB
/
data_source_ibm_is_instance_group_managers.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
// Copyright IBM Corp. 2017, 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/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMISInstanceGroupManagers() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISInstanceGroupManagersRead,
Schema: map[string]*schema.Schema{
"instance_group": {
Type: schema.TypeString,
Required: true,
Description: "instance group ID",
},
"instance_group_managers": {
Type: schema.TypeList,
Description: "List of instance group managers",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the instance group manager.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the instance group manager.",
},
"manager_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of instance group manager.",
},
"aggregation_window": {
Type: schema.TypeInt,
Computed: true,
Description: "The time window in seconds to aggregate metrics prior to evaluation",
},
"cooldown": {
Type: schema.TypeInt,
Computed: true,
Description: "The duration of time in seconds to pause further scale actions after scaling has taken place",
},
"max_membership_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The maximum number of members in a managed instance group",
},
"min_membership_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The minimum number of members in a managed instance group",
},
"manager_id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of instance group manager.",
},
"policies": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: "list of Policies associated with instancegroup manager",
},
"actions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instance_group_manager_action": {
Type: schema.TypeString,
Computed: true,
},
"instance_group_manager_action_name": {
Type: schema.TypeString,
Computed: true,
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
},
}
}
func dataSourceIBMISInstanceGroupManagersRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
instanceGroupID := d.Get("instance_group").(string)
// Support for pagination
start := ""
allrecs := []vpcv1.InstanceGroupManagerIntf{}
for {
listInstanceGroupManagerOptions := vpcv1.ListInstanceGroupManagersOptions{
InstanceGroupID: &instanceGroupID,
}
if start != "" {
listInstanceGroupManagerOptions.Start = &start
}
instanceGroupManagerCollections, response, err := sess.ListInstanceGroupManagers(&listInstanceGroupManagerOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Getting InstanceGroup Managers %s\n%s", err, response)
}
start = flex.GetNext(instanceGroupManagerCollections.Next)
allrecs = append(allrecs, instanceGroupManagerCollections.Managers...)
if start == "" {
break
}
}
instanceGroupMnagers := make([]map[string]interface{}, 0)
for _, instanceGroupManagerIntf := range allrecs {
instanceGroupManager := instanceGroupManagerIntf.(*vpcv1.InstanceGroupManager)
if *instanceGroupManager.ManagerType == "scheduled" {
manager := map[string]interface{}{
"id": fmt.Sprintf("%s/%s", instanceGroupID, *instanceGroupManager.ID),
"manager_id": *instanceGroupManager.ID,
"name": *instanceGroupManager.Name,
"manager_type": *instanceGroupManager.ManagerType,
}
actions := make([]map[string]interface{}, 0)
if instanceGroupManager.Actions != nil {
for _, action := range instanceGroupManager.Actions {
actn := map[string]interface{}{
"instance_group_manager_action": action.ID,
"instance_group_manager_action_name": action.Name,
"resource_type": action.ResourceType,
}
actions = append(actions, actn)
}
manager["actions"] = actions
}
instanceGroupMnagers = append(instanceGroupMnagers, manager)
} else {
manager := map[string]interface{}{
"id": fmt.Sprintf("%s/%s", instanceGroupID, *instanceGroupManager.ID),
"manager_id": *instanceGroupManager.ID,
"name": *instanceGroupManager.Name,
"aggregation_window": *instanceGroupManager.AggregationWindow,
"cooldown": *instanceGroupManager.Cooldown,
"max_membership_count": *instanceGroupManager.MaxMembershipCount,
"min_membership_count": *instanceGroupManager.MinMembershipCount,
"manager_type": *instanceGroupManager.ManagerType,
}
policies := make([]string, 0)
if instanceGroupManager.Policies != nil {
for i := 0; i < len(instanceGroupManager.Policies); i++ {
policies = append(policies, string(*(instanceGroupManager.Policies[i].ID)))
}
}
manager["policies"] = policies
instanceGroupMnagers = append(instanceGroupMnagers, manager)
}
}
d.Set("instance_group_managers", instanceGroupMnagers)
d.SetId(dataSourceIBMISInstanceGroupManagersID(d))
return nil
}
// dataSourceIBMISInstanceGroupManagersID returns a reasonable ID for a instance group manager list.
func dataSourceIBMISInstanceGroupManagersID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}