Skip to content

Commit d662b54

Browse files
nickzhums00Kai0
andauthored
Quickstart for Track 2 preview management libraries and update root readme for track 2 (#11384)
* Update root readme and quickstart for Track 2 preview management libraries Co-authored-by: Kerwin Sun <[email protected]>
1 parent f755eb6 commit d662b54

File tree

3 files changed

+272
-3
lines changed

3 files changed

+272
-3
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ The client libraries are supported on Python 2.7 and 3.5.3 or later.
1919
Each service might have a number of libraries available from each of the following categories:
2020
* [Client - New Releases](#Client-New-Releases)
2121
* [Client - Previous Versions](#Client-Previous-Versions)
22-
* [Management](#Management)
22+
* [Management - New Releases](#Management-New-Releases)
23+
* [Management - Previous Versions](#Management-Previous-Versions)
2324

2425
### Client: New Releases
2526

@@ -33,9 +34,15 @@ You can find the [most up to date list of all of the new packages on our page](h
3334

3435
Last stable versions of packages that have been provided for usage with Azure and are production-ready. These libraries provide you with similar functionalities to the Preview ones as they allow you to use and consume existing resources and interact with them, for example: upload a blob. They might not implement the [guidelines](https://azure.github.io/azure-sdk/python_introduction.html) or have the same feature set as the Novemeber releases. They do however offer wider coverage of services.
3536

36-
### Management
37+
### Management: New Releases
38+
A new set of management libraries that follow the [Azure SDK Design Guidelines for Python](https://azure.github.io/azure-sdk/python/guidelines/) are now in Public Preview. These new libraries provide a number of core capabilities that are shared amongst all Azure SDKs, including the intuitive Azure Identity library, an HTTP Pipeline with custom policies, error-handling, distributed tracing, and much more.
39+
You can find the list of new packages [on this page](https://azure.github.io/azure-sdk/releases/latest/python.html). Documentation and code samples for these new libraries can be found [here](https://azure.github.io/azure-sdk-for-python)
3740

38-
Libraries which enable you to provision specific resources. They are responsible for directly mirroring and consuming Azure service's REST endpoints. The management libraries use the `azure-mgmt-<service name>` convention for their package names.
41+
> NOTE: If you need to ensure your code is ready for production use one of the stable, non-preview libraries.
42+
43+
### Management: Previous Versions
44+
For a complete list of management libraries which enable you to provision and manage Azure resources, please check [here](https://azure.github.io/azure-sdk/releases/latest/all/python.html). They might not have the same feature set as the new releases but they do offer wider coverage of services.
45+
Management libraries can be identified by namespaces that start with `azure-mgmt-`, e.g. `azure-mgmt-compute`
3946

4047
## Need help?
4148

doc/sphinx/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ section of the project.
6565

6666
installation
6767
quickstart_authentication
68+
mgmt_preview_quickstart
6869
multicloud
6970
exceptions
7071
Service Management (Legacy) <servicemanagement>
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
Quickstart Tutorial - Resource Management (Preview Libraries)
2+
===============================================================
3+
4+
We are excited to announce that a new set of management libraries are now in Public Preview.
5+
Those packages share a number of new features such as Azure Identity support,
6+
HTTP pipeline, error-handling.,etc, and they also follow the new Azure SDK guidelines which
7+
create easy-to-use APIs that are idiomatic, compatible, and dependable.
8+
9+
You can find the details of those new libraries `here <https://azure.github.io/azure-sdk/releases/latest/#python>`__
10+
11+
In this basic quickstart guide, we will walk you through how to
12+
authenticate to Azure using the preview libraries and start interacting with
13+
Azure resources. There are several possible approaches to
14+
authentication. This document illustrates the most common scenario
15+
16+
Prerequisites
17+
-------------
18+
19+
| You will need the following values to authenticate to Azure
20+
21+
- **Subscription ID**
22+
- **Client ID**
23+
- **Client Secret**
24+
- **Tenant ID**
25+
26+
These values can be obtained from the portal, here's the instructions:
27+
28+
Get Subscription ID
29+
^^^^^^^^^^^^^^^^^^^
30+
31+
1. Login into your Azure account
32+
2. Select Subscriptions in the left sidebar
33+
3. Select whichever subscription is needed
34+
4. Click on Overview
35+
5. Copy the Subscription ID
36+
37+
Get Client ID / Client Secret / Tenant ID
38+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
40+
For information on how to get Client ID, Client Secret, and Tenant ID, please refer to `this document <https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal>`__
41+
42+
Setting Environment Variables
43+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
45+
After you obtained the values, you need to set the following values as
46+
your environment variables
47+
48+
- ``AZURE_CLIENT_ID``
49+
- ``AZURE_CLIENT_SECRET``
50+
- ``AZURE_TENANT_ID``
51+
- ``AZURE_SUBSCRIPTION_ID``
52+
53+
To set the following environment variables on your development system:
54+
55+
Windows (Note: Administrator access is required)
56+
57+
1. Open the Control Panel
58+
2. Click System Security, then System
59+
3. Click Advanced system settings on the left
60+
4. Inside the System Properties window, click the Environment Variables… button.
61+
5. Click on the property you would like to change, then click the Edit… button. If the property name is not listed, then click the New… button.
62+
63+
Linux-based OS
64+
::
65+
66+
export AZURE_CLIENT_ID="__CLIENT_ID__"
67+
export AZURE_CLIENT_SECRET="__CLIENT_SECRET__"
68+
export AZURE_TENANT_ID="__TENANT_ID__"
69+
export AZURE_SUBSCRIPTION_ID="__SUBSCRIPTION_ID__"
70+
71+
Authentication and Creating Management Client
72+
------------------------------------------------------
73+
74+
Now that the environment is setup, all you need to do is to create an
75+
authenticated client. Our default option is to use
76+
**DefaultAzureCredential** and in this guide we have picked
77+
**Resources** as our target service, but you can set it up similarly for any other service that you are using.
78+
79+
To authenticate to Azure and create
80+
a management client, simply do the following:
81+
82+
::
83+
84+
import azure.mgmt.resource
85+
import azure.mgmt.network
86+
from azure.identity import DefaultAzureCredential
87+
...
88+
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID")
89+
credential = DefaultAzureCredential()
90+
resource_client = azure.mgmt.resource.ResourceManagementClient(credential=credential, subscription_id=subscription_id)
91+
network_client = azure.mgmt.network.NetworkManagementClient(credential=credential, subscription_id=subscription_id)
92+
93+
More information and different authentication approaches using Azure Identity can be found in
94+
`this document <https://docs.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python>`__
95+
96+
Managing Resources
97+
------------------
98+
99+
Now that we are authenticated, we can use the Resource client (azure.mgmt.resource.ResourceManagementClient) we have created to perform operations on Resource Group. In this example, we will show to manage Resource Groups.
100+
101+
**Create a resource group**
102+
103+
::
104+
105+
location = "westus2"
106+
group_name = "my_resource_group_name"
107+
group = resource_client.resource_groups.create_or_update(
108+
group_name,
109+
{'location': location}
110+
)
111+
112+
**Update a resource group**
113+
114+
::
115+
116+
group_name = "my_resource_group_name"
117+
group.tags = {
118+
"environment":"test",
119+
"department":"tech"
120+
}
121+
updated_group = resource_client.resource_groups.create_or_update(group_name, group)
122+
123+
**List all resource groups**
124+
125+
::
126+
127+
group_list = self.resource_client.resource_groups.list()
128+
for g in group_list:
129+
print_resource_group(g)
130+
131+
**Delete a resource group**
132+
133+
::
134+
135+
delete_async_op = resource_client.resource_groups.begin_delete(group_name)
136+
delete_async_op.wait()
137+
138+
Managing Network
139+
------------------
140+
We can use the Network client (azure.mgmt.resource.NetworkManagementClient) we have created to perform operations on Network related resources. In this example, we will show how to manage Public IP Addresses.
141+
142+
**Create a Network Public IP Address**
143+
144+
::
145+
146+
GROUP_NAME = "testgroup"
147+
PUBLIC_IP_ADDRESS = "public_ip_address_name"
148+
149+
# Create Resource Group
150+
resource_client.resource_groups.create_or_update(
151+
GROUP_NAME,
152+
{"location": "eastus"}
153+
)
154+
155+
# Create Public IP Address
156+
public_ip_address = network_client.public_ip_addresses.begin_create_or_update(
157+
GROUP_NAME,
158+
PUBLIC_IP_ADDRESS,
159+
{
160+
"location": "eastus"
161+
}
162+
).result()
163+
print("Create Public IP Address:\n{}".format(public_ip_address))
164+
165+
**Get a Network Public IP Address**
166+
167+
::
168+
169+
public_ip_address = network_client.public_ip_addresses.get(
170+
GROUP_NAME,
171+
PUBLIC_IP_ADDRESS
172+
)
173+
print("Get Public IP Address:\n{}".format(public_ip_address))
174+
175+
**Update tags in Network Public IP Address**
176+
177+
::
178+
179+
# Update Public IP Address
180+
public_ip_address = network_client.public_ip_addresses.update_tags(
181+
GROUP_NAME,
182+
PUBLIC_IP_ADDRESS,
183+
{
184+
"tags": {
185+
"tag1": "value1",
186+
"tag2": "value2"
187+
}
188+
}
189+
)
190+
print("Updated Public IP Address \n{}".format(public_ip_address))
191+
192+
**Delete a Network Public IP Address**
193+
194+
::
195+
196+
# Delete Public IP Address
197+
public_ip_address = network_client.public_ip_addresses.begin_delete(
198+
GROUP_NAME,
199+
PUBLIC_IP_ADDRESS
200+
).result()
201+
print("Delete Public IP Address.\n")
202+
203+
Async and sync operations
204+
-------------------------
205+
In python>=3.5, Azure Python SDK provides the choice for user to use the asynchronous client for asynchronous programming.
206+
207+
**Create Management Client in async**
208+
::
209+
210+
from azure.identity.aio import DefaultAzureCredential
211+
from azure.mgmt.network.aio import NetworkManagementClient
212+
from azure.mgmt.resource.resources.aio import ResourceManagementClient
213+
214+
SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
215+
credential = DefaultAzureCredential()
216+
resource_client = ResourceManagementClient(
217+
credential=credential,
218+
subscription_id=SUBSCRIPTION_ID
219+
)
220+
network_client = NetworkManagementClient(
221+
credential=credential,
222+
subscription_id=SUBSCRIPTION_ID
223+
)
224+
225+
**Create a Network Public IP Address**
226+
::
227+
228+
GROUP_NAME = "testgroup"
229+
PUBLIC_IP_ADDRESS = "public_ip_address_name"
230+
231+
# Create Resource Group
232+
await resource_client.resource_groups.create_or_update(
233+
GROUP_NAME,
234+
{"location": "eastus"}
235+
)
236+
237+
# Create Public IP Address
238+
async_poller = await network_client.public_ip_addresses.begin_create_or_update(
239+
GROUP_NAME,
240+
PUBLIC_IP_ADDRESS,
241+
{
242+
"location": "eastus"
243+
}
244+
)
245+
public_ip_address = await async_poller.result()
246+
print("Create Public IP Address:\n{}".format(public_ip_address))
247+
248+
Need help?
249+
----------
250+
- File an issue via `Github Issues <https://github.com/Azure/azure-sdk-for-python/issues>`__ and make sure you add the "Preview" label to the issue
251+
- Check `previous questions <https://stackoverflow.com/questions/tagged/azure+python>`__ or ask new ones on StackOverflow using azure and python tags.
252+
253+
Contributing
254+
------------
255+
For details on contributing to this repository, see the contributing guide.
256+
257+
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
258+
259+
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repositories using our CLA.
260+
261+
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

0 commit comments

Comments
 (0)