0
# Client Factory Functions
1
2
**⚠️ DEPRECATED**: All client factory functions are deprecated as of version 1.1.28. Use [azure-identity](https://pypi.org/project/azure-identity/) and `AzureCliCredential` instead.
3
4
Client factory functions for creating Azure service clients from various authentication sources including CLI profiles, JSON authentication dictionaries, and authentication files. These functions provide automatic parameter filling and client instantiation.
5
6
## Migration Notice
7
8
These functions are no longer recommended. Use the modern authentication approach:
9
10
```python
11
from azure.identity import AzureCliCredential
12
from azure.mgmt.compute import ComputeManagementClient
13
14
# New recommended approach
15
credential = AzureCliCredential()
16
client = ComputeManagementClient(credential, subscription_id)
17
```
18
19
## Capabilities
20
21
### CLI Profile Client Creation
22
23
Create SDK clients using Azure CLI credentials and profile settings.
24
25
```python { .api }
26
def get_client_from_cli_profile(client_class, **kwargs):
27
"""
28
Return a SDK client initialized with current CLI credentials, CLI default
29
subscription and CLI default cloud.
30
31
**DEPRECATED**: This method is deprecated as of version 1.1.28.
32
Use azure-identity and AzureCliCredential instead.
33
34
This method is not working for azure-cli-core>=2.21.0 (released in March 2021).
35
36
For compatible azure-cli-core (< 2.20.0), this method will automatically
37
fill the following client parameters:
38
- credentials/credential
39
- subscription_id
40
- base_url
41
42
Parameters provided in kwargs will override CLI parameters and be passed
43
directly to the client.
44
45
Args:
46
client_class: A SDK client class to instantiate
47
**kwargs: Additional parameters passed to client constructor
48
49
Returns:
50
An instantiated client of the specified class
51
52
Raises:
53
ImportError: If azure-cli-core package is not available
54
55
Example:
56
from azure.common.client_factory import get_client_from_cli_profile
57
from azure.mgmt.compute import ComputeManagementClient
58
client = get_client_from_cli_profile(ComputeManagementClient)
59
"""
60
...
61
```
62
63
### JSON Dictionary Client Creation
64
65
Create SDK clients from JSON authentication dictionaries.
66
67
```python { .api }
68
def get_client_from_json_dict(client_class, config_dict, **kwargs):
69
"""
70
Return a SDK client initialized with a JSON auth dict.
71
72
**DEPRECATED**: This method is deprecated as of version 1.1.28.
73
Use azure-identity for modern authentication.
74
75
This method will fill automatically the following client parameters:
76
- credentials
77
- subscription_id
78
- base_url
79
- tenant_id
80
81
Parameters provided in kwargs will override parameters and be passed
82
directly to the client.
83
84
Args:
85
client_class: A SDK client class to instantiate
86
config_dict (dict): Authentication configuration dictionary
87
**kwargs: Additional parameters passed to client constructor
88
89
Returns:
90
An instantiated client of the specified class
91
92
Raises:
93
ValueError: For autorest v3/track2 clients (not supported)
94
95
Example:
96
from azure.common.client_factory import get_client_from_json_dict
97
from azure.mgmt.compute import ComputeManagementClient
98
99
config_dict = {
100
"clientId": "ad735158-65ca-11e7-ba4d-ecb1d756380e",
101
"clientSecret": "b70bb224-65ca-11e7-810c-ecb1d756380e",
102
"subscriptionId": "bfc42d3a-65ca-11e7-95cf-ecb1d756380e",
103
"tenantId": "c81da1d8-65ca-11e7-b1d1-ecb1d756380e",
104
"activeDirectoryEndpointUrl": "https://login.microsoftonline.com",
105
"resourceManagerEndpointUrl": "https://management.azure.com/",
106
"activeDirectoryGraphResourceId": "https://graph.windows.net/",
107
"sqlManagementEndpointUrl": "https://management.core.windows.net:8443/",
108
"galleryEndpointUrl": "https://gallery.azure.com/",
109
"managementEndpointUrl": "https://management.core.windows.net/"
110
}
111
client = get_client_from_json_dict(ComputeManagementClient, config_dict)
112
"""
113
...
114
```
115
116
### Authentication File Client Creation
117
118
Create SDK clients from authentication files.
119
120
```python { .api }
121
def get_client_from_auth_file(client_class, auth_path=None, **kwargs):
122
"""
123
Return a SDK client initialized with auth file.
124
125
**DEPRECATED**: This method is deprecated as of version 1.1.28.
126
Use azure-identity for modern authentication.
127
128
You can specify the file path directly, or fill the environment variable
129
AZURE_AUTH_LOCATION. File must be UTF-8.
130
131
This method will fill automatically the following client parameters:
132
- credentials
133
- subscription_id
134
- base_url
135
136
Parameters provided in kwargs will override parameters and be passed
137
directly to the client.
138
139
Args:
140
client_class: A SDK client class to instantiate
141
auth_path (str, optional): Path to the authentication file
142
**kwargs: Additional parameters passed to client constructor
143
144
Returns:
145
An instantiated client of the specified class
146
147
Raises:
148
KeyError: If AZURE_AUTH_LOCATION not set and no path provided
149
FileNotFoundError: If provided file path does not exist
150
json.JSONDecodeError: If provided file is not valid JSON
151
UnicodeDecodeError: If file is not UTF8 compliant
152
153
Example:
154
from azure.common.client_factory import get_client_from_auth_file
155
from azure.mgmt.compute import ComputeManagementClient
156
client = get_client_from_auth_file(ComputeManagementClient)
157
"""
158
...
159
```
160
161
### Internal Helper Functions
162
163
Internal utility functions used by the client factory methods.
164
165
```python { .api }
166
def _instantiate_client(client_class, **kwargs):
167
"""
168
Internal function to instantiate a client from kwargs, filtering kwargs
169
to match client signature.
170
171
Args:
172
client_class: The client class to instantiate
173
**kwargs: Parameters to pass to client constructor
174
175
Returns:
176
Instantiated client instance
177
"""
178
...
179
180
def _client_resource(client_class, cloud):
181
"""
182
Internal function to return resource and base URL for a client.
183
184
Args:
185
client_class: The client class to get resource info for
186
cloud: The cloud configuration object
187
188
Returns:
189
tuple: (resource, base_url) - either or both can be None
190
"""
191
...
192
193
def _is_autorest_v3(client_class):
194
"""
195
Internal function to detect if client is autorest v3/track2.
196
197
Args:
198
client_class: The client class to check
199
200
Returns:
201
bool: True if client is autorest v3/track2
202
"""
203
...
204
```
205
206
## Legacy Credential Functions
207
208
Functions for working with Azure CLI credentials (also deprecated).
209
210
```python { .api }
211
def get_cli_profile():
212
"""
213
Return a CLI profile class.
214
215
**DEPRECATED**: This method is deprecated as of version 1.1.28.
216
Not working for azure-cli-core>=2.21.0 (released in March 2021).
217
218
Returns:
219
azure.cli.core._profile.Profile: A CLI Profile instance
220
221
Raises:
222
ImportError: If azure-cli-core package is not available
223
"""
224
...
225
226
def get_azure_cli_credentials(resource=None, with_tenant=False):
227
"""
228
Return Credentials and default SubscriptionID of current loaded profile of the CLI.
229
230
**DEPRECATED**: This method is deprecated as of version 1.1.28.
231
Not working for azure-cli-core>=2.21.0 (released in March 2021).
232
233
Use azure-identity and AzureCliCredential instead.
234
235
Args:
236
resource (str, optional): Alternative resource for credentials if not ARM
237
with_tenant (bool): If True, return three-tuple with tenant ID
238
239
Returns:
240
tuple: (credentials, subscription_id) or (credentials, subscription_id, tenant_id)
241
242
Raises:
243
NotImplementedError: For azure-cli-core>=2.21.0
244
"""
245
...
246
247
def get_cli_active_cloud():
248
"""
249
Return a CLI active cloud.
250
251
**DEPRECATED**: This method is deprecated as of version 1.1.28.
252
Not working for azure-cli-core>=2.21.0 (released in March 2021).
253
254
Returns:
255
azure.cli.core.cloud.Cloud: A CLI Cloud instance
256
257
Raises:
258
ImportError: If azure-cli-core package is not available
259
"""
260
...
261
```
262
263
## Authentication File Format
264
265
The authentication file should be a UTF-8 encoded JSON file with the following structure:
266
267
```json
268
{
269
"clientId": "ad735158-65ca-11e7-ba4d-ecb1d756380e",
270
"clientSecret": "b70bb224-65ca-11e7-810c-ecb1d756380e",
271
"subscriptionId": "bfc42d3a-65ca-11e7-95cf-ecb1d756380e",
272
"tenantId": "c81da1d8-65ca-11e7-b1d1-ecb1d756380e",
273
"activeDirectoryEndpointUrl": "https://login.microsoftonline.com",
274
"resourceManagerEndpointUrl": "https://management.azure.com/",
275
"activeDirectoryGraphResourceId": "https://graph.windows.net/",
276
"sqlManagementEndpointUrl": "https://management.core.windows.net:8443/",
277
"galleryEndpointUrl": "https://gallery.azure.com/",
278
"managementEndpointUrl": "https://management.core.windows.net/"
279
}
280
```
281
282
## Migration Examples
283
284
### From CLI Profile to Azure Identity
285
286
**Old approach (deprecated):**
287
```python
288
from azure.common.client_factory import get_client_from_cli_profile
289
from azure.mgmt.compute import ComputeManagementClient
290
291
client = get_client_from_cli_profile(ComputeManagementClient)
292
```
293
294
**New approach:**
295
```python
296
from azure.identity import AzureCliCredential
297
from azure.mgmt.compute import ComputeManagementClient
298
299
credential = AzureCliCredential()
300
client = ComputeManagementClient(credential, subscription_id)
301
```
302
303
### From Auth File to Azure Identity
304
305
**Old approach (deprecated):**
306
```python
307
from azure.common.client_factory import get_client_from_auth_file
308
from azure.mgmt.compute import ComputeManagementClient
309
310
client = get_client_from_auth_file(ComputeManagementClient, "path/to/auth.json")
311
```
312
313
**New approach:**
314
```python
315
from azure.identity import ClientSecretCredential
316
from azure.mgmt.compute import ComputeManagementClient
317
import json
318
319
with open("path/to/auth.json") as f:
320
auth_data = json.load(f)
321
322
credential = ClientSecretCredential(
323
tenant_id=auth_data["tenantId"],
324
client_id=auth_data["clientId"],
325
client_secret=auth_data["clientSecret"]
326
)
327
client = ComputeManagementClient(credential, auth_data["subscriptionId"])
328
```