예제 #1
0
    def lock(self):
        validate_creds_exists(self.app)
        db = DBWrapper(self.app.creds)
        command_client = APIClient(db.get_configure()).get_command_api_client()
        enterprise_id = db.get_enterprise_id()
        device_client = APIClient(db.get_configure()).get_device_api_client()

        if self.app.pargs.device:
            device_name = self.app.pargs.device
            kwargs = {'name': device_name}
            try:
                search_response = device_client.get_all_devices(enterprise_id,
                                                                limit=1,
                                                                offset=0,
                                                                **kwargs)
                if not search_response.results or len(
                        search_response.results) == 0:
                    self.app.log.debug(
                        f'[device-command-lock] Device does not exist with name {device_name}'
                    )
                    self.app.render(
                        f'Device does not exist with name {device_name}')
                    return
                response = search_response.results[0]
                device_id = response.id
            except ApiException as e:
                self.app.log.error(
                    f"[device-command-lock] Failed to list devices: {e}")
                self.app.render(f"ERROR: {parse_error_message(self.app, e)}")
                return
        else:
            device = db.get_device()
            if not device or not device.get('id'):
                self.app.log.debug(
                    '[device-command-lock] There is no active device.')
                self.app.render('There is no active device.')
                return

            device_id = device.get('id')

        command_request = CommandRequest(command=DeviceCommandEnum.LOCK.name)
        try:
            response = command_client.run_command(enterprise_id, device_id,
                                                  command_request)
        except ApiException as e:
            self.app.log.error(
                f"[device-command-lock] Failed to fire the lock command: {e}")
            self.app.render(f"ERROR: {parse_error_message(self.app, e)}")
            return

        if not self.app.pargs.json:
            renderable = self._command_basic_response(response)
            self.app.render(renderable,
                            format=OutputFormat.TABULATED.value,
                            headers="keys",
                            tablefmt="plain")
        else:
            renderable = self._command_basic_response(response,
                                                      OutputFormat.JSON)
            self.app.render(renderable, format=OutputFormat.JSON.value)
예제 #2
0
    def wipe(self):
        validate_creds_exists(self.app)
        db = DBWrapper(self.app.creds)
        command_client = APIClient(db.get_configure()).get_command_api_client()
        enterprise_id = db.get_enterprise_id()
        device_client = APIClient(db.get_configure()).get_device_api_client()

        if self.app.pargs.device:
            device_name = self.app.pargs.device
            kwargs = {'name': device_name}
            try:
                search_response = device_client.get_all_devices(enterprise_id, limit=1, offset=0, **kwargs)
                if not search_response.results or len(search_response.results) == 0:
                    self.app.log.debug(f'[device-command-wipe] Device does not exist with name {device_name}')
                    self.app.render(f'Device does not exist with name {device_name}\n')
                    return
                response = search_response.results[0]
                device_id = response.id
            except ApiException as e:
                self.app.log.error(f"[device-command-wipe] Failed to list devices: {e}")
                self.app.render(f"ERROR: {parse_error_message(self.app, e)}\n")
                return
        else:
            device = db.get_device()
            if not device or not device.get('id'):
                self.app.log.debug('[device-command-wipe] There is no active device.')
                self.app.render('There is no active device.\n')
                return

            device_id = device.get('id')

        external_storage = self.app.pargs.external_storage
        frp = self.app.pargs.frp

        if external_storage is None:
            self.app.log.info('[device-command-wipe] External storage value is empty')
            self.app.render('External storage value is empty\n')

        if frp is None:
            self.app.log.info('[device-command-wipe] Factory reset production value is empty')
            self.app.render('Factory reset production value is empty\n')

        command_request = CommandRequest(command_args={"wipe_external_storage": external_storage, 'wipe_FRP': frp},
                                         command=DeviceCommandEnum.WIPE.name)
        try:
            response = command_client.run_command(enterprise_id, device_id, command_request)
        except ApiException as e:
            self.app.log.error(f"[device-command-wipe] Failed to fire the wipe command: {e}")
            self.app.render(f"ERROR: {parse_error_message(self.app, e)}\n")
            return

        if not self.app.pargs.json:
            renderable = self._command_basic_response(response)
            self.app.render(renderable, format=OutputFormat.TABULATED.value, headers="keys", tablefmt="plain")
        else:
            renderable = self._command_basic_response(response, OutputFormat.JSON)
            self.app.render(renderable, format=OutputFormat.JSON.value)