-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_instance_group_manager_actions.go
236 lines (208 loc) · 8.39 KB
/
data_source_ibm_is_instance_group_manager_actions.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
// 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 DataSourceIBMISInstanceGroupManagerActions() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISInstanceGroupManagerActionsRead,
Schema: map[string]*schema.Schema{
"instance_group": {
Type: schema.TypeString,
Required: true,
Description: "instance group ID",
},
"instance_group_manager": {
Type: schema.TypeString,
Required: true,
Description: "Instance group manager ID",
},
"instance_group_manager_actions": {
Type: schema.TypeList,
Description: "List of instance group manager actions",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "instance group manager action name",
},
"action_id": {
Type: schema.TypeString,
Computed: true,
Description: "Instance group manager action ID",
},
"instance_group": {
Type: schema.TypeString,
Required: true,
Description: "instance group ID",
},
"run_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time the scheduled action will run.",
},
"membership_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of members the instance group should have at the scheduled time.",
},
"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",
},
"target_manager": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this instance group manager of type autoscale.",
},
"target_manager_name": {
Type: schema.TypeString,
Computed: true,
Description: "Instance group manager name of type autoscale.",
},
"cron_spec": {
Type: schema.TypeString,
Computed: true,
Description: "The cron specification for a recurring scheduled action. Actions can be applied a maximum of one time within a 5 min period.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The status of the instance group action- `active`: Action is ready to be run- `completed`: Action was completed successfully- `failed`: Action could not be completed successfully- `incompatible`: Action parameters are not compatible with the group or manager- `omitted`: Action was not applied because this action's manager was disabled.",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the instance group manager action was modified.",
},
"action_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of action for the instance group.",
},
"last_applied_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time the scheduled action was last applied. If empty the action has never been applied.",
},
"next_run_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time the scheduled action will next run. If empty the system is currently calculating the next run time.",
},
"auto_delete": {
Type: schema.TypeBool,
Computed: true,
},
"auto_delete_timeout": {
Type: schema.TypeInt,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the instance group manager action was modified.",
},
},
},
},
},
}
}
func dataSourceIBMISInstanceGroupManagerActionsRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
instanceGroupManagerID := d.Get("instance_group_manager").(string)
instanceGroupID := d.Get("instance_group").(string)
// Support for pagination
start := ""
allrecs := []vpcv1.InstanceGroupManagerActionIntf{}
for {
listInstanceGroupManagerActionsOptions := vpcv1.ListInstanceGroupManagerActionsOptions{
InstanceGroupID: &instanceGroupID,
InstanceGroupManagerID: &instanceGroupManagerID,
}
if start != "" {
listInstanceGroupManagerActionsOptions.Start = &start
}
instanceGroupManagerActionsCollection, response, err := sess.ListInstanceGroupManagerActions(&listInstanceGroupManagerActionsOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Getting InstanceGroup Manager Actions %s\n%s", err, response)
}
if instanceGroupManagerActionsCollection != nil && *instanceGroupManagerActionsCollection.TotalCount == int64(0) {
break
}
start = flex.GetNext(instanceGroupManagerActionsCollection.Next)
allrecs = append(allrecs, instanceGroupManagerActionsCollection.Actions...)
if start == "" {
break
}
}
actions := make([]map[string]interface{}, 0)
for _, data := range allrecs {
instanceGroupManagerAction := data.(*vpcv1.InstanceGroupManagerAction)
action := map[string]interface{}{
"name": *instanceGroupManagerAction.Name,
"auto_delete": *instanceGroupManagerAction.AutoDelete,
"auto_delete_timeout": flex.IntValue(instanceGroupManagerAction.AutoDeleteTimeout),
"created_at": instanceGroupManagerAction.CreatedAt.String(),
"action_id": *instanceGroupManagerAction.ID,
"resource_type": *instanceGroupManagerAction.ResourceType,
"status": *instanceGroupManagerAction.Status,
"updated_at": instanceGroupManagerAction.UpdatedAt.String(),
"action_type": *instanceGroupManagerAction.ActionType,
}
if instanceGroupManagerAction.CronSpec != nil {
action["cron_spec"] = *instanceGroupManagerAction.CronSpec
}
if instanceGroupManagerAction.LastAppliedAt != nil {
action["last_applied_at"] = instanceGroupManagerAction.LastAppliedAt.String()
}
if instanceGroupManagerAction.NextRunAt != nil {
action["last_applied_at"] = instanceGroupManagerAction.NextRunAt.String()
}
instanceGroupManagerScheduledActionGroupGroup := instanceGroupManagerAction.Group
if instanceGroupManagerScheduledActionGroupGroup != nil && instanceGroupManagerScheduledActionGroupGroup.MembershipCount != nil {
action["membership_count"] = flex.IntValue(instanceGroupManagerScheduledActionGroupGroup.MembershipCount)
}
instanceGroupManagerScheduledActionManagerManagerInt := instanceGroupManagerAction.Manager
if instanceGroupManagerScheduledActionManagerManagerInt != nil {
instanceGroupManagerScheduledActionManagerManager := instanceGroupManagerScheduledActionManagerManagerInt.(*vpcv1.InstanceGroupManagerScheduledActionManager)
if instanceGroupManagerScheduledActionManagerManager != nil && instanceGroupManagerScheduledActionManagerManager.ID != nil {
if instanceGroupManagerScheduledActionManagerManager.MaxMembershipCount != nil {
action["max_membership_count"] = flex.IntValue(instanceGroupManagerScheduledActionManagerManager.MaxMembershipCount)
}
action["min_membership_count"] = flex.IntValue(instanceGroupManagerScheduledActionManagerManager.MinMembershipCount)
action["target_manager_name"] = *instanceGroupManagerScheduledActionManagerManager.Name
action["target_manager"] = *instanceGroupManagerScheduledActionManagerManager.ID
}
}
actions = append(actions, action)
}
d.Set("instance_group_manager_actions", actions)
d.SetId(dataSourceIBMISInstanceGroupManagerActionsID(d))
return nil
}
func dataSourceIBMISInstanceGroupManagerActionsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}