v1_synthetics_CreateSyntheticsAPITest_1241981394/
v1_synthetics_CreateSyntheticsAPITest_1241981394.rs

1// Create an API test with a file payload returns "OK - Returns the created test
2// details." response
3use datadog_api_client::datadog;
4use datadog_api_client::datadogV1::api_synthetics::SyntheticsAPI;
5use datadog_api_client::datadogV1::model::SyntheticsAPITest;
6use datadog_api_client::datadogV1::model::SyntheticsAPITestConfig;
7use datadog_api_client::datadogV1::model::SyntheticsAPITestType;
8use datadog_api_client::datadogV1::model::SyntheticsAssertion;
9use datadog_api_client::datadogV1::model::SyntheticsAssertionJSONPathOperator;
10use datadog_api_client::datadogV1::model::SyntheticsAssertionJSONPathTarget;
11use datadog_api_client::datadogV1::model::SyntheticsAssertionJSONPathTargetTarget;
12use datadog_api_client::datadogV1::model::SyntheticsAssertionOperator;
13use datadog_api_client::datadogV1::model::SyntheticsAssertionTarget;
14use datadog_api_client::datadogV1::model::SyntheticsAssertionTargetValue;
15use datadog_api_client::datadogV1::model::SyntheticsAssertionTimingsScope;
16use datadog_api_client::datadogV1::model::SyntheticsAssertionType;
17use datadog_api_client::datadogV1::model::SyntheticsAssertionXPathOperator;
18use datadog_api_client::datadogV1::model::SyntheticsAssertionXPathTarget;
19use datadog_api_client::datadogV1::model::SyntheticsAssertionXPathTargetTarget;
20use datadog_api_client::datadogV1::model::SyntheticsBasicAuth;
21use datadog_api_client::datadogV1::model::SyntheticsBasicAuthOauthClient;
22use datadog_api_client::datadogV1::model::SyntheticsBasicAuthOauthClientType;
23use datadog_api_client::datadogV1::model::SyntheticsBasicAuthOauthTokenApiAuthentication;
24use datadog_api_client::datadogV1::model::SyntheticsConfigVariable;
25use datadog_api_client::datadogV1::model::SyntheticsConfigVariableType;
26use datadog_api_client::datadogV1::model::SyntheticsTestDetailsSubType;
27use datadog_api_client::datadogV1::model::SyntheticsTestOptions;
28use datadog_api_client::datadogV1::model::SyntheticsTestOptionsHTTPVersion;
29use datadog_api_client::datadogV1::model::SyntheticsTestOptionsRetry;
30use datadog_api_client::datadogV1::model::SyntheticsTestRequest;
31use datadog_api_client::datadogV1::model::SyntheticsTestRequestBodyFile;
32use datadog_api_client::datadogV1::model::SyntheticsTestRequestBodyType;
33use datadog_api_client::datadogV1::model::SyntheticsTestRequestCertificate;
34use datadog_api_client::datadogV1::model::SyntheticsTestRequestCertificateItem;
35use datadog_api_client::datadogV1::model::SyntheticsTestRequestProxy;
36use std::collections::BTreeMap;
37
38#[tokio::main]
39async fn main() {
40    let body =
41        SyntheticsAPITest::new(
42            SyntheticsAPITestConfig::new()
43                .assertions(
44                    vec![
45                        SyntheticsAssertion::SyntheticsAssertionTarget(
46                            Box::new(
47                                SyntheticsAssertionTarget::new(
48                                    SyntheticsAssertionOperator::IS,
49                                    SyntheticsAssertionTargetValue::SyntheticsAssertionTargetValueString(
50                                        "text/html".to_string(),
51                                    ),
52                                    SyntheticsAssertionType::HEADER,
53                                ).property("{{ PROPERTY }}".to_string()),
54                            ),
55                        ),
56                        SyntheticsAssertion::SyntheticsAssertionTarget(
57                            Box::new(
58                                SyntheticsAssertionTarget::new(
59                                    SyntheticsAssertionOperator::LESS_THAN,
60                                    SyntheticsAssertionTargetValue::SyntheticsAssertionTargetValueNumber(
61                                        2000.0 as f64,
62                                    ),
63                                    SyntheticsAssertionType::RESPONSE_TIME,
64                                ).timings_scope(SyntheticsAssertionTimingsScope::WITHOUT_DNS),
65                            ),
66                        ),
67                        SyntheticsAssertion::SyntheticsAssertionJSONPathTarget(
68                            Box::new(
69                                SyntheticsAssertionJSONPathTarget::new(
70                                    SyntheticsAssertionJSONPathOperator::VALIDATES_JSON_PATH,
71                                    SyntheticsAssertionType::BODY,
72                                ).target(
73                                    SyntheticsAssertionJSONPathTargetTarget::new()
74                                        .json_path("topKey".to_string())
75                                        .operator("isNot".to_string())
76                                        .target_value(
77                                            SyntheticsAssertionTargetValue::SyntheticsAssertionTargetValueString(
78                                                "0".to_string(),
79                                            ),
80                                        ),
81                                ),
82                            ),
83                        ),
84                        SyntheticsAssertion::SyntheticsAssertionXPathTarget(
85                            Box::new(
86                                SyntheticsAssertionXPathTarget::new(
87                                    SyntheticsAssertionXPathOperator::VALIDATES_X_PATH,
88                                    SyntheticsAssertionType::BODY,
89                                ).target(
90                                    SyntheticsAssertionXPathTargetTarget::new()
91                                        .operator("contains".to_string())
92                                        .target_value(
93                                            SyntheticsAssertionTargetValue::SyntheticsAssertionTargetValueString(
94                                                "0".to_string(),
95                                            ),
96                                        )
97                                        .x_path("target-xpath".to_string()),
98                                ),
99                            ),
100                        )
101                    ],
102                )
103                .config_variables(
104                    vec![
105                        SyntheticsConfigVariable::new("PROPERTY".to_string(), SyntheticsConfigVariableType::TEXT)
106                            .example("content-type".to_string())
107                            .pattern("content-type".to_string())
108                    ],
109                )
110                .request(
111                    SyntheticsTestRequest::new()
112                        .basic_auth(
113                            SyntheticsBasicAuth::SyntheticsBasicAuthOauthClient(
114                                Box::new(
115                                    SyntheticsBasicAuthOauthClient::new(
116                                        "https://datadog-token.com".to_string(),
117                                        "client-id".to_string(),
118                                        "client-secret".to_string(),
119                                        SyntheticsBasicAuthOauthTokenApiAuthentication::HEADER,
120                                        SyntheticsBasicAuthOauthClientType::OAUTH_CLIENT,
121                                    )
122                                        .audience("audience".to_string())
123                                        .resource("resource".to_string())
124                                        .scope("yoyo".to_string()),
125                                ),
126                            ),
127                        )
128                        .body_type(SyntheticsTestRequestBodyType::APPLICATION_OCTET_STREAM)
129                        .certificate(
130                            SyntheticsTestRequestCertificate::new()
131                                .cert(
132                                    SyntheticsTestRequestCertificateItem::new()
133                                        .content("cert-content".to_string())
134                                        .filename("cert-filename".to_string())
135                                        .updated_at("2020-10-16T09:23:24.857Z".to_string()),
136                                )
137                                .key(
138                                    SyntheticsTestRequestCertificateItem::new()
139                                        .content("key-content".to_string())
140                                        .filename("key-filename".to_string())
141                                        .updated_at("2020-10-16T09:23:24.857Z".to_string()),
142                                ),
143                        )
144                        .files(
145                            vec![
146                                SyntheticsTestRequestBodyFile::new()
147                                    .content("file content".to_string())
148                                    .name("file name".to_string())
149                                    .original_file_name("image.png".to_string())
150                                    .type_("file type".to_string())
151                            ],
152                        )
153                        .headers(BTreeMap::from([("unique".to_string(), "examplesynthetic".to_string())]))
154                        .method("GET".to_string())
155                        .persist_cookies(true)
156                        .proxy(
157                            SyntheticsTestRequestProxy::new(
158                                "https://datadoghq.com".to_string(),
159                            ).headers(BTreeMap::from([])),
160                        )
161                        .timeout(10.0 as f64)
162                        .url("https://datadoghq.com".to_string()),
163                ),
164            vec!["aws:us-east-2".to_string()],
165            "BDD test payload: synthetics_api_http_test_payload.json".to_string(),
166            "Example-Synthetic".to_string(),
167            SyntheticsTestOptions::new()
168                .accept_self_signed(false)
169                .allow_insecure(true)
170                .follow_redirects(true)
171                .http_version(SyntheticsTestOptionsHTTPVersion::HTTP2)
172                .min_failure_duration(10)
173                .min_location_failed(1)
174                .monitor_name("Example-Synthetic".to_string())
175                .monitor_priority(5)
176                .retry(SyntheticsTestOptionsRetry::new().count(3).interval(10.0 as f64))
177                .tick_every(60),
178            SyntheticsAPITestType::API,
179        )
180            .subtype(SyntheticsTestDetailsSubType::HTTP)
181            .tags(vec!["testing:api".to_string()]);
182    let configuration = datadog::Configuration::new();
183    let api = SyntheticsAPI::with_config(configuration);
184    let resp = api.create_synthetics_api_test(body).await;
185    if let Ok(value) = resp {
186        println!("{:#?}", value);
187    } else {
188        println!("{:#?}", resp.unwrap_err());
189    }
190}