Exemplo n.º 1
0
def node_group_template_update(context, values, ignore_default=False):
    session = get_session()
    try:
        with session.begin():
            ngt_id = values['id']
            ngt = _node_group_template_get(context, session, ngt_id)
            if not ngt:
                raise ex.NotFoundException(
                    ngt_id, _("NodeGroupTemplate id '%s' not found"))
            elif not ignore_default and ngt.is_default:
                raise ex.UpdateFailedException(
                    ngt_id,
                    _("NodeGroupTemplate id '%s' can not be updated. "
                      "It is a default template."))

            # Check to see that the node group template to be updated is not in
            # use by an existing cluster.
            for template_relationship in ngt.templates_relations:
                if len(template_relationship.cluster_template.clusters) > 0:
                    raise ex.UpdateFailedException(
                        ngt_id,
                        _("NodeGroupTemplate id '%s' can not be updated. "
                          "It is referenced by an existing cluster."))

            ngt.update(values)
    except db_exc.DBDuplicateEntry as e:
        raise ex.DBDuplicateEntry(
            _("Duplicate entry for NodeGroupTemplate: %s") % e.columns)

    return ngt
Exemplo n.º 2
0
Arquivo: api.py Projeto: 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)
Exemplo n.º 3
0
Arquivo: api.py Projeto: 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
Exemplo n.º 4
0
def data_source_update(context, values):
    session = get_session()
    try:
        with session.begin():
            ds_id = values['id']
            data_source = _data_source_get(context, session, ds_id)
            if not data_source:
                raise ex.NotFoundException(ds_id,
                                           _("DataSource id '%s' not found"))
            else:
                jobs = job_execution_get_all(context)
                pending_jobs = [
                    job for job in jobs if job.info["status"] == "PENDING"
                ]
                for job in pending_jobs:
                    if job.data_source_urls:
                        if ds_id in job.data_source_urls:
                            raise ex.UpdateFailedException(
                                _("DataSource is used in a "
                                  "PENDING Job and can not be updated."))
            data_source.update(values)
    except db_exc.DBDuplicateEntry as e:
        raise ex.DBDuplicateEntry(
            _("Duplicate entry for DataSource: %s") % e.columns)

    return data_source
Exemplo n.º 5
0
Arquivo: acl.py Projeto: madar010/mad
def check_tenant_for_update(context, object):
    if object.tenant_id != context.tenant_id:
        raise ex.UpdateFailedException(
            object.id,
            _("{object} with id '%s' could not be updated because "
              "it wasn't created in this tenant").format(
                  object=type(object).__name__))
Exemplo n.º 6
0
def check_protected_from_update(object, data):
    if object.is_protected and data.get('is_protected', True):
        raise ex.UpdateFailedException(
            object.id,
            _("{object} with id '%s' could not be updated "
              "because it's marked as protected").format(
                  object=type(object).__name__))
Exemplo n.º 7
0
def node_group_template_update(context, values, ignore_prot_on_def=False):
    session = get_session()
    try:
        with session.begin():
            ngt_id = values['id']
            ngt = _node_group_template_get(context, session, ngt_id)
            if not ngt:
                raise ex.NotFoundException(
                    ngt_id, _("NodeGroupTemplate id '%s' not found"))

            validate.check_tenant_for_update(context, ngt)
            if not (ngt.is_default and ignore_prot_on_def):
                validate.check_protected_from_update(ngt, values)

            # Check to see that the node group template to be updated is not in
            # use by an existing cluster.
            for template_relationship in ngt.templates_relations:
                if len(template_relationship.cluster_template.clusters) > 0:
                    raise ex.UpdateFailedException(
                        ngt_id,
                        _("NodeGroupTemplate id '%s' can not be updated. "
                          "It is referenced by an existing cluster."))

            ngt.update(values)

            # Here we update any cluster templates that reference the
            # updated node group template
            for template_relationship in ngt.templates_relations:
                ct_id = template_relationship.cluster_template_id
                ct = cluster_template_get(
                    context, template_relationship.cluster_template_id)
                node_groups = ct.node_groups
                ct_node_groups = []
                for ng in node_groups:
                    # Need to fill in all node groups, not just
                    # the modified group
                    ng_to_add = ng
                    if ng.node_group_template_id == ngt_id:
                        # use the updated node group template
                        ng_to_add = ngt
                    ng_to_add = ng_to_add.to_dict()
                    ng_to_add.update({
                        "count":
                        ng["count"],
                        "node_group_template_id":
                        ng.node_group_template_id
                    })
                    ng_to_add.pop("updated_at", None)
                    ng_to_add.pop("created_at", None)
                    ng_to_add.pop("id", None)
                    ct_node_groups.append(ng_to_add)
                ct_update = {"id": ct_id, "node_groups": ct_node_groups}
                cluster_template_update(context, ct_update, ignore_prot_on_def)

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

    return ngt
Exemplo n.º 8
0
def check_data_source_update(data, **kwargs):
    ctx = context.ctx()
    jobs = c.API.job_execution_get_all(ctx)
    pending_jobs = [job for job in jobs if job.info.status == "PENDING"]
    for job in pending_jobs:
        if kwargs["data_source_id"] in job.data_source_urls:
            raise ex.UpdateFailedException(
                _("DataSource is used in a "
                  "PENDING Job and can not be updated."))
