|
| 1 | +import random |
| 2 | +import string |
| 3 | +import uuid |
| 4 | + |
| 5 | +from office365.delta_collection import DeltaCollection |
| 6 | +from office365.intune.devices.alternative_security_id import AlternativeSecurityId |
| 7 | +from office365.intune.devices.device import Device |
| 8 | +from office365.runtime.client_value_collection import ClientValueCollection |
| 9 | +from office365.runtime.queries.create_entity import CreateEntityQuery |
| 10 | + |
| 11 | + |
| 12 | +class DeviceCollection(DeltaCollection[Device]): |
| 13 | + """Device's collection""" |
| 14 | + |
| 15 | + def __init__(self, context, resource_path=None): |
| 16 | + super(DeviceCollection, self).__init__(context, Device, resource_path) |
| 17 | + |
| 18 | + def add( |
| 19 | + self, |
| 20 | + display_name, |
| 21 | + operating_system, |
| 22 | + operating_system_version, |
| 23 | + account_enabled=False, |
| 24 | + alternative_security_id=None, |
| 25 | + device_id=None, |
| 26 | + ): |
| 27 | + """Create and register a new device in the organization. |
| 28 | + :param str display_name: The display name for the device |
| 29 | + :param str operating_system: |
| 30 | + :param str operating_system_version: |
| 31 | + :param bool account_enabled: |
| 32 | + :param AlternativeSecurityId alternative_security_id: |
| 33 | + :param str device_id: |
| 34 | + """ |
| 35 | + |
| 36 | + if alternative_security_id is None: |
| 37 | + key_id = "base64" + "".join( |
| 38 | + random.choice(string.ascii_lowercase + string.digits) for _ in range(10) |
| 39 | + ) |
| 40 | + alternative_security_id = AlternativeSecurityId(2, key_id) |
| 41 | + if device_id is None: |
| 42 | + device_id = str(uuid.uuid4()) |
| 43 | + |
| 44 | + return_type = Device(self.context) |
| 45 | + return_type.set_property("displayName", display_name) |
| 46 | + return_type.set_property("operatingSystem", operating_system) |
| 47 | + return_type.set_property("operatingSystemVersion", operating_system_version) |
| 48 | + return_type.set_property("accountEnabled", account_enabled) |
| 49 | + return_type.set_property("deviceId", device_id) |
| 50 | + return_type.set_property( |
| 51 | + "alternativeSecurityIds", |
| 52 | + ClientValueCollection(AlternativeSecurityId, [alternative_security_id]), |
| 53 | + ) |
| 54 | + self.add_child(return_type) |
| 55 | + qry = CreateEntityQuery(self, return_type, return_type) |
| 56 | + self.context.add_query(qry) |
| 57 | + return return_type |
0 commit comments