start working on managed IPs

This commit is contained in:
2021-07-08 14:10:14 -05:00
parent 1e19902df1
commit e685c8a773
5 changed files with 134 additions and 4 deletions

View File

@ -56,12 +56,15 @@ class DBModel:
# ------ VM & ACCOUNT MANAGEMENT ---------
def all_non_deleted_vms(self):
self.cursor.execute("SELECT id, host, network_name, last_seen_ipv4, last_seen_ipv6 FROM vms WHERE deleted IS NULL")
return list(map(lambda x: dict(id=x[0], host=x[1], network_name=x[2], last_seen_ipv4=x[3], last_seen_ipv6=x[4]), self.cursor.fetchall()))
def all_non_deleted_vm_ids(self,):
def all_non_deleted_vm_ids(self):
self.cursor.execute("SELECT id FROM vms WHERE deleted IS NULL")
return list(map(lambda x: x[0], self.cursor.fetchall()))
def operating_systems_dict(self,):
def operating_systems_dict(self):
self.cursor.execute("SELECT id, template_image_file_name, description FROM os_images WHERE deprecated = FALSE")
operatingSystems = dict()
@ -70,7 +73,7 @@ class DBModel:
return operatingSystems
def vm_sizes_dict(self,):
def vm_sizes_dict(self):
self.cursor.execute("SELECT id, dollars_per_month, vcpus, memory_mb, bandwidth_gb_per_month FROM vm_sizes")
vmSizes = dict()
@ -301,6 +304,21 @@ class DBModel:
# ------ HOSTS ---------
def list_hosts_with_networks(self):
self.cursor.execute("""
SELECT hosts.id, hosts.last_health_check, host_network.network_name, host_network.public_ipv4_cidr_block FROM hosts
JOIN host_network ON host_network.host = hosts.id
""")
hosts = dict()
for row in self.cursor.fetchall():
if row[0] not in hosts:
hosts[row[0]] = dict(last_health_check=row[1], networks=[])
hosts[row[0]]["networks"].append(dict(network_name=row[2], public_ipv4_cidr_block=row[3]))
return hosts
def authorized_for_host(self, id, token) -> bool:
self.cursor.execute("SELECT id FROM hosts WHERE id = %s AND token = %s", (id, token))
return self.cursor.fetchone() != None
@ -317,8 +335,27 @@ class DBModel:
self.cursor.execute("SELECT id, https_url FROM hosts WHERE last_health_check > NOW() - INTERVAL '20 seconds'")
return list(map(lambda x: OnlineHost(id=x[0], url=x[1]), self.cursor.fetchall()))
def create_operation(self, online_hosts: List[OnlineHost], email: str, payload: str) -> int:
def list_all_operations(self):
self.cursor.execute("""
SELECT operations.id, operations.email, operations.created, operations.payload,
host_operation.host host_operation.assignment_status, host_operation.assigned,
host_operation.completed, host_operation.results FROM operations
JOIN host_operation ON host_operation.operation = operations.id
""")
operations = dict()
for row in self.cursor.fetchall():
if row[0] not in operations:
operations[row[0]] = dict(email=row[1], created=row[2], payload=row[3], hosts=[])
operations[row[0]]["hosts"].append(dict(
host=row[4], assignment_status=row[5], assigned=row[6],
completed=row[7], results=row[8],
))
return operations
def create_operation(self, online_hosts: List[OnlineHost], email: str, payload: str) -> int:
self.cursor.execute( "INSERT INTO operations (email, payload) VALUES (%s, %s) RETURNING id", (email, payload) )
operation_id = self.cursor.fetchone()[0]