v1_synthetics_UpdateAPITest/
v1_synthetics_UpdateAPITest.rs

1// Edit an API test returns "OK" response
2use datadog_api_client::datadog;
3use datadog_api_client::datadogV1::api_synthetics::SyntheticsAPI;
4use datadog_api_client::datadogV1::model::SyntheticsAPITest;
5use datadog_api_client::datadogV1::model::SyntheticsAPITestConfig;
6use datadog_api_client::datadogV1::model::SyntheticsAPITestType;
7use datadog_api_client::datadogV1::model::SyntheticsAssertion;
8use datadog_api_client::datadogV1::model::SyntheticsAssertionJSONPathOperator;
9use datadog_api_client::datadogV1::model::SyntheticsAssertionJSONPathTarget;
10use datadog_api_client::datadogV1::model::SyntheticsAssertionJSONPathTargetTarget;
11use datadog_api_client::datadogV1::model::SyntheticsAssertionJSONSchemaMetaSchema;
12use datadog_api_client::datadogV1::model::SyntheticsAssertionJSONSchemaOperator;
13use datadog_api_client::datadogV1::model::SyntheticsAssertionJSONSchemaTarget;
14use datadog_api_client::datadogV1::model::SyntheticsAssertionJSONSchemaTargetTarget;
15use datadog_api_client::datadogV1::model::SyntheticsAssertionOperator;
16use datadog_api_client::datadogV1::model::SyntheticsAssertionTarget;
17use datadog_api_client::datadogV1::model::SyntheticsAssertionTargetValue;
18use datadog_api_client::datadogV1::model::SyntheticsAssertionType;
19use datadog_api_client::datadogV1::model::SyntheticsConfigVariable;
20use datadog_api_client::datadogV1::model::SyntheticsConfigVariableType;
21use datadog_api_client::datadogV1::model::SyntheticsTestDetailsSubType;
22use datadog_api_client::datadogV1::model::SyntheticsTestOptions;
23use datadog_api_client::datadogV1::model::SyntheticsTestOptionsRetry;
24use datadog_api_client::datadogV1::model::SyntheticsTestPauseStatus;
25use datadog_api_client::datadogV1::model::SyntheticsTestRequest;
26use datadog_api_client::datadogV1::model::SyntheticsTestRequestCertificate;
27use datadog_api_client::datadogV1::model::SyntheticsTestRequestCertificateItem;
28use std::collections::BTreeMap;
29
30#[tokio::main]
31async fn main() {
32    // there is a valid "synthetics_api_test" in the system
33    let synthetics_api_test_public_id = std::env::var("SYNTHETICS_API_TEST_PUBLIC_ID").unwrap();
34    let body =
35        SyntheticsAPITest::new(
36            SyntheticsAPITestConfig::new()
37                .assertions(
38                    vec![
39                        SyntheticsAssertion::SyntheticsAssertionTarget(
40                            Box::new(
41                                SyntheticsAssertionTarget::new(
42                                    SyntheticsAssertionOperator::IS,
43                                    SyntheticsAssertionTargetValue::SyntheticsAssertionTargetValueString(
44                                        "text/html".to_string(),
45                                    ),
46                                    SyntheticsAssertionType::HEADER,
47                                ).property("{{ PROPERTY }}".to_string()),
48                            ),
49                        ),
50                        SyntheticsAssertion::SyntheticsAssertionTarget(
51                            Box::new(
52                                SyntheticsAssertionTarget::new(
53                                    SyntheticsAssertionOperator::LESS_THAN,
54                                    SyntheticsAssertionTargetValue::SyntheticsAssertionTargetValueNumber(
55                                        2000.0 as f64,
56                                    ),
57                                    SyntheticsAssertionType::RESPONSE_TIME,
58                                ),
59                            ),
60                        ),
61                        SyntheticsAssertion::SyntheticsAssertionJSONPathTarget(
62                            Box::new(
63                                SyntheticsAssertionJSONPathTarget::new(
64                                    SyntheticsAssertionJSONPathOperator::VALIDATES_JSON_PATH,
65                                    SyntheticsAssertionType::BODY,
66                                ).target(
67                                    SyntheticsAssertionJSONPathTargetTarget::new()
68                                        .json_path("topKey".to_string())
69                                        .operator("isNot".to_string())
70                                        .target_value(
71                                            SyntheticsAssertionTargetValue::SyntheticsAssertionTargetValueString(
72                                                "0".to_string(),
73                                            ),
74                                        ),
75                                ),
76                            ),
77                        ),
78                        SyntheticsAssertion::SyntheticsAssertionJSONSchemaTarget(
79                            Box::new(
80                                SyntheticsAssertionJSONSchemaTarget::new(
81                                    SyntheticsAssertionJSONSchemaOperator::VALIDATES_JSON_SCHEMA,
82                                    SyntheticsAssertionType::BODY,
83                                ).target(
84                                    SyntheticsAssertionJSONSchemaTargetTarget::new()
85                                        .json_schema(
86                                            r#"{"type": "object", "properties":{"slideshow":{"type":"object"}}}"#.to_string(),
87                                        )
88                                        .meta_schema(SyntheticsAssertionJSONSchemaMetaSchema::DRAFT_07),
89                                ),
90                            ),
91                        )
92                    ],
93                )
94                .config_variables(
95                    vec![
96                        SyntheticsConfigVariable::new("PROPERTY".to_string(), SyntheticsConfigVariableType::TEXT)
97                            .example("content-type".to_string())
98                            .pattern("content-type".to_string())
99                    ],
100                )
101                .request(
102                    SyntheticsTestRequest::new()
103                        .certificate(
104                            SyntheticsTestRequestCertificate::new()
105                                .cert(
106                                    SyntheticsTestRequestCertificateItem::new()
107                                        .filename("cert-filename".to_string())
108                                        .updated_at("2020-10-16T09:23:24.857Z".to_string()),
109                                )
110                                .key(
111                                    SyntheticsTestRequestCertificateItem::new()
112                                        .filename("key-filename".to_string())
113                                        .updated_at("2020-10-16T09:23:24.857Z".to_string()),
114                                ),
115                        )
116                        .headers(BTreeMap::from([("unique".to_string(), "examplesynthetic".to_string())]))
117                        .method("GET".to_string())
118                        .timeout(10.0 as f64)
119                        .url("https://datadoghq.com".to_string()),
120                ),
121            vec!["aws:us-east-2".to_string()],
122            "BDD test payload: synthetics_api_test_payload.json".to_string(),
123            "Example-Synthetic-updated".to_string(),
124            SyntheticsTestOptions::new()
125                .accept_self_signed(false)
126                .allow_insecure(true)
127                .follow_redirects(true)
128                .min_failure_duration(10)
129                .min_location_failed(1)
130                .monitor_name("Test-TestSyntheticsAPITestLifecycle-1623076664".to_string())
131                .monitor_priority(5)
132                .retry(SyntheticsTestOptionsRetry::new().count(3).interval(10.0 as f64))
133                .tick_every(60),
134            SyntheticsAPITestType::API,
135        )
136            .status(SyntheticsTestPauseStatus::LIVE)
137            .subtype(SyntheticsTestDetailsSubType::HTTP)
138            .tags(vec!["testing:api".to_string()]);
139    let configuration = datadog::Configuration::new();
140    let api = SyntheticsAPI::with_config(configuration);
141    let resp = api
142        .update_api_test(synthetics_api_test_public_id.clone(), body)
143        .await;
144    if let Ok(value) = resp {
145        println!("{:#?}", value);
146    } else {
147        println!("{:#?}", resp.unwrap_err());
148    }
149}