File size: 5,810 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
Multi-cloud - use Azure on all regions
======================================

You can use the Azure SDK for Python to connect to all regions where Azure is available
(`list of Azure regions is available here <https://azure.microsoft.com/regions/services>`_).

By default, the Azure SDK for Python is configured to connect to public Azure.

Using predeclared cloud definition
----------------------------------

.. important:: The `msrestazure` package must be superior or equals to 0.4.11 for this section.

You can use the `azure_cloud` module of `msrestazure`

.. code:: python

  from msrestazure.azure_cloud import AZURE_CHINA_CLOUD
  from msrestazure.azure_active_directory import UserPassCredentials
  from azure.mgmt.resource import ResourceManagementClient

  credentials = UserPassCredentials(
      login,
      password,
      cloud_environment=AZURE_CHINA_CLOUD
  )
  client = ResourceManagementClient(
      credentials,
      subscription_id,
      base_url=AZURE_CHINA_CLOUD.endpoints.resource_manager
  )
  
Available cloud definition are
  - AZURE_PUBLIC_CLOUD
  - AZURE_CHINA_CLOUD
  - AZURE_US_GOV_CLOUD
  - AZURE_GERMAN_CLOUD

Using your own cloud definition (e.g. Azure Stack)
--------------------------------------------------

ARM has a metadata endpoint to help you:

.. code:: python

  from msrestazure.azure_cloud import get_cloud_from_metadata_endpoint
  from msrestazure.azure_active_directory import UserPassCredentials
  from azure.mgmt.resource import ResourceManagementClient

  mystack_cloud = get_cloud_from_metadata_endpoint("https://myazurestack-arm-endpoint.com")
  credentials = UserPassCredentials(
      login,
      password,
      cloud_environment=mystack_cloud
  )
  client = ResourceManagementClient(
      credentials,
      subscription_id,
      base_url=mystack_cloud.endpoints.resource_manager
  )

Using ADAL
----------

To connect to another region, a few things have to be considered:

- What is the endpoint where to ask for a token (authentication)?
- What is the endpoint where I will use this token (usage)?

This is a generic example:

.. code:: python

    import adal
    from msrestazure.azure_active_directory import AdalAuthentication
    from azure.mgmt.resource import ResourceManagementClient

    # Service Principal
    tenant = 'ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL'
    client_id = 'ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL'
    password = 'password

    # Public Azure - default values
    authentication_endpoint = 'https://login.microsoftonline.com/'
    azure_endpoint = 'https://management.azure.com/'
        
    context = adal.AuthenticationContext(authentication_endpoint+tenant)
    credentials = AdalAuthentication(
        context.acquire_token_with_client_credentials,
        azure_endpoint,
        client_id,
        password
    )
    subscription_id = '33333333-3333-3333-3333-333333333333'

    resource_client = ResourceManagementClient(
        credentials,
        subscription_id,
        base_url=azure_endpoint
    )


Azure Government
~~~~~~~~~~~~~~~~

.. code:: python

    import adal
    from msrestazure.azure_active_directory import AdalAuthentication
    from azure.mgmt.resource import ResourceManagementClient

    # Service Principal
    tenant = 'ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL'
    client_id = 'ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL'
    password = 'password

    # Government
    authentication_endpoint = 'https://login.microsoftonline.us/'
    azure_endpoint = 'https://management.usgovcloudapi.net/'
        
    context = adal.AuthenticationContext(authentication_endpoint+tenant)
    credentials = AdalAuthentication(
        context.acquire_token_with_client_credentials,
        azure_endpoint,
        client_id,
        password
    )
    subscription_id = '33333333-3333-3333-3333-333333333333'

    resource_client = ResourceManagementClient(
        credentials,
        subscription_id,
        base_url=azure_endpoint
    )

Azure Germany
~~~~~~~~~~~~~

.. code:: python

    import adal
    from msrestazure.azure_active_directory import AdalAuthentication
    from azure.mgmt.resource import ResourceManagementClient

    # Service Principal
    tenant = 'ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL'
    client_id = 'ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL'
    password = 'password

    # Azure Germany
    authentication_endpoint = 'https://login.microsoftonline.de/'
    azure_endpoint = 'https://management.microsoftazure.de/'
        
    context = adal.AuthenticationContext(authentication_endpoint+tenant)
    credentials = AdalAuthentication(
        context.acquire_token_with_client_credentials,
        azure_endpoint,
        client_id,
        password
    )
    subscription_id = '33333333-3333-3333-3333-333333333333'

    resource_client = ResourceManagementClient(
        credentials,
        subscription_id,
        base_url=azure_endpoint
    )

Azure China
~~~~~~~~~~~

.. code:: python

    import adal
    from msrestazure.azure_active_directory import AdalAuthentication
    from azure.mgmt.resource import ResourceManagementClient

    # Service Principal
    tenant = 'ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL'
    client_id = 'ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL'
    password = 'password

    # Azure China
    authentication_endpoint = 'https://login.chinacloudapi.cn/'
    azure_endpoint = 'https://management.chinacloudapi.cn/'
        
    context = adal.AuthenticationContext(authentication_endpoint+tenant)
    credentials = AdalAuthentication(
        context.acquire_token_with_client_credentials,
        azure_endpoint,
        client_id,
        password
    )
    subscription_id = '33333333-3333-3333-3333-333333333333'

    resource_client = ResourceManagementClient(
        credentials,
        subscription_id,
        base_url=azure_endpoint
    )