def update(self): validate_creds_exists(self.app) db = DBWrapper(self.app.creds) group_client = APIClient(db.get_configure()).get_group_api_client() enterprise_id = db.get_enterprise_id() data = DeviceGroupUpdate() group_name = self.app.pargs.name kwargs = {'name': group_name} try: search_response = group_client.get_all_groups(enterprise_id, limit=1, offset=0, **kwargs) if not search_response.results or len( search_response.results) == 0: self.app.log.debug( f'[group-update] Group does not exist with name {group_name}' ) self.app.render(f'Group does not exist with name {group_name}') return response = search_response.results[0] group_id = response.id except ApiException as e: self.app.log.error(f"[group-update] Failed to list groups: {e}") self.app.render(f"ERROR: {parse_error_message(self.app, e)}") return if self.app.pargs.new_name: data.name = self.app.pargs.new_name else: self.app.log.debug('[group-update] name cannot be empty.') self.app.render('name cannot be empty.') return try: response = group_client.partial_update_group( group_id, enterprise_id, data) except ApiException as e: self.app.log.error( f"[group-update] Failed to update details of a group: {e}") self.app.render(f"ERROR: {parse_error_message(self.app, e)}") return if not self.app.pargs.json: renderable = self._group_basic_response(response) self.app.render(renderable, format=OutputFormat.TABULATED.value, headers="keys", tablefmt="plain") else: renderable = self._group_basic_response(response, OutputFormat.JSON) self.app.render(renderable, format=OutputFormat.JSON.value)
def remove(self): validate_creds_exists(self.app) db = DBWrapper(self.app.creds) group_client = APIClient(db.get_configure()).get_group_api_client() enterprise_id = db.get_enterprise_id() data = DeviceGroupUpdate() if self.app.pargs.group: group_name = self.app.pargs.group kwargs = {'name': group_name} try: search_response = group_client.get_all_groups(enterprise_id, limit=1, offset=0, **kwargs) if not search_response.results or len( search_response.results) == 0: self.app.log.debug( f'[group-remove] Group does not exist with name {group_name}' ) self.app.render( f'Group does not exist with name {group_name}') return response = search_response.results[0] group_id = response.id except ApiException as e: self.app.log.error( f"[group-remove] Failed to list groups: {e}") self.app.render(f"ERROR: {parse_error_message(self.app, e)}") return else: group = db.get_group() if group is None or group.get('name') is None: self.app.log.debug('[group-remove] There is no active group.') self.app.render('There is no active group.') return group_id = group.get('id') devices = self.app.pargs.devices if not devices or len(devices) == 0: self.app.log.debug('[group-remove] devices cannot be empty.') self.app.render('devices cannot be empty.') return device_client = APIClient(db.get_configure()).get_device_api_client() request_device_ids = [] for device_name in devices: 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'[group-remove] 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] request_device_ids.append(response.id) except ApiException as e: self.app.log.error(f"Failed to list devices: {e}") self.app.render(f"ERROR: {parse_error_message(self.app, e)}") return device_client = APIClient(db.get_configure()).get_device_api_client() current_device_ids = self._get_group_device_ids( device_client, enterprise_id, group_id) if current_device_ids is None: return latest_devices = list( set(current_device_ids) - set(request_device_ids)) data.device_ids = latest_devices try: response = group_client.partial_update_group( group_id, enterprise_id, data) except ApiException as e: self.app.log.error( f"[group-remove] Failed to remove device from group: {e}") self.app.render(f"ERROR: {parse_error_message(self.app, e)}") return if not self.app.pargs.json: renderable = self._group_basic_response(response) self.app.render(renderable, format=OutputFormat.TABULATED.value, headers="keys", tablefmt="plain") else: renderable = self._group_basic_response(response, OutputFormat.JSON) self.app.render(renderable, format=OutputFormat.JSON.value)