Skip to content

Add convert rule JSON to terraform to Datadog API Spec. #1867

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .apigentools-info
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"spec_versions": {
"v1": {
"apigentools_version": "1.6.6",
"regenerated": "2024-07-11 14:29:33.577935",
"spec_repo_commit": "3eedf0af"
"regenerated": "2024-07-11 16:35:06.139859",
"spec_repo_commit": "d83fe206"
},
"v2": {
"apigentools_version": "1.6.6",
"regenerated": "2024-07-11 14:29:33.606047",
"spec_repo_commit": "3eedf0af"
"regenerated": "2024-07-11 16:35:06.158034",
"spec_repo_commit": "d83fe206"
}
}
}
86 changes: 86 additions & 0 deletions .generator/schemas/v2/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17377,6 +17377,18 @@ components:
required:
- status
type: object
SecurityMonitoringRuleConvertPayload:
description: Convert a rule from JSON to Terraform.
oneOf:
- $ref: '#/components/schemas/SecurityMonitoringStandardRulePayload'
- $ref: '#/components/schemas/SecurityMonitoringSignalRulePayload'
SecurityMonitoringRuleConvertResponse:
description: Result of the convert rule request containing Terraform content.
properties:
terraformContent:
description: Terraform string as a result of converting the rule from JSON.
type: string
type: object
SecurityMonitoringRuleCreatePayload:
description: Create a new rule.
oneOf:
Expand Down Expand Up @@ -33246,6 +33258,47 @@ paths:
tags:
- Security Monitoring
x-codegen-request-body-name: body
/api/v2/security_monitoring/rules/convert:
post:
description: 'Convert a rule that doesn''t (yet) exist from JSON to Terraform
for datadog provider

resource datadog_security_monitoring_rule.'
operationId: ConvertSecurityMonitoringRuleFromJSONToTerraform
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/SecurityMonitoringRuleConvertPayload'
required: true
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/SecurityMonitoringRuleConvertResponse'
description: OK
'400':
$ref: '#/components/responses/BadRequestResponse'
'401':
$ref: '#/components/responses/ConcurrentModificationResponse'
'403':
$ref: '#/components/responses/NotAuthorizedResponse'
'404':
$ref: '#/components/responses/NotFoundResponse'
'429':
$ref: '#/components/responses/TooManyRequestsResponse'
security:
- apiKeyAuth: []
appKeyAuth: []
- AuthZ:
- security_monitoring_rules_write
summary: Convert a rule from JSON to Terraform
tags:
- Security Monitoring
x-codegen-request-body-name: body
x-unstable: '**Note**: This feature is in private beta. Reach out to support
to request access.'
/api/v2/security_monitoring/rules/test:
post:
description: Test a rule.
Expand Down Expand Up @@ -33403,6 +33456,39 @@ paths:
tags:
- Security Monitoring
x-codegen-request-body-name: body
/api/v2/security_monitoring/rules/{rule_id}/convert:
get:
description: 'Convert an existing rule from JSON to Terraform for datadog provider

resource datadog_security_monitoring_rule.'
operationId: ConvertExistingSecurityMonitoringRule
parameters:
- $ref: '#/components/parameters/SecurityMonitoringRuleID'
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/SecurityMonitoringRuleConvertResponse'
description: OK
'400':
$ref: '#/components/responses/BadRequestResponse'
'403':
$ref: '#/components/responses/NotAuthorizedResponse'
'404':
$ref: '#/components/responses/NotFoundResponse'
'429':
$ref: '#/components/responses/TooManyRequestsResponse'
security:
- apiKeyAuth: []
appKeyAuth: []
- AuthZ:
- security_monitoring_rules_read
summary: Convert an existing rule from JSON to Terraform
tags:
- Security Monitoring
x-unstable: '**Note**: This feature is in private beta. Reach out to support
to request access.'
/api/v2/security_monitoring/rules/{rule_id}/test:
post:
description: Test an existing rule.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-06-24T19:47:12.909Z

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-06-24T19:55:06.421Z

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Convert an existing rule from JSON to Terraform returns "OK" response

require "datadog_api_client"
DatadogAPIClient.configure do |config|
config.unstable_operations["v2.convert_existing_security_monitoring_rule".to_sym] = true
end
api_instance = DatadogAPIClient::V2::SecurityMonitoringAPI.new

# there is a valid "security_rule" in the system
SECURITY_RULE_ID = ENV["SECURITY_RULE_ID"]
p api_instance.convert_existing_security_monitoring_rule(SECURITY_RULE_ID)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Convert a rule from JSON to Terraform returns "OK" response

