-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_ruleset.go
99 lines (79 loc) · 2.11 KB
/
resource_ruleset.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
package main
import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/jfcantu/threatstack-golang/threatstack"
)
func resourceRuleset() *schema.Resource {
return &schema.Resource{
Create: resourceRulesetCreate,
Read: resourceRulesetRead,
Update: resourceRulesetUpdate,
Delete: resourceRulesetDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceRulesetCreate(resourceData *schema.ResourceData, meta interface{}) error {
client := meta.(*threatstack.Client)
name := resourceData.Get("name").(string)
desc := resourceData.Get("description").(string)
ruleset, err := client.Rulesets.Create(
&threatstack.Ruleset{
Name: name,
Description: desc,
RuleIDs: []string{},
})
if err != nil {
return err
}
resourceData.SetId(ruleset.ID)
return resourceRulesetRead(resourceData, meta)
}
func resourceRulesetRead(resourceData *schema.ResourceData, meta interface{}) error {
client := meta.(*threatstack.Client)
data, err := client.Rulesets.Get(resourceData.Id())
if err != nil {
return nil
}
resourceData.Set("name", data.Name)
resourceData.Set("description", data.Description)
return nil
}
func resourceRulesetUpdate(resourceData *schema.ResourceData, meta interface{}) error {
client := meta.(*threatstack.Client)
id := resourceData.Id()
name := resourceData.Get("name").(string)
desc := resourceData.Get("description").(string)
current, err := client.Rulesets.Get(id)
if err != nil {
return err
}
_, err = client.Rulesets.Update(
&threatstack.Ruleset{
ID: id,
Name: name,
Description: desc,
RuleIDs: current.RuleIDs,
})
if err != nil {
return nil
}
return resourceRulesetRead(resourceData, meta)
}
func resourceRulesetDelete(resourceData *schema.ResourceData, meta interface{}) error {
client := meta.(*threatstack.Client)
id := resourceData.Id()
err := client.Rulesets.Delete(id)
if err != nil {
return nil
}
return nil
}