Beispiel #1
0
    def test_find_node_group_template_by_name(self):
        ctx = context.ctx()
        t = self.api.node_group_template_create(ctx, c.SAMPLE_NGT)

        found = utils.find_node_group_template_by_name(ctx, c.SAMPLE_NGT["name"])
        self.assertEqual(t["id"], found["id"])

        found = utils.find_node_group_template_by_name(ctx, "fred")
        self.assertIsNone(found)
Beispiel #2
0
    def test_find_node_group_template_by_name(self):
        ctx = context.ctx()
        t = self.api.node_group_template_create(ctx, c.SAMPLE_NGT)

        found = utils.find_node_group_template_by_name(ctx,
                                                       c.SAMPLE_NGT["name"])
        self.assertEqual(t["id"], found["id"])

        found = utils.find_node_group_template_by_name(ctx, "fred")
        self.assertIsNone(found)
Beispiel #3
0
def check_usage_of_existing(ctx, ng_templates, cl_templates):
    '''Determine if any of the specified templates are in use

    This method searches for the specified templates by name and
    determines whether or not any existing templates are in use
    by a cluster or cluster template. Returns True if any of
    the templates are in use.

    :param ng_templates: A list of dictionaries. Each dictionary
                         has a "template" entry that represents
                         a node group template.
    :param cl_templates: A list of dictionaries. Each dictionary
                         has a "template" entry that represents
                         a cluster template
    :returns: True if any of the templates are in use, False otherwise
    '''
    error = False
    clusters = conductor.API.cluster_get_all(ctx)

    for ng_info in ng_templates:
        ng = u.find_node_group_template_by_name(ctx,
                                                ng_info["template"]["name"])
        if ng:
            cluster_users, template_users = u.check_node_group_template_usage(
                ng["id"], clusters)

            if cluster_users:
                LOG.warning(
                    _LW("Node group template {name} "
                        "in use by clusters {clusters}").format(
                            name=ng["name"], clusters=cluster_users))
            if template_users:
                LOG.warning(
                    _LW("Node group template {name} "
                        "in use by cluster templates {templates}").format(
                            name=ng["name"], templates=template_users))

            if cluster_users or template_users:
                LOG.warning(
                    _LW("Update of node group template "
                        "{name} is not allowed").format(name=ng["name"]))
                error = True

    for cl_info in cl_templates:
        cl = u.find_cluster_template_by_name(ctx, cl_info["template"]["name"])
        if cl:
            cluster_users = u.check_cluster_template_usage(cl["id"], clusters)

            if cluster_users:
                LOG.warning(
                    _LW("Cluster template {name} "
                        "in use by clusters {clusters}").format(
                            name=cl["name"], clusters=cluster_users))

                LOG.warning(
                    _LW("Update of cluster template "
                        "{name} is not allowed").format(name=cl["name"]))
                error = True

    return error
Beispiel #4
0
def do_node_group_template_delete():
    ctx = Context(tenant_id=CONF.command.tenant_id)

    t = u.find_node_group_template_by_name(ctx, CONF.command.template_name)
    if t:
        delete_node_group_template(ctx, t)
    else:
        LOG.warning("Deletion of node group template {name} failed, "
                    "no such template".format(name=CONF.command.template_name))
Beispiel #5
0
def do_node_group_template_delete():
    ctx = Context(tenant_id=CONF.command.tenant_id)

    t = u.find_node_group_template_by_name(ctx, CONF.command.template_name)
    if t:
        delete_node_group_template(ctx, t)
    else:
        LOG.warning("Deletion of node group template {name} failed, "
                    "no such template".format(name=CONF.command.template_name))
Beispiel #6
0
def check_usage_of_existing(ctx, ng_templates, cl_templates):
    '''Determine if any of the specified templates are in use

    This method searches for the specified templates by name and
    determines whether or not any existing templates are in use
    by a cluster or cluster template. Returns True if any of
    the templates are in use.

    :param ng_templates: A list of dictionaries. Each dictionary
                         has a "template" entry that represents
                         a node group template.
    :param cl_templates: A list of dictionaries. Each dictionary
                         has a "template" entry that represents
                         a cluster template
    :returns: True if any of the templates are in use, False otherwise
    '''
    error = False
    clusters = conductor.API.cluster_get_all(ctx)

    for ng_info in ng_templates:
        ng = u.find_node_group_template_by_name(ctx,
                                                ng_info["template"]["name"])
        if ng:
            cluster_users, template_users = u.check_node_group_template_usage(
                ng["id"], clusters)

            if cluster_users:
                LOG.warning(_LW("Node group template {name} "
                            "in use by clusters {clusters}").format(
                                name=ng["name"], clusters=cluster_users))
            if template_users:
                LOG.warning(_LW("Node group template {name} "
                            "in use by cluster templates {templates}").format(
                                name=ng["name"], templates=template_users))

            if cluster_users or template_users:
                LOG.warning(_LW("Update of node group template "
                            "{name} is not allowed").format(name=ng["name"]))
                error = True

    for cl_info in cl_templates:
        cl = u.find_cluster_template_by_name(ctx, cl_info["template"]["name"])
        if cl:
            cluster_users = u.check_cluster_template_usage(cl["id"], clusters)

            if cluster_users:
                LOG.warning(_LW("Cluster template {name} "
                            "in use by clusters {clusters}").format(
                                name=cl["name"], clusters=cluster_users))

                LOG.warning(_LW("Update of cluster template "
                            "{name} is not allowed").format(name=cl["name"]))
                error = True

    return error
