-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fill gaps with IoT Central REST apis (#390)
* add device groups and roles commands * include role_id in help * add device group tests * add central roles unit tests * update version * fix formatting * remove unused * Update mock import. * add preview tag * add int tests for new commands Co-authored-by: Paymaun Heidari <[email protected]> Co-authored-by: Paymaun <[email protected]>
- Loading branch information
1 parent
dfe4d72
commit 87a4b79
Showing
21 changed files
with
613 additions
and
3 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 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. | ||
# -------------------------------------------------------------------------------------------- | ||
# Dev note - think of this as a controller | ||
|
||
from azext_iot.constants import CENTRAL_ENDPOINT | ||
from azext_iot.central.providers.preview import CentralDeviceGroupProviderPreview | ||
from azext_iot.central.models.enum import ApiVersion | ||
|
||
|
||
def list_device_groups( | ||
cmd, | ||
app_id: str, | ||
token=None, | ||
central_dns_suffix=CENTRAL_ENDPOINT, | ||
api_version=ApiVersion.preview.value, | ||
): | ||
provider = CentralDeviceGroupProviderPreview(cmd=cmd, app_id=app_id, token=token) | ||
return provider.list_device_groups(central_dns_suffix=central_dns_suffix) |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# 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. | ||
# -------------------------------------------------------------------------------------------- | ||
# Dev note - think of this as a controller | ||
|
||
from azext_iot.constants import CENTRAL_ENDPOINT | ||
from azext_iot.central.providers.preview import CentralRoleProviderPreview | ||
from azext_iot.central.models.enum import ApiVersion | ||
|
||
|
||
def get_role( | ||
cmd, | ||
app_id: str, | ||
role_id: str, | ||
token=None, | ||
central_dns_suffix=CENTRAL_ENDPOINT, | ||
api_version=ApiVersion.preview.value, | ||
): | ||
provider = CentralRoleProviderPreview(cmd=cmd, app_id=app_id, token=token) | ||
|
||
return provider.get_role(role_id=role_id, central_dns_suffix=central_dns_suffix) | ||
|
||
|
||
def list_roles( | ||
cmd, | ||
app_id: str, | ||
token=None, | ||
central_dns_suffix=CENTRAL_ENDPOINT, | ||
api_version=ApiVersion.preview.value, | ||
): | ||
provider = CentralRoleProviderPreview(cmd=cmd, app_id=app_id, token=token) | ||
|
||
return provider.list_roles(central_dns_suffix=central_dns_suffix) |
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
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# 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. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
class DeviceGroupPreview: | ||
def __init__(self, group: dict): | ||
self.display_name = group.get("displayName") | ||
self.id = group.get("id") | ||
pass |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# 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. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
class RolePreview: | ||
def __init__(self, device: dict): | ||
self.display_name = device.get("displayName") | ||
self.id = device.get("id") | ||
pass |
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
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
48 changes: 48 additions & 0 deletions
48
azext_iot/central/providers/preview/device_group_provider_preview.py
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 |
---|---|---|
@@ -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. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
|
||
from typing import List | ||
from azext_iot.central.models.deviceGroupPreview import DeviceGroupPreview | ||
from knack.log import get_logger | ||
from azext_iot.constants import CENTRAL_ENDPOINT | ||
from azext_iot.central import services as central_services | ||
from azext_iot.central.models.enum import ApiVersion | ||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
class CentralDeviceGroupProviderPreview: | ||
def __init__(self, cmd, app_id: str, token=None): | ||
""" | ||
Provider for device groups 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_groups = {} | ||
|
||
def list_device_groups(self, central_dns_suffix=CENTRAL_ENDPOINT) -> List[DeviceGroupPreview]: | ||
device_groups = central_services.device_group.list_device_groups( | ||
cmd=self._cmd, | ||
app_id=self._app_id, | ||
token=self._token, | ||
central_dns_suffix=central_dns_suffix, | ||
api_version=ApiVersion.preview.value, | ||
) | ||
|
||
# add to cache | ||
self._device_groups.update({device_group.id: device_group for device_group in device_groups}) | ||
|
||
return self._device_groups |
71 changes: 71 additions & 0 deletions
71
azext_iot/central/providers/preview/role_provider_preview.py
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# 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 typing import List | ||
from azext_iot.central.models.rolePreview import RolePreview | ||
from knack.util import CLIError | ||
from knack.log import get_logger | ||
from azext_iot.constants import CENTRAL_ENDPOINT | ||
from azext_iot.central import services as central_services | ||
from azext_iot.central.models.enum import ApiVersion | ||
from azext_iot.central import models as central_models | ||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
class CentralRoleProviderPreview: | ||
def __init__(self, cmd, app_id: str, token=None): | ||
""" | ||
Provider for roles 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._roles = {} | ||
|
||
def list_roles(self, central_dns_suffix=CENTRAL_ENDPOINT) -> List[RolePreview]: | ||
roles = central_services.role.list_roles( | ||
cmd=self._cmd, | ||
app_id=self._app_id, | ||
token=self._token, | ||
central_dns_suffix=central_dns_suffix, | ||
api_version=ApiVersion.preview.value, | ||
) | ||
|
||
# add to cache | ||
self._roles.update({role.id: role for role in roles}) | ||
|
||
return self._roles | ||
|
||
def get_role( | ||
self, role_id, central_dns_suffix=CENTRAL_ENDPOINT, | ||
) -> central_models.RolePreview: | ||
# get or add to cache | ||
role = self._roles.get(role_id) | ||
if not role: | ||
role = central_services.role.get_role( | ||
cmd=self._cmd, | ||
app_id=self._app_id, | ||
role_id=role_id, | ||
token=self._token, | ||
central_dns_suffix=central_dns_suffix, | ||
api_version=ApiVersion.preview.value, | ||
) | ||
self._roles[role_id] = role | ||
|
||
if not role: | ||
raise CLIError("No role found with id: '{}'.".format(role_id)) | ||
|
||
return role |
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
Oops, something went wrong.