Beispiel #1
0
def test_name():
    # Arrange
    test_api = CobblerAPI()
    titem = Item(test_api)

    # Act
    titem.name = "testname"

    # Assert
    assert titem.name == "testname"
Beispiel #2
0
    def rename(self, ref: item_base.Item, newname, with_sync: bool = True, with_triggers: bool = True):
        """
        Allows an object "ref" to be given a new name without affecting the rest of the object tree.

        :param ref: The reference to the object which should be renamed.
        :param newname: The new name for the object.
        :param with_sync: If a sync should be triggered when the object is renamed.
        :param with_triggers: If triggers should be run when the object is renamed.
        """
        # Nothing to do when it is the same name
        if newname == ref.name:
            return

        # Save the old name
        oldname = ref.name
        # Reserve the new name
        self.listing[newname] = None
        # Delete the old item
        self.collection_mgr.serialize_delete_one_item(ref)
        self.listing.pop(oldname)
        # Change the name of the object
        ref.name = newname
        # Save just this item
        self.collection_mgr.serialize_one_item(ref)
        self.listing[newname] = ref

        # for mgmt classes, update all objects that use it
        if ref.COLLECTION_TYPE == "mgmtclass":
            for what in ["distro", "profile", "system"]:
                items = self.api.find_items(what, {"mgmt_classes": oldname})
                for item in items:
                    for i in range(0, len(item.mgmt_classes)):
                        if item.mgmt_classes[i] == oldname:
                            item.mgmt_classes[i] = newname
                    self.api.add_item(what, item, save=True)

        # for menus, update all objects that use it
        if ref.COLLECTION_TYPE == "menu":
            for what in ["profile", "image"]:
                items = self.api.find_items(what, {"menu": oldname})
                for item in items:
                    item.menu = newname
                    self.api.add_item(what, item, save=True)

        # for a repo, rename the mirror directory
        if ref.COLLECTION_TYPE == "repo":
            path = os.path.join(self.api.settings().webdir, "repo_mirror")
            old_path = os.path.join(path, oldname)
            if os.path.exists(old_path):
                new_path = os.path.join(path, ref.name)
                os.renames(old_path, new_path)

        # for a distro, rename the mirror and references to it
        if ref.COLLECTION_TYPE == 'distro':
            path = utils.find_distro_path(self.api.settings(), ref)

            # create a symlink for the new distro name
            utils.link_distro(self.api.settings(), ref)

            # Test to see if the distro path is based directly on the name of the distro. If it is, things need to
            # updated accordingly.
            if os.path.exists(path) \
                    and path == str(os.path.join(self.api.settings().webdir, "distro_mirror", ref.name)):
                newpath = os.path.join(self.api.settings().webdir, "distro_mirror", ref.name)
                os.renames(path, newpath)

                # update any reference to this path ...
                distros = self.api.distros()
                for d in distros:
                    if d.kernel.find(path) == 0:
                        d.kernel = d.kernel.replace(path, newpath)
                        d.initrd = d.initrd.replace(path, newpath)
                        self.collection_mgr.serialize_one_item(d)

        if ref.COLLECTION_TYPE in ('profile', 'system'):
            if ref.parent is not None:
                ref.parent.children.remove(oldname)

        # Now descend to any direct ancestors and point them at the new object allowing the original object to be
        # removed without orphanage. Direct ancestors will either be profiles or systems. Note that we do have to
        # care as setting the parent is only really meaningful for subprofiles. We ideally want a more generic parent
        # setter.
        kids = ref.get_children()
        for k in kids:
            if self.api.find_profile(name=k) is not None:
                k = self.api.find_profile(name=k)
                if ref.COLLECTION_TYPE == "distro":
                    k.distro = newname
                else:
                    k.parent = newname
                self.api.profiles().add(k, save=True, with_sync=with_sync, with_triggers=with_triggers)
            elif self.api.find_menu(name=k) is not None:
                k = self.api.find_menu(name=k)
                k.parent = newname
                self.api.menus().add(k, save=True, with_sync=with_sync, with_triggers=with_triggers)
            elif self.api.find_system(name=k) is not None:
                k = self.api.find_system(name=k)
                k.profile = newname
                self.api.systems().add(k, save=True, with_sync=with_sync, with_triggers=with_triggers)
            else:
                raise CX("Internal error, unknown child type for child \"%s\"!" % k)