/
data_source_ibm_cis_alert_webhooks.go
112 lines (100 loc) · 3.17 KB
/
data_source_ibm_cis_alert_webhooks.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package cis
import (
"fmt"
"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/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const cisWebhookList = "cis_webhooks"
func DataSourceIBMCISWebhooks() *schema.Resource {
return &schema.Resource{
Read: dataIBMCISWebhookRead,
Schema: map[string]*schema.Schema{
cisID: {
Type: schema.TypeString,
Description: "CIS instance crn",
Required: true,
ValidateFunc: validate.InvokeDataSourceValidator(
"ibm_cis_webhooks",
"cis_id"),
},
cisWebhookList: {
Type: schema.TypeList,
Description: "Collection of Webhook details",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
cisWebhookID: {
Type: schema.TypeString,
Computed: true,
Description: "Webhook ID",
},
cisWebhookName: {
Type: schema.TypeString,
Computed: true,
Description: "Webhook Name",
},
cisWebhookURL: {
Type: schema.TypeString,
Computed: true,
Description: "Webhook URL",
},
cisWebhookType: {
Type: schema.TypeString,
Computed: true,
Description: "Webhook Type",
},
},
},
},
},
}
}
func DataSourceIBMCISAlertWebhooksValidator() *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})
iBMCISAlertWebhooksValidator := validate.ResourceValidator{
ResourceName: "ibm_cis_webhooks",
Schema: validateSchema}
return &iBMCISAlertWebhooksValidator
}
func dataIBMCISWebhookRead(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(conns.ClientSession).CisWebhookSession()
if err != nil {
return fmt.Errorf("[ERROR] Error while getting the cisWebhookession %s", err)
}
crn := d.Get(cisID).(string)
sess.Crn = core.StringPtr(crn)
opt := sess.NewListWebhooksOptions()
result, resp, err := sess.ListWebhooks(opt)
if err != nil || result == nil {
return fmt.Errorf("[ERROR] Error Listing all Webhooks %q: %s %s", d.Id(), err, resp)
}
webhooks := make([]map[string]interface{}, 0)
for _, instance := range result.Result {
webhook := map[string]interface{}{}
webhook[cisWebhookID] = *instance.ID
webhook[cisWebhookName] = *instance.Name
webhook[cisWebhookURL] = *instance.URL
webhook[cisWebhookType] = *instance.Type
webhooks = append(webhooks, webhook)
}
d.SetId(dataSourcecisWebhookCheckID(d))
d.Set(cisID, crn)
d.Set(cisWebhookList, webhooks)
return nil
}
func dataSourcecisWebhookCheckID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}