Skip to content

Commit

Permalink
sample
Browse files Browse the repository at this point in the history
  • Loading branch information
msyyc committed Oct 27, 2020
1 parent 27cb450 commit 26e88e2
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
87 changes: 87 additions & 0 deletions samples/eventgrid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
page_type: sample
languages:
- python
products:
- azure
description: "These code samples will show you how to manage eventgrid using Azure SDK for Python."
urlFragment: eventgrid
---

# Getting started - Managing eventgrid using Azure Python SDK

These code samples will show you how to manage eventgrid using Azure SDK for Python.

## Features

This project framework provides examples for the following services:

### eventgrid
* [] 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/)

## Getting Started

### Prerequisites

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`

2. Store your credentials an environment variables.
For example, in Linux-based OS, you can do
```bash
export AZURE_TENANT_ID="xxx"
export AZURE_CLIENT_ID="xxx"
export AZURE_CLIENT_SECRET="xxx"
export SUBSCRIPTION_ID="xxx"
```

### Installation

1. If you don't already have it, [install Python](https://www.python.org/downloads/).

This sample (and the SDK) is compatible with Python 2.7, 3.3, 3.4, 3.5 and 3.6.

2. General recommendation for Python development is to use a Virtual Environment.
For more information, see https://docs.python.org/3/tutorial/venv.html

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):

```
python -m venv mytestenv # Might be "python3" or "py -3.6" depending on your Python installation
cd mytestenv
source bin/activate # Linux shell (Bash, ZSH, etc.) only
./scripts/activate # PowerShell only
./scripts/activate.bat # Windows CMD only
```

### Quickstart

1. Clone the repository.

```
git clone https://github.com/Azure-Samples/azure-samples-python-management.git
```

2. Install the dependencies using pip.

```
cd azure-samples-python-management/samples/eventgrid
pip install -r requirements.txt
```

## Demo

A demo app is included to show how to use the project.

To run the complete demo, execute `python example.py`

To run each individual demo, point directly to the file. For example (i.e. not complete list):

1. `python manage_ServiceName.py`

If the script starts with `disable_***.py`, it means that it is unavailable now.

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

## Resources

- https://github.com/Azure/azure-sdk-for-python
91 changes: 91 additions & 0 deletions samples/eventgrid/manage_servicename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

import os
import time
from azure.identity import DefaultAzureCredential
from azure.mgmt.eventgrid import EventGridManagementClient
from azure.mgmt.resource import ResourceManagementClient

# - other dependence -
# - end -


def main():

SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
TIME = str(time.time()).replace('.','')
GROUP_NAME = "testeventgrid" + TIME
EVENTGRID = "eventgrid" + TIME
LOCATION='eastus'

# Create client
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
resource_client = ResourceManagementClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
eventgrid_client = EventGridManagementClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
# - init depended client -
# - end -

# Create resource group
resource_client.resource_groups.create_or_update(
GROUP_NAME,
{"location": LOCATION}
)

# - init depended resources -
# - end -

# Create eventgrid
eventgrid = eventgrid_client.domains.begin_create_or_update(
GROUP_NAME,
EVENTGRID,
{
"location":LOCATION
}
).result()
print("Create eventgrid:\n{}".format(eventgrid))

# Get eventgrid
eventgrid = eventgrid_client.domains.get(
GROUP_NAME,
EVENTGRID
)
print("Get eventgrid:\n{}".format(eventgrid))

# Update eventgrid
eventgrid = eventgrid_client.domains.begin_update(
GROUP_NAME,
EVENTGRID,
{
"tags": {
"tag1": "value1",
"tag2": "value2"
}
}
).result()
print("Update eventgrid:\n{}".format(eventgrid))

# Delete eventgrid
eventgrid_client.domains.begin_delete(
GROUP_NAME,
EVENTGRID
).result()
print("Delete eventgrid.\n")

# Delete Group
resource_client.resource_groups.begin_delete(
GROUP_NAME
).result()


if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions samples/eventgrid/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
azure-identity
azure-mgmt-resource==15.0.0
azure-mgmt-eventgrid==8.0.0b1

0 comments on commit 26e88e2

Please sign in to comment.