-
Notifications
You must be signed in to change notification settings - Fork 2
/
trusted-sources.go
200 lines (162 loc) · 6.65 KB
/
trusted-sources.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
192
193
194
195
196
197
198
199
200
package resources
import (
"context"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/api"
trustedsources "github.com/CheckPointSW/terraform-provider-infinity-next/internal/resources/trusted-sources"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceTrustedSources() *schema.Resource {
return &schema.Resource{
Description: "Trusted sources that serve as a baseline for comparison for benign behavior, " +
"and how many users or addresses must exhibit similar activity for it to be considered bengin by the learning model",
CreateContext: resourceTrustedSourcesCreate,
ReadContext: resourceTrustedSourcesRead,
UpdateContext: resourceTrustedSourcesUpdate,
DeleteContext: resourceTrustedSourcesDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
CustomizeDiff: func(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
if diff.HasChange("sources_identifiers") {
return diff.SetNewComputed("sources_identifiers_ids")
}
return nil
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "The name of the resource, also acts as it's unique ID",
Required: true,
},
"min_num_of_sources": {
Type: schema.TypeInt,
Description: "Minimum number of users or addresses that must exhibit similar activity for the behavior to be considered benign",
Required: true,
},
"sources_identifiers": {
Type: schema.TypeSet,
Description: "The trusted sources identifier values",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"sources_identifiers_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func resourceTrustedSourcesCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
createInput, err := trustedsources.CreateTrustedSourceBehaviorInputFromResourceData(d)
if err != nil {
return utils.DiagError("unable to perform TrustedSourceBehavior Create", err, diags)
}
behavior, err := trustedsources.NewTrustedSourceBehavior(c, createInput)
if err != nil {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("unable to perform TrustedSourceBehavior Create", err, diags)
}
isValid, err := c.PublishChanges()
if err != nil || !isValid {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("failed to Publish following TrustedSourceBehavior Create", err, diags)
}
if err := trustedsources.ReadTrustedSourceBehaviorToResourceData(behavior, d); err != nil {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("Unable to perform TrustedSourceBehavior Read after Create", err, diags)
}
return diags
}
func resourceTrustedSourcesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
behavior, err := trustedsources.GetTrustedSourceBehavior(c, d.Id())
if err != nil {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("Unable to perform TrustedSourceBehavior Get before read", err, diags)
}
if err := trustedsources.ReadTrustedSourceBehaviorToResourceData(behavior, d); err != nil {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("Unable to perform TrustedSourceBehavior read to state file", err, diags)
}
return diags
}
func resourceTrustedSourcesUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
updateInput, err := trustedsources.UpdateTrustedSourceBehaviorInputFromResourceData(d)
if err != nil {
return utils.DiagError("unable to perform TrustedSourceBehavior Update", err, diags)
}
result, err := trustedsources.UpdateTrustedSourceBehavior(c, d.Id(), updateInput)
if err != nil || !result {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("unable to perform TrustedSourceBehavior Update", err, diags)
}
isValid, err := c.PublishChanges()
if err != nil || !isValid {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("failed to Publish following TrustedSourceBehavior Update", err, diags)
}
behavior, err := trustedsources.GetTrustedSourceBehavior(c, d.Id())
if err != nil {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("Unable to perform TrustedSourceBehavior Get before read after update", err, diags)
}
if err := trustedsources.ReadTrustedSourceBehaviorToResourceData(behavior, d); err != nil {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("Unable to perform TrustedSourceBehavior read to state file after update", err, diags)
}
return diags
}
func resourceTrustedSourcesDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
result, err := trustedsources.DeleteTrustedSourceBehavior(c, d.Id())
if err != nil || !result {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("unable to perform TrustedSourceBehavior Delete", err, diags)
}
isValid, err := c.PublishChanges()
if err != nil || !isValid {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("failed to Publish following TrustedSourceBehavior Delete", err, diags)
}
d.SetId("")
return diags
}