def generate_csi_expand_volume_response(capacity_bytes, node_expansion_required=True):
    logger.debug("creating response for expand volume")
    res = csi_pb2.ControllerExpandVolumeResponse(
        capacity_bytes=capacity_bytes,
        node_expansion_required=node_expansion_required,
    )

    logger.debug("finished creating expand volume response")
    return res
    def ControllerExpandVolume(self, request, context):
        set_current_thread_name(request.volume_id)
        logger.info("ControllerExpandVolume")
        secrets = request.secrets

        try:
            utils.validate_expand_volume_request(request)

            try:
                volume_id_info = utils.get_volume_id_info(request.volume_id)
            except ObjectIdError as ex:
                logger.warning("volume id is invalid. error : {}".format(ex))
                context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
                return csi_pb2.ControllerExpandVolumeResponse()
            system_id = volume_id_info.system_id
            array_type = volume_id_info.array_type
            volume_id = volume_id_info.object_id
            array_connection_info = utils.get_array_connection_info_from_secrets(
                secrets, system_id=system_id)
            with get_agent(array_connection_info,
                           array_type).get_mediator() as array_mediator:
                logger.debug(array_mediator)

                required_bytes = request.capacity_range.required_bytes
                max_size = array_mediator.maximal_volume_size_in_bytes

                volume_before_expand = array_mediator.get_object_by_id(
                    volume_id, config.VOLUME_TYPE_NAME)
                if not volume_before_expand:
                    raise array_errors.ObjectNotFoundError(volume_id)

                if volume_before_expand.capacity_bytes >= required_bytes:
                    context.set_code(grpc.StatusCode.OK)
                    return utils.generate_csi_expand_volume_response(
                        volume_before_expand.capacity_bytes,
                        node_expansion_required=False)

                if required_bytes > max_size:
                    message = messages.SizeOutOfRangeError_message.format(
                        required_bytes, max_size)
                    logger.error(message)
                    context.set_code(grpc.StatusCode.OUT_OF_RANGE)
                    context.set_details(message)
                    return csi_pb2.ControllerExpandVolumeResponse()

                logger.debug("expanding volume {0}".format(volume_id))
                array_mediator.expand_volume(volume_id=volume_id,
                                             required_bytes=required_bytes)

                volume_after_expand = array_mediator.get_object_by_id(
                    volume_id, config.VOLUME_TYPE_NAME)
                if not volume_after_expand:
                    raise array_errors.ObjectNotFoundError(volume_id)

            res = utils.generate_csi_expand_volume_response(
                volume_after_expand.capacity_bytes)
            logger.info("finished expanding volume")
            return res

        except array_errors.PermissionDeniedError as ex:
            context.set_code(grpc.StatusCode.PERMISSION_DENIED)
            context.set_details(ex.message)
            return csi_pb2.ControllerExpandVolumeResponse()

        except array_errors.ObjectNotFoundError as ex:
            logger.info("Volume not found: {0}".format(ex))
            context.set_code(grpc.StatusCode.NOT_FOUND)
            context.set_details(ex.message)
            return csi_pb2.ControllerExpandVolumeResponse()

        except (ValidationException, array_errors.IllegalObjectID) as ex:
            logger.exception(ex)
            context.set_details(ex.message)
            context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            return csi_pb2.ControllerExpandVolumeResponse()

        except array_errors.NotEnoughSpaceInPool as ex:
            logger.exception(ex)
            context.set_details(ex.message)
            context.set_code(grpc.StatusCode.RESOURCE_EXHAUSTED)
            return csi_pb2.ControllerExpandVolumeResponse()

        except Exception as ex:
            logger.debug("an internal exception occurred")
            logger.exception(ex)
            context.set_code(grpc.StatusCode.INTERNAL)
            context.set_details(
                'an internal exception occurred : {}'.format(ex))
            return csi_pb2.ControllerExpandVolumeResponse()