Esempio n. 1
0
def cluster_template_create(context, values):
    values = values.copy()
    cluster_template = m.ClusterTemplate()
    node_groups = values.pop("node_groups") or []
    cluster_template.update(values)

    session = get_session()
    with session.begin():
        try:
            cluster_template.save(session=session)
        except db_exc.DBDuplicateEntry as e:
            raise ex.DBDuplicateEntry(
                _("Duplicate entry for ClusterTemplate: %s") % e.columns)

        try:
            for ng in node_groups:
                node_group = m.TemplatesRelation()
                node_group.update({"cluster_template_id": cluster_template.id})
                node_group.update(ng)
                node_group.save(session=session)

        except db_exc.DBDuplicateEntry as e:
            raise ex.DBDuplicateEntry(
                _("Duplicate entry for TemplatesRelation: %s") % e.columns)

    return cluster_template_get(context, cluster_template.id)
Esempio n. 2
0
def cluster_template_create(context, values):
    values = values.copy()
    cluster_template = m.ClusterTemplate()
    node_groups = values.pop("node_groups") or []
    cluster_template.update(values)

    session = get_session()
    try:
        with session.begin():
            session.add(cluster_template)
            session.flush(objects=[cluster_template])

            for ng in node_groups:
                node_group = m.TemplatesRelation()
                node_group.update({"cluster_template_id": cluster_template.id})
                node_group.update(ng)
                session.add(node_group)

    except db_exc.DBDuplicateEntry as e:
        raise ex.DBDuplicateEntry(
            _("Duplicate entry for object %(object)s. Failed on columns: "
              "%(columns)s") % {
                  "object": e.value,
                  "columns": e.columns
              })

    return cluster_template_get(context, cluster_template.id)
Esempio n. 3
0
File: api.py Progetto: uladz/sahara
def cluster_template_update(context, values, ignore_default=False):
    explicit_node_groups = "node_groups" in values
    if explicit_node_groups:
        node_groups = values.pop("node_groups")
        if node_groups is None:
            node_groups = []

    session = get_session()
    cluster_template_id = values['id']
    try:
        with session.begin():
            cluster_template = (_cluster_template_get(
                context, session, cluster_template_id))
            if not cluster_template:
                raise ex.NotFoundException(
                    cluster_template_id,
                    _("Cluster Template id '%s' not found!"))

            elif not ignore_default and cluster_template.is_default:
                raise ex.UpdateFailedException(
                    cluster_template_id,
                    _("ClusterTemplate id '%s' can not be updated. "
                      "It is a default template.")
                )

            validate.check_tenant_for_update(context, cluster_template)
            validate.check_protected_from_update(cluster_template, values)

            if len(cluster_template.clusters) > 0:
                raise ex.UpdateFailedException(
                    cluster_template_id,
                    _("Cluster Template id '%s' can not be updated. "
                      "It is referenced by at least one cluster.")
                )
            cluster_template.update(values)
            # The flush here will cause a duplicate entry exception if
            # unique constraints are violated, before we go ahead and delete
            # the node group templates
            session.flush(objects=[cluster_template])

            # If node_groups has not been specified, then we are
            # keeping the old ones so don't delete!
            if explicit_node_groups:
                model_query(m.TemplatesRelation,
                            context, session=session).filter_by(
                    cluster_template_id=cluster_template_id).delete()

                for ng in node_groups:
                    node_group = m.TemplatesRelation()
                    node_group.update(ng)
                    node_group.update({"cluster_template_id":
                                       cluster_template_id})
                    session.add(node_group)

    except db_exc.DBDuplicateEntry as e:
        raise ex.DBDuplicateEntry(
            _("Duplicate entry for ClusterTemplate: %s") % e.columns)

    return cluster_template_get(context, cluster_template_id)
Esempio n. 4
0
File: api.py Progetto: lhcxx/sahara
def cluster_template_update(context, values, ignore_default=False):
    node_groups = values.pop("node_groups", [])

    session = get_session()
    with session.begin():
        cluster_template_id = values['id']
        cluster_template = (_cluster_template_get(
            context, session, cluster_template_id))
        if not cluster_template:
            raise ex.NotFoundException(
                cluster_template_id,
                _("Cluster Template id '%s' not found!"))

        elif not ignore_default and cluster_template.is_default:
            raise ex.UpdateFailedException(
                cluster_template_id,
                _("ClusterTemplate id '%s' can not be updated. "
                  "It is a default template.")
            )

        name = values.get('name')
        if name:
            same_name_tmpls = model_query(
                m.ClusterTemplate, context).filter_by(
                name=name).all()
            if (len(same_name_tmpls) > 0 and
                    same_name_tmpls[0].id != cluster_template_id):
                raise ex.DBDuplicateEntry(
                    _("Cluster Template can not be updated. "
                      "Another cluster template with name %s already exists.")
                    % name
                )

        if len(cluster_template.clusters) > 0:
            raise ex.UpdateFailedException(
                cluster_template_id,
                _("Cluster Template id '%s' can not be updated. "
                  "It is referenced by at least one cluster.")
            )
        cluster_template.update(values)

        model_query(m.TemplatesRelation, context).filter_by(
            cluster_template_id=cluster_template_id).delete()
        for ng in node_groups:
            node_group = m.TemplatesRelation()
            node_group.update(ng)
            node_group.update({"cluster_template_id": cluster_template_id})
            node_group.save(session=session)

    return cluster_template