Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Python sample for IoT Central 1.0.0b1 #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 116 additions & 32 deletions python/run.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,133 @@
import os, uuid, sys

import os
import random

from azure.identity._credentials.browser import InteractiveBrowserCredential
from azure.mgmt.iotcentral import IotCentralClient
from azure.mgmt.iotcentral.models import App, AppSkuInfo, AppPatch
from msrestazure.azure_active_directory import MSIAuthentication
from azure.common.credentials import UserPassCredentials, get_azure_cli_credentials

# login with az login
creds = get_azure_cli_credentials()
subId = "FILL IN SUB ID"
appName = "iot-central-app-tocreate"
from azure.mgmt.iotcentral.models import App, AppSkuInfo, SystemAssignedServiceIdentityType

region = "westus"
resourceGroup = "myResourceGroup"
skuName = "ST2"


# Get subscription info from environment.

tenantId = os.environ["AZURE_TENANT_ID"]
subscriptionId = os.environ["AZURE_SUBSCRIPTION_ID"]

if (tenantId is None) or (subscriptionId is None):
raise Exception("Expected environment variables.")


# Make IOTC client.

credential = InteractiveBrowserCredential(tenant_id = tenantId)
iotc = IotCentralClient(credential, subscriptionId)

client = IotCentralClient(creds[0], subId)

result = client.apps.check_name_availability(appName)
print(result)
# Choose app name.

app = App(location="unitedstates", sku=AppSkuInfo(name="ST2"))
appName = f"pysdk-{random.randint(100000, 999999)}-{random.randint(100000, 999999)}"


# Define the app configuration.

app = App(location = region, sku = AppSkuInfo(name = skuName))
app.subdomain = appName
app.display_name = appName
app.identity = { "type": "SystemAssigned" }

client.apps.create_or_update(resourceGroup, appName, app)

getResult = client.apps.get(resourceGroup, appName)
print(getResult)
# Create the app.

updateApp = AppPatch()
updateApp.display_name = appName + "-new-name"
print(f"\nCreating {appName}. Check browser window for login.")
poller = iotc.apps.begin_create_or_update(resourceGroup, appName, app)
result = poller.result()

client.apps.update(resourceGroup, appName, updateApp)
if result.state != "created":
raise Exception("Expected 'created' state.")

appsInGroup = client.apps.list_by_resource_group(resourceGroup)
appsInGroup.next()
for item in appsInGroup.current_page:
print(item)
print("OK")


# Make sure it's idempotent.

print(f"\nUpdating {appName}.")
poller = iotc.apps.begin_create_or_update(resourceGroup, appName, app)
result = poller.result()

if result.state != "created":
raise Exception("Expected 'created' state.")

print("OK")

operations = client.operations.list()
operations.next()
for item in operations.current_page:
print(item)

appTemplates = client.apps.list_templates()
for item in appTemplates:
# List all the apps in the resource group.

print(f"\nListing IoT Central apps in '{resourceGroup}'")
appsInGroup = iotc.apps.list_by_resource_group(resourceGroup)

for item in appsInGroup:
print(item)

# deleteResult = client.apps.delete(resourceGroup, appName)
# print(deleteResult)

print("done")
# Update the app tags.

print(f"\nUpdating {appName} tags.")
tag = "mytag"
value = "myvalue"
app.tags = { tag: value }
poller = iotc.apps.begin_create_or_update(resourceGroup, appName, app)
result = poller.result()

if result.tags[tag] != value:
raise Exception("Expected updated tag.")

print("OK")


# Delete the app.

print(f"\nDeleting {appName}")
poller = iotc.apps.begin_delete(resourceGroup, appName)
result = poller.result()

if result:
print(result)
raise Exception("Expected 'None'.")

print("OK")


# Verify that we can't create in geography as we could before.

print("\nMake sure we can't use geography.")
app.location = "unitedstates"
appName = "us-" + appName
app.subdomain = app.display_name = appName

try:
poller = iotc.apps.begin_create_or_update(resourceGroup, appName, app)
result = poller.result()
print("It worked but it shouldn't have!")

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add raise Exception here?

Copy link
Author

@bretmulvey bretmulvey Nov 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inside a try who's except is the success condition and prints "OK", so raising here would print "OK".

except:
print("OK")


# Verify that S1 SKU is no longer allowed.

print(f"\nMake sure we can't use S1 SKU.")
appNameS1 = "s1-" + appName
app = App(location = region, sku = AppSkuInfo(name = "S1"))
app.subdomain = appNameS1
app.display_name = appNameS1

try:
poller = iotc.apps.begin_create_or_update(resourceGroup, appNameS1, app)
result = poller.result()
print("It worked but it shouldn't have!")

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here add raise Exception

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception here means success (we expect exception) so raising exception here would print "OK".

except:
print("OK")