'Returns a list of security groups for the given instance.'

@extensions.expected_errors(404)
def index(self, req, server_id):

    context = _authorize_context(req)
    self.security_group_api.ensure_default(context)
    instance = common.get_instance(self.compute_api, context, server_id)
    try:
        groups = self.security_group_api.get_instance_security_groups(context, instance, True)
    except (exception.SecurityGroupNotFound, exception.InstanceNotFound) as exp:
        msg = exp.format_message()
        raise exc.HTTPNotFound(explanation=msg)
    result = [self._format_security_group(context, group) for group in groups]
    return {'security_groups': list(sorted(result, key=(lambda k: (k['tenant_id'], k['name']))))}
