Exemple #1
0
    def disassociate_with_groups(self, groups):
        """Disassociate the host from the provided groups

        Args:
            groups: The group name(s) to disassociate the host from.
            Accepts a single group string or a list or tuple of groups

        Returns:
            bool: True on complete success, False otherwise

        Raises:
            InvalidGroup: The group provided as argument does not exist.

        """
        if not isinstance(groups, (list, tuple)):
            groups = [groups]
        groups = [group.lower() for group in groups]
        host_group_names = [group.name.lower() for group in self.groups]
        for group_name in groups:
            if group_name.lower() not in host_group_names:
                raise InvalidGroup(group_name)
        inventory_groups = [
            group for group in self.inventory.groups
            if group.name.lower() in groups
        ]

        return all([
            group._remove_host_by_id(self.id)  # pylint: disable=protected-access
            for group in inventory_groups
        ])
Exemple #2
0
    def associate_with_groups(self, groups):
        """Associate the host with the provided groups.

        Args:
            groups: The groups to associate the host with.
            Accepts a single group string or a list or tuple of groups.

        Returns:
            bool: True on complete success, False otherwise.

        Raises:
            InvalidGroup: The group provided as argument does not exist.

        """
        if not isinstance(groups, (list, tuple)):
            groups = [groups]
        inventory_groups = [group for group in self.inventory.groups]
        lower_inventory_group_names = [
            group.name.lower() for group in inventory_groups
        ]
        missing_groups = [
            group_name for group_name in groups
            if group_name.lower() not in lower_inventory_group_names
        ]
        if missing_groups:
            raise InvalidGroup(missing_groups)
        lower_group_names = [name.lower() for name in groups]
        final_groups = [
            group for group in inventory_groups
            if group.name.lower() in lower_group_names
        ]
        return all([
            group._add_host_by_id(self.id)  # pylint: disable=protected-access
            for group in final_groups
        ])
Exemple #3
0
    def delete_group(self, name):
        """Deletes the group

        Args:
            name: The name of the group to delete

        Returns:
            bool: True on success, False otherwise

        Raises:
            InvalidGroup: The group provided as argument does not exist.

        """
        group = self._tower.get_group_by_name(name)
        if not group:
            raise InvalidGroup(name)
        return group.delete()
Exemple #4
0
    def disassociate_group_by_name(self, name):
        """Disassociate a group from the group.

        Args:
            name: The name of the group to disassociate.

        Returns:
            bool: True on success, False otherwise.

        Raises:
            InvalidGroup: The group provided as argument does not exist.

        """
        group = self.inventory.get_group_by_name(name)
        if not group:
            raise InvalidGroup(name)
        return self._disassociate_group_by_id(group.id)
Exemple #5
0
    def associate_group_by_name(self, name):
        """Associate a group to the group by name

        Args:
            name: The name of the group to associate with the group

        Returns:
            bool: True on success, False otherwise

        Raises:
            InvalidGroup: The group provided as argument does not exist.

        """
        group = self._tower.get_group_by_name(name)
        if not group:
            raise InvalidGroup(name)
        return self._associate_group_by_id(group.id)
Exemple #6
0
    def delete_group(self, name):
        """Deletes the group.

        Args:
            name: The name of the group to delete.

        Returns:
            bool: True on success, False otherwise.

        Raises:
            InvalidGroup: The group provided as argument does not exist.

        """
        group = next(
            self._tower.groups.filter({
                'inventory': self.id,
                'name__iexact': name
            }), None)
        if not group:
            raise InvalidGroup(name)
        return group.delete()