-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_rule_test.go
93 lines (75 loc) · 2.25 KB
/
resource_rule_test.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
package main
import (
"fmt"
"math/rand"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/jfcantu/threatstack-golang/threatstack"
)
type testRuleData struct {
Name string
Title string
Description string
Type string
Severity int
AggregateFields []string
Filter string
Window int
Threshold int
Suppressions []string
Enabled bool
}
func initializeTestRuleData() *testRuleData {
d := new(testRuleData)
d.Name = acctest.RandomWithPrefix("tf")
d.Title = acctest.RandomWithPrefix("tf")
d.Description = acctest.RandomWithPrefix("tf")
d.Severity = (rand.Intn(3-1) + 1)
d.Filter = acctest.RandomWithPrefix("tf")
d.Window = getValidRuleWindows()[rand.Intn(len(getValidRuleWindows()))]
d.Threshold = getValidRuleThresholds()[rand.Intn(len(getValidRuleThresholds()))]
return d
}
func testAccCheckThreatstackRuleExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
cli := testAccProvider.Meta().(*threatstack.Client)
res, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
if res.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}
if _, err := cli.Rules.Get(res.Primary.Attributes["ruleset"], res.Primary.ID); err != nil {
return fmt.Errorf("Error retrieving rule: %s", err.Error())
}
return nil
}
}
func testAccCheckThreatstackRuleDestroyed(s *terraform.State) error {
cli := testAccProvider.Meta().(*threatstack.Client)
for _, res := range s.RootModule().Resources {
if res.Type != "threatstack_ruleset" {
continue
}
_, err := cli.Rulesets.Get(res.Primary.ID)
if err == nil {
return fmt.Errorf("Ruleset %s still exists", res.Primary.ID)
}
if !strings.Contains(err.Error(), "404") {
return err
}
}
return nil
}
func testAccThreatstackRuleTestRuleset() string {
return fmt.Sprintf(
`
resource "threatstack_ruleset" "test" {
name = "%s"
description = "Ruleset to hold rules being tested"
}
`, fmt.Sprintf("tf%s", acctest.RandString(5)))
}