-
Notifications
You must be signed in to change notification settings - Fork 74
Add provisioning filters #171
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
Changes from 7 commits
e46f332
6e9e550
f628cbb
9b8a2ca
97600a1
aaa3fa8
0a08012
eae51c4
9a8428c
be661f9
eaf4b11
cba39f0
881842f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,10 +68,16 @@ def registration_info( | |
| device_id=None, | ||
| token=None, | ||
| central_dns_suffix="azureiotcentral.com", | ||
| device_status=None, | ||
| ): | ||
| provider = CentralDeviceProvider(cmd=cmd, app_id=app_id, token=token) | ||
| provider = CentralDeviceProvider(cmd=cmd, app_id=app_id, token=token,) | ||
| if not device_id: | ||
| return provider.get_all_registration_info(central_dns_suffix=central_dns_suffix) | ||
| return provider.get_all_registration_info( | ||
| central_dns_suffix=central_dns_suffix, device_status=device_status | ||
|
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. What pattern are we following, new line for parameter or on the same line? 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. We use an auto-formatter called "Black", that tool is deciding the spacing/line breaks for us |
||
| ) | ||
|
|
||
| return provider.get_device_registration_info( | ||
| device_id=device_id, central_dns_suffix=central_dns_suffix | ||
| device_id=device_id, | ||
| central_dns_suffix=central_dns_suffix, | ||
| device_status=device_status, | ||
| ) | ||
| 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. | ||
| # -------------------------------------------------------------------------------------------- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # 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. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| """ | ||
| Enum definitions for central | ||
|
|
||
| """ | ||
|
|
||
| from enum import Enum | ||
|
|
||
|
|
||
| class DeviceStatus(Enum): | ||
| """ | ||
| Type of Device status. | ||
| """ | ||
|
|
||
| provisioned = "provisioned" | ||
| registered = "registered" | ||
| blocked = "blocked" | ||
| unassociated = "unassociated" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| from knack.log import get_logger | ||
| from azext_iot.central import services as central_services | ||
| from azext_iot.dps.services import global_service as dps_global_service | ||
| from azext_iot.central.models.enum import DeviceStatus | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
@@ -170,44 +171,91 @@ def get_device_credentials( | |
| return credentials | ||
|
|
||
| def get_device_registration_info( | ||
| self, device_id, central_dns_suffix="azureiotcentral.com", | ||
| self, | ||
| device_id, | ||
| device_status: DeviceStatus, | ||
| central_dns_suffix="azureiotcentral.com", | ||
| ): | ||
| dps_state = {} | ||
|
valluriraj marked this conversation as resolved.
|
||
| info = self._device_registration_info.get(device_id) | ||
|
|
||
| if not info: | ||
| credentials = self.get_device_credentials( | ||
| device_id=device_id, central_dns_suffix=central_dns_suffix | ||
| device_info = self.get_device(device_id) | ||
| device_essential_info = central_services.device.device_populate_essential_info( | ||
| device_info, device_status | ||
| ) | ||
| id_scope = credentials["idScope"] | ||
| key = credentials["symmetricKey"]["primaryKey"] | ||
| dps_state = dps_global_service.get_registration_state( | ||
| id_scope=id_scope, key=key, device_id=device_id | ||
| if ( | ||
| device_essential_info.get("deviceStatus") | ||
| is DeviceStatus.provisioned.value | ||
| ): | ||
| credentials = self.get_device_credentials( | ||
| device_id=device_id, central_dns_suffix=central_dns_suffix | ||
| ) | ||
| id_scope = credentials["idScope"] | ||
| key = credentials["symmetricKey"]["primaryKey"] | ||
| dps_state = dps_global_service.get_registration_state( | ||
| id_scope=id_scope, key=key, device_id=device_id | ||
| ) | ||
| dps_state = self.dps_populate_essential_info( | ||
|
valluriraj marked this conversation as resolved.
|
||
| dps_state, device_essential_info.get("deviceStatus") | ||
| ) | ||
| central_info = self.get_device(device_id) | ||
| info = { | ||
| "@device_id": device_id, | ||
| "dps_state": dps_state, | ||
| "central_info": central_info, | ||
| "device_info": device_essential_info, | ||
| } | ||
|
|
||
| self._device_registration_info[device_id] = info | ||
|
|
||
| return info | ||
|
|
||
| def get_all_registration_info(self, central_dns_suffix="azureiotcentral.com"): | ||
| def dps_populate_essential_info(self, dps_info, device_status): | ||
| error = { | ||
| "provisioned": "None", | ||
| "registered": "Device it not yet provisioned.", | ||
|
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. typo:
Member
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. nit: The 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. I'd suggest that cleaning up error messages is a P2 since I have a task in our backlog to clean up all error messages once we are ready to ship. Let me know if this is something PR blocking and I can work with you to resolve |
||
| "blocked": "Device is blocked by admin", | ||
| "unassociated": "Device does not have a valid template associated with it", | ||
| } | ||
|
|
||
| filtered_dps_info = { | ||
| "status": dps_info.get("status"), | ||
| "error": error.get(device_status), | ||
| } | ||
| return filtered_dps_info | ||
|
|
||
| def get_all_registration_info( | ||
| self, device_status, central_dns_suffix="azureiotcentral.com" | ||
| ): | ||
|
|
||
| logger.warning("This command may take a long time to complete execution.") | ||
| devices = self.list_devices(central_dns_suffix=central_dns_suffix) | ||
|
|
||
| real_devices = [ | ||
| device for device in devices.values() if not device["simulated"] | ||
| ] | ||
| if len(devices) != len(real_devices): | ||
|
|
||
|
valluriraj marked this conversation as resolved.
|
||
| real_devices_with_status = [ | ||
| central_services.device.update_device_status(device) | ||
| for device in real_devices | ||
| ] | ||
|
|
||
| filtered_devices = real_devices_with_status | ||
|
|
||
| if device_status: | ||
| filtered_devices = [ | ||
|
valluriraj marked this conversation as resolved.
|
||
| device | ||
| for device in real_devices_with_status | ||
| if device.get("deviceStatus") is device_status | ||
|
valluriraj marked this conversation as resolved.
Outdated
|
||
| ] | ||
|
|
||
| if len(devices) != len(filtered_devices): | ||
| logger.warning( | ||
| "Getting registration info for following devices. " | ||
| "All other devices are simulated. " | ||
| "{}".format([device["id"] for device in real_devices]) | ||
| "Getting registration info for real devices. " | ||
| "{}".format([device["id"] for device in filtered_devices]) | ||
| ) | ||
| result = [ | ||
| self.get_device_registration_info(device["id"]) for device in real_devices | ||
| self.get_device_registration_info(device["id"], device["deviceStatus"]) | ||
| for device in filtered_devices | ||
| ] | ||
|
|
||
| return result | ||
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.
Why is there a
,at the end?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.
We use an auto-formatter called "Black", that is adding this additional comma at the end
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.
Though in this particular case it should probably be removed
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.
I would not worry about this one. This is standard
python blackbehavior, in this case adding a comma after the last kwarg element. If you remove it now (which is fine), the next change to this module and a re-run of the formatter would re-introduce it.python blackis an opinionated formatter with minimal configuration. Idea for usingBlackis to save time and mental energy for more important matters. By using it, you agree to cede control over minutiae of hand-formatting. AlsoBlackwill check that the reformatted code still produces a valid AST that is equivalent to the original. It's a relatively newer Python tool that has immense popularity in the open source world.