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