Beispiel #7
0
def add_node_group_templates(ctx, node_groups):

    error = False
    ng_info = {"ids": {},
               "created": [],
               "updated": []}

    def do_reversals(ng_info):
        reverse_node_group_template_updates(ctx, ng_info["updated"])
        reverse_node_group_template_creates(ctx, ng_info["created"])
        return {}, True

    try:
        for ng in node_groups:
            template = ng['template']
            current = u.find_node_group_template_by_name(ctx, template['name'])
            if current:

                # Track what we see in the current template that is different
                # from our update values. Save it for possible rollback.
                # Note, this is not perfect because it does not recurse through
                # nested structures to get an exact diff, but it ensures that
                # we track only fields that are valid in the JSON schema
                updated_fields = u.value_diff(current.to_dict(), template)

                # Always attempt to update.  Since the template value is a
                # combination of JSON and config values, there is no useful
                # timestamp we can use to skip an update.
                # If sqlalchemy determines no change in fields, it will not
                # mark it as updated.
                try:
                    template = conductor.API.node_group_template_update(
                        ctx, current['id'], template, ignore_default=True)
                except Exception as e:
                    LOG.warning(_LW("Update of node group template {info} "
                                "failed, {reason}").format(
                                    info=u.name_and_id(current),
                                    reason=e))
                    raise Handled()

                if template['updated_at'] != current['updated_at']:
                    ng_info["updated"].append((template, updated_fields))
                    LOG.info(_LI("Updated node group template {info} "
                             "from {path}").format(
                                 info=u.name_and_id(template),
                                 path=ng["path"]))
                else:
                    LOG.debug("No change to node group template {info} "
                              "from {path}".format(
                                  info=u.name_and_id(current),
                                  path=ng['path']))
            else:
                template['is_default'] = True
                try:
                    template = conductor.API.node_group_template_create(
                        ctx, template)
                except Exception as e:
                    LOG.warning(_LW("Creation of node group template "
                                "from {path} failed, {reason}").format(
                                    path=ng['path'], reason=e))
                    raise Handled()

                ng_info["created"].append(template)
                LOG.info(_LI("Created node group template {info} "
                         "from {path}").format(info=u.name_and_id(template),
                                               path=ng["path"]))

            # For the purposes of substituion we need a dict of id by name
            ng_info["ids"][template['name']] = template['id']

    except Handled:
        ng_info, error = do_reversals(ng_info)

    except Exception as e:
        LOG.warning(_LW("Unhandled exception while processing "
                    "node group templates, {reason}").format(reason=e))
        ng_info, error = do_reversals(ng_info)

    return ng_info, error
Beispiel #8
0
def add_node_group_templates(ctx, node_groups):

    error = False
    ng_info = {"ids": {}, "created": [], "updated": []}

    def do_reversals(ng_info):
        reverse_node_group_template_updates(ctx, ng_info["updated"])
        reverse_node_group_template_creates(ctx, ng_info["created"])
        return {}, True

    try:
        for ng in node_groups:
            template = ng['template']
            current = u.find_node_group_template_by_name(ctx, template['name'])
            if current:

                # Track what we see in the current template that is different
                # from our update values. Save it for possible rollback.
                # Note, this is not perfect because it does not recurse through
                # nested structures to get an exact diff, but it ensures that
                # we track only fields that are valid in the JSON schema
                updated_fields = u.value_diff(current.to_dict(), template)

                # Always attempt to update.  Since the template value is a
                # combination of JSON and config values, there is no useful
                # timestamp we can use to skip an update.
                # If sqlalchemy determines no change in fields, it will not
                # mark it as updated.
                try:
                    template = conductor.API.node_group_template_update(
                        ctx, current['id'], template, ignore_prot_on_def=True)
                except Exception as e:
                    LOG.warning(
                        _LW("Update of node group template {info} "
                            "failed, {reason}").format(
                                info=u.name_and_id(current), reason=e))
                    raise Handled()

                if template['updated_at'] != current['updated_at']:
                    ng_info["updated"].append((template, updated_fields))
                    LOG.info(
                        _LI("Updated node group template {info} "
                            "from {path}").format(info=u.name_and_id(template),
                                                  path=ng["path"]))
                else:
                    LOG.debug("No change to node group template {info} "
                              "from {path}".format(info=u.name_and_id(current),
                                                   path=ng['path']))
            else:
                template['is_default'] = True
                try:
                    template = conductor.API.node_group_template_create(
                        ctx, template)
                except Exception as e:
                    LOG.warning(
                        _LW("Creation of node group template "
                            "from {path} failed, {reason}").format(
                                path=ng['path'], reason=e))
                    raise Handled()

                ng_info["created"].append(template)
                LOG.info(
                    _LI("Created node group template {info} "
                        "from {path}").format(info=u.name_and_id(template),
                                              path=ng["path"]))

            # For the purposes of substitution we need a dict of id by name
            ng_info["ids"][template['name']] = template['id']

    except Handled:
        ng_info, error = do_reversals(ng_info)

    except Exception as e:
        LOG.warning(
            _LW("Unhandled exception while processing "
                "node group templates, {reason}").format(reason=e))
        ng_info, error = do_reversals(ng_info)

    return ng_info, error