-
Notifications
You must be signed in to change notification settings - Fork 13
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
bretmulvey
wants to merge
1
commit into
Azure-Samples:master
Choose a base branch
from
bretmulvey:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
|
||
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!") | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here add raise Exception There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add raise Exception here?
There was a problem hiding this comment.
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".