Skip to content

Commit 203c048

Browse files
authored
Merge pull request #44 from Azure-Samples/kaihuis-notificationhub-samples
add notificationhub samples
2 parents c66b8c1 + 705b4c5 commit 203c048

File tree

4 files changed

+268
-0
lines changed

4 files changed

+268
-0
lines changed

samples/notificationhubs/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 Notification Hub using Azure SDK for Python."
8+
urlFragment: notificationhub
9+
---
10+
11+
# Getting started - Managing Notification Hub using Azure Python SDK
12+
13+
These code samples will show you how to manage Notification Hub using Azure SDK for Python.
14+
15+
## Features
16+
17+
This project framework provides examples for the following services:
18+
19+
### Storage
20+
* Using the Azure SDK for Python - Notification Hub Manamgement Library [azure-mgmt-notificationhubs](https://pypi.org/project/azure-mgmt-notificationhubs/) for the [Notification Hub API](https://docs.microsoft.com/en-us/rest/api/notificationhubs/)
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/notificationhubs
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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.identity import DefaultAzureCredential
11+
from azure.mgmt.notificationhubs import NotificationHubsManagementClient
12+
from azure.mgmt.resource import ResourceManagementClient
13+
14+
15+
def main():
16+
17+
SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
18+
GROUP_NAME = "testgroupx"
19+
NAMESPACE = "namespacexxyyzz"
20+
21+
# Create client
22+
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
23+
resource_client = ResourceManagementClient(
24+
credential=DefaultAzureCredential(),
25+
subscription_id=SUBSCRIPTION_ID
26+
)
27+
notificationhubs_client = NotificationHubsManagementClient(
28+
credential=DefaultAzureCredential(),
29+
subscription_id=SUBSCRIPTION_ID
30+
)
31+
32+
# Create resource group
33+
resource_client.resource_groups.create_or_update(
34+
GROUP_NAME,
35+
{"location": "eastus"}
36+
)
37+
38+
# Create namespace
39+
namespace = notificationhubs_client.namespaces.create_or_update(
40+
GROUP_NAME,
41+
NAMESPACE,
42+
{
43+
"location": "eastus"
44+
}
45+
)
46+
print("Create namespace:\n{}".format(namespace))
47+
48+
# Get namespace
49+
namespace = notificationhubs_client.namespaces.get(
50+
GROUP_NAME,
51+
NAMESPACE
52+
)
53+
while namespace.status == "Created":
54+
time.sleep(30)
55+
namespace = notificationhubs_client.namespaces.get(
56+
GROUP_NAME,
57+
NAMESPACE,
58+
)
59+
print("Get namespace:\n{}".format(namespace))
60+
61+
# Update namespace
62+
namespace = notificationhubs_client.namespaces.patch(
63+
GROUP_NAME,
64+
NAMESPACE,
65+
{
66+
"enabled": True
67+
}
68+
)
69+
print("Update namespace:\n{}".format(namespace))
70+
71+
# Delete namespace
72+
notificationhubs_client.namespaces.begin_delete(
73+
GROUP_NAME,
74+
NAMESPACE
75+
).result()
76+
print("Delete namespace.\n")
77+
78+
# Delete Group
79+
resource_client.resource_groups.begin_delete(
80+
GROUP_NAME
81+
).result()
82+
83+
84+
if __name__ == "__main__":
85+
main()
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.identity import DefaultAzureCredential
11+
from azure.mgmt.notificationhubs import NotificationHubsManagementClient
12+
from azure.mgmt.resource import ResourceManagementClient
13+
14+
15+
def main():
16+
17+
SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
18+
GROUP_NAME = "testgroupx"
19+
NOTIFICATION_HUB = "notification_hubxxyyzz"
20+
NAMESPACE = "namespacexxyz"
21+
22+
# Create client
23+
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
24+
resource_client = ResourceManagementClient(
25+
credential=DefaultAzureCredential(),
26+
subscription_id=SUBSCRIPTION_ID
27+
)
28+
notificationhubs_client = NotificationHubsManagementClient(
29+
credential=DefaultAzureCredential(),
30+
subscription_id=SUBSCRIPTION_ID
31+
)
32+
33+
# Create resource group
34+
resource_client.resource_groups.create_or_update(
35+
GROUP_NAME,
36+
{"location": "eastus"}
37+
)
38+
39+
# - init depended resources -
40+
# Create namespace
41+
namespace = notificationhubs_client.namespaces.create_or_update(
42+
GROUP_NAME,
43+
NAMESPACE,
44+
{
45+
"location": "eastus"
46+
}
47+
)
48+
namespace = notificationhubs_client.namespaces.get(
49+
GROUP_NAME,
50+
NAMESPACE
51+
)
52+
while namespace.status == "Created":
53+
time.sleep(30)
54+
namespace = notificationhubs_client.namespaces.get(
55+
GROUP_NAME,
56+
NAMESPACE,
57+
)
58+
print("Create namespace:\n{}".format(namespace))
59+
# - end -
60+
61+
# Create notification hub
62+
notification_hub = notificationhubs_client.notification_hubs.create_or_update(
63+
GROUP_NAME,
64+
NAMESPACE,
65+
NOTIFICATION_HUB,
66+
{
67+
"location": "eastus"
68+
}
69+
)
70+
print("Create notification hub:\n{}".format(notification_hub))
71+
72+
# Get notification hub
73+
notification_hub = notificationhubs_client.notification_hubs.get(
74+
GROUP_NAME,
75+
NAMESPACE,
76+
NOTIFICATION_HUB
77+
)
78+
print("Get notification hub:\n{}".format(notification_hub))
79+
80+
# Delete notification hub
81+
notificationhubs_client.notification_hubs.delete(
82+
GROUP_NAME,
83+
NAMESPACE,
84+
NOTIFICATION_HUB
85+
)
86+
print("Delete notification hub.\n")
87+
88+
# Delete Group
89+
resource_client.resource_groups.begin_delete(
90+
GROUP_NAME
91+
).result()
92+
93+
94+
if __name__ == "__main__":
95+
main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
azure-identity
2+
azure-mgmt-notificationhubs==7.0.0b1
3+
azure-mgmt-resource==15.0.0

0 commit comments

Comments
 (0)