Skip to content

Commit

Permalink
[datadog_synthetics_test] Add support for metadata for Synthetics grp…
Browse files Browse the repository at this point in the history
…c tests (#2094)

* Add support for metadata for grpc tests

* Update docs

* Remove grpc metadata for api steps

* Update cassettes and fix test
  • Loading branch information
romainberger authored Sep 12, 2023
1 parent 4613864 commit 4d9aec0
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 90 deletions.
28 changes: 25 additions & 3 deletions datadog/resource_datadog_synthetics_test_.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func resourceDatadogSyntheticsTest() *schema.Resource {
"request_basicauth": syntheticsTestRequestBasicAuth(),
"request_proxy": syntheticsTestRequestProxy(),
"request_client_certificate": syntheticsTestRequestClientCertificate(),
"request_metadata": syntheticsTestRequestMetadata(),
"assertion": syntheticsAPIAssertion(),
"browser_variable": syntheticsBrowserVariable(),
"config_variable": syntheticsConfigVariable(),
Expand Down Expand Up @@ -416,6 +417,14 @@ func syntheticsTestRequestClientCertificateItem() *schema.Schema {
}
}

func syntheticsTestRequestMetadata() *schema.Schema {
return &schema.Schema{
Description: "Metadata to include when performing the gRPC test.",
Type: schema.TypeMap,
Optional: true,
}
}

func syntheticsAPIAssertion() *schema.Schema {
return &schema.Schema{
Description: "Assertions used for the test. Multiple `assertion` blocks are allowed with the structure below.",
Expand Down Expand Up @@ -1455,7 +1464,7 @@ func buildSyntheticsAPITestStruct(d *schema.ResourceData) *datadogV1.SyntheticsA
request.SetPersistCookies(attr.(bool))
}

request = *completeSyntheticsTestRequest(request, d.Get("request_headers").(map[string]interface{}), d.Get("request_query").(map[string]interface{}), d.Get("request_basicauth").([]interface{}), d.Get("request_client_certificate").([]interface{}), d.Get("request_proxy").([]interface{}))
request = *completeSyntheticsTestRequest(request, d.Get("request_headers").(map[string]interface{}), d.Get("request_query").(map[string]interface{}), d.Get("request_basicauth").([]interface{}), d.Get("request_client_certificate").([]interface{}), d.Get("request_proxy").([]interface{}), d.Get("request_metadata").(map[string]interface{}))

config := datadogV1.NewSyntheticsAPITestConfigWithDefaults()

Expand Down Expand Up @@ -1527,7 +1536,7 @@ func buildSyntheticsAPITestStruct(d *schema.ResourceData) *datadogV1.SyntheticsA
request.SetPersistCookies(requestMap["persist_cookies"].(bool))
}

request = *completeSyntheticsTestRequest(request, stepMap["request_headers"].(map[string]interface{}), stepMap["request_query"].(map[string]interface{}), stepMap["request_basicauth"].([]interface{}), stepMap["request_client_certificate"].([]interface{}), stepMap["request_proxy"].([]interface{}))
request = *completeSyntheticsTestRequest(request, stepMap["request_headers"].(map[string]interface{}), stepMap["request_query"].(map[string]interface{}), stepMap["request_basicauth"].([]interface{}), stepMap["request_client_certificate"].([]interface{}), stepMap["request_proxy"].([]interface{}), map[string]interface{}{})

step.SetRequest(request)

Expand Down Expand Up @@ -1580,7 +1589,7 @@ func buildSyntheticsAPITestStruct(d *schema.ResourceData) *datadogV1.SyntheticsA
return syntheticsTest
}

