|
| 1 | +package fwprovider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/DataDog/datadog-api-client-go/v2/api/datadogV2" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource/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 | + _ datasource.DataSourceWithConfigure = &csmThreatsPoliciesDataSource{} |
| 18 | +) |
| 19 | + |
| 20 | +type csmThreatsPoliciesDataSource struct { |
| 21 | + api *datadogV2.CSMThreatsApi |
| 22 | + auth context.Context |
| 23 | +} |
| 24 | + |
| 25 | +type csmThreatsPoliciesDataSourceModel struct { |
| 26 | + Id types.String `tfsdk:"id"` |
| 27 | + PolicyIds types.List `tfsdk:"policy_ids"` |
| 28 | + Policies []csmThreatsPolicyModel `tfsdk:"policies"` |
| 29 | +} |
| 30 | + |
| 31 | +func NewCSMThreatsPoliciesDataSource() datasource.DataSource { |
| 32 | + return &csmThreatsPoliciesDataSource{} |
| 33 | +} |
| 34 | + |
| 35 | +func (r *csmThreatsPoliciesDataSource) Configure(_ context.Context, request datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { |
| 36 | + providerData := request.ProviderData.(*FrameworkProvider) |
| 37 | + r.api = providerData.DatadogApiInstances.GetCSMThreatsApiV2() |
| 38 | + r.auth = providerData.Auth |
| 39 | +} |
| 40 | + |
| 41 | +func (*csmThreatsPoliciesDataSource) Metadata(_ context.Context, _ datasource.MetadataRequest, response *datasource.MetadataResponse) { |
| 42 | + response.TypeName = "csm_threats_policies" |
| 43 | +} |
| 44 | + |
| 45 | +func (r *csmThreatsPoliciesDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { |
| 46 | + var state csmThreatsPoliciesDataSourceModel |
| 47 | + response.Diagnostics.Append(request.Config.Get(ctx, &state)...) |
| 48 | + if response.Diagnostics.HasError() { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + res, _, err := r.api.ListCSMThreatsAgentPolicies(r.auth) |
| 53 | + if err != nil { |
| 54 | + response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error while fetching agent rules")) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + data := res.GetData() |
| 59 | + policyIds := make([]string, len(data)) |
| 60 | + policies := make([]csmThreatsPolicyModel, len(data)) |
| 61 | + |
| 62 | + for idx, policy := range res.GetData() { |
| 63 | + var policyModel csmThreatsPolicyModel |
| 64 | + policyModel.Id = types.StringValue(policy.GetId()) |
| 65 | + attributes := policy.Attributes |
| 66 | + policyModel.Name = types.StringValue(attributes.GetName()) |
| 67 | + policyModel.Description = types.StringValue(attributes.GetDescription()) |
| 68 | + policyModel.Enabled = types.BoolValue(attributes.GetEnabled()) |
| 69 | + policyModel.Tags, _ = types.SetValueFrom(ctx, types.StringType, attributes.GetHostTags()) |
| 70 | + policyIds[idx] = policy.GetId() |
| 71 | + policies[idx] = policyModel |
| 72 | + } |
| 73 | + |
| 74 | + stateId := strings.Join(policyIds, "--") |
| 75 | + state.Id = types.StringValue(computeDataSourceID(&stateId)) |
| 76 | + tfAgentRuleIds, diags := types.ListValueFrom(ctx, types.StringType, policyIds) |
| 77 | + response.Diagnostics.Append(diags...) |
| 78 | + state.PolicyIds = tfAgentRuleIds |
| 79 | + state.Policies = policies |
| 80 | + |
| 81 | + response.Diagnostics.Append(response.State.Set(ctx, &state)...) |
| 82 | +} |
| 83 | + |
| 84 | +func (*csmThreatsPoliciesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, response *datasource.SchemaResponse) { |
| 85 | + response.Schema = schema.Schema{ |
| 86 | + Description: "Use this data source to retrieve information about existing policies.", |
| 87 | + Attributes: map[string]schema.Attribute{ |
| 88 | + "id": utils.ResourceIDAttribute(), |
| 89 | + "policy_ids": schema.ListAttribute{ |
| 90 | + Computed: true, |
| 91 | + Description: "List of IDs for the policies.", |
| 92 | + ElementType: types.StringType, |
| 93 | + }, |
| 94 | + "policies": schema.ListAttribute{ |
| 95 | + Computed: true, |
| 96 | + Description: "List of policies", |
| 97 | + ElementType: types.ObjectType{ |
| 98 | + AttrTypes: map[string]attr.Type{ |
| 99 | + "id": types.StringType, |
| 100 | + "tags": types.SetType{ElemType: types.StringType}, |
| 101 | + "name": types.StringType, |
| 102 | + "description": types.StringType, |
| 103 | + "enabled": types.BoolType, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + } |
| 109 | +} |
0 commit comments