-
Notifications
You must be signed in to change notification settings - Fork 74
Validate field names match DCM #161
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
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
562a08e
Adding ability to get DCM given device_id
3ed1476
Merge remote-tracking branch 'upstream/dev' into prbans/central-fetch…
f91a8db
Moved some things around
b83b756
Moved to using app-id
9da2858
output is now cleaner
fbe057e
Validate field names match DCM
644b9f3
Validates telemetry name
b8044a5
Added unit test
3604d73
Added provider tests
d368930
file rename
aae6583
Merge remote-tracking branch 'upstream/dev' into prbans/validate-tele…
6d54316
Use CLIError for errors, no longer print
ad56f4e
Address PR comments
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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # coding=utf-8 | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # coding=utf-8 | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| from .device_provider import CentralDeviceProvider | ||
|
|
||
| __all__ = ["CentralDeviceProvider"] |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # coding=utf-8 | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| from knack.util import CLIError | ||
| from azext_iot.central import services as central_services | ||
|
|
||
|
|
||
| class CentralDeviceProvider: | ||
| def __init__(self, cmd, app_id, token=None): | ||
| """ | ||
| Provider for device/device_template APIs | ||
|
|
||
| Args: | ||
| cmd: command passed into az | ||
| app_id: name of app (used for forming request URL) | ||
| token: (OPTIONAL) authorization token to fetch device details from IoTC. | ||
| MUST INCLUDE type (e.g. 'SharedAccessToken ...', 'Bearer ...') | ||
| Useful in scenarios where user doesn't own the app | ||
| therefore AAD token won't work, but a SAS token generated by owner will | ||
| """ | ||
| self._cmd = cmd | ||
| self._app_id = app_id | ||
| self._token = token | ||
| self._device_templates = {} | ||
| self._devices = {} | ||
|
|
||
| def get_device_template( | ||
| self, device_id, central_dns_suffix="azureiotcentral.com", | ||
| ): | ||
| device = self.get_device(device_id, central_dns_suffix) | ||
| device_template_urn = device["instanceOf"] | ||
|
|
||
| if not device_template_urn: | ||
| raise CLIError( | ||
| "No device template urn found for device '{}'".format(device_id) | ||
| ) | ||
|
|
||
| # get or add to cache | ||
| if ( | ||
| device_template_urn not in self._device_templates | ||
| or not self._device_templates.get(device_template_urn) | ||
| ): | ||
| self._device_templates[ | ||
| device_template_urn | ||
| ] = central_services.device_template.get_device_template( | ||
| self._cmd, | ||
| device_template_urn, | ||
| self._app_id, | ||
| self._token, | ||
| central_dns_suffix, | ||
| ) | ||
|
|
||
| device_template = self._device_templates[device_template_urn] | ||
| if not device_template: | ||
| raise CLIError( | ||
| "No device template for device with id: '{}'.".format(device_id) | ||
| ) | ||
|
|
||
| return device_template | ||
|
|
||
| def get_device( | ||
| self, device_id, central_dns_suffix="azureiotcentral.com", | ||
| ): | ||
| if not device_id: | ||
| raise CLIError("Device id must be specified.") | ||
|
|
||
| # get or add to cache | ||
| if device_id not in self._devices or not self._devices.get(device_id): | ||
| self._devices[device_id] = central_services.device.get_device( | ||
| self._cmd, device_id, self._app_id, self._token, central_dns_suffix | ||
| ) | ||
|
|
||
| device = self._devices[device_id] | ||
| if not device: | ||
| raise CLIError("No device found with id: '{}'.".format(device_id)) | ||
|
|
||
| return device |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # coding=utf-8 | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| from . import device, device_template | ||
|
|
||
|
|
||
| __all__ = ["device", "device_template"] |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # coding=utf-8 | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Nothing in this file should be used outside of service/central | ||
|
|
||
| from azext_iot import constants | ||
| from azext_iot.common import auth | ||
|
|
||
|
|
||
| def get_headers(token, cmd): | ||
| if not token: | ||
| aad_token = auth.get_aad_token(cmd, resource="https://apps.azureiotcentral.com") | ||
| token = "Bearer {}".format(aad_token["accessToken"]) | ||
|
|
||
| return {"Authorization": token, "User-Agent": constants.USER_AGENT} |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # coding=utf-8 | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
| # This is largely derived from https://docs.microsoft.com/en-us/rest/api/iotcentral/devices | ||
|
|
||
| import requests | ||
|
|
||
| from knack.util import CLIError | ||
| from . import _utility as utility | ||
|
|
||
|
|
||
| def get_device( | ||
| cmd, | ||
| device_id: str, | ||
| app_id: str, | ||
| token: str, | ||
| central_dns_suffix="azureiotcentral.com", | ||
| ) -> dict: | ||
| """ | ||
| Get device info given a device id | ||
|
|
||
| Args: | ||
| cmd: command passed into az | ||
| device_id: unique case-sensitive device id, | ||
| app_id: name of app (used for forming request URL) | ||
| token: (OPTIONAL) authorization token to fetch device details from IoTC. | ||
| MUST INCLUDE type (e.g. 'SharedAccessToken ...', 'Bearer ...') | ||
| central_dns_suffix: {centralDnsSuffixInPath} as found in docs | ||
|
|
||
| Returns: | ||
| device: dict | ||
| """ | ||
|
|
||
| url = "https://{}.{}/api/preview/devices/{}".format( | ||
| app_id, central_dns_suffix, device_id | ||
| ) | ||
| headers = utility.get_headers(token, cmd) | ||
|
|
||
| response = requests.get(url, headers=headers) | ||
|
|
||
| body = response.json() | ||
|
|
||
| if "error" in body: | ||
| raise CLIError(body["error"]) | ||
|
|
||
| return body |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # coding=utf-8 | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
| # This is largely derived from https://docs.microsoft.com/en-us/rest/api/iotcentral/devicetemplates | ||
|
|
||
| import requests | ||
|
|
||
| from knack.util import CLIError | ||
| from . import _utility as utility | ||
|
|
||
|
|
||
| def get_device_template( | ||
| cmd, | ||
| device_template_urn: str, | ||
| app_id: str, | ||
| token: str, | ||
| central_dns_suffix="azureiotcentral.com", | ||
| ) -> dict: | ||
| """ | ||
| Get device template given a device id | ||
|
|
||
| Args: | ||
| cmd: command passed into az | ||
| device_template_urn: case sensitive device template urn, | ||
| app_id: name of app (used for forming request URL) | ||
| token: (OPTIONAL) authorization token to fetch device details from IoTC. | ||
| MUST INCLUDE type (e.g. 'SharedAccessToken ...', 'Bearer ...') | ||
| central_dns_suffix: {centralDnsSuffixInPath} as found in docs | ||
|
|
||
| Returns: | ||
| device: dict | ||
| """ | ||
| url = "https://{}.{}/api/preview/deviceTemplates/{}".format( | ||
| app_id, central_dns_suffix, device_template_urn | ||
| ) | ||
| headers = utility.get_headers(token, cmd) | ||
|
|
||
| response = requests.get(url, headers=headers) | ||
|
|
||
| body = response.json() | ||
|
|
||
| if "error" in body: | ||
| raise CLIError(body["error"]) | ||
|
|
||
| return body |
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # coding=utf-8 | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| from azure.cli.core._profile import Profile | ||
|
|
||
|
|
||
| def get_aad_token(cmd, resource=None): | ||
| """ | ||
| get AAD token to access to a specified resource | ||
| :param resource: Azure resource endpoints. Default to Azure Resource Manager | ||
| Use 'az cloud show' command for other Azure resources | ||
| """ | ||
| resource = resource or cmd.cli_ctx.cloud.endpoints.active_directory_resource_id | ||
| profile = Profile(cli_ctx=cmd.cli_ctx) | ||
| creds, subscription, tenant = profile.get_raw_token( | ||
| subscription=None, resource=resource | ||
| ) | ||
| return { | ||
| "tokenType": creds[0], | ||
| "accessToken": creds[1], | ||
| "expiresOn": creds[2].get("expiresOn", "N/A"), | ||
| "subscription": subscription, | ||
| "tenant": tenant, | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.