diff --git a/app.py b/app.py index 3a85d76..a9fdc75 100644 --- a/app.py +++ b/app.py @@ -46,8 +46,13 @@ def hostname(name): starrs = connect_starrs( app.config['STARRS_DB_NAME'], app.config['STARRS_DB_USER'], app.config['STARRS_DB_HOST'], app.config['STARRS_DB_PASS']) - result = check_hostname(starrs, name) - return str(result) + valid, available = check_hostname(starrs, name) + if not valid: + return 'invalid' + if not available: + return 'taken' + else: + return 'ok' @app.route("/vm/") diff --git a/proxmox.py b/proxmox.py index d6e930f..2ba2cd6 100644 --- a/proxmox.py +++ b/proxmox.py @@ -140,7 +140,7 @@ def get_user_usage(proxmox, user): usage['cpu'] += int(config['cores'] * config.get('sockets', 1)) usage['mem'] += (int(config['memory']) // 1024) for disk in get_vm_disks(proxmox, vm['vmid'], config): - usage['disk'] += int(disk[1][:-1]) + usage['disk'] += int(disk[1]) return usage diff --git a/starrs.py b/starrs.py index 77046df..725cbaa 100644 --- a/starrs.py +++ b/starrs.py @@ -1,6 +1,5 @@ import psycopg2 - def connect_starrs(db, user, host, password): try: starrs = psycopg2.connect( @@ -43,12 +42,24 @@ def check_hostname(starrs, hostname): try: c.execute("BEGIN") c.callproc("api.initialize", ('root', )) - c.callproc("api.check_dns_hostname", (hostname, 'csh.rit.edu')) - results = c.fetchall() + c.callproc("api.validate_name", (hostname,)) + valid = False + if hostname == c.fetchall()[0][0]: + valid = True c.execute("COMMIT") + c.execute("BEGIN") + c.callproc("api.initialize", ('root', )) + c.callproc("api.check_dns_hostname", (hostname, 'csh.rit.edu')) + available = False + if not c.fetchall()[0][0]: + available = True + c.execute("COMMIT") + except(psycopg2.InternalError): + valid = False + available = False finally: c.close() - return results[0][0] + return valid, available def register_starrs(starrs, name, owner, mac, addr): diff --git a/static/js/script.js b/static/js/script.js index 3ef2c7e..170dc05 100644 --- a/static/js/script.js +++ b/static/js/script.js @@ -367,7 +367,7 @@ $("#renew-vm").click(function(){ credentials: 'same-origin', method: 'post' }).then((response) => { - return swal(`${vmname} is now renewing!`, { + return swal(`${vmname} has been renewed!`, { icon: "success", }); }).then(() => { @@ -394,7 +394,7 @@ $("#create-vm").click(function(){ }).then((response) => { return response.text() }).then((text) => { - if (text == "False") { + if (text == 'ok') { var loader = document.createElement('div'); loader.setAttribute('class', 'loader'); var info = document.createElement('span'); @@ -431,8 +431,10 @@ $("#create-vm").click(function(){ }); } }); - } else { - swal("Uh oh...", `That name is already taken! Please try another name.`, "error"); + } else if (text == 'invalid') { + swal("Uh oh...", `That name is not a valid name! Please try another name.`, "error"); + } else if (text == 'taken') { + swal("Uh oh...", `That name is not available! Please try another name.`, "error"); } }).catch(err => { if (err) {