Exemplo n.º 9
0
Arquivo: api.py Projeto: lhcxx/sahara
def node_group_template_update(context, values, ignore_default=False):
    session = get_session()
    with session.begin():
        ngt_id = values['id']
        node_group_template = (
            _node_group_template_get(context, session, ngt_id))
        if not node_group_template:
            raise ex.NotFoundException(
                ngt_id, _("NodeGroupTemplate id '%s' not found"))
        elif not ignore_default and node_group_template.is_default:
            raise ex.UpdateFailedException(
                ngt_id,
                _("NodeGroupTemplate id '%s' can not be updated. "
                  "It is a default template.")
            )

        name = values.get('name')
        if name and name != node_group_template.name:
            same_name_tmpls = model_query(
                m.NodeGroupTemplate, context).filter_by(name=name).all()
            if (len(same_name_tmpls) > 0 and
                    same_name_tmpls[0].id != ngt_id):
                raise ex.DBDuplicateEntry(
                    _("Node Group Template can not be updated. "
                      "Another node group template with name %s "
                      "already exists.")
                    % name
                )

        # Check to see that the node group template to be updated is not in
        # use by an existing cluster.
        for template_relationship in node_group_template.templates_relations:
            if len(template_relationship.cluster_template.clusters) > 0:
                raise ex.UpdateFailedException(
                    ngt_id,
                    _("NodeGroupTemplate id '%s' can not be updated. "
                      "It is referenced by an existing cluster.")
                )

        node_group_template.update(values)
    return node_group_template
Exemplo n.º 10
0
def job_binary_update(context, values):
    """Returns a JobBinary updated with the provided values."""
    jb_id = values["id"]
    session = get_session()
    try:
        with session.begin():
            jb = _job_binary_get(context, session, jb_id)
            if not jb:
                raise ex.NotFoundException(jb_id,
                                           _("JobBinary id '%s' not found"))

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

            # We do not want to update the url for internal binaries
            new_url = values.get("url", None)
            if new_url and "internal-db://" in jb["url"]:
                if jb["url"] != new_url:
                    raise ex.UpdateFailedException(
                        jb_id,
                        _("The url for JobBinary Id '%s' can not "
                          "be updated because it is an internal-db url."))
            jobs = job_execution_get_all(context)
            pending_jobs = [
                job for job in jobs if job.info["status"] == "PENDING"
            ]
            if len(pending_jobs) > 0:
                for job in pending_jobs:
                    if _check_job_binary_referenced(context, session, jb_id,
                                                    job.job_id):
                        raise ex.UpdateFailedException(
                            jb_id,
                            _("JobBinary Id '%s' is used in a PENDING job "
                              "and can not be updated."))
            jb.update(values)
    except db_exc.DBDuplicateEntry as e:
        raise ex.DBDuplicateEntry(
            _("Duplicate entry for JobBinary: %s") % e.columns)

    return jb
Exemplo n.º 11
0
Arquivo: acl.py Projeto: madar010/mad
def check_protected_from_update(object, data):
    if object.is_protected and data.get('is_protected', True):
        # Okay, the only thing we can allow here is a change
        # to 'is_public', so we have to make sure no other values
        # are changing
        if 'is_public' in data:
            obj = object.to_dict()
            if all(k == 'is_public' or (k in obj and obj[k] == v)
                   for k, v in six.iteritems(data)):
                return

        raise ex.UpdateFailedException(
            object.id,
            _("{object} with id '%s' could not be updated "
              "because it's marked as protected").format(
                  object=type(object).__name__))
Exemplo n.º 12
0
def check_data_source_update(data, data_source_id):
    ctx = context.ctx()
    jobs = c.API.job_execution_get_all(ctx)
    pending_jobs = [job for job in jobs if job.info["status"] == "PENDING"]
    for job in pending_jobs:
        if data_source_id in job.data_source_urls:
            raise ex.UpdateFailedException(
                _("DataSource is used in a "
                  "PENDING Job and can not be updated."))

    ds = c.API.data_source_get(ctx, data_source_id)
    if 'name' in data and data['name'] != ds.name:
        b.check_data_source_unique_name(data['name'])

    check_data = {'type': data.get('type', None) or ds.type,
                  'url': data.get('url', None) or ds.url,
                  'credentials': data.get(
                      'credentials', None) or ds.credentials}
    _check_data_source(check_data)
Exemplo n.º 13
0
def node_group_template_update(context, values):
    session = get_session()
    with session.begin():
        ngt_id = values['id']
        node_group_template = (_node_group_template_get(
            context, session, ngt_id))
        if not node_group_template:
            raise ex.NotFoundException(
                ngt_id, _("NodeGroupTemplate id '%s' not found"))

        # Check to see that the node group template to be updated is not in
        # use by an existing cluster.
        for template_relationship in node_group_template.templates_relations:
            if len(template_relationship.cluster_template.clusters) > 0:
                raise ex.UpdateFailedException(
                    ngt_id,
                    _("NodeGroupTemplate id '%s' can not be updated. "
                      "It is referenced by an existing cluster."))

        node_group_template.update(values)
    return node_group_template