|
| 1 | +package fwprovider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/DataDog/datadog-api-client-go/v2/api/datadogV2" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | + |
| 15 | + "github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + _ datasource.DataSourceWithConfigure = &csmThreatsAgentRulesDataSource{} |
| 20 | +) |
| 21 | + |
| 22 | +type csmThreatsAgentRulesDataSource struct { |
| 23 | + api *datadogV2.CSMThreatsApi |
| 24 | + auth context.Context |
| 25 | +} |
| 26 | + |
| 27 | +type csmThreatsAgentRulesDataSourceModel struct { |
| 28 | + Id types.String `tfsdk:"id"` |
| 29 | + AgentRulesIds types.List `tfsdk:"agent_rules_ids"` |
| 30 | + AgentRules []csmThreatsAgentRuleModel `tfsdk:"agent_rules"` |
| 31 | +} |
| 32 | + |
| 33 | +func NewCSMThreatsAgentRulesDataSource() datasource.DataSource { |
| 34 | + return &csmThreatsAgentRulesDataSource{} |
| 35 | +} |
| 36 | + |
| 37 | +func (r *csmThreatsAgentRulesDataSource) Configure(_ context.Context, request datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { |
| 38 | + providerData := request.ProviderData.(*FrameworkProvider) |
| 39 | + r.api = providerData.DatadogApiInstances.GetCSMThreatsApiV2() |
| 40 | + r.auth = providerData.Auth |
| 41 | +} |
| 42 | + |
| 43 | +func (*csmThreatsAgentRulesDataSource) Metadata(_ context.Context, _ datasource.MetadataRequest, response *datasource.MetadataResponse) { |
| 44 | + response.TypeName = "csm_threats_agent_rules" |
| 45 | +} |
| 46 | + |
| 47 | +func (r *csmThreatsAgentRulesDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { |
| 48 | + var state csmThreatsAgentRulesDataSourceModel |
| 49 | + response.Diagnostics.Append(request.Config.Get(ctx, &state)...) |
| 50 | + if response.Diagnostics.HasError() { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + res, _, err := r.api.ListCSMThreatsAgentRules(r.auth) |
| 55 | + if err != nil { |
| 56 | + response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error while fetching agent rules")) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + data := res.GetData() |
| 61 | + agentRuleIds := make([]string, len(data)) |
| 62 | + agentRules := make([]csmThreatsAgentRuleModel, len(data)) |
| 63 | + |
| 64 | + for idx, agentRule := range res.GetData() { |
| 65 | + var agentRuleModel csmThreatsAgentRuleModel |
| 66 | + agentRuleModel.Id = types.StringValue(agentRule.GetId()) |
| 67 | + attributes := agentRule.Attributes |
| 68 | + agentRuleModel.Name = types.StringValue(attributes.GetName()) |
| 69 | + agentRuleModel.Description = types.StringValue(attributes.GetDescription()) |
| 70 | + agentRuleModel.Enabled = types.BoolValue(attributes.GetEnabled()) |
| 71 | + agentRuleModel.Expression = types.StringValue(*attributes.Expression) |
| 72 | + |
| 73 | + agentRuleIds[idx] = agentRule.GetId() |
| 74 | + agentRules[idx] = agentRuleModel |
| 75 | + } |
| 76 | + |
| 77 | + stateId := strings.Join(agentRuleIds, "--") |
| 78 | + state.Id = types.StringValue(computeAgentRulesDataSourceID(&stateId)) |
| 79 | + tfAgentRuleIds, diags := types.ListValueFrom(ctx, types.StringType, agentRuleIds) |
| 80 | + response.Diagnostics.Append(diags...) |
| 81 | + state.AgentRulesIds = tfAgentRuleIds |
| 82 | + state.AgentRules = agentRules |
| 83 | + |
| 84 | + response.Diagnostics.Append(response.State.Set(ctx, &state)...) |
| 85 | +} |
| 86 | + |
| 87 | +func computeAgentRulesDataSourceID(agentruleIds *string) string { |
| 88 | + // Key for hashing |
| 89 | + var b strings.Builder |
| 90 | + if agentruleIds != nil { |
| 91 | + b.WriteString(*agentruleIds) |
| 92 | + } |
| 93 | + keyStr := b.String() |
| 94 | + h := sha256.New() |
| 95 | + h.Write([]byte(keyStr)) |
| 96 | + |
| 97 | + return fmt.Sprintf("%x", h.Sum(nil)) |
| 98 | +} |
| 99 | + |
| 100 | +func (*csmThreatsAgentRulesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, response *datasource.SchemaResponse) { |
| 101 | + response.Schema = schema.Schema{ |
| 102 | + Description: "Use this data source to retrieve information about existing Agent rules.", |
| 103 | + Attributes: map[string]schema.Attribute{ |
| 104 | + "id": utils.ResourceIDAttribute(), |
| 105 | + "agent_rules_ids": schema.ListAttribute{ |
| 106 | + Computed: true, |
| 107 | + Description: "List of IDs for the Agent rules.", |
| 108 | + ElementType: types.StringType, |
| 109 | + }, |
| 110 | + "agent_rules": schema.ListAttribute{ |
| 111 | + Computed: true, |
| 112 | + Description: "List of Agent rules", |
| 113 | + ElementType: types.ObjectType{ |
| 114 | + AttrTypes: map[string]attr.Type{ |
| 115 | + "id": types.StringType, |
| 116 | + "name": types.StringType, |
| 117 | + "description": types.StringType, |
| 118 | + "enabled": types.BoolType, |
| 119 | + "expression": types.StringType, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + } |
| 125 | +} |
0 commit comments