Skip to content

Commit

Permalink
Merge branch 'master' into hzhang-use-request-data-for-undo
Browse files Browse the repository at this point in the history
  • Loading branch information
HantingZhang2 authored Feb 22, 2024
2 parents 157b692 + f853875 commit 7ba2e9e
Show file tree
Hide file tree
Showing 25 changed files with 1,136 additions and 609 deletions.
270 changes: 145 additions & 125 deletions .generator/poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .generator/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "Apache-2.0"
[tool.poetry.dependencies]
python = "^3.10"
click = "^8.1.4"
PyYAML = "^6.0"
PyYAML = "^6.0.1"
jsonref = "^1.1.0"
jinja2 = "^3.1.2"
pytest = "^7.4.0"
Expand Down
17 changes: 17 additions & 0 deletions .generator/src/generator/templates/api.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
use reqwest;
use serde::{Serialize, Deserialize};
use crate::datadog::*;
{%- for _, _, operation in operations if "x-unstable" in operation %}
{%- if loop.first %}
use log::warn;
{%- endif %}
{%- endfor %}

{%- set structName = name.replace(" ", "")+"API" %}
{% for path, method, operation in operations|sort(attribute="2.operationId", case_sensitive=true) %}
Expand Down Expand Up @@ -104,6 +109,18 @@ impl {{ structName }} {
{{ operation.description | block_comment }}
{%- endif %}
pub async fn {{operation.operationId | snake_case}}_with_http_info(&self{% for name, parameter in requiredParams %}, {{name|variable_name}}: {{ get_type_for_parameter(parameter, version) }}{% endfor %}{% if operation|has_optional_parameter %}, params: {{operation.operationId}}OptionalParams{% endif %}) -> Result<ResponseContent<{% if returnType %}{{returnType}}{% else %}(){% endif %}>, Error<{{operation.operationId}}Error>> {
{%- if "x-unstable" in operation %}
let operation_id = "{{ version }}.{{ operation.operationId | snake_case }}".to_string();
if self.config.is_unstable_operation_enabled(&operation_id) {
warn!("Using unstable operation {}", operation_id);
} else {
let local_error = UnstableOperationDisabledError {
msg: "Operation '{{ version }}.{{ operation.operationId | snake_case }}' is not enabled".to_string(),
};
return Err(Error::UnstableOperationDisabledError(local_error));
}
{%- endif %}

let local_configuration = &self.config;

{% for name, parameter in operation|parameters if parameter.required != true %}
Expand Down
8 changes: 8 additions & 0 deletions .generator/src/generator/templates/common_mod.j2
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ pub struct ResponseContent<T> {
pub entity: Option<T>,
}

#[derive(Debug, Clone)]
pub struct UnstableOperationDisabledError {
pub msg: String,
}

#[derive(Debug)]
pub enum Error<T> {
Reqwest(reqwest::Error),
ReqwestMiddleware(reqwest_middleware::Error),
Serde(serde_json::Error),
Io(std::io::Error),
ResponseError(ResponseContent<T>),
UnstableOperationDisabledError(UnstableOperationDisabledError)
}

impl <T> fmt::Display for Error<T> {
Expand All @@ -25,6 +31,7 @@ impl <T> fmt::Display for Error<T> {
Error::Serde(e) => ("serde", e.to_string()),
Error::Io(e) => ("IO", e.to_string()),
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
Error::UnstableOperationDisabledError(e) => ("unstable_operation_disabled", e.msg.to_string()),
};
write!(f, "error in {}: {}", module, e)
}
Expand All @@ -38,6 +45,7 @@ impl <T: fmt::Debug> error::Error for Error<T> {
Error::Serde(e) => e,
Error::Io(e) => e,
Error::ResponseError(_) => return None,
Error::UnstableOperationDisabledError(_) => return None,
})
}
}
Expand Down
52 changes: 51 additions & 1 deletion .generator/src/generator/templates/configuration.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% include "partial_header.j2" %}
use std::env;
use std::collections::HashMap;
use log::warn;

