-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathresource_datadog_cloud_workload_security_agent_rule.go
177 lines (140 loc) · 6.34 KB
/
resource_datadog_cloud_workload_security_agent_rule.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
package datadog
import (
"context"
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceDatadogCloudWorkloadSecurityAgentRule() *schema.Resource {
return &schema.Resource{
Description: "Provides a Datadog Cloud Workload Security Agent Rule API resource for agent rules. Deprecated, use datadog_csm_threats_agent_rules resource instead: https://registry.terraform.io/providers/DataDog/datadog/latest/docs/resources/csm_threats_agent_rule",
DeprecationMessage: "This resource is deprecated — use the datadog_csm_threats_agent_rules resource instead: https://registry.terraform.io/providers/DataDog/datadog/latest/docs/resources/csm_threats_agent_rule",
CreateContext: resourceDatadogCloudWorkloadSecurityAgentRuleCreate,
ReadContext: resourceDatadogCloudWorkloadSecurityAgentRuleRead,
UpdateContext: resourceDatadogCloudWorkloadSecurityAgentRuleUpdate,
DeleteContext: resourceDatadogCloudWorkloadSecurityAgentRuleDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
SchemaFunc: func() map[string]*schema.Schema {
return cloudWorkloadSecurityAgentRuleSchema()
},
}
}
func cloudWorkloadSecurityAgentRuleSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The description of the Agent rule.",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether the Agent rule is enabled.",
},
"expression": {
Type: schema.TypeString,
Required: true,
Description: "The SECL expression of the Agent rule.",
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the Agent rule.",
},
}
}
func resourceDatadogCloudWorkloadSecurityAgentRuleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
agentRuleCreate := buildCwsAgentRuleCreatePayload(d)
response, httpResponse, err := apiInstances.GetCSMThreatsApiV2().CreateCloudWorkloadSecurityAgentRule(auth, *agentRuleCreate)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpResponse, "error creating cloud workload security agent rule")
}
if err := utils.CheckForUnparsed(response); err != nil {
return diag.FromErr(err)
}
return updateCloudWorkloadSecurityAgentRuleState(d, &response)
}
func resourceDatadogCloudWorkloadSecurityAgentRuleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
id := d.Id()
agentRuleResponse, httpResponse, err := apiInstances.GetCSMThreatsApiV2().GetCloudWorkloadSecurityAgentRule(auth, id)
if err != nil {
if httpResponse != nil && httpResponse.StatusCode == 404 {
d.SetId("")
return nil
}
return utils.TranslateClientErrorDiag(err, httpResponse, "error fetching cloud workload security agent rule")
}
if err := utils.CheckForUnparsed(agentRuleResponse); err != nil {
return diag.FromErr(err)
}
return updateCloudWorkloadSecurityAgentRuleState(d, &agentRuleResponse)
}
func resourceDatadogCloudWorkloadSecurityAgentRuleUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
agentRuleId := d.Id()
agentRuleUpdate := buildCwsAgentRuleUpdatePayload(d)
agentRuleResponse, httpResponse, err := apiInstances.GetCSMThreatsApiV2().UpdateCloudWorkloadSecurityAgentRule(auth, agentRuleId, *agentRuleUpdate)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpResponse, "error updating cloud workload security agent rule")
}
return updateCloudWorkloadSecurityAgentRuleState(d, &agentRuleResponse)
}
func resourceDatadogCloudWorkloadSecurityAgentRuleDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
agentRuleId := d.Id()
if httpResponse, err := apiInstances.GetCSMThreatsApiV2().DeleteCloudWorkloadSecurityAgentRule(auth, agentRuleId); err != nil {
return utils.TranslateClientErrorDiag(err, httpResponse, "error deleting cloud workload security agent rule")
}
return nil
}
func updateCloudWorkloadSecurityAgentRuleState(d *schema.ResourceData, agentRuleResponse *datadogV2.CloudWorkloadSecurityAgentRuleResponse) diag.Diagnostics {
data := agentRuleResponse.GetData()
d.SetId(data.GetId())
attributes := data.GetAttributes()
d.Set("description", attributes.GetDescription())
d.Set("enabled", attributes.GetEnabled())
d.Set("expression", attributes.GetExpression())
d.Set("name", attributes.GetName())
return nil
}
func buildCwsAgentRuleUpdatePayload(d *schema.ResourceData) *datadogV2.CloudWorkloadSecurityAgentRuleUpdateRequest {
payload := datadogV2.CloudWorkloadSecurityAgentRuleUpdateRequest{}
payload.Data.Type = datadogV2.CLOUDWORKLOADSECURITYAGENTRULETYPE_AGENT_RULE
if attr, ok := d.GetOk("description"); ok {
payload.Data.Attributes.SetDescription(attr.(string))
}
if attr, ok := d.GetOk("expression"); ok {
payload.Data.Attributes.SetExpression(attr.(string))
}
payload.Data.Attributes.SetEnabled(d.Get("enabled").(bool))
return &payload
}
func buildCwsAgentRuleCreatePayload(d *schema.ResourceData) *datadogV2.CloudWorkloadSecurityAgentRuleCreateRequest {
payload := datadogV2.CloudWorkloadSecurityAgentRuleCreateRequest{}
payload.Data.Type = datadogV2.CLOUDWORKLOADSECURITYAGENTRULETYPE_AGENT_RULE
payload.Data.Attributes.SetExpression(d.Get("expression").(string))
payload.Data.Attributes.SetName(d.Get("name").(string))
if attr, ok := d.GetOk("description"); ok {
payload.Data.Attributes.SetDescription(attr.(string))
}
if attr, ok := d.GetOk("enabled"); ok {
payload.Data.Attributes.SetEnabled(attr.(bool))
}
return &payload
}