instead of getting all pools, try to get the pool and then create it on failure

This commit is contained in:
Jordan Rodgers 2018-03-08 01:14:43 -05:00
parent 88ef6daa8a
commit cefe605bb9

View file

@ -4,6 +4,7 @@ from proxstar.vm import VM
from proxstar.util import * from proxstar.util import *
from proxstar.proxmox import * from proxstar.proxmox import *
from rq.registry import StartedJobRegistry from rq.registry import StartedJobRegistry
from proxmoxer.core import ResourceException
class User(object): class User(object):
@ -17,14 +18,18 @@ class User(object):
@lazy_property @lazy_property
def vms(self): def vms(self):
proxmox = connect_proxmox() proxmox = connect_proxmox()
pools = get_pools(proxmox, db) try:
if self.name not in pools: # try to get the users vms from their pool
vms = proxmox.pools(self.name).get()['members']
except ResourceException:
# they likely don't have a pool yet, try to create it
if is_user(self.name) and not is_rtp(self.name): if is_user(self.name) and not is_rtp(self.name):
proxmox.pools.post( proxmox.pools.post(
poolid=self.name, comment='Managed by Proxstar') poolid=self.name, comment='Managed by Proxstar')
# if created, their pool is empty so return empty array
return []
else: else:
return [] return []
vms = proxmox.pools(self.name).get()['members']
for vm in vms: for vm in vms:
if 'name' not in vm: if 'name' not in vm:
vms.remove(vm) vms.remove(vm)