func completeSyntheticsTestRequest(request datadogV1.SyntheticsTestRequest, requestHeaders map[string]interface{}, requestQuery map[string]interface{}, basicAuth []interface{}, requestClientCertificates []interface{}, requestProxy []interface{}) *datadogV1.SyntheticsTestRequest {
func completeSyntheticsTestRequest(request datadogV1.SyntheticsTestRequest, requestHeaders map[string]interface{}, requestQuery map[string]interface{}, basicAuth []interface{}, requestClientCertificates []interface{}, requestProxy []interface{}, requestMetadata map[string]interface{}) *datadogV1.SyntheticsTestRequest {
if len(requestHeaders) > 0 {
headers := make(map[string]string, len(requestHeaders))

Expand Down Expand Up @@ -1740,6 +1749,16 @@ func completeSyntheticsTestRequest(request datadogV1.SyntheticsTestRequest, requ
}
}

if len(requestMetadata) > 0 {
metadata := make(map[string]string, len(requestMetadata))

for k, v := range requestMetadata {
metadata[k] = v.(string)
}

request.SetMetadata(metadata)
}

return &request
}

Expand Down Expand Up @@ -2939,6 +2958,9 @@ func updateSyntheticsAPITestLocalState(d *schema.ResourceData, syntheticsTest *d
if err := d.Set("request_query", actualRequest.GetQuery()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("request_metadata", actualRequest.GetMetadata()); err != nil {
return diag.FromErr(err)
}

if basicAuth, ok := actualRequest.GetBasicAuthOk(); ok {
localAuth := buildLocalBasicAuth(basicAuth)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-05-17T17:05:32.491138-04:00
2023-09-06T16:08:33.60798+02:00

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-05-17T17:05:32.492389-04:00
2023-09-06T16:07:32.808885+02:00

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-05-03T13:54:19.887347+02:00
2023-09-06T15:29:38.324954+02:00
52 changes: 26 additions & 26 deletions datadog/tests/cassettes/TestAccDatadogSyntheticsGRPCTest_Basic.yaml

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions datadog/tests/resource_datadog_synthetics_test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,10 @@ func createSyntheticsGRPCTestStep(ctx context.Context, accProvider func() (*sche
"datadog_synthetics_test.grpc", "request_definition.0.port", "50050"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.grpc", "request_definition.0.service", "Hello"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.grpc", "request_metadata.%", "1"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.grpc", "request_metadata.header", "value"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.grpc", "assertion.#", "4"),
resource.TestCheckResourceAttr(
Expand Down Expand Up @@ -2435,6 +2439,10 @@ resource "datadog_synthetics_test" "grpc" {
service = "Hello"
}
request_metadata = {
header = "value"
}
assertion {
type = "responseTime"
operator = "lessThan"
Expand Down Expand Up @@ -2489,6 +2497,10 @@ func updateSyntheticsGRPCTestStep(ctx context.Context, accProvider func() (*sche
"datadog_synthetics_test.grpc", "request_definition.0.port", "50050"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.grpc", "request_definition.0.service", ""),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.grpc", "request_metadata.%", "1"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.grpc", "request_metadata.header", "value-updated"),
resource.TestCheckResourceAttr(
"datadog_synthetics_test.grpc", "assertion.#", "2"),
resource.TestCheckResourceAttr(
Expand Down Expand Up @@ -2540,6 +2552,10 @@ resource "datadog_synthetics_test" "grpc" {
service = ""
}
request_metadata = {
header = "value-updated"
}
assertion {
type = "responseTime"
operator = "lessThan"
Expand Down
1 change: 1 addition & 0 deletions docs/resources/synthetics_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ resource "datadog_synthetics_test" "test_browser" {
- `request_client_certificate` (Block List, Max: 1) Client certificate to use when performing the test request. Exactly one nested block is allowed with the structure below. (see [below for nested schema](#nestedblock--request_client_certificate))
- `request_definition` (Block List, Max: 1) Required if `type = "api"`. The synthetics test request. (see [below for nested schema](#nestedblock--request_definition))
- `request_headers` (Map of String) Header name and value map.
- `request_metadata` (Map of String) Metadata to include when performing the gRPC test.
- `request_proxy` (Block List, Max: 1) The proxy to perform the test. (see [below for nested schema](#nestedblock--request_proxy))
- `request_query` (Map of String) Query arguments name and value map.
- `set_cookie` (String) Cookies to be used for a browser test request, using the [Set-Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) syntax.
Expand Down

0 comments on commit 4d9aec0

Please sign in to comment.