#[derive(Debug, Clone)]
pub struct Configuration {
Expand All @@ -17,18 +19,65 @@ pub struct Configuration {
{%- endfor %}
{%- endfor %}
{%- endif %}

unstable_operations: HashMap<String, bool>,
}

impl Configuration {
pub fn new() -> Configuration {
Configuration::default()
}

pub fn set_unstable_operation_enabled(&mut self, operation: &str, enabled: bool) -> bool {
if self.unstable_operations.contains_key(operation) {
self.unstable_operations.insert(operation.to_string(), enabled);
return true;
}

warn!(
"Operation {} is not an unstable operation, can't enable/disable",
operation
);

false
}

pub fn is_unstable_operation_enabled(&self, operation: &str) -> bool {
if self.unstable_operations.contains_key(operation) {
return self.unstable_operations.get(operation).unwrap().clone();
}

warn!(
"Operation {} is not an unstable operation, is always enabled",
operation
);

false
}

pub fn is_unstable_operation(&self, operation: &str) -> bool {
if self.unstable_operations.contains_key(operation) {
return true;
}

false
}
}

impl Default for Configuration {
fn default() -> Self {
let http_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new());
let unstable_operations = HashMap::from([
{%- for version, api in apis.items() %}
{%- for operations in api.values() %}
{%- for _, _, operation in operations|sort(attribute="2.operationId") %}
{%- if "x-unstable" in operation %}
("{{ version }}.{{ operation.operationId | snake_case }}".to_owned(), false),
{%- endif %}
{%- endfor %}
{%- endfor %}
{%- endfor %}
]);

Configuration {
base_path: "https://api.datadoghq.com".to_owned(),
user_agent: Some(format!(
Expand All @@ -50,6 +99,7 @@ impl Default for Configuration {
{%- endfor %}
{%- endfor %}
{%- endif %}
unstable_operations,
}
}
}
4 changes: 1 addition & 3 deletions .generator/src/generator/templates/function_mappings.j2
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ pub fn initialize_api_instance(world: &mut DatadogWorld, api: String) {
{%- set structName = name.replace(" ", "")+"API" %}
"{{name.replace(" ", "")}}" => {
{%- for version in versions %}
if world.api_instances.{{version}}_{{fieldName}}.is_none() {
world.api_instances.{{version}}_{{fieldName}} = Some(datadog{{ version.upper() }}::api::{{fieldName}}::{{structName}}::with_config(world.config.clone()));
}
world.api_instances.{{version}}_{{fieldName}} = Some(datadog{{ version.upper() }}::api::{{fieldName}}::{{structName}}::with_config(world.config.clone()));
{%- endfor %}
},
{%- endfor %}
Expand Down
3 changes: 3 additions & 0 deletions .generator/src/generator/templates/model_simple.j2
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub struct {{ name }} {
{%- endif %}
{%- if schema.deprecated %}
#[deprecated]
{%- endif %}
{%- if required and nullable %}
#[serialize_always]
{%- endif %}
#[serde(rename = "{{ attr }}"{% if not required and nullable%}, default, with = "::serde_with::rust::double_option"{% endif %})]
pub {{propertyName}}: {{dataType}},
Expand Down
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/target/
**/*.rs.bk
Cargo.lock
__pycache__
__pycache__

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ sha256 = "1.4.0"
tokio = { version = "1.10", features = ["macros", "rt-multi-thread", "time"] }
lazy_static = "1.4.0"
minijinja = "1.0.10"
convert_case = "0.6.0"

[[test]]
harness = false # allows Cucumber to print output instead of libtest
Expand Down
85 changes: 85 additions & 0 deletions src/datadog/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.
use log::warn;
use std::collections::HashMap;
use std::env;

#[derive(Debug, Clone)]
Expand All @@ -10,17 +12,99 @@ pub struct Configuration {
pub client: reqwest_middleware::ClientWithMiddleware,
pub api_key_auth: Option<String>,
pub app_key_auth: Option<String>,
unstable_operations: HashMap<String, bool>,
}

impl Configuration {
pub fn new() -> Configuration {
Configuration::default()
}

pub fn set_unstable_operation_enabled(&mut self, operation: &str, enabled: bool) -> bool {
if self.unstable_operations.contains_key(operation) {
self.unstable_operations
.insert(operation.to_string(), enabled);
return true;
}

warn!(
"Operation {} is not an unstable operation, can't enable/disable",
operation
);

false
}

pub fn is_unstable_operation_enabled(&self, operation: &str) -> bool {
if self.unstable_operations.contains_key(operation) {
return self.unstable_operations.get(operation).unwrap().clone();
}

warn!(
"Operation {} is not an unstable operation, is always enabled",
operation
);

false
}

pub fn is_unstable_operation(&self, operation: &str) -> bool {
if self.unstable_operations.contains_key(operation) {
return true;
}

false
}
}

impl Default for Configuration {
fn default() -> Self {
let http_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new());
let unstable_operations = HashMap::from([
("v2.get_active_billing_dimensions".to_owned(), false),
("v2.get_monthly_cost_attribution".to_owned(), false),
("v2.create_dora_deployment".to_owned(), false),
("v2.create_dora_incident".to_owned(), false),
("v2.create_incident".to_owned(), false),
("v2.create_incident_integration".to_owned(), false),
("v2.create_incident_todo".to_owned(), false),
("v2.delete_incident".to_owned(), false),
("v2.delete_incident_integration".to_owned(), false),
("v2.delete_incident_todo".to_owned(), false),
("v2.get_incident".to_owned(), false),
("v2.get_incident_integration".to_owned(), false),
("v2.get_incident_todo".to_owned(), false),
("v2.list_incident_attachments".to_owned(), false),
("v2.list_incident_integrations".to_owned(), false),
("v2.list_incidents".to_owned(), false),
("v2.list_incident_todos".to_owned(), false),
("v2.search_incidents".to_owned(), false),
("v2.update_incident".to_owned(), false),
("v2.update_incident_attachments".to_owned(), false),
("v2.update_incident_integration".to_owned(), false),
("v2.update_incident_todo".to_owned(), false),
("v2.query_scalar_data".to_owned(), false),
("v2.query_timeseries_data".to_owned(), false),
("v2.get_finding".to_owned(), false),
("v2.list_findings".to_owned(), false),
("v2.mute_findings".to_owned(), false),
("v2.create_scorecard_outcomes_batch".to_owned(), false),
("v2.create_scorecard_rule".to_owned(), false),
("v2.delete_scorecard_rule".to_owned(), false),
("v2.list_scorecard_outcomes".to_owned(), false),
("v2.list_scorecard_rules".to_owned(), false),
("v2.create_incident_service".to_owned(), false),
("v2.delete_incident_service".to_owned(), false),
("v2.get_incident_service".to_owned(), false),
("v2.list_incident_services".to_owned(), false),
("v2.update_incident_service".to_owned(), false),
("v2.create_incident_team".to_owned(), false),
("v2.delete_incident_team".to_owned(), false),
("v2.get_incident_team".to_owned(), false),
("v2.list_incident_teams".to_owned(), false),
("v2.update_incident_team".to_owned(), false),
]);

Configuration {
base_path: "https://api.datadoghq.com".to_owned(),
user_agent: Some(format!(
Expand All @@ -33,6 +117,7 @@ impl Default for Configuration {
client: http_client.build(),
api_key_auth: env::var("DD_API_KEY").ok(),
app_key_auth: env::var("DD_APP_KEY").ok(),
unstable_operations,
}
}
}
10 changes: 10 additions & 0 deletions src/datadog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ pub struct ResponseContent<T> {
pub entity: Option<T>,
}

#[derive(Debug, Clone)]
pub struct UnstableOperationDisabledError {
pub msg: String,
}

#[derive(Debug)]
pub enum Error<T> {
Reqwest(reqwest::Error),
ReqwestMiddleware(reqwest_middleware::Error),
Serde(serde_json::Error),
Io(std::io::Error),
ResponseError(ResponseContent<T>),
UnstableOperationDisabledError(UnstableOperationDisabledError),
}

impl<T> fmt::Display for Error<T> {
Expand All @@ -25,6 +31,9 @@ impl<T> fmt::Display for Error<T> {
Error::Serde(e) => ("serde", e.to_string()),
Error::Io(e) => ("IO", e.to_string()),
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
Error::UnstableOperationDisabledError(e) => {
("unstable_operation_disabled", e.msg.to_string())
}
};
write!(f, "error in {}: {}", module, e)
}
Expand All @@ -38,6 +47,7 @@ impl<T: fmt::Debug> error::Error for Error<T> {
Error::Serde(e) => e,
Error::Io(e) => e,
Error::ResponseError(_) => return None,
Error::UnstableOperationDisabledError(_) => return None,
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde_with::skip_serializing_none;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SharedDashboardUpdateRequest {
/// Timeframe setting for the shared dashboard.
#[serialize_always]
#[serde(rename = "global_time")]
pub global_time: Option<crate::datadogV1::model::SharedDashboardUpdateRequestGlobalTime>,
/// Whether to allow viewers to select a different global time setting for the shared dashboard.
Expand Down
Loading

0 comments on commit 7ba2e9e

Please sign in to comment.