/
data_source_ibm_cis_waf_rules.go
155 lines (144 loc) · 4.14 KB
/
data_source_ibm_cis_waf_rules.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
package ibm
import (
"log"
"time"
"github.com/IBM/go-sdk-core/v3/core"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
const cisWAFRules = "waf_rules"
func dataSourceIBMCISWAFRules() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMCISWAFRuleRead,
Schema: map[string]*schema.Schema{
cisID: {
Type: schema.TypeString,
Description: "CIS object id",
Required: true,
},
cisDomainID: {
Type: schema.TypeString,
Description: "CISzone - Domain",
Required: true,
DiffSuppressFunc: suppressDomainIDDiff,
},
cisWAFRulePackageID: {
Type: schema.TypeString,
Description: "WAF rule package id",
Required: true,
DiffSuppressFunc: suppressDomainIDDiff,
},
cisWAFRules: {
Type: schema.TypeList,
Description: "Collection of WAF Rules",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "WAF Rule id",
},
cisWAFRuleID: {
Type: schema.TypeString,
Computed: true,
Description: "WAF Rule id",
},
cisWAFRulePackageID: {
Type: schema.TypeString,
Computed: true,
Description: "WAF Package id",
},
cisWAFRuleMode: {
Type: schema.TypeString,
Computed: true,
Description: "CIS WAF Rule mode",
},
cisWAFRuleDesc: {
Type: schema.TypeString,
Computed: true,
Description: "CIS WAF Rule descriptions",
},
cisWAFRulePriority: {
Type: schema.TypeString,
Computed: true,
Description: "CIS WAF Rule Priority",
},
cisWAFRuleGroup: {
Type: schema.TypeList,
Computed: true,
Description: "CIS WAF Rule group",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
cisWAFRuleGroupID: {
Type: schema.TypeString,
Computed: true,
Description: "waf rule group id",
},
cisWAFRuleGroupName: {
Type: schema.TypeString,
Computed: true,
Description: "waf rule group name",
},
},
},
},
cisWAFRuleAllowedModes: {
Type: schema.TypeList,
Computed: true,
Description: "CIS WAF Rule allowed modes",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}
func dataSourceIBMCISWAFRuleRead(d *schema.ResourceData, meta interface{}) error {
cisClient, err := meta.(ClientSession).CisWAFRuleClientSession()
if err != nil {
return err
}
crn := d.Get(cisID).(string)
zoneID, _, err := convertTftoCisTwoVar(d.Get(cisDomainID).(string))
cisClient.Crn = core.StringPtr(crn)
cisClient.ZoneID = core.StringPtr(zoneID)
packageID, _, _, _ := convertTfToCisThreeVar(d.Get(cisWAFRulePackageID).(string))
opt := cisClient.NewListWafRulesOptions(packageID)
opt.SetPage(1)
opt.SetPerPage(1000)
result, response, err := cisClient.ListWafRules(opt)
if err != nil {
log.Printf("List waf rules failed %s\n", response)
return err
}
rules := []interface{}{}
for _, i := range result.Result {
groups := []interface{}{}
group := map[string]interface{}{}
group[cisWAFRuleGroupID] = *i.Group.ID
group[cisWAFRuleGroupName] = *i.Group.Name
groups = append(groups, group)
rule := map[string]interface{}{}
rule["id"] = convertCisToTfFourVar(*i.ID, *i.PackageID, zoneID, crn)
rule[cisWAFRuleID] = *i.ID
rule[cisWAFRulePackageID] = *i.PackageID
rule[cisWAFRuleMode] = *i.Mode
rule[cisWAFRuleDesc] = *i.Description
rule[cisWAFRulePriority] = *i.Priority
rule[cisWAFRuleGroup] = groups
rule[cisWAFRuleAllowedModes] = flattenStringList(i.AllowedModes)
rules = append(rules, rule)
}
d.SetId(dataSourceIBMCISWAFRulesID(d))
d.Set(cisID, crn)
d.Set(cisDomainID, zoneID)
d.Set(cisWAFRulePackageID, packageID)
d.Set(cisWAFRules, rules)
return nil
}
func dataSourceIBMCISWAFRulesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}