/
data_source_ibm_pi_cloud_instance.go
163 lines (151 loc) · 5.11 KB
/
data_source_ibm_pi_cloud_instance.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
// Copyright IBM Corp. 2017, 2021 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/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func DataSourceIBMPICloudInstance() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPICloudInstanceRead,
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_Capabilities: {
Computed: true,
Description: "Lists the capabilities for this cloud instance.",
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeList,
},
Attr_Enabled: {
Computed: true,
Description: "Indicates whether the tenant is enabled.",
Type: schema.TypeBool,
},
Attr_PVMInstances: {
Computed: true,
Description: "PVM instances owned by the Cloud Instance.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_CreationDate: {
Computed: true,
Description: "Date of PVM instance creation.",
Type: schema.TypeString,
},
Attr_Href: {
Computed: true,
Description: "Link to Cloud Instance resource.",
Type: schema.TypeString,
},
Attr_ID: {
Computed: true,
Description: "PVM Instance ID.",
Type: schema.TypeString,
},
Attr_Name: {
Computed: true,
Description: "Name of the server.",
Type: schema.TypeString,
},
Attr_Status: {
Computed: true,
Description: "The status of the instance.",
Type: schema.TypeString,
},
Attr_SysType: {
Computed: true,
Description: "System type used to host the instance.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
Attr_Region: {
Computed: true,
Description: "The region the cloud instance lives.",
Type: schema.TypeString,
},
Attr_TenantID: {
Computed: true,
Description: "The tenant ID that owns this cloud instance.",
Type: schema.TypeString,
},
Attr_TotalInstances: {
Computed: true,
Description: "The count of lpars that belong to this specific cloud instance.",
Type: schema.TypeFloat,
},
Attr_TotalMemoryConsumed: {
Computed: true,
Description: "The total memory consumed by this service instance.",
Type: schema.TypeFloat,
},
Attr_TotalProcessorsConsumed: {
Computed: true,
Description: "The total processors consumed by this service instance.",
Type: schema.TypeFloat,
},
Attr_TotalSSDStorageConsumed: {
Computed: true,
Description: "The total SSD Storage consumed by this service instance.",
Type: schema.TypeFloat,
},
Attr_TotalStandardStorageConsumed: {
Computed: true,
Description: "The total Standard Storage consumed by this service instance.",
Type: schema.TypeFloat,
},
},
}
}
func dataSourceIBMPICloudInstanceRead(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)
cloud_instance := instance.NewIBMPICloudInstanceClient(ctx, sess, cloudInstanceID)
cloud_instance_data, err := cloud_instance.Get(cloudInstanceID)
if err != nil {
return diag.FromErr(err)
}
d.SetId(*cloud_instance_data.CloudInstanceID)
d.Set(Attr_Capabilities, cloud_instance_data.Capabilities)
d.Set(Attr_Enabled, cloud_instance_data.Enabled)
d.Set(Attr_PVMInstances, flattenpvminstances(cloud_instance_data.PvmInstances))
d.Set(Attr_Region, cloud_instance_data.Region)
d.Set(Attr_TenantID, (cloud_instance_data.TenantID))
d.Set(Attr_TotalInstances, cloud_instance_data.Usage.Instances)
d.Set(Attr_TotalMemoryConsumed, cloud_instance_data.Usage.Memory)
d.Set(Attr_TotalProcessorsConsumed, cloud_instance_data.Usage.Processors)
d.Set(Attr_TotalSSDStorageConsumed, cloud_instance_data.Usage.StorageSSD)
d.Set(Attr_TotalStandardStorageConsumed, cloud_instance_data.Usage.StorageStandard)
return nil
}
func flattenpvminstances(list []*models.PVMInstanceReference) []map[string]interface{} {
pvms := make([]map[string]interface{}, 0)
for _, lpars := range list {
l := map[string]interface{}{
Attr_CreationDate: lpars.CreationDate.String(),
Attr_ID: *lpars.PvmInstanceID,
Attr_Href: *lpars.Href,
Attr_Name: *lpars.ServerName,
Attr_Status: *lpars.Status,
Attr_SysType: lpars.SysType,
}
pvms = append(pvms, l)
}
return pvms
}