'Remove the security group associated with the instance.'

def remove_from_instance(self, context, instance, security_group_name):

    neutron = neutronapi.get_client(context)
    try:
        security_group_id = neutronv20.find_resourceid_by_name_or_id(neutron, 'security_group', security_group_name, context.project_id)
    except n_exc.NeutronClientException as e:
        exc_info = sys.exc_info()
        if (e.status_code == 404):
            msg = (_('Security    group    %(name)s    is    not    found    for    project    %(project)s') % {'name': security_group_name, 'project': context.project_id})
            self.raise_not_found(msg)
        else:
            LOG.exception(_LE('Neutron    Error:'))
            six.reraise(*exc_info)
    params = {'device_id': instance.uuid}
    try:
        ports = neutron.list_ports(**params).get('ports')
    except n_exc.NeutronClientException:
        with excutils.save_and_reraise_exception():
            LOG.exception(_LE('Neutron    Error:'))
    if (not ports):
        msg = (_('instance_id    %s    could    not    be    found    as    device    id    on    any    ports') % instance.uuid)
        self.raise_not_found(msg)
    found_security_group = False
    for port in ports:
        try:
            port.get('security_groups', []).remove(security_group_id)
        except ValueError:
            continue
        updated_port = {'security_groups': port['security_groups']}
        try:
            LOG.info(_LI('Adding    security    group    %(security_group_id)s    to    port    %(port_id)s'), {'security_group_id': security_group_id, 'port_id': port['id']})
            neutron.update_port(port['id'], {'port': updated_port})
            found_security_group = True
        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.exception(_LE('Neutron    Error:'))
    if (not found_security_group):
        msg = (_('Security    group    %(security_group_name)s    not    associated    with    the    instance    %(instance)s') % {'security_group_name': security_group_name, 'instance': instance.uuid})
        self.raise_not_found(msg)