require "datadog_api_client"
DatadogAPIClient.configure do |config|
config.unstable_operations["v2.convert_security_monitoring_rule_from_json_to_terraform".to_sym] = true
end
api_instance = DatadogAPIClient::V2::SecurityMonitoringAPI.new

body = DatadogAPIClient::V2::SecurityMonitoringStandardRulePayload.new({
name: "Example-Security-Monitoring",
queries: [
DatadogAPIClient::V2::SecurityMonitoringStandardRuleQuery.new({
query: "@test:true",
aggregation: DatadogAPIClient::V2::SecurityMonitoringRuleQueryAggregation::COUNT,
group_by_fields: [],
distinct_fields: [],
metric: "",
}),
],
filters: [],
cases: [
DatadogAPIClient::V2::SecurityMonitoringRuleCaseCreate.new({
name: "",
status: DatadogAPIClient::V2::SecurityMonitoringRuleSeverity::INFO,
condition: "a > 0",
notifications: [],
}),
],
options: DatadogAPIClient::V2::SecurityMonitoringRuleOptions.new({
evaluation_window: DatadogAPIClient::V2::SecurityMonitoringRuleEvaluationWindow::FIFTEEN_MINUTES,
keep_alive: DatadogAPIClient::V2::SecurityMonitoringRuleKeepAlive::ONE_HOUR,
max_signal_duration: DatadogAPIClient::V2::SecurityMonitoringRuleMaxSignalDuration::ONE_DAY,
}),
message: "Test rule",
tags: [],
is_enabled: true,
type: DatadogAPIClient::V2::SecurityMonitoringRuleTypeCreate::LOG_DETECTION,
})
p api_instance.convert_security_monitoring_rule_from_json_to_terraform(body)
6 changes: 6 additions & 0 deletions features/scenarios_model_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,9 @@
"v2.CreateSecurityMonitoringRule" => {
"body" => "SecurityMonitoringRuleCreatePayload",
},
"v2.ConvertSecurityMonitoringRuleFromJSONToTerraform" => {
"body" => "SecurityMonitoringRuleConvertPayload",
},
"v2.TestSecurityMonitoringRule" => {
"body" => "SecurityMonitoringRuleTestRequest",
},
Expand All @@ -1655,6 +1658,9 @@
"rule_id" => "String",
"body" => "SecurityMonitoringRuleUpdatePayload",
},
"v2.ConvertExistingSecurityMonitoringRule" => {
"rule_id" => "String",
},
"v2.TestExistingSecurityMonitoringRule" => {
"rule_id" => "String",
"body" => "SecurityMonitoringRuleTestRequest",
Expand Down
47 changes: 47 additions & 0 deletions features/v2/security_monitoring.feature
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,53 @@ Feature: Security Monitoring
When the request is sent
Then the response status is 200 OK

@skip @team:DataDog/k9-cloud-security-platform
Scenario: Convert a rule from JSON to Terraform returns "Bad Request" response
Given new "ConvertSecurityMonitoringRuleFromJSONToTerraform" request
And body with value {"name":"{{ unique }}", "queries":[{"query":"@test:true","aggregation":"count","groupByFields":[],"distinctFields":[],"metric":""}],"filters":[],"cases":[{"name":"","status":"info","condition":"a > 0","notifications":[]}],"options":{"evaluationWindow":900,"keepAlive":3600,"maxSignalDuration":86400},"message":"Test rule","tags":[],"isEnabled":true, "type":"log_detection"}
When the request is sent
Then the response status is 400 Bad Request

@skip @team:DataDog/k9-cloud-security-platform
Scenario: Convert a rule from JSON to Terraform returns "Not Found" response
Given new "ConvertSecurityMonitoringRuleFromJSONToTerraform" request
And body with value {"name":"{{ unique }}", "queries":[{"query":"@test:true","aggregation":"count","groupByFields":[],"distinctFields":[],"metric":""}],"filters":[],"cases":[{"name":"","status":"info","condition":"a > 0","notifications":[]}],"options":{"evaluationWindow":900,"keepAlive":3600,"maxSignalDuration":86400},"message":"Test rule","tags":[],"isEnabled":true, "type":"log_detection"}
When the request is sent
Then the response status is 404 Not Found

@team:DataDog/k9-cloud-security-platform
Scenario: Convert a rule from JSON to Terraform returns "OK" response
Given operation "ConvertSecurityMonitoringRuleFromJSONToTerraform" enabled
And new "ConvertSecurityMonitoringRuleFromJSONToTerraform" request
And body with value {"name":"{{ unique }}", "queries":[{"query":"@test:true","aggregation":"count","groupByFields":[],"distinctFields":[],"metric":""}],"filters":[],"cases":[{"name":"","status":"info","condition":"a > 0","notifications":[]}],"options":{"evaluationWindow":900,"keepAlive":3600,"maxSignalDuration":86400},"message":"Test rule","tags":[],"isEnabled":true, "type":"log_detection"}
When the request is sent
Then the response status is 200 OK
And the response "terraformContent" is equal to "resource \"datadog_security_monitoring_rule\" \"{{ unique_lower }}\" {\n\tname = \"{{ unique }}\"\n\tenabled = true\n\tquery {\n\t\tquery = \"@test:true\"\n\t\tgroup_by_fields = []\n\t\tdistinct_fields = []\n\t\taggregation = \"count\"\n\t\tname = \"\"\n\t}\n\toptions {\n\t\tkeep_alive = 3600\n\t\tmax_signal_duration = 86400\n\t\tdetection_method = \"threshold\"\n\t\tevaluation_window = 900\n\t}\n\tcase {\n\t\tname = \"\"\n\t\tstatus = \"info\"\n\t\tnotifications = []\n\t\tcondition = \"a > 0\"\n\t}\n\tmessage = \"Test rule\"\n\ttags = []\n\thas_extended_title = false\n\ttype = \"log_detection\"\n}\n"

@skip @team:DataDog/k9-cloud-security-platform
Scenario: Convert an existing rule from JSON to Terraform returns "Bad Request" response
Given new "ConvertExistingSecurityMonitoringRule" request
And request contains "rule_id" parameter from "REPLACE.ME"
When the request is sent
Then the response status is 400 Bad Request

@skip @team:DataDog/k9-cloud-security-platform
Scenario: Convert an existing rule from JSON to Terraform returns "Not Found" response
Given new "ConvertExistingSecurityMonitoringRule" request
And request contains "rule_id" parameter from "REPLACE.ME"
When the request is sent
Then the response status is 404 Not Found

@team:DataDog/k9-cloud-security-platform
Scenario: Convert an existing rule from JSON to Terraform returns "OK" response
Given operation "ConvertExistingSecurityMonitoringRule" enabled
And new "ConvertExistingSecurityMonitoringRule" request
And there is a valid "security_rule" in the system
And request contains "rule_id" parameter from "security_rule.id"
When the request is sent
Then the response status is 200 OK
And the response "terraformContent" is equal to "resource \"datadog_security_monitoring_rule\" \"{{ unique_lower }}\" {\n\tname = \"{{ unique }}\"\n\tenabled = true\n\tquery {\n\t\tquery = \"@test:true\"\n\t\tgroup_by_fields = []\n\t\tdistinct_fields = []\n\t\taggregation = \"count\"\n\t\tname = \"\"\n\t}\n\toptions {\n\t\tkeep_alive = 3600\n\t\tmax_signal_duration = 86400\n\t\tdetection_method = \"threshold\"\n\t\tevaluation_window = 900\n\t}\n\tcase {\n\t\tname = \"\"\n\t\tstatus = \"info\"\n\t\tnotifications = []\n\t\tcondition = \"a > 0\"\n\t}\n\tmessage = \"Test rule\"\n\ttags = []\n\thas_extended_title = false\n\ttype = \"log_detection\"\n}\n"

@skip-validation @team:DataDog/k9-cloud-security-platform
Scenario: Create a cloud_configuration rule returns "OK" response
Given new "CreateSecurityMonitoringRule" request
Expand Down
12 changes: 12 additions & 0 deletions features/v2/undo.json
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,12 @@
"type": "unsafe"
}
},
"ConvertSecurityMonitoringRuleFromJSONToTerraform": {
"tag": "Security Monitoring",
"undo": {
"type": "idempotent"
}
},
"TestSecurityMonitoringRule": {
"tag": "Security Monitoring",
"undo": {
Expand Down Expand Up @@ -1805,6 +1811,12 @@
"type": "idempotent"
}
},
"ConvertExistingSecurityMonitoringRule": {
"tag": "Security Monitoring",
"undo": {
"type": "idempotent"
}
},
"TestExistingSecurityMonitoringRule": {
"tag": "Security Monitoring",
"undo": {
Expand Down
Loading
Loading