def delete_server(request, server_id): if (not request.user.is_superuser): return simple.direct_to_template(request, template = 'not_admin.html', extra_context = {'user':request.user}, ) if request.method == 'POST': try: VTDriver.deleteServer(VTDriver.getServerById(server_id)) return HttpResponseRedirect(reverse('dashboard')) except Exception as e: logging.error(e) e = HttpUtils.processException(e) return simple.direct_to_template(request, template = 'servers/delete_server.html', extra_context = {'user':request.user, 'exception':e, 'next':reverse("admin_servers")}, ) elif request.method == 'GET': return simple.direct_to_template(request, template = 'servers/delete_server.html', extra_context = {'user':request.user, 'next':reverse("admin_servers"),'object':VTDriver.getServerById(server_id)}, )
def manageEthernet(request,rangeId=None,action=None,macId=None): if not action in NETWORKING_POSSIBLE_ACTIONS: raise Exception("Unknown action") #Define context extra_context = {"section": "networking","subsection":"ethernet",} #Add process if (action == NETWORKING_ACTION_ADD): if request.method == "GET": #Show form extra_context["form"] = HttpUtils.getFormFromModel(MacRange) return simple.direct_to_template( request, extra_context = extra_context, template="networking/ethernet/rangeCrud.html", ) return elif request.method == "POST": try: instance = HttpUtils.getInstanceFromForm(request,MacRange) #Create Range EthernetController.createRange(instance) return HttpResponseRedirect("/networking/ethernet/") except Exception as e: print e extra_context["form"] = HttpUtils.processExceptionForm(e,request,MacRange) #Process creation query return simple.direct_to_template( request, extra_context = extra_context, template="networking/ethernet/rangeCrud.html", ) #Show if ((action == None) or (action==NETWORKING_ACTION_SHOW)) and (not rangeId==None): instance = EthernetController.getRange(rangeId) extra_context["range"] = instance #return HttpResponseRedirect("/networking/ethernet/") return simple.direct_to_template( request, extra_context = extra_context, template="networking/ethernet/rangeDetail.html", ) #Edit #TODO #Add excluded Mac if (action == NETWORKING_ACTION_ADDEXCLUDED) and (request.method == "POST"): if not request.method == "POST": raise Exception("Invalid method") try: instance = EthernetController.getRange(rangeId) extra_context["range"] = instance #Create excluded EthernetController.addExcludedMac(instance,request) return HttpResponseRedirect("/networking/ethernet/"+rangeId) except Exception as e: print e extra_context["errors"] = HttpUtils.processException(e) pass return simple.direct_to_template( request, extra_context = extra_context, template="networking/ethernet/rangeDetail.html", ) #Release excluded Mac if (action == NETWORKING_ACTION_REMOVEXCLUDED) and (request.method == "POST"): try: instance = EthernetController.getRange(rangeId) #Create excluded #FIXME: Why initial instance is not refreshed? EthernetController.removeExcludedMac(instance,macId) instance = EthernetController.getRange(rangeId) extra_context["range"] = instance return HttpResponseRedirect("/networking/ethernet/"+rangeId) except Exception as e: print e extra_context["errors"] = HttpUtils.processException(e) pass return simple.direct_to_template( request, extra_context = extra_context, template="networking/ethernet/rangeDetail.html", ) #Delete if (action == NETWORKING_ACTION_DELETE) and (request.method == "POST"): try: EthernetController.deleteRange(rangeId) return HttpResponseRedirect("/networking/ethernet/") except Exception as e: print e extra_context["errors"] = HttpUtils.processException(e) pass #Listing ranges extra_context["ranges"] = EthernetController.listRanges() return simple.direct_to_template( request, extra_context = extra_context, template = "networking/ethernet/index.html", )
def servers_crud(request, server_id=None): """Show a page for the user to add/edit an VTServer """ if (not request.user.is_superuser): return simple.direct_to_template(request, template = 'not_admin.html', extra_context = {'user':request.user}, ) vmProjects = {} vmSlices = {} try: for vm in VTDriver.getVMsInServer(VTDriver.getServerById(server_id)): if vm.projectName not in vmProjects: vmProjects[vm.projectName] = vm.projectId if vm.sliceName not in vmSlices: vmSlices[vm.sliceName] = vm.sliceId except Exception as e: print e pass serverFormClass = HttpUtils.getFormFromModel(VTServer) ifaceFormClass = HttpUtils.getFormFromModel(NetworkInterface) IfaceFormSetClass = modelformset_factory(NetworkInterface) if server_id != None: server = get_object_or_404(VTServer, pk=server_id) else: server = None if request.method == "GET": #serverForm = serverFormClass(instance=server) serverForm = ServerForm(instance=server, prefix ="server") if server != None: mgmt = server.getNetworkInterfaces().filter(isMgmt = True) if mgmt: mgmt = mgmt.get() mgmtIfaceForm = MgmtBridgeForm({'mgmtBridge-name':mgmt.getName(), 'mgmtBridge-mac':mgmt.getMacStr()}, prefix ="mgmtBridge") else: mgmtIfaceForm = MgmtBridgeForm(prefix ="mgmtBridge") data = server.getNetworkInterfaces().filter(isMgmt = False) if data: IfaceFormSetClass = modelformset_factory(NetworkInterface,extra = 0) ifaceformset = IfaceFormSetClass(queryset= data) else: mgmtIfaceForm = MgmtBridgeForm(prefix ="mgmtBridge") ifaceformset = IfaceFormSetClass(queryset= NetworkInterface.objects.none()) elif request.method == "POST": #serverForm = serverFormClass(request.POST, instance=server) serverForm = ServerForm(request.POST, instance=server, prefix ="server") ifaceformset = IfaceFormSetClass(request.POST) mgmtIfaceForm = MgmtBridgeForm(request.POST, prefix ="mgmtBridge") if serverForm.is_valid() and ifaceformset.is_valid() and mgmtIfaceForm.is_valid(): ifaces = ifaceformset.save(commit = False) if server == None: server = serverForm.save(commit = False) try: server = VTDriver.crudServerFromInstance(server) VTDriver.setMgmtBridge(request, server) VTDriver.crudDataBridgeFromInstance(server, ifaces,request.POST.getlist("DELETE")) except Exception as e: print e e = HttpUtils.processException(e) context = {"exception":e, "serverForm": serverForm, 'vmProjects': vmProjects, 'vmSlices': vmSlices,'ifaceformset' : ifaceformset, 'mgmtIfaceForm' : mgmtIfaceForm} if server_id != None: context["server"] = server return simple.direct_to_template( request, template="servers/servers_crud.html", extra_context=context, ) # Returns to server's admin page and rollback transactions return HttpResponseRedirect('/servers/admin/') else: return HttpResponseNotAllowed("GET", "POST") context = {"serverForm": serverForm, 'vmProjects': vmProjects, 'vmSlices': vmSlices,'ifaceformset' : ifaceformset, 'mgmtIfaceForm' : mgmtIfaceForm} if server_id != None: context["server"] = server return simple.direct_to_template( request, template="servers/servers_crud.html", extra_context=context, )
def createOrUpdateServerFromPOST(request, instance): #return XenServer.constructor(server.getName(),server.getOSType(),server.getOSDistribution(),server.getOSVersion(),server.getAgentURL(),save=True) server = XenServer.objects.get(uuid = instance.getUUID()) if server: return server.updateServer(HttpUtils.getFieldInPost(request,VTServer, "name"), HttpUtils.getFieldInPost(request,VTServer, "operatingSystemType"), HttpUtils.getFieldInPost(request,VTServer, "operatingSystemDistribution"), HttpUtils.getFieldInPost(request,VTServer, "operatingSystemVersion"), HttpUtils.getFieldInPost(request,VTServer, "numberOfCPUs"), HttpUtils.getFieldInPost(request,VTServer, "CPUFrequency"), HttpUtils.getFieldInPost(request,VTServer, "memory"), HttpUtils.getFieldInPost(request,VTServer, "discSpaceGB"), HttpUtils.getFieldInPost(request,VTServer, "agentURL"), save=True) else: return XenServer.constructor(HttpUtils.getFieldInPost(request,VTServer, "name"), HttpUtils.getFieldInPost(request,VTServer, "operatingSystemType"), HttpUtils.getFieldInPost(request,VTServer, "operatingSystemDistribution"), HttpUtils.getFieldInPost(request,VTServer, "operatingSystemVersion"), HttpUtils.getFieldInPost(request,VTServer, "numberOfCPUs"), HttpUtils.getFieldInPost(request,VTServer, "CPUFrequency"), HttpUtils.getFieldInPost(request,VTServer, "memory"), HttpUtils.getFieldInPost(request,VTServer, "discSpaceGB"), HttpUtils.getFieldInPost(request,VTServer, "agentURL"), save=True)
def setMgmtBridge(request, server): name = HttpUtils.getFieldInPost(request, "mgmtBridge-name") mac = HttpUtils.getFieldInPost(request, "mgmtBridge-mac") server.setMgmtBridge(name, mac)
def createServerFromPOST(request, instance): from vt_manager_kvm.models.VTServer import VTServer controller = VTDriver.getDriver(HttpUtils.getFieldInPost(request,VTServer,"virtTech")) return controller.createOrUpdateServerFromPOST(request, instance)