Skip to content

Commit f99e56a

Browse files
authored
Merge pull request #39 from Azure-Samples/t2-eventgrid-2020-10-27
[T2] eventgrid
2 parents 5227fcb + 26e88e2 commit f99e56a

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

samples/eventgrid/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- python
5+
products:
6+
- azure
7+
description: "These code samples will show you how to manage eventgrid using Azure SDK for Python."
8+
urlFragment: eventgrid
9+
---
10+
11+
# Getting started - Managing eventgrid using Azure Python SDK
12+
13+
These code samples will show you how to manage eventgrid using Azure SDK for Python.
14+
15+
## Features
16+
17+
This project framework provides examples for the following services:
18+
19+
### eventgrid
20+
* [] Using the Azure SDK for Python - eventgrid Management Library [azure-mgmt-eventgrid](https://pypi.org/project/azure-mgmt-eventgrid/) for the [eventgrid API](https://docs.microsoft.com/en-us/rest/api/eventgrid/)
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/eventgrid
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_ServiceName.py`
80+
81+
If the script starts with `disable_***.py`, it means that it is unavailable now.
82+
83+
The sample files do not have dependency each other and each file represents an individual end-to-end scenario. Please look at the sample that contains the scenario you are interested in
84+
85+
## Resources
86+
87+
- https://github.com/Azure/azure-sdk-for-python
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
from azure.identity import DefaultAzureCredential
10+
from azure.mgmt.eventgrid import EventGridManagementClient
11+
from azure.mgmt.resource import ResourceManagementClient
12+
13+
# - other dependence -
14+
# - end -
15+
16+
17+
def main():
18+
19+
SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
20+
TIME = str(time.time()).replace('.','')
21+
GROUP_NAME = "testeventgrid" + TIME
22+
EVENTGRID = "eventgrid" + TIME
23+
LOCATION='eastus'
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+
eventgrid_client = EventGridManagementClient(
32+
credential=DefaultAzureCredential(),
33+
subscription_id=SUBSCRIPTION_ID
34+
)
35+
# - init depended client -
36+
# - end -
37+
38+
# Create resource group
39+
resource_client.resource_groups.create_or_update(
40+
GROUP_NAME,
41+
{"location": LOCATION}
42+
)
43+
44+
# - init depended resources -
45+
# - end -
46+
47+
# Create eventgrid
48+
eventgrid = eventgrid_client.domains.begin_create_or_update(
49+
GROUP_NAME,
50+
EVENTGRID,
51+
{
52+
"location":LOCATION
53+
}
54+
).result()
55+
print("Create eventgrid:\n{}".format(eventgrid))
56+
57+
# Get eventgrid
58+
eventgrid = eventgrid_client.domains.get(
59+
GROUP_NAME,
60+
EVENTGRID
61+
)
62+
print("Get eventgrid:\n{}".format(eventgrid))
63+
64+
# Update eventgrid
65+
eventgrid = eventgrid_client.domains.begin_update(
66+
GROUP_NAME,
67+
EVENTGRID,
68+
{
69+
"tags": {
70+
"tag1": "value1",
71+
"tag2": "value2"
72+
}
73+
}
74+
).result()
75+
print("Update eventgrid:\n{}".format(eventgrid))
76+
77+
# Delete eventgrid
78+
eventgrid_client.domains.begin_delete(
79+
GROUP_NAME,
80+
EVENTGRID
81+
).result()
82+
print("Delete eventgrid.\n")
83+
84+
# Delete Group
85+
resource_client.resource_groups.begin_delete(
86+
GROUP_NAME
87+
).result()
88+
89+
90+
if __name__ == "__main__":
91+
main()

samples/eventgrid/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
azure-identity
2+
azure-mgmt-resource==15.0.0
3+
azure-mgmt-eventgrid==8.0.0b1

0 commit comments

Comments
 (0)