-
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 3 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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
|
|
||
| from knack.util import CLIError | ||
| from azext_iot.central.providers import CentralDeviceProvider | ||
| from azext_iot.common.shared import DeviceStatus | ||
|
|
||
|
|
||
| def list_devices( | ||
|
|
@@ -68,10 +69,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,) | ||
|
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. Why is there a 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 is adding this additional comma at the end 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. Though in this particular case it should probably be removed
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. I would not worry about this one. This is standard
|
||
| 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 |
|---|---|---|
|
|
@@ -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.common.shared import DeviceStatus | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
@@ -170,11 +171,15 @@ def get_device_credentials( | |
| return credentials | ||
|
|
||
| def get_device_registration_info( | ||
| self, device_id, central_dns_suffix="azureiotcentral.com", | ||
| self, device_id, device_status, central_dns_suffix="azureiotcentral.com" | ||
|
valluriraj marked this conversation as resolved.
Outdated
|
||
| ): | ||
| info = self._device_registration_info.get(device_id) | ||
|
|
||
| if not info: | ||
| central_info = self.get_device(device_id) | ||
| central_info = self.update_device_info_filter_status( | ||
|
valluriraj marked this conversation as resolved.
Outdated
|
||
| central_info, device_status | ||
| ) | ||
| credentials = self.get_device_credentials( | ||
| device_id=device_id, central_dns_suffix=central_dns_suffix | ||
| ) | ||
|
|
@@ -183,7 +188,9 @@ def get_device_registration_info( | |
| dps_state = dps_global_service.get_registration_state( | ||
| id_scope=id_scope, key=key, device_id=device_id | ||
| ) | ||
| central_info = self.get_device(device_id) | ||
| dps_state = self.filter_dps_info( | ||
| dps_state, central_info.get("deviceStatus") | ||
| ) | ||
| info = { | ||
| "@device_id": device_id, | ||
| "dps_state": dps_state, | ||
|
|
@@ -194,20 +201,92 @@ def get_device_registration_info( | |
|
|
||
| return info | ||
|
|
||
| def get_all_registration_info(self, central_dns_suffix="azureiotcentral.com"): | ||
| def filter_dps_info(self, dps_info, device_status): | ||
|
valluriraj marked this conversation as resolved.
Outdated
valluriraj marked this conversation as resolved.
Outdated
|
||
| 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"), | ||
| } | ||
|
|
||
| filtered_dps_info.update({"error": error.get(device_status)}) | ||
|
valluriraj marked this conversation as resolved.
Outdated
|
||
| return filtered_dps_info | ||
|
|
||
| def update_device_info_filter_status(self, device, value): | ||
|
valluriraj marked this conversation as resolved.
Outdated
|
||
| if value is None: | ||
|
valluriraj marked this conversation as resolved.
Outdated
|
||
| return self.update_device_status(device) | ||
| updated_device_data = { | ||
| "id": device["id"], | ||
| "displayName": device.get("displayName"), | ||
|
valluriraj marked this conversation as resolved.
Outdated
|
||
| "instanceOf": device.get("instanceOf"), | ||
| "simulated": device.get("simulated"), | ||
| "deviceStatus": value, | ||
| } | ||
| return updated_device_data | ||
|
|
||
| def update_device_status(self, device): | ||
| if device["approved"] is False: | ||
|
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 say extract this to a function: then just call: 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. infact, I think this code could be moved into 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. or perhaps even at the service level |
||
| updated_device = self.update_device_info_filter_status( | ||
| device, DeviceStatus.blocked.value | ||
| ) | ||
| else: | ||
| if device.get("instanceOf") is None: | ||
| updated_device = self.update_device_info_filter_status( | ||
| device, DeviceStatus.unassociated.value | ||
| ) | ||
| else: | ||
| if device["provisioned"] is False: | ||
|
valluriraj marked this conversation as resolved.
Outdated
|
||
| updated_device = self.update_device_info_filter_status( | ||
| device, DeviceStatus.registered.value | ||
| ) | ||
| else: | ||
| updated_device = self.update_device_info_filter_status( | ||
| device, DeviceStatus.provisioned.value | ||
| ) | ||
| return updated_device | ||
|
|
||
| def update_all_device_status(self, devices): | ||
|
valluriraj marked this conversation as resolved.
Outdated
|
||
| filtered_device_list = [] | ||
| for device in devices: | ||
| filtered_device_list.append(self.update_device_status(device)) | ||
| return filtered_device_list | ||
|
|
||
| def filter_device_list(self, devices, device_status): | ||
| filtered_device_list = [] | ||
| updated_device_list = self.update_all_device_status(devices) | ||
| if device_status is None: | ||
| return updated_device_list | ||
|
|
||
| for device in updated_device_list: | ||
| if device.get("deviceStatus") is device_status: | ||
| filtered_device_list.append(device) | ||
| return filtered_device_list | ||
|
|
||
| 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.
|
||
| filtered_devices = self.filter_device_list(real_devices, device_status) | ||
|
|
||
| if len(devices) != len(filtered_devices): | ||
| logger.warning( | ||
| "Getting registration info for following devices. " | ||
| "All other devices are simulated. " | ||
|
valluriraj marked this conversation as resolved.
Outdated
|
||
| "{}".format([device["id"] for device in 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 | ||
Uh oh!
There was an error while loading. Please reload this page.