/
data_source_ibm_pi_instances.go
262 lines (246 loc) · 8.65 KB
/
data_source_ibm_pi_instances.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright IBM Corp. 2017, 2022 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package power
import (
"context"
"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func DataSourceIBMPIInstances() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPIInstancesAllRead,
Schema: map[string]*schema.Schema{
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
// Attributes
Attr_PVMInstances: {
Computed: true,
Description: "List of power virtual server instances for the respective cloud instance.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_HealthStatus: {
Computed: true,
Description: "The health of the instance.",
Type: schema.TypeString,
},
Attr_LicenseRepositoryCapacity: {
Computed: true,
Description: "The VTL license repository capacity TB value.",
Type: schema.TypeInt,
},
Attr_MaxMem: {
Computed: true,
Description: "The maximum amount of memory that can be allocated to the instance without shutting down or rebooting the LPAR.",
Type: schema.TypeFloat,
},
Attr_MaxProc: {
Computed: true,
Description: "The maximum number of processors that can be allocated to the instance without shutting down or rebooting the LPAR.",
Type: schema.TypeFloat,
},
Attr_MaxVirtualCores: {
Computed: true,
Description: "The maximum number of virtual cores that can be assigned without rebooting the instance.",
Type: schema.TypeInt,
},
Attr_Memory: {
Computed: true,
Description: "The amount of memory that is allocated to the instance.",
Type: schema.TypeFloat,
},
Attr_MinMem: {
Computed: true,
Description: "The minimum amount of memory that must be allocated to the instance.",
Type: schema.TypeFloat,
},
Attr_MinProc: {
Computed: true,
Description: "The minimum number of processors that must be allocated to the instance. ",
Type: schema.TypeFloat,
},
Attr_MinVirtualCores: {
Computed: true,
Description: "The minimum number of virtual cores that can be assigned without rebooting the instance.",
Type: schema.TypeInt,
},
Attr_Networks: {
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_ExternalIP: {
Computed: true,
Description: "The external IP address of the instance.",
Type: schema.TypeString,
},
Attr_IP: {
Computed: true,
Description: "The IP address of the instance.",
Type: schema.TypeString,
},
Attr_MacAddress: {
Computed: true,
Description: "The MAC address of the instance.",
Type: schema.TypeString,
},
Attr_NetworkID: {
Computed: true,
Description: "The network ID of the instance.",
Type: schema.TypeString,
},
Attr_NetworkName: {
Computed: true,
Description: "The network name of the instance.",
Type: schema.TypeString,
},
Attr_Type: {
Computed: true,
Description: "The type of the network.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
Attr_PinPolicy: {
Computed: true,
Description: "The pinning policy of the instance.",
Type: schema.TypeString,
},
Attr_PlacementGroupID: {
Computed: true,
Description: "The ID of the placement group that the instance is a member.",
Type: schema.TypeString,
},
Attr_Processors: {
Computed: true,
Description: "The number of processors that are allocated to the instance.",
Type: schema.TypeFloat,
},
Attr_ProcType: {
Computed: true,
Description: "The procurement type of the instance. Supported values are shared and dedicated.",
Type: schema.TypeString,
},
Attr_PVMInstanceID: {
Computed: true,
Description: "The unique identifier of the instance.",
Type: schema.TypeString,
},
Attr_SharedProcessorPool: {
Computed: true,
Description: "The name of the shared processor pool for the instance.",
Type: schema.TypeString,
},
Attr_SharedProcessorPoolID: {
Computed: true,
Description: "The ID of the shared processor pool for the instance.",
Type: schema.TypeString,
},
Attr_Status: {
Computed: true,
Description: "The status of the instance.",
Type: schema.TypeString,
},
Attr_StoragePool: {
Computed: true,
Description: "The storage Pool where server is deployed.",
Type: schema.TypeString,
},
Attr_StoragePoolAffinity: {
Computed: true,
Description: "Indicates if all volumes attached to the server must reside in the same storage pool.",
Type: schema.TypeBool,
},
Attr_StorageType: {
Computed: true,
Description: "The storage type where server is deployed.",
Type: schema.TypeString,
},
Attr_VirtualCoresAssigned: {
Computed: true,
Description: "The virtual cores that are assigned to the instance.",
Type: schema.TypeInt,
},
},
},
Type: schema.TypeList,
},
},
}
}
func dataSourceIBMPIInstancesAllRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
powerC := instance.NewIBMPIInstanceClient(ctx, sess, cloudInstanceID)
powervmdata, err := powerC.GetAll()
if err != nil {
return diag.FromErr(err)
}
var clientgenU, _ = uuid.GenerateUUID()
d.SetId(clientgenU)
d.Set(Attr_PVMInstances, flattenPvmInstances(powervmdata.PvmInstances))
return nil
}
func flattenPvmInstances(list []*models.PVMInstanceReference) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(list))
for _, i := range list {
l := map[string]interface{}{
Attr_LicenseRepositoryCapacity: i.LicenseRepositoryCapacity,
Attr_MaxMem: i.Maxmem,
Attr_MaxProc: i.Maxproc,
Attr_MaxVirtualCores: i.VirtualCores.Max,
Attr_Memory: *i.Memory,
Attr_MinMem: i.Minmem,
Attr_MinProc: i.Minproc,
Attr_MinVirtualCores: i.VirtualCores.Min,
Attr_Networks: flattenPvmInstanceNetworks(i.Networks),
Attr_PinPolicy: i.PinPolicy,
Attr_PlacementGroupID: i.PlacementGroup,
Attr_Processors: *i.Processors,
Attr_ProcType: *i.ProcType,
Attr_PVMInstanceID: *i.PvmInstanceID,
Attr_SharedProcessorPool: i.SharedProcessorPool,
Attr_SharedProcessorPoolID: i.SharedProcessorPoolID,
Attr_Status: *i.Status,
Attr_StoragePool: i.StoragePool,
Attr_StoragePoolAffinity: i.StoragePoolAffinity,
Attr_StorageType: i.StorageType,
Attr_VirtualCoresAssigned: i.VirtualCores.Assigned,
}
if i.Health != nil {
l[Attr_HealthStatus] = i.Health.Status
}
result = append(result, l)
}
return result
}
func flattenPvmInstanceNetworks(list []*models.PVMInstanceNetwork) (networks []map[string]interface{}) {
if list != nil {
networks = make([]map[string]interface{}, len(list))
for i, pvmip := range list {
p := make(map[string]interface{})
p[Attr_ExternalIP] = pvmip.ExternalIP
p[Attr_IP] = pvmip.IPAddress
p[Attr_MacAddress] = pvmip.MacAddress
p[Attr_NetworkID] = pvmip.NetworkID
p[Attr_NetworkName] = pvmip.NetworkName
p[Attr_Type] = pvmip.Type
networks[i] = p
}
return networks
}
return
}