def signOut(): fileObj = fileHandler.FilesHandler() if fileObj.creds_check(): return Response(fileObj.jsonFile_delete(), mimetype='application/json') else: return "<h1> Please login before you can get vm list </h1>"
def connect(self): print(self.get_hostip()) powershellObj = powershell_handler.PowerShellHandler() arguments = " " + str(self.get_hostip()) + " " + str( self.get_username()) + " " + str(self.get_password()) if powershellObj.runTestCmds( ".\\PowerShellScripts\\ConnectAndListVM.ps1", arguments=arguments) is True: FileObj = fileHandler.FilesHandler() return FileObj.read_vmlist() else: return "<h2>Some Error Occured while reading VM Details :(</h2>"
def get_vm_list(): if request.method == 'POST': fileObj = fileHandler.FilesHandler() if fileObj.creds_check(): credsData = fileObj.jsonFile_Reader() controller = Controller.Creds(hostip=credsData["hostip"], username=credsData["username"], password=credsData["password"]) return Response(controller.connect(), mimetype='application/json') else: return "<h1> Please login before you can get vm list </h1>" else: return "<h1>Please Send a POST Request</h1>"
def EnterMaintanenceMode(): if request.method == 'POST': print(request.get_json(force=True)) mode = request.get_json(force=True)["mode"] fileObj = fileHandler.FilesHandler() if mode == 'ON': if fileObj.creds_check(): credsData = fileObj.jsonFile_Reader() controller = Controller.Creds(hostip=credsData["hostip"], username=credsData["username"], password=credsData["password"]) listvm = controller.connect() fileObj.jsonFile_CreateMaintanenceData(data=listvm) listvm = controller.MaintanenceModeOn(mdata=listvm) for vm in listvm: controller.powerOff(vmname=str(vm)) return json.dumps({"mode": "Activated"}) else: return "<h1> Please login before you can get vm list </h1>" else: if fileObj.creds_check(): credsData = fileObj.jsonFile_Reader() controller = Controller.Creds(hostip=credsData["hostip"], username=credsData["username"], password=credsData["password"]) if fileObj.mode_check(): listvm = fileObj.jsonFile_ReadMaintanenceData() listvm = controller.MaintanenceModeOff(mdata=listvm) listvm = json.loads(str(listvm)) for vm in listvm: controller.powerOn(vmname=str(vm)) return json.dumps({"mode": "Deactivated"}) else: return "<h2>Please Turn On Maintanence Mode Before Turning Off :) </h2>" else: return "<h1> Please login before you can get vm list </h1>" else: return "<h2>Send a POST Request</h2>"
def powerOff(self, vmname): powershellObj = powershell_handler.PowerShellHandler() arguments = " " + str(self.get_hostip()) + " " + str( self.get_username()) + " " + str(self.get_password() + " " + str(vmname)) if powershellObj.runTestCmds(".\\PowerShellScripts\\PowerOffVM.ps1", arguments=arguments) is True: FileObj = fileHandler.FilesHandler() if FileObj.check_vm_power_operation(state="PoweredOff"): return json.dumps({"operation": "Success"}) else: return json.dumps({"operation": "Pending"}) else: return "<h2>Something went wrong :(</h2>"
def powerOff(): if request.method == 'POST': print(request.get_json(force=True)) vmname = request.get_json(force=True)["vmname"] fileObj = fileHandler.FilesHandler() if fileObj.creds_check(): credsData = fileObj.jsonFile_Reader() controller = Controller.Creds(hostip=credsData["hostip"], username=credsData["username"], password=credsData["password"]) return Response(controller.powerOff(vmname=vmname), mimetype='application/json') else: return "<h1> Please login before you can get vm list </h1>" else: return "<h1>Send a Post Request</h1>"
def CreateVM(): if request.method == 'POST': print(request.get_json(force=True)) vmname = request.get_json(force=True)['vmname'] memory = request.get_json(force=True)['memory'] vdisk = request.get_json(force=True)['vdisk'] fileObj = fileHandler.FilesHandler() if fileObj.creds_check(): credsData = fileObj.jsonFile_Reader() controller = Controller.Creds(hostip=credsData["hostip"], username=credsData["username"], password=credsData["password"]) return Response(controller.CreateVM(vmname=vmname, memory=memory, diskspace=vdisk), mimetype='application/json') else: return "<h1> Please login before you can get vm list </h1>" else: return "<h1>Send a POST Request</h1>"
def HyperConnect(): if request.method == 'POST': print(request.get_json(force=True)) host_ip = request.get_json(force=True)['hostip'] username = request.get_json(force=True)['username'] password = request.get_json(force=True)['password'] fileObj = fileHandler.FilesHandler() fileObj.jsonFile_writer({ "hostip": host_ip, "username": username, "password": password }) controller = Controller.Creds(hostip=host_ip, username=username, password=password) return Response(controller.connect(), mimetype='application/json') else: return "<h1>Not Proper</h1>"
# print(name + " " + state.split('\n')[0]) # # outputDict = {} # # count = 0 # # for element in VMList: # outputDict[count] = element # count += 1 # # outputDict = json.dumps(outputDict) # # print(outputDict) # str = json.load(open("ste.json")) fhand = fh.FilesHandler() mdata = fhand.jsonFile_ReadMaintanenceData() poweredOnVM = [] for index in mdata: if mdata[index]['state'] == "PoweredOn": poweredOnVM.append(mdata[index]['Name']) print(mdata[index]) print(str(poweredOnVM)) count = 0 st = '' for item in poweredOnVM: if count == 0: st.join(item)