def start_check_cluster_health(cluster_id, send_report_url,
                               user=None, session=None, check_health={}):
    """Start to check cluster health."""
    cluster = cluster_api.get_cluster_internal(cluster_id, session=session)

    if cluster.state.state != 'SUCCESSFUL':
        logging.debug("state is %s" % cluster.state.state)
        err_msg = "Healthcheck starts only after cluster finished deployment!"
        raise exception.Forbidden(err_msg)

    reports = utils.list_db_objects(
        session, models.HealthCheckReport,
        cluster_id=cluster.id, state='verifying'
    )
    if reports:
        err_msg = 'Healthcheck in progress, please wait for it to complete!'
        raise exception.Forbidden(err_msg)

    # Clear all preivous report
    # TODO(grace): the delete should be moved into celery task.
    # We should consider the case that celery task is down.
    utils.del_db_objects(
        session, models.HealthCheckReport, cluster_id=cluster.id
    )

    from compass.tasks import client as celery_client
    celery_client.celery.send_task(
        'compass.tasks.cluster_health',
        (cluster.id, send_report_url, user.email)
    )
    return {
        "cluster_id": cluster.id,
        "status": "start to check cluster health."
    }
def start_check_cluster_health(cluster_id,
                               send_report_url,
                               user=None,
                               session=None,
                               check_health={}):
    """Start to check cluster health."""
    cluster = cluster_api.get_cluster_internal(cluster_id, session=session)

    if cluster.state.state != 'SUCCESSFUL':
        logging.debug("state is %s" % cluster.state.state)
        err_msg = "Healthcheck starts only after cluster finished deployment!"
        raise exception.Forbidden(err_msg)

    reports = utils.list_db_objects(session,
                                    models.HealthCheckReport,
                                    cluster_id=cluster.id,
                                    state='verifying')
    if reports:
        err_msg = 'Healthcheck in progress, please wait for it to complete!'
        raise exception.Forbidden(err_msg)

    # Clear all preivous report
    # TODO(grace): the delete should be moved into celery task.
    # We should consider the case that celery task is down.
    utils.del_db_objects(session,
                         models.HealthCheckReport,
                         cluster_id=cluster.id)

    from compass.tasks import client as celery_client
    celery_client.celery.send_task('compass.tasks.cluster_health',
                                   (cluster.id, send_report_url, user.email))
    return {
        "cluster_id": cluster.id,
        "status": "start to check cluster health."
    }
def add_report_record(cluster_id, name=None, report={},
                      state='verifying', session=None, **kwargs):
    """Create a health check report record."""
    # Replace any white space into '-'
    words = name.split()
    name = '-'.join(words)
    cluster = cluster_api.get_cluster_internal(cluster_id, session=session)
    return utils.add_db_object(
        session, models.HealthCheckReport, True, cluster.id, name,
        report=report, state=state, **kwargs
    )
def add_report_record(cluster_id, name=None, report={},
                      state='verifying', session=None, **kwargs):
    """Create a health check report record."""
    # Replace any white space into '-'
    words = name.split()
    name = '-'.join(words)
    cluster = cluster_api.get_cluster_internal(cluster_id, session=session)
    return utils.add_db_object(
        session, models.HealthCheckReport, True, cluster.id, name,
        report=report, state=state, **kwargs
    )
def delete_reports(cluster_id, name=None, user=None, session=None):
    # TODO(grace): better to separate this function into two.
    # One is to delete a report of a cluster, the other to delete all
    # reports under a cluster.
    if name:
        report = _get_report(cluster_id, name, session=session)
        return utils.del_db_object(session, report)
    else:
        cluster = cluster_api.get_cluster_internal(cluster_id, session=session)
        return utils.del_db_objects(session,
                                    models.HealthCheckReport,
                                    cluster_id=cluster.id)
def delete_reports(cluster_id, name=None, user=None, session=None):
    # TODO(grace): better to separate this function into two.
    # One is to delete a report of a cluster, the other to delete all
    # reports under a cluster.
    if name:
        report = _get_report(cluster_id, name, session=session)
        return utils.del_db_object(session, report)
    else:
        cluster = cluster_api.get_cluster_internal(
            cluster_id, session=session
        )
        return utils.del_db_objects(
            session, models.HealthCheckReport, cluster_id=cluster.id
        )
def set_error(cluster_id, report={}, session=None,
              state='error', error_message=None):
    cluster = cluster_api.get_cluster_internal(cluster_id, session=session)
    logging.debug(
        "updates all reports as %s in cluster %s",
        state, cluster_id
    )
    return utils.update_db_objects(
        session, models.HealthCheckReport,
        updates={
            'report': {},
            'state': 'error',
            'error_message': error_message
        }, cluster_id=cluster.id
    )
def set_error(cluster_id, report={}, session=None,
              state='error', error_message=None):
    cluster = cluster_api.get_cluster_internal(cluster_id, session=session)
    logging.debug(
        "updates all reports as %s in cluster %s",
        state, cluster_id
    )
    return utils.update_db_objects(
        session, models.HealthCheckReport,
        updates={
            'report': {},
            'state': 'error',
            'error_message': error_message
        }, cluster_id=cluster.id
    )
def _get_report(cluster_id, name, session=None):
    cluster = cluster_api.get_cluster_internal(cluster_id, session=session)
    return utils.get_db_object(
        session, models.HealthCheckReport, cluster_id=cluster.id, name=name
    )
def list_health_reports(cluster_id, user=None, session=None):
    """List all reports in the specified cluster."""
    cluster = cluster_api.get_cluster_internal(cluster_id, session=session)
    return utils.list_db_objects(
        session, models.HealthCheckReport, cluster_id=cluster.id
    )
def _get_report(cluster_id, name, session=None):
    cluster = cluster_api.get_cluster_internal(cluster_id, session=session)
    return utils.get_db_object(
        session, models.HealthCheckReport, cluster_id=cluster.id, name=name
    )
def list_health_reports(cluster_id, user=None, session=None):
    """List all reports in the specified cluster."""
    cluster = cluster_api.get_cluster_internal(cluster_id, session=session)
    return utils.list_db_objects(
        session, models.HealthCheckReport, cluster_id=cluster.id
    )