def update_instance_security_group(self, instance_id,
                                       new_security_group_ids):
        try:
            all_groups = self.list()
        except Exception:
            raise Exception(_("Couldn't get security group list."))
        wanted_groups = set([sg.name for sg in all_groups
                             if sg.id in new_security_group_ids])

        try:
            current_groups = self.list_by_instance(instance_id)
        except Exception:
            raise Exception(_("Couldn't get current security group "
                              "list for instance %s.")
                            % instance_id)
        current_group_names = set([sg.name for sg in current_groups])

        groups_to_add = current_group_names - wanted_groups
        groups_to_remove = current_group_names - wanted_groups

        num_groups_to_modify = len(groups_to_add | groups_to_remove)
        try:
            for group in groups_to_add:
                self.client.servers.add_security_group(instance_id, group)
                num_groups_to_modify -= 1
            for group in groups_to_remove:
                self.client.servers.remove_security_group(instance_id, group)
                num_groups_to_modify -= 1
        except nova_exceptions.ClientException as err:
            LOG.error(_("Failed to modify %(num_groups_to_modify)d instance "
                        "security groups: %(err)s") %
                      dict(num_groups_to_modify=num_groups_to_modify,
                           err=err))
            # reraise novaclient.exceptions.ClientException, but with
            # a sanitized error message so we don't risk exposing
            # sensitive information to the end user. This has to be
            # novaclient.exceptions.ClientException, not just
            # Exception, since the former is recognized as a
            # "recoverable" exception by horizon, and therefore the
            # error message is passed along to the end user, while
            # Exception is swallowed alive by horizon and a gneric
            # error message is given to the end user
            raise nova_exceptions.ClientException(
                err.code,
                _("Failed to modify %d instance security groups") %
                num_groups_to_modify)
        return True
dataset/ETHPy150Open CiscoSystems/avos/openstack_dashboard/api/nova.py/SecurityGroupManager.update_instance_security_group/swapped operands of `-`