|
| 1 | +package fwprovider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/DataDog/datadog-api-client-go/v2/api/datadogV2" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | + |
| 13 | + "github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + _ resource.ResourceWithConfigure = &csmThreatsPoliciesListResource{} |
| 18 | + _ resource.ResourceWithImportState = &csmThreatsPoliciesListResource{} |
| 19 | +) |
| 20 | + |
| 21 | +type csmThreatsPoliciesListResource struct { |
| 22 | + api *datadogV2.CSMThreatsApi |
| 23 | + auth context.Context |
| 24 | +} |
| 25 | + |
| 26 | +type csmThreatsPoliciesListModel struct { |
| 27 | + ID types.String `tfsdk:"id"` |
| 28 | + Entries []csmThreatsPoliciesListEntryModel `tfsdk:"entries"` |
| 29 | +} |
| 30 | + |
| 31 | +type csmThreatsPoliciesListEntryModel struct { |
| 32 | + PolicyID types.String `tfsdk:"policy_id"` |
| 33 | + Name types.String `tfsdk:"name"` |
| 34 | + Priority types.Int64 `tfsdk:"priority"` |
| 35 | +} |
| 36 | + |
| 37 | +func NewCSMThreatsPoliciesListResource() resource.Resource { |
| 38 | + return &csmThreatsPoliciesListResource{} |
| 39 | +} |
| 40 | + |
| 41 | +func (r *csmThreatsPoliciesListResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) { |
| 42 | + response.TypeName = "csm_threats_policies_list" |
| 43 | +} |
| 44 | + |
| 45 | +func (r *csmThreatsPoliciesListResource) Configure(_ context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) { |
| 46 | + providerData := request.ProviderData.(*FrameworkProvider) |
| 47 | + r.api = providerData.DatadogApiInstances.GetCSMThreatsApiV2() |
| 48 | + r.auth = providerData.Auth |
| 49 | +} |
| 50 | + |
| 51 | +func (r *csmThreatsPoliciesListResource) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) { |
| 52 | + resource.ImportStatePassthroughID(ctx, path.Root("id"), request, response) |
| 53 | +} |
| 54 | + |
| 55 | +func (r *csmThreatsPoliciesListResource) Schema(_ context.Context, _ resource.SchemaRequest, response *resource.SchemaResponse) { |
| 56 | + response.Schema = schema.Schema{ |
| 57 | + Description: "Provides a Datadog CSM Threats policies API resource.", |
| 58 | + Attributes: map[string]schema.Attribute{ |
| 59 | + "id": utils.ResourceIDAttribute(), |
| 60 | + }, |
| 61 | + Blocks: map[string]schema.Block{ |
| 62 | + "entries": schema.SetNestedBlock{ |
| 63 | + Description: "A set of policies that belong to this list/batch. All non-listed policies get deleted.", |
| 64 | + NestedObject: schema.NestedBlockObject{ |
| 65 | + Attributes: map[string]schema.Attribute{ |
| 66 | + "policy_id": schema.StringAttribute{ |
| 67 | + Description: "The ID of the policy to manage (from `csm_threats_policy`).", |
| 68 | + Required: true, |
| 69 | + }, |
| 70 | + "priority": schema.Int64Attribute{ |
| 71 | + Description: "The priority of the policy in this list.", |
| 72 | + Required: true, |
| 73 | + }, |
| 74 | + "name": schema.StringAttribute{ |
| 75 | + Description: "Optional name. If omitted, fallback to the policy_id as name.", |
| 76 | + Optional: true, |
| 77 | + Computed: true, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func (r *csmThreatsPoliciesListResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) { |
| 87 | + var plan csmThreatsPoliciesListModel |
| 88 | + response.Diagnostics.Append(request.Plan.Get(ctx, &plan)...) |
| 89 | + if response.Diagnostics.HasError() { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + plan.ID = types.StringValue("policies_list") |
| 94 | + |
| 95 | + updatedEntries, err := r.applyBatchPolicies(ctx, plan.Entries, &response.Diagnostics) |
| 96 | + if err != nil { |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + plan.Entries = updatedEntries |
| 101 | + response.Diagnostics.Append(response.State.Set(ctx, &plan)...) |
| 102 | +} |
| 103 | + |
| 104 | +func (r *csmThreatsPoliciesListResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) { |
| 105 | + var state csmThreatsPoliciesListModel |
| 106 | + response.Diagnostics.Append(request.State.Get(ctx, &state)...) |
| 107 | + if response.Diagnostics.HasError() { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + if state.ID.IsUnknown() || state.ID.IsNull() || state.ID.ValueString() == "" { |
| 112 | + response.State.RemoveResource(ctx) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + listResponse, httpResp, err := r.api.ListCSMThreatsAgentPolicies(r.auth) |
| 117 | + if err != nil { |
| 118 | + if httpResp != nil && httpResp.StatusCode == 404 { |
| 119 | + response.State.RemoveResource(ctx) |
| 120 | + return |
| 121 | + } |
| 122 | + response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error while fetching agent policies")) |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + newEntries := make([]csmThreatsPoliciesListEntryModel, 0) |
| 127 | + for _, policyData := range listResponse.GetData() { |
| 128 | + policyID := policyData.GetId() |
| 129 | + if policyID == "CWS_DD" { |
| 130 | + continue |
| 131 | + } |
| 132 | + attributes := policyData.Attributes |
| 133 | + |
| 134 | + name := attributes.GetName() |
| 135 | + priorirty := attributes.GetPriority() |
| 136 | + |
| 137 | + entry := csmThreatsPoliciesListEntryModel{ |
| 138 | + PolicyID: types.StringValue(policyID), |
| 139 | + Name: types.StringValue(name), |
| 140 | + Priority: types.Int64Value(int64(priorirty)), |
| 141 | + } |
| 142 | + newEntries = append(newEntries, entry) |
| 143 | + } |
| 144 | + |
| 145 | + state.Entries = newEntries |
| 146 | + response.Diagnostics.Append(response.State.Set(ctx, &state)...) |
| 147 | +} |
| 148 | + |
| 149 | +func (r *csmThreatsPoliciesListResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) { |
| 150 | + var plan csmThreatsPoliciesListModel |
| 151 | + response.Diagnostics.Append(request.Plan.Get(ctx, &plan)...) |
| 152 | + if response.Diagnostics.HasError() { |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + updatedEntries, err := r.applyBatchPolicies(ctx, plan.Entries, &response.Diagnostics) |
| 157 | + if err != nil { |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + plan.Entries = updatedEntries |
| 162 | + response.Diagnostics.Append(response.State.Set(ctx, &plan)...) |
| 163 | +} |
| 164 | + |
| 165 | +func (r *csmThreatsPoliciesListResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) { |
| 166 | + _, err := r.applyBatchPolicies(ctx, []csmThreatsPoliciesListEntryModel{}, &response.Diagnostics) |
| 167 | + if err != nil { |
| 168 | + return |
| 169 | + } |
| 170 | + response.State.RemoveResource(ctx) |
| 171 | +} |
| 172 | + |
| 173 | +func (r *csmThreatsPoliciesListResource) applyBatchPolicies(ctx context.Context, entries []csmThreatsPoliciesListEntryModel, diags *diag.Diagnostics) ([]csmThreatsPoliciesListEntryModel, error) { |
| 174 | + listResp, httpResp, err := r.api.ListCSMThreatsAgentPolicies(r.auth) |
| 175 | + if err != nil { |
| 176 | + if httpResp != nil && httpResp.StatusCode == 404 { |
| 177 | + diags.Append(utils.FrameworkErrorDiag(err, "error while fetching agent policies")) |
| 178 | + return nil, err |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + existingPolicies := make(map[string]struct{}) |
| 183 | + for _, policy := range listResp.GetData() { |
| 184 | + if policy.GetId() == "CWS_DD" { |
| 185 | + continue |
| 186 | + } |
| 187 | + existingPolicies[policy.GetId()] = struct{}{} |
| 188 | + } |
| 189 | + |
| 190 | + var batchItems []datadogV2.CloudWorkloadSecurityAgentPolicyBatchUpdateAttributesPoliciesItems |
| 191 | + |
| 192 | + for i := range entries { |
| 193 | + policyID := entries[i].PolicyID.ValueString() |
| 194 | + name := entries[i].Name.ValueString() |
| 195 | + |
| 196 | + if name == "" { |
| 197 | + name = policyID |
| 198 | + entries[i].Name = types.StringValue(name) |
| 199 | + } |
| 200 | + priority := entries[i].Priority.ValueInt64() |
| 201 | + |
| 202 | + item := datadogV2.CloudWorkloadSecurityAgentPolicyBatchUpdateAttributesPoliciesItems{ |
| 203 | + Id: &policyID, |
| 204 | + Name: &name, |
| 205 | + Priority: &priority, |
| 206 | + } |
| 207 | + |
| 208 | + batchItems = append(batchItems, item) |
| 209 | + delete(existingPolicies, policyID) |
| 210 | + } |
| 211 | + |
| 212 | + for policyID := range existingPolicies { |
| 213 | + DeleteTrue := true |
| 214 | + item := datadogV2.CloudWorkloadSecurityAgentPolicyBatchUpdateAttributesPoliciesItems{ |
| 215 | + Id: &policyID, |
| 216 | + Delete: &DeleteTrue, |
| 217 | + } |
| 218 | + batchItems = append(batchItems, item) |
| 219 | + } |
| 220 | + |
| 221 | + patchID := "batch_update_req" |
| 222 | + typ := datadogV2.CLOUDWORKLOADSECURITYAGENTPOLICYBATCHUPDATEDATATYPE_POLICIES |
| 223 | + attributes := datadogV2.NewCloudWorkloadSecurityAgentPolicyBatchUpdateAttributes() |
| 224 | + attributes.SetPolicies(batchItems) |
| 225 | + data := datadogV2.NewCloudWorkloadSecurityAgentPolicyBatchUpdateData(*attributes, patchID, typ) |
| 226 | + batchReq := datadogV2.NewCloudWorkloadSecurityAgentPolicyBatchUpdateRequest(*data) |
| 227 | + |
| 228 | + response, _, err := r.api.BatchUpdateCSMThreatsAgentPolicy( |
| 229 | + r.auth, |
| 230 | + *batchReq, |
| 231 | + ) |
| 232 | + if err != nil { |
| 233 | + diags.Append(utils.FrameworkErrorDiag(err, "error while applying batch policies")) |
| 234 | + return nil, err |
| 235 | + } |
| 236 | + |
| 237 | + finalEntries := make([]csmThreatsPoliciesListEntryModel, 0) |
| 238 | + for _, policy := range response.GetData() { |
| 239 | + policyID := policy.GetId() |
| 240 | + attributes := policy.Attributes |
| 241 | + |
| 242 | + name := "" |
| 243 | + if attributes.GetName() == "" { |
| 244 | + name = policyID |
| 245 | + } |
| 246 | + name = attributes.GetName() |
| 247 | + priority := attributes.GetPriority() |
| 248 | + |
| 249 | + entry := csmThreatsPoliciesListEntryModel{ |
| 250 | + PolicyID: types.StringValue(policyID), |
| 251 | + Name: types.StringValue(name), |
| 252 | + Priority: types.Int64Value(int64(priority)), |
| 253 | + } |
| 254 | + finalEntries = append(finalEntries, entry) |
| 255 | + } |
| 256 | + |
| 257 | + return finalEntries, nil |
| 258 | +} |
0 commit comments