Skip to content
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

[datadog_synthetics_test] Add javascript assertion to Synthetics Assertion #2576

Merged
merged 3 commits into from
Sep 18, 2024
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
30 changes: 27 additions & 3 deletions datadog/resource_datadog_synthetics_test_.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,13 @@ func syntheticsAPIAssertion() *schema.Schema {
"type": {
Description: "Type of assertion. **Note** Only some combinations of `type` and `operator` are valid (please refer to [Datadog documentation](https://docs.datadoghq.com/api/latest/synthetics/#create-a-test)).",
Type: schema.TypeString,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewSyntheticsAssertionTypeFromValue, datadogV1.NewSyntheticsAssertionBodyHashTypeFromValue),
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewSyntheticsAssertionTypeFromValue, datadogV1.NewSyntheticsAssertionBodyHashTypeFromValue, datadogV1.NewSyntheticsAssertionJavascriptTypeFromValue),
Required: true,
},
"operator": {
Description: "Assertion operator. **Note** Only some combinations of `type` and `operator` are valid (please refer to [Datadog documentation](https://docs.datadoghq.com/api/latest/synthetics/#create-a-test)).",
Type: schema.TypeString,
Required: true,
Optional: true,
ValidateFunc: validateSyntheticsAssertionOperator,
},
"property": {
Expand All @@ -496,6 +496,11 @@ func syntheticsAPIAssertion() *schema.Schema {
Type: schema.TypeString,
Optional: true,
},
"code": {
Description: "If assertion type is `javascript`, this is the JavaScript code that performs the assertions.",
Type: schema.TypeString,
Optional: true,
},
"targetjsonschema": {
Description: "Expected structure if `operator` is `validatesJSONSchema`. Exactly one nested block is allowed with the structure below.",
Type: schema.TypeList,
Expand Down Expand Up @@ -2360,7 +2365,16 @@ func buildDatadogAssertions(attr []interface{}) []datadogV1.SyntheticsAssertion
assertionMap := assertion.(map[string]interface{})
if v, ok := assertionMap["type"]; ok {
assertionType := v.(string)
if v, ok := assertionMap["operator"]; ok {
if assertionType == string(datadogV1.SYNTHETICSASSERTIONJAVASCRIPTTYPE_JAVASCRIPT) {
// Handling the case for javascript assertion that does not contains any `operator`
assertionJavascript := datadogV1.NewSyntheticsAssertionJavascriptWithDefaults()
assertionJavascript.SetType(datadogV1.SYNTHETICSASSERTIONJAVASCRIPTTYPE_JAVASCRIPT)
if v, ok := assertionMap["code"]; ok {
assertionCode := v.(string)
assertionJavascript.SetCode((assertionCode))
}
assertions = append(assertions, datadogV1.SyntheticsAssertionJavascriptAsSyntheticsAssertion(assertionJavascript))
} else if v, ok := assertionMap["operator"]; ok {
assertionOperator := v.(string)
if assertionOperator == string(datadogV1.SYNTHETICSASSERTIONJSONSCHEMAOPERATOR_VALIDATES_JSON_SCHEMA) {
assertionJSONSchemaTarget := datadogV1.NewSyntheticsAssertionJSONSchemaTarget(datadogV1.SyntheticsAssertionJSONSchemaOperator(assertionOperator), datadogV1.SyntheticsAssertionType(assertionType))
Expand Down Expand Up @@ -2617,6 +2631,16 @@ func buildTerraformAssertions(actualAssertions []datadogV1.SyntheticsAssertion)
if v, ok := assertionTarget.GetTypeOk(); ok {
localAssertion["type"] = string(*v)
}
} else if assertion.SyntheticsAssertionJavascript != nil {
assertionTarget := assertion.SyntheticsAssertionJavascript

if v, ok := assertionTarget.GetTypeOk(); ok {
localAssertion["type"] = string(*v)
}

if v, ok := assertionTarget.GetCodeOk(); ok {
localAssertion["code"] = v
}
}
localAssertions[i] = localAssertion
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-08-22T20:29:14.605282+02:00
2024-09-11T16:27:48.498745+02:00

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-08-22T20:28:50.151023+02:00
2024-09-11T16:27:48.498854+02:00

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-08-22T20:29:13.277568+02:00
2024-09-11T16:26:32.901369+02:00
120 changes: 60 additions & 60 deletions datadog/tests/cassettes/TestAccDatadogSyntheticsAPITest_Updated.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-08-22T20:28:50.145892+02:00
2024-09-11T16:26:32.902496+02:00

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-08-22T20:29:22.46882+02:00
2024-09-11T16:29:08.367667+02:00

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions datadog/tests/resource_datadog_synthetics_test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ func createSyntheticsAPITestStep(ctx context.Context, accProvider func() (*schem
resource.TestCheckResourceAttr(
"datadog_synthetics_test.foo", "variables_from_script", "dd.variable.set('FOO', 'hello');"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.foo", "assertion.#", "5"),
"datadog_synthetics_test.foo", "assertion.#", "6"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.foo", "assertion.0.type", "header"),
resource.TestCheckResourceAttr(
Expand Down Expand Up @@ -973,6 +973,10 @@ func createSyntheticsAPITestStep(ctx context.Context, accProvider func() (*schem
"datadog_synthetics_test.foo", "assertion.3.operator", "doesNotContain"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.foo", "assertion.3.target", "terraform"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.foo", "assertion.5.type", "javascript"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.foo", "assertion.5.code", "const hello = 'world';"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.foo", "locations.#", "1"),
resource.TestCheckTypeSetElemAttr(
Expand Down Expand Up @@ -1101,6 +1105,10 @@ resource "datadog_synthetics_test" "foo" {
operator = "md5"
target = "a"
}
assertion {
type = "javascript"
code = "const hello = 'world';"
}
locations = [ "aws:eu-central-1" ]

options_list {
Expand Down Expand Up @@ -1352,7 +1360,7 @@ func createSyntheticsAPITestStepNewAssertionsOptions(ctx context.Context, accPro
resource.TestCheckResourceAttr(
"datadog_synthetics_test.bar", "request_client_certificate.0.key.0.filename", "key"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.bar", "assertion.#", "11"),
"datadog_synthetics_test.bar", "assertion.#", "12"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.bar", "assertion.0.type", "header"),
resource.TestCheckResourceAttr(
Expand Down Expand Up @@ -1453,6 +1461,10 @@ func createSyntheticsAPITestStepNewAssertionsOptions(ctx context.Context, accPro
"datadog_synthetics_test.bar", "assertion.10.targetjsonpath.0.jsonpath", "$.myKey"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.bar", "assertion.10.targetjsonpath.0.operator", "isUndefined"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.bar", "assertion.11.type", "javascript"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.bar", "assertion.11.code", "const hello = 'world';"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.bar", "locations.#", "1"),
resource.TestCheckResourceAttr(
Expand Down Expand Up @@ -1616,6 +1628,10 @@ resource "datadog_synthetics_test" "bar" {
operator = "isUndefined"
}
}
assertion {
type = "javascript"
code = "const hello = 'world';"
}

locations = [ "aws:eu-central-1" ]
options_list {
Expand Down
10 changes: 6 additions & 4 deletions docs/resources/synthetics_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,11 +580,12 @@ Optional:

Required:

- `operator` (String) Assertion operator. **Note** Only some combinations of `type` and `operator` are valid (please refer to [Datadog documentation](https://docs.datadoghq.com/api/latest/synthetics/#create-a-test)).
- `type` (String) Type of assertion. **Note** Only some combinations of `type` and `operator` are valid (please refer to [Datadog documentation](https://docs.datadoghq.com/api/latest/synthetics/#create-a-test)). Valid values are `body`, `header`, `statusCode`, `certificate`, `responseTime`, `property`, `recordEvery`, `recordSome`, `tlsVersion`, `minTlsVersion`, `latency`, `packetLossPercentage`, `packetsReceived`, `networkHop`, `receivedMessage`, `grpcHealthcheckStatus`, `grpcMetadata`, `grpcProto`, `connection`, `bodyHash`.
- `type` (String) Type of assertion. **Note** Only some combinations of `type` and `operator` are valid (please refer to [Datadog documentation](https://docs.datadoghq.com/api/latest/synthetics/#create-a-test)). Valid values are `body`, `header`, `statusCode`, `certificate`, `responseTime`, `property`, `recordEvery`, `recordSome`, `tlsVersion`, `minTlsVersion`, `latency`, `packetLossPercentage`, `packetsReceived`, `networkHop`, `receivedMessage`, `grpcHealthcheckStatus`, `grpcMetadata`, `grpcProto`, `connection`, `bodyHash`, `javascript`.

Optional:

- `code` (String) If assertion type is `javascript`, this is the JavaScript code that performs the assertions.
- `operator` (String) Assertion operator. **Note** Only some combinations of `type` and `operator` are valid (please refer to [Datadog documentation](https://docs.datadoghq.com/api/latest/synthetics/#create-a-test)).
- `property` (String) If assertion type is `header`, this is the header name.
- `target` (String) Expected value. Depends on the assertion type, refer to [Datadog documentation](https://docs.datadoghq.com/api/latest/synthetics/#create-a-test) for details.
- `targetjsonpath` (Block List, Max: 1) Expected structure if `operator` is `validatesJSONPath`. Exactly one nested block is allowed with the structure below. (see [below for nested schema](#nestedblock--api_step--assertion--targetjsonpath))
Expand Down Expand Up @@ -792,11 +793,12 @@ Optional:

Required:

- `operator` (String) Assertion operator. **Note** Only some combinations of `type` and `operator` are valid (please refer to [Datadog documentation](https://docs.datadoghq.com/api/latest/synthetics/#create-a-test)).
- `type` (String) Type of assertion. **Note** Only some combinations of `type` and `operator` are valid (please refer to [Datadog documentation](https://docs.datadoghq.com/api/latest/synthetics/#create-a-test)). Valid values are `body`, `header`, `statusCode`, `certificate`, `responseTime`, `property`, `recordEvery`, `recordSome`, `tlsVersion`, `minTlsVersion`, `latency`, `packetLossPercentage`, `packetsReceived`, `networkHop`, `receivedMessage`, `grpcHealthcheckStatus`, `grpcMetadata`, `grpcProto`, `connection`, `bodyHash`.
- `type` (String) Type of assertion. **Note** Only some combinations of `type` and `operator` are valid (please refer to [Datadog documentation](https://docs.datadoghq.com/api/latest/synthetics/#create-a-test)). Valid values are `body`, `header`, `statusCode`, `certificate`, `responseTime`, `property`, `recordEvery`, `recordSome`, `tlsVersion`, `minTlsVersion`, `latency`, `packetLossPercentage`, `packetsReceived`, `networkHop`, `receivedMessage`, `grpcHealthcheckStatus`, `grpcMetadata`, `grpcProto`, `connection`, `bodyHash`, `javascript`.

Optional:

- `code` (String) If assertion type is `javascript`, this is the JavaScript code that performs the assertions.
- `operator` (String) Assertion operator. **Note** Only some combinations of `type` and `operator` are valid (please refer to [Datadog documentation](https://docs.datadoghq.com/api/latest/synthetics/#create-a-test)).
- `property` (String) If assertion type is `header`, this is the header name.
- `target` (String) Expected value. Depends on the assertion type, refer to [Datadog documentation](https://docs.datadoghq.com/api/latest/synthetics/#create-a-test) for details.
- `targetjsonpath` (Block List, Max: 1) Expected structure if `operator` is `validatesJSONPath`. Exactly one nested block is allowed with the structure below. (see [below for nested schema](#nestedblock--assertion--targetjsonpath))
Expand Down
Loading