Skip to content

Commit edfd5d0

Browse files
authored
Merge pull request #46 from Azure-Samples/kaihuis-relay-samples
add relay samples
2 parents fb86d5a + 442e6db commit edfd5d0

File tree

5 files changed

+364
-0
lines changed

5 files changed

+364
-0
lines changed

samples/relay/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 Relay using Azure SDK for Python."
8+
urlFragment: relay
9+
---
10+
11+
# Getting started - Managing Relay using Azure Python SDK
12+
13+
These code samples will show you how to manage Relay 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 - Relay Manamgement Library [azure-mgmt-relay](https://pypi.org/project/azure-mgmt-relay/) for the [Relay API](https://docs.microsoft.com/en-us/rest/api/relay/)
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/relay
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_wcfrelay.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: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
9+
from azure.identity import DefaultAzureCredential
10+
from azure.mgmt.relay import RelayAPI
11+
from azure.mgmt.resource import ResourceManagementClient
12+
13+
14+
def main():
15+
16+
SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
17+
GROUP_NAME = "testgroupx"
18+
NAMESPACE = "namespacexxyzx"
19+
HYBRIDCONNECTION = "hybridconnectionxxyyzz"
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+
relay_client = RelayAPI(
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+
# - init depended resources -
39+
# Create namespace
40+
namespace = relay_client.namespaces.begin_create_or_update(
41+
GROUP_NAME,
42+
NAMESPACE,
43+
{
44+
"location": "eastus",
45+
"tags": {
46+
"tag1": "value1",
47+
"tag2": "value2"
48+
},
49+
"sku": {
50+
"tier": "standard"
51+
}
52+
}
53+
).result()
54+
print("Create namespace:\n{}".format(namespace))
55+
# - end -
56+
57+
# Create hybridconnection
58+
hybridconnection = relay_client.hybrid_connections.create_or_update(
59+
GROUP_NAME,
60+
NAMESPACE,
61+
HYBRIDCONNECTION,
62+
{
63+
"requires_client_authorization": True,
64+
"user_metadata": "User data for HybridConnection"
65+
}
66+
)
67+
print("Create hybridconnection:\n{}".format(hybridconnection))
68+
69+
# Get hybridconnection
70+
hybridconnection = relay_client.hybrid_connections.get(
71+
GROUP_NAME,
72+
NAMESPACE,
73+
HYBRIDCONNECTION
74+
)
75+
print("Get hybridconnection:\n{}".format(hybridconnection))
76+
77+
# Delete hybridconnection
78+
hybridconnection = relay_client.hybrid_connections.delete(
79+
GROUP_NAME,
80+
NAMESPACE,
81+
HYBRIDCONNECTION
82+
)
83+
print("Delete hybridconnection.\n")
84+
85+
# Delete Group
86+
resource_client.resource_groups.begin_delete(
87+
GROUP_NAME
88+
).result()
89+
90+
91+
if __name__ == "__main__":
92+
main()

samples/relay/manage_namespace.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
9+
from azure.identity import DefaultAzureCredential
10+
from azure.mgmt.relay import RelayAPI
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+
GROUP_NAME = "testgroupx"
21+
NAMESPACE = "namespacexxyyzz"
22+
23+
# Create client
24+
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
25+
resource_client = ResourceManagementClient(
26+
credential=DefaultAzureCredential(),
27+
subscription_id=SUBSCRIPTION_ID
28+
)
29+
relay_client = RelayAPI(
30+
credential=DefaultAzureCredential(),
31+
subscription_id=SUBSCRIPTION_ID
32+
)
33+
34+
# Create resource group
35+
resource_client.resource_groups.create_or_update(
36+
GROUP_NAME,
37+
{"location": "eastus"}
38+
)
39+
40+
# Create namespace
41+
namespace = relay_client.namespaces.begin_create_or_update(
42+
GROUP_NAME,
43+
NAMESPACE,
44+
{
45+
"location": "eastus",
46+
"tags": {
47+
"tag1": "value1",
48+
"tag2": "value2"
49+
},
50+
"sku": {
51+
"tier": "standard"
52+
}
53+
}
54+
).result()
55+
print("Create namespace:\n{}".format(namespace))
56+
57+
# Get namespace
58+
namespace = relay_client.namespaces.get(
59+
GROUP_NAME,
60+
NAMESPACE
61+
)
62+
print("Get namespace:\n{}".format(namespace))
63+
64+
# Update namespace
65+
namespace = relay_client.namespaces.update(
66+
GROUP_NAME,
67+
NAMESPACE,
68+
{
69+
"tags": {
70+
"tag1": "value2"
71+
}
72+
}
73+
)
74+
print("Update namespace:\n{}".format(namespace))
75+
76+
# Delete namespace
77+
namespace = relay_client.namespaces.begin_delete(
78+
GROUP_NAME,
79+
NAMESPACE
80+
).result()
81+
print("Delete namespace.\n")
82+
83+
# Delete Group
84+
resource_client.resource_groups.begin_delete(
85+
GROUP_NAME
86+
).result()
87+
88+
89+
if __name__ == "__main__":
90+
main()

samples/relay/manage_wcfrelay.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
9+
from azure.identity import DefaultAzureCredential
10+
from azure.mgmt.relay import RelayAPI
11+
from azure.mgmt.resource import ResourceManagementClient
12+
13+
14+
def main():
15+
16+
SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
17+
GROUP_NAME = "testgroupx"
18+
WCFRELAY = "wcfrelayxxyyzz"
19+
NAMESPACE = "namespacexxyze"
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+
relay_client = RelayAPI(
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+
# - init depended resources -
39+
# Create namespace
40+
namespace = relay_client.namespaces.begin_create_or_update(
41+
GROUP_NAME,
42+
NAMESPACE,
43+
{
44+
"location": "eastus",
45+
"tags": {
46+
"tag1": "value1",
47+
"tag2": "value2"
48+
},
49+
"sku": {
50+
"tier": "standard"
51+
}
52+
}
53+
).result()
54+
print("Create namespace:\n{}".format(namespace))
55+
# - end -
56+
57+
# Create wcfrelay
58+
wcfrelay = relay_client.wcf_relays.create_or_update(
59+
GROUP_NAME,
60+
NAMESPACE,
61+
WCFRELAY,
62+
{
63+
"relay_type": "NetTcp",
64+
"requires_client_authorization": True,
65+
"requires_transport_security": True,
66+
"user_metadata": "User dta for WcfRelay"
67+
}
68+
)
69+
print("Create wcfrelay:\n{}".format(wcfrelay))
70+
71+
# Get wcfrelay
72+
wcfrelay = relay_client.wcf_relays.get(
73+
GROUP_NAME,
74+
NAMESPACE,
75+
WCFRELAY
76+
)
77+
print("Get wcfrelay:\n{}".format(wcfrelay))
78+
79+
# Delete wcfrelay
80+
wcfrelay = relay_client.wcf_relays.delete(
81+
GROUP_NAME,
82+
NAMESPACE,
83+
WCFRELAY
84+
)
85+
print("Delete wcfrelay.\n")
86+
87+
# Delete Group
88+
resource_client.resource_groups.begin_delete(
89+
GROUP_NAME
90+
).result()
91+
92+
93+
if __name__ == "__main__":
94+
main()

samples/relay/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-relay==1.0.0b1
3+
azure-mgmt-resource==15.0.0

0 commit comments

Comments
 (0)