Skip to content

use TokenCredential in services #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion sdk/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async-trait = "0.1.36"
oauth2 = { version = "4.0.0-alpha.2" }

[dev-dependencies]
tokio = "0.3"
tokio = "0.2"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got some runtime errors regarding the hyper dependency. I think we may need to wait for hyper to upgrade.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created #60 to track upgrading.

env_logger = "0.8"

[features]
Expand Down
2 changes: 2 additions & 0 deletions services/mgmt/addons/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ version = "0.1.0"
edition = "2018"

[dependencies]
azure_core = { path = "../../../sdk/core", version = "0.1.0" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
reqwest = { version = "0.10", features = ["json"] }
bytes = "0.5"
snafu = "0.6"

[dev-dependencies]
azure_identity = { path = "../../../sdk/identity", version = "0.1.0" }
tokio = { version = "0.2", features = ["macros"] }

[features]
Expand Down
10 changes: 6 additions & 4 deletions services/mgmt/addons/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ pub struct OperationConfig {
pub api_version: String,
pub client: reqwest::Client,
pub base_path: String,
pub bearer_access_token: Option<String>,
pub token_credential: Option<Box<dyn azure_core::TokenCredential>>,
pub token_credential_resource: String,
}
impl OperationConfig {
pub fn new(bearer_access_token: &str) -> Self {
pub fn new(token_credential: Box<dyn azure_core::TokenCredential>) -> Self {
Self {
bearer_access_token: Some(bearer_access_token.to_owned()),
token_credential: Some(token_credential),
..Default::default()
}
}
Expand All @@ -27,7 +28,8 @@ impl Default for OperationConfig {
api_version: API_VERSION.to_owned(),
client: reqwest::Client::new(),
base_path: "https://management.azure.com".to_owned(),
bearer_access_token: None,
token_credential: None,
token_credential_resource: "https://management.azure.com/".to_owned(),
}
}
}
2 changes: 1 addition & 1 deletion services/mgmt/addons/src/package_2017_05/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct CanonicalSupportPlanProperties {
#[serde(rename = "provisioningState", skip_serializing_if = "Option::is_none")]
pub provisioning_state: Option<canonical_support_plan_properties::ProvisioningState>,
}
mod canonical_support_plan_properties {
pub mod canonical_support_plan_properties {
use super::*;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum ProvisioningState {
Expand Down
55 changes: 45 additions & 10 deletions services/mgmt/addons/src/package_2017_05/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ pub mod operations {
let client = &operation_config.client;
let uri_str = &format!("{}/providers/Microsoft.Addons/operations", &operation_config.base_path,);
let mut req_builder = client.get(uri_str);
if let Some(token) = &operation_config.bearer_access_token {
req_builder = req_builder.bearer_auth(token);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(list::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let req = req_builder.build().context(list::BuildRequestError)?;
Expand Down Expand Up @@ -60,6 +64,9 @@ pub mod operations {
source: serde_json::Error,
body: bytes::Bytes,
},
GetTokenError {
source: azure_core::errors::AzureError,
},
}
}
}
Expand All @@ -79,8 +86,12 @@ pub mod support_plan_types {
&operation_config.base_path, subscription_id, provider_name, plan_type_name
);
let mut req_builder = client.get(uri_str);
if let Some(token) = &operation_config.bearer_access_token {
req_builder = req_builder.bearer_auth(token);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(get::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let req = req_builder.build().context(get::BuildRequestError)?;
Expand Down Expand Up @@ -129,6 +140,9 @@ pub mod support_plan_types {
source: serde_json::Error,
body: bytes::Bytes,
},
GetTokenError {
source: azure_core::errors::AzureError,
},
}
}
pub async fn create_or_update(
Expand All @@ -143,8 +157,12 @@ pub mod support_plan_types {
&operation_config.base_path, subscription_id, provider_name, plan_type_name
);
let mut req_builder = client.put(uri_str);
if let Some(token) = &operation_config.bearer_access_token {
req_builder = req_builder.bearer_auth(token);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(create_or_update::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let req = req_builder.build().context(create_or_update::BuildRequestError)?;
Expand Down Expand Up @@ -204,6 +222,9 @@ pub mod support_plan_types {
source: serde_json::Error,
body: bytes::Bytes,
},
GetTokenError {
source: azure_core::errors::AzureError,
},
}
}
pub async fn delete(
Expand All @@ -218,8 +239,12 @@ pub mod support_plan_types {
&operation_config.base_path, subscription_id, provider_name, plan_type_name
);
let mut req_builder = client.delete(uri_str);
if let Some(token) = &operation_config.bearer_access_token {
req_builder = req_builder.bearer_auth(token);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(delete::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let req = req_builder.build().context(delete::BuildRequestError)?;
Expand Down Expand Up @@ -272,6 +297,9 @@ pub mod support_plan_types {
source: serde_json::Error,
body: bytes::Bytes,
},
GetTokenError {
source: azure_core::errors::AzureError,
},
}
}
}
Expand All @@ -290,8 +318,12 @@ pub mod canonical_support_plan_types {
&operation_config.base_path, subscription_id, provider_name
);
let mut req_builder = client.get(uri_str);
if let Some(token) = &operation_config.bearer_access_token {
req_builder = req_builder.bearer_auth(token);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(get::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let req = req_builder.build().context(get::BuildRequestError)?;
Expand Down Expand Up @@ -337,6 +369,9 @@ pub mod canonical_support_plan_types {
source: serde_json::Error,
body: bytes::Bytes,
},
GetTokenError {
source: azure_core::errors::AzureError,
},
}
}
}
4 changes: 2 additions & 2 deletions services/mgmt/addons/src/package_2018_03/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct CanonicalSupportPlanProperties {
#[serde(rename = "provisioningState", skip_serializing_if = "Option::is_none")]
pub provisioning_state: Option<canonical_support_plan_properties::ProvisioningState>,
}
mod canonical_support_plan_properties {
pub mod canonical_support_plan_properties {
use super::*;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum ProvisioningState {
Expand Down Expand Up @@ -40,7 +40,7 @@ pub struct CanonicalSupportPlanInfoDefinition {
#[serde(rename = "oneTimeCharge", skip_serializing_if = "Option::is_none")]
pub one_time_charge: Option<canonical_support_plan_info_definition::OneTimeCharge>,
}
mod canonical_support_plan_info_definition {
pub mod canonical_support_plan_info_definition {
use super::*;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum SupportPlanType {
Expand Down
55 changes: 45 additions & 10 deletions services/mgmt/addons/src/package_2018_03/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ pub mod operations {
let client = &operation_config.client;
let uri_str = &format!("{}/providers/Microsoft.Addons/operations", &operation_config.base_path,);
let mut req_builder = client.get(uri_str);
if let Some(token) = &operation_config.bearer_access_token {
req_builder = req_builder.bearer_auth(token);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(list::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let req = req_builder.build().context(list::BuildRequestError)?;
Expand Down Expand Up @@ -60,6 +64,9 @@ pub mod operations {
source: serde_json::Error,
body: bytes::Bytes,
},
GetTokenError {
source: azure_core::errors::AzureError,
},
}
}
}
Expand All @@ -79,8 +86,12 @@ pub mod support_plan_types {
&operation_config.base_path, subscription_id, provider_name, plan_type_name
);
let mut req_builder = client.get(uri_str);
if let Some(token) = &operation_config.bearer_access_token {
req_builder = req_builder.bearer_auth(token);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(get::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let req = req_builder.build().context(get::BuildRequestError)?;
Expand Down Expand Up @@ -129,6 +140,9 @@ pub mod support_plan_types {
source: serde_json::Error,
body: bytes::Bytes,
},
GetTokenError {
source: azure_core::errors::AzureError,
},
}
}
pub async fn create_or_update(
Expand All @@ -143,8 +157,12 @@ pub mod support_plan_types {
&operation_config.base_path, subscription_id, provider_name, plan_type_name
);
let mut req_builder = client.put(uri_str);
if let Some(token) = &operation_config.bearer_access_token {
req_builder = req_builder.bearer_auth(token);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(create_or_update::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let req = req_builder.build().context(create_or_update::BuildRequestError)?;
Expand Down Expand Up @@ -204,6 +222,9 @@ pub mod support_plan_types {
source: serde_json::Error,
body: bytes::Bytes,
},
GetTokenError {
source: azure_core::errors::AzureError,
},
}
}
pub async fn delete(
Expand All @@ -218,8 +239,12 @@ pub mod support_plan_types {
&operation_config.base_path, subscription_id, provider_name, plan_type_name
);
let mut req_builder = client.delete(uri_str);
if let Some(token) = &operation_config.bearer_access_token {
req_builder = req_builder.bearer_auth(token);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(delete::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let req = req_builder.build().context(delete::BuildRequestError)?;
Expand Down Expand Up @@ -272,6 +297,9 @@ pub mod support_plan_types {
source: serde_json::Error,
body: bytes::Bytes,
},
GetTokenError {
source: azure_core::errors::AzureError,
},
}
}
pub async fn list_info(
Expand All @@ -284,8 +312,12 @@ pub mod support_plan_types {
&operation_config.base_path, subscription_id
);
let mut req_builder = client.post(uri_str);
if let Some(token) = &operation_config.bearer_access_token {
req_builder = req_builder.bearer_auth(token);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(list_info::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let req = req_builder.build().context(list_info::BuildRequestError)?;
Expand Down Expand Up @@ -333,6 +365,9 @@ pub mod support_plan_types {
source: serde_json::Error,
body: bytes::Bytes,
},
GetTokenError {
source: azure_core::errors::AzureError,
},
}
}
}
2 changes: 2 additions & 0 deletions services/mgmt/adp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ version = "0.1.0"
edition = "2018"

[dependencies]
azure_core = { path = "../../../sdk/core", version = "0.1.0" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
reqwest = { version = "0.10", features = ["json"] }
bytes = "0.5"
snafu = "0.6"

[dev-dependencies]
azure_identity = { path = "../../../sdk/identity", version = "0.1.0" }
tokio = { version = "0.2", features = ["macros"] }

[features]
Expand Down
10 changes: 6 additions & 4 deletions services/mgmt/adp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ pub struct OperationConfig {
pub api_version: String,
pub client: reqwest::Client,
pub base_path: String,
pub bearer_access_token: Option<String>,
pub token_credential: Option<Box<dyn azure_core::TokenCredential>>,
pub token_credential_resource: String,
}
impl OperationConfig {
pub fn new(bearer_access_token: &str) -> Self {
pub fn new(token_credential: Box<dyn azure_core::TokenCredential>) -> Self {
Self {
bearer_access_token: Some(bearer_access_token.to_owned()),
token_credential: Some(token_credential),
..Default::default()
}
}
Expand All @@ -23,7 +24,8 @@ impl Default for OperationConfig {
api_version: API_VERSION.to_owned(),
client: reqwest::Client::new(),
base_path: "https://management.azure.com".to_owned(),
bearer_access_token: None,
token_credential: None,
token_credential_resource: "https://management.azure.com/".to_owned(),
}
}
}
6 changes: 3 additions & 3 deletions services/mgmt/adp/src/package_2020_07_01_preview/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct AccountProperties {
#[serde(rename = "provisioningState", skip_serializing)]
pub provisioning_state: Option<account_properties::ProvisioningState>,
}
mod account_properties {
pub mod account_properties {
use super::*;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum ProvisioningState {
Expand Down Expand Up @@ -72,7 +72,7 @@ pub struct DataPoolBaseProperties {
#[serde(skip_serializing_if = "Vec::is_empty")]
pub locations: Vec<DataPoolLocation>,
}
mod data_pool_base_properties {
pub mod data_pool_base_properties {
use super::*;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum ProvisioningState {
Expand Down Expand Up @@ -209,7 +209,7 @@ pub struct SystemData {
#[serde(rename = "lastModifiedAt", skip_serializing_if = "Option::is_none")]
pub last_modified_at: Option<String>,
}
mod system_data {
pub mod system_data {
use super::*;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum CreatedByType {
Expand Down
Loading