def disable_project_credentials(request):
    if request.method == "POST":
        nova = get_nova_admin_connection()
        form = DisableProject(request.POST)
        if form.is_valid():
            name = form.cleaned_data['project_name']
            conn = nova.connection_for(settings.NOVA_ADMIN_USER, name)
            vpn = [x for x in nova.get_vpns() if x.project_id == name]
            if vpn:
                # NOTE(todd): Check, because it could already be shut-off
                vpn = vpn[0]
            try:
                nova.disable_project_credentials(name)
                if vpn and vpn.instance_id:
                    conn.terminate_instances([vpn.instance_id])
            except NovaResponseError, e:
                messages.error(request,
                               'Unable to disable project %s: %s - %s' %
                               (name, e.code, e.message))
                return redirect('syspanel_security')
            else:
                messages.success(request,
                                 'Project %s has been successfully disabled.' %
                                     form.cleaned_data['project_name'])
            return render_to_response(
                      'django_nova_syspanel/security/disable_project_credentials.html',
                      context_instance = template.RequestContext(request))
        else:
            messages.error(request, 'Invalid form data')
            return redirect('syspanel_security')
def console(request, project_id):
    nova = get_nova_admin_connection()
    conn = nova.connection_for(settings.NOVA_ADMIN_USER, project_id)
    vpn = [x for x in nova.get_vpns() if x.project_id == project_id][0]
    console = conn.get_console_output(vpn.instance_id)
    response = http.HttpResponse(mimetype='text/plain')
    response.write(console.output)
    response.flush()
    return response
def disable_public_ips(request):
    if request.method == "POST":
        try:
            nova = get_nova_admin_connection()
            nova.disable_all_floating_ips()
        except NovaResponseError, e:
            messages.error(request,
                           'Unable to shut off public IPs: %s - %s' %
                           (e.code, e.message,))
        else:
            messages.success(request, 'Public IPs have been turned off.')
def disable_ip(request):
    if request.method == "POST":
        conn = get_nova_admin_connection()
        form = DisableIpAddress(request.POST)
        if form.is_valid():
            try:
                conn.block_ips(form.cleaned_data['cidr'])
            except NovaResponseError, e:
                messages.error(request,
                               'Unable to block IPs range %s: %s %s' %
                               (form.cleaned_data['cidr'], e.code, e.message))
            else:
                messages.success(request,
                                 'IPs range %shas been successfully blocked' %
                                 form.cleaned_data['cidr'])
def disable_vpn(request):
    if request.method == "POST":
        nova = get_nova_admin_connection()
        conn = nova.connection_for(settings.NOVA_ADMIN_USER,
                                   settings.NOVA_PROJECT)
        try:
            collector = []
            for vpn in nova.get_vpns():
                if not vpn.instance_id:
                    continue
                collector.append(vpn)
                if len(collector) >= 5:
                    conn.terminate_instances([x.instance_id for x in collector])
                    collector = []
            if collector:
                conn.terminate_instances([x.instance_id for x in collector])
        except NovaResponseError, e:
            messages.error(request, 'Unable to shut off all VPNs: %s - %s' %
                                    (e.code, e.message,))
        else:
            messages.success(request, 'VPNs have been successfully turned off.')
        return redirect('syspanel_security')
def index(request):
    nova = get_nova_admin_connection()
    nodes = nova.get_hosts()
    return render_to_response('django_nova_syspanel/cloudview/index.html', {
        'nodes': nodes,
    }, context_instance = template.RequestContext(request))
def index(request):
    nova = get_nova_admin_connection()
    vpns = nova.get_vpns()
    return render_to_response('django_nova_syspanel/vpns/index.html',{
        'vpns': vpns,
    }, context_instance = template.RequestContext(request))
def send_credentials(request, project_id, user_id):
    # TODO we need to pass a user id as well
    nova = get_nova_admin_connection()
    nova.get_zip(user_id, project_id)
    return redirect('django_nova_syspanel/vpns')
def launch(request, project_id):
    nova = get_nova_admin_connection()
    nova.start_vpn(project_id)
    return redirect('django_nova_syspanel/vpns')
def terminate(request, project_id):
    nova = get_nova_admin_connection()
    conn = nova.connection_for(settings.NOVA_ADMIN_USER, project_id)
    vpn = [x for x in nova.get_vpns() if x.project_id == project_id][0]
    conn.terminate_instances([vpn.instance_id])
    return redirect('django_nova_syspanel/vpns')