-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_cis_alerts.go
191 lines (174 loc) · 5.42 KB
/
data_source_ibm_cis_alerts.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package cis
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/networking-go-sdk/alertsv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
cisAlerts = "alert_policies"
)
func DataSourceIBMCISAlert() *schema.Resource {
return &schema.Resource{
Read: dataIBMCISAlertPolicyRead,
Schema: map[string]*schema.Schema{
cisID: {
Type: schema.TypeString,
Description: "CIS instance crn",
Required: true,
ValidateFunc: validate.InvokeDataSourceValidator(
"ibm_cis_alerts",
"cis_id"),
},
cisAlerts: {
Type: schema.TypeList,
Computed: true,
Description: "Container for response information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
cisAlertID: {
Type: schema.TypeString,
Computed: true,
Description: "Policy ID",
},
cisAlertName: {
Type: schema.TypeString,
Computed: true,
Description: "Policy name",
},
cisAlertDescription: {
Type: schema.TypeString,
Computed: true,
Description: "Policy Description",
},
cisAlertEnabled: {
Type: schema.TypeBool,
Computed: true,
Description: "Is the alert policy active",
},
cisAlertType: {
Type: schema.TypeString,
Computed: true,
Description: "Condition for the alert",
},
cisAlertMechanisms: {
Type: schema.TypeList,
Computed: true,
Description: "Delivery mechanisms for the alert, can include an email, a webhook, or both.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
cisAlertEmail: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
},
cisAlertWebhook: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
},
},
},
},
cisAlertFilters: {
Type: schema.TypeString,
Computed: true,
Description: "Filters based on filter type",
},
cisAlertConditions: {
Type: schema.TypeString,
Computed: true,
Description: "Conditions based on filter type",
},
},
},
},
},
}
}
func DataSourceIBMCISAlertsValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "cis_id",
ValidateFunctionIdentifier: validate.ValidateCloudData,
Type: validate.TypeString,
CloudDataType: "resource_instance",
CloudDataRange: []string{"service:internet-svcs"},
Required: true})
iBMCISAlertsValidator := validate.ResourceValidator{
ResourceName: "ibm_cis_alerts",
Schema: validateSchema}
return &iBMCISAlertsValidator
}
func dataIBMCISAlertPolicyRead(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(conns.ClientSession).CisAlertsSession()
if err != nil {
return err
}
crn := d.Get(cisID).(string)
sess.Crn = core.StringPtr(crn)
opt := sess.NewGetAlertPoliciesOptions()
result, resp, err := sess.GetAlertPolicies(opt)
if err != nil {
log.Printf("[WARN] List all alerts failed: %v\n", resp)
return err
}
alertList := make([]map[string]interface{}, 0)
for _, alertObj := range result.Result {
alertOutput := map[string]interface{}{}
alertOutput[cisAlertID] = *alertObj.ID
alertOutput[cisAlertName] = *alertObj.Name
alertOutput[cisAlertDescription] = *alertObj.Description
alertOutput[cisAlertEnabled] = *alertObj.Enabled
alertOutput[cisAlertType] = *alertObj.AlertType
filterOpt, err := json.Marshal(alertObj.Filters)
if err != nil {
return fmt.Errorf("[ERROR] Error marshalling the created filters: %s", err)
}
alertOutput[cisAlertFilters] = string(filterOpt)
conditionsOpt, err := json.Marshal(alertObj.Conditions)
if err != nil {
return fmt.Errorf("[ERROR] Error marshalling the created Conditions: %s", err)
}
alertOutput[cisAlertConditions] = string(conditionsOpt)
alertOutput[cisAlertMechanisms] = dataflattenCISMechanism(*alertObj.Mechanisms)
alertList = append(alertList, alertOutput)
}
d.SetId(dataSourceCISAlertsCheckID(d))
d.Set(cisID, crn)
d.Set(cisAlerts, alertList)
return nil
}
func dataSourceCISAlertsCheckID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataflattenCISMechanism(Mechanism alertsv1.ListAlertPoliciesRespResultItemMechanisms) interface{} {
emailoutput := []string{}
webhookoutput := []string{}
output := map[string]interface{}{}
flatten := []map[string]interface{}{}
for _, mech := range Mechanism.Email {
emailoutput = append(emailoutput, *mech.ID)
}
for _, mech := range Mechanism.Webhooks {
webhookoutput = append(webhookoutput, *mech.ID)
}
output[cisAlertEmail] = emailoutput
output[cisAlertWebhook] = webhookoutput
flatten = append(flatten, output)
return flatten
}