-
Notifications
You must be signed in to change notification settings - Fork 720
Expand file tree
/
Copy pathdata_source_ibm_en_metrics.go
More file actions
235 lines (218 loc) · 7.23 KB
/
data_source_ibm_en_metrics.go
File metadata and controls
235 lines (218 loc) · 7.23 KB
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
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package eventnotification
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/event-notifications-go-admin-sdk/eventnotificationsv1"
)
func DataSourceIBMEnMetrics() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMEnMetricsRead,
Schema: map[string]*schema.Schema{
"instance_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Unique identifier for IBM Cloud Event Notifications instance.",
},
"destination_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Destination type. Allowed values are [smtp_custom].",
},
"gte": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "GTE (greater than equal), start timestamp in UTC.",
},
"lte": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "LTE (less than equal), end timestamp in UTC.",
},
"destination_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Unique identifier for Destination.",
},
"subscription_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Unique identifier for Subscription.",
},
"source_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Unique identifier for Source.",
},
"email_to": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Receiver email id.",
},
"notification_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Notification Id.",
},
"subject": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Email subject.",
},
"metrics": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "array of metrics.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "key.",
},
"doc_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "doc count.",
},
"histogram": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Payload describing histogram.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"buckets": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "List of buckets.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"doc_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "Total count.",
},
"key_as_string": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Timestamp.",
},
},
},
},
},
},
},
},
},
},
},
}
}
func dataSourceIBMEnMetricsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
eventNotificationsClient, err := meta.(conns.ClientSession).EventNotificationsApiV1()
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_en_metrics", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
getMetricsOptions := &eventnotificationsv1.GetMetricsOptions{}
getMetricsOptions.SetInstanceID(d.Get("instance_id").(string))
getMetricsOptions.SetDestinationType(d.Get("destination_type").(string))
getMetricsOptions.SetGte(d.Get("gte").(string))
getMetricsOptions.SetLte(d.Get("lte").(string))
if _, ok := d.GetOk("destination_id"); ok {
getMetricsOptions.SetDestinationID(d.Get("destination_id").(string))
}
if _, ok := d.GetOk("subscription_id"); ok {
getMetricsOptions.SetSubscriptionID(d.Get("subscription_id").(string))
}
if _, ok := d.GetOk("source_id"); ok {
getMetricsOptions.SetSourceID(d.Get("source_id").(string))
}
if _, ok := d.GetOk("email_to"); ok {
getMetricsOptions.SetEmailTo(d.Get("email_to").(string))
}
if _, ok := d.GetOk("notification_id"); ok {
getMetricsOptions.SetNotificationID(d.Get("notification_id").(string))
}
if _, ok := d.GetOk("subject"); ok {
getMetricsOptions.SetSubject(d.Get("subject").(string))
}
metricsres, _, err := eventNotificationsClient.GetMetricsWithContext(context, getMetricsOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetMetricsWithContext failed: %s", err.Error()), "(Data) ibm_en_metrics", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
d.SetId(dataSourceIBMEnMetricsID(d))
metrics := []map[string]interface{}{}
if metricsres.Metrics != nil {
for _, modelItem := range metricsres.Metrics {
modelMap, err := dataSourceIBMEnMetricsMetricToMap(&modelItem)
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_en_metrics", "read")
return tfErr.GetDiag()
}
metrics = append(metrics, modelMap)
}
}
if err = d.Set("metrics", metrics); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting metrics: %s", err), "(Data) ibm_en_metrics", "read")
return tfErr.GetDiag()
}
return nil
}
// dataSourceIBMEnMetricsID returns a reasonable ID for the list.
func dataSourceIBMEnMetricsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceIBMEnMetricsMetricToMap(model *eventnotificationsv1.Metric) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Key != nil {
modelMap["key"] = model.Key
}
if model.DocCount != nil {
modelMap["doc_count"] = flex.IntValue(model.DocCount)
}
if model.Histogram != nil {
histogramMap, err := dataSourceIBMEnMetricsHistrogramToMap(model.Histogram)
if err != nil {
return modelMap, err
}
modelMap["histogram"] = []map[string]interface{}{histogramMap}
}
return modelMap, nil
}
func dataSourceIBMEnMetricsHistrogramToMap(model *eventnotificationsv1.Histrogram) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Buckets != nil {
buckets := []map[string]interface{}{}
for _, bucketsItem := range model.Buckets {
bucketsItemMap, err := dataSourceIBMEnMetricsBucketsToMap(&bucketsItem)
if err != nil {
return modelMap, err
}
buckets = append(buckets, bucketsItemMap)
}
modelMap["buckets"] = buckets
}
return modelMap, nil
}
func dataSourceIBMEnMetricsBucketsToMap(model *eventnotificationsv1.Buckets) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.DocCount != nil {
modelMap["doc_count"] = flex.IntValue(model.DocCount)
}
if model.KeyAsString != nil {
modelMap["key_as_string"] = model.KeyAsString.String()
}
return modelMap, nil
}