From 26e88e23d0e7c4f71d1442a763ddfdfb423491a1 Mon Sep 17 00:00:00 2001 From: Yuchao Yan Date: Tue, 27 Oct 2020 17:08:36 +0800 Subject: [PATCH] sample --- samples/eventgrid/README.md | 87 +++++++++++++++++++++++ samples/eventgrid/manage_servicename.py | 91 +++++++++++++++++++++++++ samples/eventgrid/requirements.txt | 3 + 3 files changed, 181 insertions(+) create mode 100644 samples/eventgrid/README.md create mode 100644 samples/eventgrid/manage_servicename.py create mode 100644 samples/eventgrid/requirements.txt diff --git a/samples/eventgrid/README.md b/samples/eventgrid/README.md new file mode 100644 index 0000000..696059f --- /dev/null +++ b/samples/eventgrid/README.md @@ -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 \ No newline at end of file diff --git a/samples/eventgrid/manage_servicename.py b/samples/eventgrid/manage_servicename.py new file mode 100644 index 0000000..185a8f6 --- /dev/null +++ b/samples/eventgrid/manage_servicename.py @@ -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() diff --git a/samples/eventgrid/requirements.txt b/samples/eventgrid/requirements.txt new file mode 100644 index 0000000..e469df6 --- /dev/null +++ b/samples/eventgrid/requirements.txt @@ -0,0 +1,3 @@ +azure-identity +azure-mgmt-resource==15.0.0 +azure-mgmt-eventgrid==8.0.0b1 \ No newline at end of file