Skip to content

Commit 2a9453b

Browse files
authored
Allow idiomatic creation of credential options with default values (Azure#2699)
1 parent 3627ab7 commit 2a9453b

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

sdk/identity/azure_identity/src/azure_cli_credential.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ impl OutputProcessor for CliTokenResponse {
6969
/// Authenticates the identity logged in to the [Azure CLI](https://learn.microsoft.com/cli/azure/what-is-azure-cli).
7070
#[derive(Debug)]
7171
pub struct AzureCliCredential {
72-
options: AzureCliCredentialOptions,
72+
env: Env,
73+
executor: Arc<dyn Executor>,
74+
subscription: Option<String>,
75+
tenant_id: Option<String>,
7376
}
7477

7578
/// Options for constructing an [`AzureCliCredential`].
@@ -97,27 +100,31 @@ pub struct AzureCliCredentialOptions {
97100
/// you can supply your own implementation using a different asynchronous runtime.
98101
pub executor: Option<Arc<dyn Executor>>,
99102

103+
#[cfg(test)]
100104
env: Option<Env>,
101105
}
102106

103107
impl AzureCliCredential {
104108
/// Create a new `AzureCliCredential`.
105109
pub fn new(options: Option<AzureCliCredentialOptions>) -> azure_core::Result<Arc<Self>> {
106-
let mut options = options.unwrap_or_default();
110+
let options = options.unwrap_or_default();
107111
if let Some(ref tenant_id) = options.tenant_id {
108112
validate_tenant_id(tenant_id)?;
109113
}
110114
if let Some(ref subscription) = options.subscription {
111115
validate_subscription(subscription)?;
112116
}
113-
if options.env.is_none() {
114-
options.env = Some(Env::default());
115-
}
116-
if options.executor.is_none() {
117-
options.executor = Some(new_executor());
118-
}
119-
120-
Ok(Arc::new(Self { options }))
117+
#[cfg(test)]
118+
let env = options.env.unwrap_or_default();
119+
#[cfg(not(test))]
120+
let env = Env::default();
121+
122+
Ok(Arc::new(Self {
123+
env,
124+
executor: options.executor.unwrap_or(new_executor()),
125+
subscription: options.subscription,
126+
tenant_id: options.tenant_id,
127+
}))
121128
}
122129
}
123130

@@ -140,25 +147,19 @@ impl TokenCredential for AzureCliCredential {
140147

141148
let mut command = OsString::from("az account get-access-token -o json --scope ");
142149
command.push(scopes[0]);
143-
if let Some(ref tenant_id) = self.options.tenant_id {
150+
if let Some(ref tenant_id) = self.tenant_id {
144151
command.push(" --tenant ");
145152
command.push(tenant_id);
146153
}
147-
if let Some(ref subscription) = self.options.subscription {
154+
if let Some(ref subscription) = self.subscription {
148155
command.push(r#" --subscription ""#);
149156
command.push(subscription);
150157
command.push("\"");
151158
}
152159

153160
trace!("running Azure CLI command: {command:?}");
154161

155-
shell_exec::<CliTokenResponse>(
156-
// unwrap() is safe because new() ensured the values are Some
157-
self.options.executor.clone().unwrap(),
158-
self.options.env.as_ref().unwrap(),
159-
&command,
160-
)
161-
.await
162+
shell_exec::<CliTokenResponse>(self.executor.clone(), &self.env, &command).await
162163
}
163164
}
164165

sdk/identity/azure_identity/src/azure_developer_cli_credential.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub struct AzureDeveloperCliCredentialOptions {
7979
/// Defaults to the azd environment, which is the tenant of the selected Azure subscription.
8080
pub tenant_id: Option<String>,
8181

82+
#[cfg(test)]
8283
env: Option<Env>,
8384
}
8485

@@ -91,7 +92,10 @@ impl AzureDeveloperCliCredential {
9192
if let Some(ref tenant_id) = options.tenant_id {
9293
validate_tenant_id(tenant_id)?;
9394
}
95+
#[cfg(test)]
9496
let env = options.env.unwrap_or_default();
97+
#[cfg(not(test))]
98+
let env = Env::default();
9599
let executor = options.executor.unwrap_or(new_executor());
96100
Ok(Arc::new(Self {
97101
env,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
use azure_identity::{AzureCliCredentialOptions, AzureDeveloperCliCredentialOptions};
5+
6+
#[test]
7+
fn az_credential_options() {
8+
let _options = AzureCliCredentialOptions {
9+
..Default::default()
10+
};
11+
}
12+
13+
#[test]
14+
fn azd_credential_options() {
15+
let _options = AzureDeveloperCliCredentialOptions {
16+
..Default::default()
17+
};
18+
}

0 commit comments

Comments
 (0)