Skip to content

Commit f40c8e8

Browse files
authored
add servicebus samples (#24)
* add servicebus samples * fix * fix
1 parent 92ba57a commit f40c8e8

File tree

7 files changed

+774
-0
lines changed

7 files changed

+774
-0
lines changed

samples/servicebus/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- python
5+
products:
6+
- azure
7+
description: "These code samples will show you how to manage Service Bus using Azure SDK for Python."
8+
urlFragment: servicebus
9+
---
10+
11+
# Getting started - Managing Service Bus using Azure Python SDK
12+
13+
These code samples will show you how to manage Service Bus using Azure SDK for Python.
14+
15+
## Features
16+
17+
This project framework provides examples for the following services:
18+
19+
### Service Bus
20+
* Using the Azure SDK for Python - Service Bus Manamgement Library [azure-mgmt-servicebus](https://pypi.org/project/azure-mgmt-servicebus/) for the [Service Bus API](https://docs.microsoft.com/en-us/rest/api/servicebus/)
21+
22+
## Getting Started
23+
24+
### Prerequisites
25+
26+
1. Before we run the samples, we need to make sure we have setup the credentials. Follow the instructions in [register a new application using Azure portal](https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal) to obtain `subscription id`,`client id`,`client secret`, and `application id`
27+
28+
2. Store your credentials an environment variables.
29+
For example, in Linux-based OS, you can do
30+
```bash
31+
export AZURE_TENANT_ID="xxx"
32+
export AZURE_CLIENT_ID="xxx"
33+
export AZURE_CLIENT_SECRET="xxx"
34+
export SUBSCRIPTION_ID="xxx"
35+
```
36+
37+
### Installation
38+
39+
1. If you don't already have it, [install Python](https://www.python.org/downloads/).
40+
41+
This sample (and the SDK) is compatible with Python 2.7, 3.3, 3.4, 3.5 and 3.6.
42+
43+
2. General recommendation for Python development is to use a Virtual Environment.
44+
For more information, see https://docs.python.org/3/tutorial/venv.html
45+
46+
Install and initialize the virtual environment with the "venv" module on Python 3 (you must install [virtualenv](https://pypi.python.org/pypi/virtualenv) for Python 2.7):
47+
48+
```
49+
python -m venv mytestenv # Might be "python3" or "py -3.6" depending on your Python installation
50+
cd mytestenv
51+
source bin/activate # Linux shell (Bash, ZSH, etc.) only
52+
./scripts/activate # PowerShell only
53+
./scripts/activate.bat # Windows CMD only
54+
```
55+
56+
### Quickstart
57+
58+
1. Clone the repository.
59+
60+
```
61+
git clone https://github.com/Azure-Samples/azure-samples-python-management.git
62+
```
63+
64+
2. Install the dependencies using pip.
65+
66+
```
67+
cd azure-samples-python-management/samples/servicebus
68+
pip install -r requirements.txt
69+
```
70+
71+
## Demo
72+
73+
A demo app is included to show how to use the project.
74+
75+
To run the complete demo, execute `python example.py`
76+
77+
To run each individual demo, point directly to the file. For example (i.e. not complete list):
78+
79+
1. `python manage_namespace.py`
80+
81+
Each file is a separate code sample that no dependency on other files. You can look at whichever code sample you're interested in
82+
83+
## Resources
84+
85+
- https://github.com/Azure/azure-sdk-for-python
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
7+
import os
8+
import time
9+
10+
from azure.core.exceptions import HttpResponseError
11+
from azure.identity import DefaultAzureCredential
12+
from azure.mgmt.servicebus import ServiceBusManagementClient
13+
from azure.mgmt.resource import ResourceManagementClient
14+
15+
16+
def main():
17+
18+
SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
19+
GROUP_NAME = "testgroupx"
20+
NAMESPACE = "myNamespacexxyyzzzy"
21+
NAMESPACE_PRIMARY = "myNamespacexxyyzzzysecond"
22+
AUTHORIZATION_RULE_NAME = "myAuthorizationRule"
23+
DISASTER_RECOVERY_CONFIG = "mydisasterrecovercf"
24+
25+
# Create client
26+
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
27+
resource_client = ResourceManagementClient(
28+
credential=DefaultAzureCredential(),
29+
subscription_id=SUBSCRIPTION_ID
30+
)
31+
servicebus_client = ServiceBusManagementClient(
32+
credential=DefaultAzureCredential(),
33+
subscription_id=SUBSCRIPTION_ID
34+
)
35+
36+
# Create resource group
37+
resource_client.resource_groups.create_or_update(
38+
GROUP_NAME,
39+
{"location": "eastus"}
40+
)
41+
42+
# - init depended resources -
43+
# Create namespace
44+
namespace = servicebus_client.namespaces.begin_create_or_update(
45+
GROUP_NAME,
46+
NAMESPACE,
47+
{
48+
"sku": {
49+
"name": "Premium",
50+
"tier": "Premium"
51+
},
52+
"location": "eastus",
53+
"tags": {
54+
"tag1": "value1",
55+
"tag2": "value2"
56+
}
57+
}
58+
).result()
59+
60+
# Create namespace primary
61+
second_namespace = servicebus_client.namespaces.begin_create_or_update(
62+
GROUP_NAME,
63+
NAMESPACE_PRIMARY,
64+
{
65+
"sku": {
66+
"name": "Premium",
67+
"tier": "Premium"
68+
},
69+
"location": "westus",
70+
"tags": {
71+
"tag1": "value1",
72+
"tag2": "value2"
73+
}
74+
}
75+
).result()
76+
77+
# Create namespace authorization rule
78+
rule = servicebus_client.namespaces.create_or_update_authorization_rule(
79+
GROUP_NAME,
80+
NAMESPACE,
81+
AUTHORIZATION_RULE_NAME,
82+
{
83+
"rights": [
84+
"Listen",
85+
"Send"
86+
]
87+
}
88+
)
89+
# - end -
90+
91+
# Check name availability
92+
result = servicebus_client.disaster_recovery_configs.check_name_availability(
93+
GROUP_NAME,
94+
NAMESPACE,
95+
{
96+
"name": DISASTER_RECOVERY_CONFIG
97+
}
98+
)
99+
100+
# Create disaster recovery config
101+
disaster_recovery_config = servicebus_client.disaster_recovery_configs.create_or_update(
102+
GROUP_NAME,
103+
NAMESPACE,
104+
DISASTER_RECOVERY_CONFIG,
105+
{
106+
"partner_namespace": second_namespace.id
107+
}
108+
)
109+
print("Create disaster recovery config:\n{}".format(disaster_recovery_config))
110+
111+
# Get disaster recovery config
112+
disaster_recovery_config = servicebus_client.disaster_recovery_configs.get(
113+
GROUP_NAME,
114+
NAMESPACE,
115+
DISASTER_RECOVERY_CONFIG
116+
)
117+
count = 0
118+
while disaster_recovery_config.provisioning_state != "Succeeded" and count<10:
119+
time.sleep(30)
120+
disaster_recovery_config = servicebus_client.disaster_recovery_configs.get(
121+
GROUP_NAME,
122+
NAMESPACE,
123+
DISASTER_RECOVERY_CONFIG
124+
)
125+
count += 1
126+
print("Get disaster recovery config:\n{}".format(disaster_recovery_config))
127+
128+
# Fail over
129+
result = servicebus_client.disaster_recovery_configs.fail_over(
130+
GROUP_NAME,
131+
NAMESPACE_PRIMARY,
132+
DISASTER_RECOVERY_CONFIG
133+
)
134+
print("Fail over disaster recovery config.\n")
135+
136+
# Delete disaster recovery config
137+
count = 0
138+
while count<10:
139+
try:
140+
disaster_recovery_config = servicebus_client.disaster_recovery_configs.delete(
141+
GROUP_NAME,
142+
NAMESPACE_PRIMARY,
143+
DISASTER_RECOVERY_CONFIG
144+
)
145+
except HttpResponseError:
146+
time.sleep(30)
147+
count += 1
148+
else:
149+
break
150+
print("Delete disaster recovery config.\n")
151+
152+
# Delete Group
153+
resource_client.resource_groups.begin_delete(
154+
GROUP_NAME
155+
).result()
156+
157+
158+
if __name__ == "__main__":
159+
main()
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
7+
import os
8+
import time
9+
10+
from azure.core.exceptions import HttpResponseError
11+
from azure.identity import DefaultAzureCredential
12+
from azure.mgmt.servicebus import ServiceBusManagementClient
13+
from azure.mgmt.resource import ResourceManagementClient
14+
15+
16+
def main():
17+
18+
SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
19+
GROUP_NAME = "testgroupx"
20+
NAMESPACE = "namespacexxyyzz"
21+
NAMESPACE_PRIMARY = "namespaceprimaryxxyyzz"
22+
AUTHORIZATION_RULE_NAME = "myAuthorizationRule"
23+
MIGRATION_CONFIG = "$default"
24+
POST_MIGRATION_NAME = "postmigrationxxyyzz"
25+
26+
# Create client
27+
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
28+
resource_client = ResourceManagementClient(
29+
credential=DefaultAzureCredential(),
30+
subscription_id=SUBSCRIPTION_ID
31+
)
32+
servicebus_client = ServiceBusManagementClient(
33+
credential=DefaultAzureCredential(),
34+
subscription_id=SUBSCRIPTION_ID
35+
)
36+
37+
# Create resource group
38+
resource_client.resource_groups.create_or_update(
39+
GROUP_NAME,
40+
{"location": "eastus"}
41+
)
42+
43+
# - init depended resources -
44+
# Create namespace
45+
namespace = servicebus_client.namespaces.begin_create_or_update(
46+
GROUP_NAME,
47+
NAMESPACE,
48+
{
49+
"sku": {
50+
"name": "Standard",
51+
"tier": "Standard"
52+
},
53+
"location": "eastus",
54+
"tags": {
55+
"tag1": "value1",
56+
"tag2": "value2"
57+
}
58+
}
59+
).result()
60+
61+
# Create namespace primary
62+
second_namespace = servicebus_client.namespaces.begin_create_or_update(
63+
GROUP_NAME,
64+
NAMESPACE_PRIMARY,
65+
{
66+
"sku": {
67+
"name": "Premium",
68+
"tier": "Premium"
69+
},
70+
"location": "westus",
71+
"tags": {
72+
"tag1": "value1",
73+
"tag2": "value2"
74+
}
75+
}
76+
).result()
77+
78+
# Create namespace authorization rule
79+
rule = servicebus_client.namespaces.create_or_update_authorization_rule(
80+
GROUP_NAME,
81+
NAMESPACE,
82+
AUTHORIZATION_RULE_NAME,
83+
{
84+
"rights": [
85+
"Listen",
86+
"Send"
87+
]
88+
}
89+
)
90+
# - end -
91+
92+
# Create migration config
93+
migration_config = servicebus_client.migration_configs.begin_create_and_start_migration(
94+
GROUP_NAME,
95+
NAMESPACE,
96+
MIGRATION_CONFIG,
97+
{
98+
"target_namespace": second_namespace.id,
99+
"post_migration_name": POST_MIGRATION_NAME
100+
}
101+
).result()
102+
print("Create migration config:\n{}".format(migration_config))
103+
104+
# Complete migration config
105+
result = servicebus_client.migration_configs.complete_migration(
106+
GROUP_NAME,
107+
NAMESPACE,
108+
MIGRATION_CONFIG
109+
)
110+
print("Complete migration config.\n")
111+
112+
# Get migration config
113+
migration_config = servicebus_client.migration_configs.get(
114+
GROUP_NAME,
115+
NAMESPACE,
116+
MIGRATION_CONFIG
117+
)
118+
count = 0
119+
while migration_config.provisioning_state != "Succeeded" and count<10:
120+
time.sleep(30)
121+
migration_config = servicebus_client.migration_configs.get(
122+
GROUP_NAME,
123+
NAMESPACE,
124+
MIGRATION_CONFIG
125+
)
126+
count += 1
127+
print("Get migration config:\n{}".format(migration_config))
128+
129+
# Delete migration config
130+
try:
131+
migration_config = servicebus_client.migration_configs.delete(
132+
GROUP_NAME,
133+
NAMESPACE,
134+
MIGRATION_CONFIG
135+
)
136+
except HttpResponseError as e:
137+
if not str(e).startswith("(NotFound)"):
138+
raise e
139+
print("Delete migration config.\n")
140+
141+
# Delete Group
142+
resource_client.resource_groups.begin_delete(
143+
GROUP_NAME
144+
).result()
145+
146+
147+
if __name__ == "__main__":
148+
main()

0 commit comments

Comments
 (0)