-
Notifications
You must be signed in to change notification settings - Fork 392
[datadog_csm_threats] supporting cws multi-policy in terraform #2681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
homoeconomics
wants to merge
15
commits into
master
Choose a base branch
from
daniel.zhou/CWS-3394-tf-provider
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a7fc0d9
supporting cws multi-policy in terraform
homoeconomics 224823b
supporting cws policies_list
QuentinGuillard b8e52ed
add test file for policies_list
QuentinGuillard 7cf0840
remove priority
QuentinGuillard f8af22b
generate docs + modify test file
QuentinGuillard 206c963
add product_tags
QuentinGuillard ffb4446
remove unused resources + modify test files
QuentinGuillard 3768943
fix bugs
QuentinGuillard 94835fd
restore legacy anget_rule resource, fix tests
QuentinGuillard e427410
generate docs
QuentinGuillard 04dada7
[cws-4175] add `hostTagsLists` field
laurerunser 4a3a424
fix cws agent_rule resource and data_source + fix test files
QuentinGuillard 3f0069d
update go.mod and go.sum
QuentinGuillard b528f99
regen docs + gen cassettes
QuentinGuillard cf16019
remove multi_policy_agent_rule resource
QuentinGuillard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
100 changes: 0 additions & 100 deletions
100
datadog/data_source_datadog_cloud_workload_security_agent_rules.go
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
datadog/fwprovider/data_source_datadog_csm_threats_multi_policy_agent_rules.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
package fwprovider | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2" | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
|
||
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils" | ||
) | ||
|
||
var ( | ||
_ datasource.DataSourceWithConfigure = &csmThreatsMultiPolicyAgentRulesDataSource{} | ||
) | ||
|
||
type csmThreatsMultiPolicyAgentRulesDataSource struct { | ||
api *datadogV2.CSMThreatsApi | ||
auth context.Context | ||
} | ||
|
||
type csmThreatsMultiPolicyAgentRulesDataSourceModel struct { | ||
PolicyId types.String `tfsdk:"policy_id"` | ||
Id types.String `tfsdk:"id"` | ||
AgentRulesIds types.List `tfsdk:"agent_rules_ids"` | ||
AgentRules []csmThreatsMultiPolicyAgentRuleDataSourceModel `tfsdk:"agent_rules"` | ||
} | ||
|
||
type csmThreatsMultiPolicyAgentRuleDataSourceModel struct { | ||
Id types.String `tfsdk:"id"` | ||
Name types.String `tfsdk:"name"` | ||
Description types.String `tfsdk:"description"` | ||
Enabled types.Bool `tfsdk:"enabled"` | ||
Expression types.String `tfsdk:"expression"` | ||
ProductTags types.Set `tfsdk:"product_tags"` | ||
} | ||
|
||
func NewCSMThreatsMultiPolicyAgentRulesDataSource() datasource.DataSource { | ||
return &csmThreatsMultiPolicyAgentRulesDataSource{} | ||
} | ||
|
||
func (r *csmThreatsMultiPolicyAgentRulesDataSource) Configure(_ context.Context, request datasource.ConfigureRequest, response *datasource.ConfigureResponse) { | ||
if request.ProviderData == nil { | ||
return | ||
} | ||
|
||
providerData, ok := request.ProviderData.(*FrameworkProvider) | ||
if !ok { | ||
response.Diagnostics.AddError( | ||
"Unexpected Resource Configure Type", | ||
fmt.Sprintf("Expected *FrameworkProvider, got: %T. Please report this issue to the provider developers.", request.ProviderData), | ||
) | ||
return | ||
} | ||
|
||
r.api = providerData.DatadogApiInstances.GetCSMThreatsApiV2() | ||
r.auth = providerData.Auth | ||
} | ||
|
||
func (r *csmThreatsMultiPolicyAgentRulesDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { | ||
response.TypeName = "csm_threats_multi_policy_agent_rules" | ||
} | ||
|
||
func (r *csmThreatsMultiPolicyAgentRulesDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { | ||
var state csmThreatsMultiPolicyAgentRulesDataSourceModel | ||
response.Diagnostics.Append(request.Config.Get(ctx, &state)...) | ||
if response.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
params := datadogV2.NewListCSMThreatsAgentRulesOptionalParameters() | ||
if !state.PolicyId.IsNull() && !state.PolicyId.IsUnknown() { | ||
policyId := state.PolicyId.ValueString() | ||
params.WithPolicyId(policyId) | ||
} | ||
|
||
res, _, err := r.api.ListCSMThreatsAgentRules(r.auth, *params) | ||
if err != nil { | ||
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error while fetching agent rules")) | ||
return | ||
} | ||
|
||
data := res.GetData() | ||
agentRuleIds := make([]string, len(data)) | ||
agentRules := make([]csmThreatsMultiPolicyAgentRuleDataSourceModel, len(data)) | ||
|
||
for idx, agentRule := range res.GetData() { | ||
var agentRuleModel csmThreatsMultiPolicyAgentRuleDataSourceModel | ||
agentRuleModel.Id = types.StringValue(agentRule.GetId()) | ||
attributes := agentRule.Attributes | ||
agentRuleModel.Name = types.StringValue(attributes.GetName()) | ||
agentRuleModel.Description = types.StringValue(attributes.GetDescription()) | ||
agentRuleModel.Enabled = types.BoolValue(attributes.GetEnabled()) | ||
agentRuleModel.Expression = types.StringValue(*attributes.Expression) | ||
tags := attributes.GetProductTags() | ||
tagSet := make(map[string]struct{}) | ||
for _, tag := range tags { | ||
tagSet[tag] = struct{}{} | ||
} | ||
uniqueTags := make([]string, 0, len(tagSet)) | ||
for tag := range tagSet { | ||
uniqueTags = append(uniqueTags, tag) | ||
} | ||
|
||
productTags, diags := types.SetValueFrom(ctx, types.StringType, uniqueTags) | ||
if diags.HasError() { | ||
response.Diagnostics.Append(diags...) | ||
continue | ||
} | ||
agentRuleModel.ProductTags = productTags | ||
agentRuleIds[idx] = agentRule.GetId() | ||
agentRules[idx] = agentRuleModel | ||
} | ||
|
||
stateId := strings.Join(agentRuleIds, "--") | ||
state.Id = types.StringValue(computeDataSourceID(&stateId)) | ||
tfAgentRuleIds, diags := types.ListValueFrom(ctx, types.StringType, agentRuleIds) | ||
response.Diagnostics.Append(diags...) | ||
state.AgentRulesIds = tfAgentRuleIds | ||
state.AgentRules = agentRules | ||
|
||
response.Diagnostics.Append(response.State.Set(ctx, &state)...) | ||
} | ||
|
||
func (*csmThreatsMultiPolicyAgentRulesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, response *datasource.SchemaResponse) { | ||
response.Schema = schema.Schema{ | ||
Description: "Use this data source to retrieve information about existing Agent rules.", | ||
Attributes: map[string]schema.Attribute{ | ||
// Input | ||
"policy_id": schema.StringAttribute{ | ||
Description: "Listing only the rules in the policy with this field as the ID", | ||
Optional: true, | ||
}, | ||
// Output | ||
"id": schema.StringAttribute{ | ||
Description: "The ID of the data source", | ||
Computed: true, | ||
}, | ||
"agent_rules_ids": schema.ListAttribute{ | ||
Computed: true, | ||
Description: "List of IDs for the Agent rules.", | ||
ElementType: types.StringType, | ||
}, | ||
"agent_rules": schema.ListAttribute{ | ||
Computed: true, | ||
Description: "List of Agent rules", | ||
ElementType: types.ObjectType{ | ||
AttrTypes: map[string]attr.Type{ | ||
"id": types.StringType, | ||
"name": types.StringType, | ||
"description": types.StringType, | ||
"enabled": types.BoolType, | ||
"expression": types.StringType, | ||
"product_tags": types.SetType{ElemType: types.StringType}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func computeDataSourceID(ids *string) string { | ||
// Key for hashing | ||
var b strings.Builder | ||
if ids != nil { | ||
b.WriteString(*ids) | ||
} | ||
keyStr := b.String() | ||
h := sha256.New() | ||
h.Write([]byte(keyStr)) | ||
|
||
return fmt.Sprintf("%x", h.Sum(nil)) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.