-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_instance_group_manager.go
172 lines (146 loc) · 4.83 KB
/
data_source_ibm_is_instance_group_manager.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"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 DataSourceIBMISInstanceGroupManager() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISInstanceGroupManagerRead,
Schema: map[string]*schema.Schema{
"instance_group": {
Type: schema.TypeString,
Required: true,
Description: "instance group ID",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name 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 dataSourceIBMISInstanceGroupManagerRead(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
}
}
instanceGroupManagerName := d.Get("name").(string)
for _, instanceGroupManagerIntf := range allrecs {
instanceGroupManager := instanceGroupManagerIntf.(*vpcv1.InstanceGroupManager)
if instanceGroupManagerName == *instanceGroupManager.Name {
d.SetId(fmt.Sprintf("%s/%s", instanceGroupID, *instanceGroupManager.ID))
d.Set("manager_type", *instanceGroupManager.ManagerType)
d.Set("manager_id", *instanceGroupManager.ID)
if *instanceGroupManager.ManagerType == "scheduled" {
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)
}
d.Set("actions", actions)
}
} else {
d.Set("aggregation_window", *instanceGroupManager.AggregationWindow)
d.Set("cooldown", *instanceGroupManager.Cooldown)
d.Set("max_membership_count", *instanceGroupManager.MaxMembershipCount)
d.Set("min_membership_count", *instanceGroupManager.MinMembershipCount)
policies := make([]string, 0)
if instanceGroupManager.Policies != nil {
for i := 0; i < len(instanceGroupManager.Policies); i++ {
policies = append(policies, string(*(instanceGroupManager.Policies[i].ID)))
}
}
d.Set("policies", policies)
}
return nil
}
}
return fmt.Errorf("Instance group manager %s not found", instanceGroupManagerName)
}