# Path: zoe_lib/state/base.py
# class BaseRecord:
#     """
#     :type sql_manager: SQLManager
#     """
#     def __init__(self, d, sql_manager):
#         """
#         :type sql_manager: SQLManager
#         """
#         self.sql_manager = sql_manager
#         self.id = d['id']
# 
#     def serialize(self):
#         """Generates a dictionary that can be serialized in JSON."""
#         raise NotImplementedError
# 
# class BaseTable:
#     """Common abstraction for all tables."""
#     def __init__(self, sql_manager, table_name):
#         self.table_name = table_name
#         self.sql_manager = sql_manager
#         self.cursor = self.sql_manager.cursor()
# 
#     def create(self):
#         """Create this table."""
#         raise NotImplementedError
# 
#     def insert(self, **kwargs):
#         """Create a new record."""
#         raise NotImplementedError
# 
#     def delete(self, record_id):
#         """Delete a record from this table."""
#         query = 'DELETE FROM "{}" WHERE id = %s'.format(self.table_name)
#         self.cursor.execute(query, (record_id,))
#         self.sql_manager.commit()
# 
#     def update(self, record_id, **kwargs):
#         """Update the state of an execution."""
#         arg_list = []
#         value_list = []
#         for key, value in kwargs.items():
#             arg_list.append('{} = %s'.format(key))
#             value_list.append(value)
#         set_q = ", ".join(arg_list)
#         value_list.append(record_id)
#         q_base = 'UPDATE "{}" SET '.format(self.table_name) + set_q + ' WHERE id=%s'
#         query = self.cursor.mogrify(q_base, value_list)
#         self.cursor.execute(query)
#         self.sql_manager.commit()
# 
#     def select(self, only_one=False, limit=-1, **kwargs):
#         """Select records."""
#         raise NotImplementedError


import hashlib
import logging
import zoe_lib.config
from zoe_lib.state.base import BaseRecord, BaseTable
    def serialize(self):
        """Generates a dictionary that can be serialized in JSON."""
        return {
            'id': self.id,
            'internal_name': self.internal_name,
            'external_ip': self.external_ip,
            'external_port': self.external_port,
            'description': self.description
        }

    def __eq__(self, other):
        return self.id == other.id

    def activate(self, ext_ip, ext_port):
        """The backend has exposed the port."""
        self.sql_manager.ports.update(self.id, external_ip=ext_ip, external_port=ext_port)
        self.external_port = ext_port
        self.external_ip = ext_ip

    def reset(self):
        """The backend has stopped exposing the port."""
        self.sql_manager.ports.update(self.id, external_ip=None, external_port=None)
        self.external_port = None
        self.external_ip = None

    def proxy_key(self):
        """Return unique identifier that can be used ti generate proxy URLs."""
        return hashlib.sha256("{}:{}".format(self.id, zoe_lib.config.get_conf().cookie_secret).encode('utf-8')).hexdigest()



# Path: zoe_lib/state/base.py
# class BaseRecord:
#     """
#     :type sql_manager: SQLManager
#     """
#     def __init__(self, d, sql_manager):
#         """
#         :type sql_manager: SQLManager
#         """
#         self.sql_manager = sql_manager
#         self.id = d['id']
# 
#     def serialize(self):
#         """Generates a dictionary that can be serialized in JSON."""
#         raise NotImplementedError
# 
# class BaseTable:
#     """Common abstraction for all tables."""
#     def __init__(self, sql_manager, table_name):
#         self.table_name = table_name
#         self.sql_manager = sql_manager
#         self.cursor = self.sql_manager.cursor()
# 
#     def create(self):
#         """Create this table."""
#         raise NotImplementedError
# 
#     def insert(self, **kwargs):
#         """Create a new record."""
#         raise NotImplementedError
# 
#     def delete(self, record_id):
#         """Delete a record from this table."""
#         query = 'DELETE FROM "{}" WHERE id = %s'.format(self.table_name)
#         self.cursor.execute(query, (record_id,))
#         self.sql_manager.commit()
# 
#     def update(self, record_id, **kwargs):
#         """Update the state of an execution."""
#         arg_list = []
#         value_list = []
#         for key, value in kwargs.items():
#             arg_list.append('{} = %s'.format(key))
#             value_list.append(value)
#         set_q = ", ".join(arg_list)
#         value_list.append(record_id)
#         q_base = 'UPDATE "{}" SET '.format(self.table_name) + set_q + ' WHERE id=%s'
#         query = self.cursor.mogrify(q_base, value_list)
#         self.cursor.execute(query)
#         self.sql_manager.commit()
# 
#     def select(self, only_one=False, limit=-1, **kwargs):
#         """Select records."""
#         raise NotImplementedError


# Path: zoe_lib/state/port.py
import hashlib
import logging
import zoe_lib.config
from zoe_lib.state.base import BaseRecord, BaseTable

    def serialize(self):
        """Generates a dictionary that can be serialized in JSON."""
        return {
            'id': self.id,
            'internal_name': self.internal_name,
            'external_ip': self.external_ip,
            'external_port': self.external_port,
            'description': self.description
        }

    def __eq__(self, other):
        return self.id == other.id

    def activate(self, ext_ip, ext_port):
        """The backend has exposed the port."""
        self.sql_manager.ports.update(self.id, external_ip=ext_ip, external_port=ext_port)
        self.external_port = ext_port
        self.external_ip = ext_ip

    def reset(self):
        """The backend has stopped exposing the port."""
        self.sql_manager.ports.update(self.id, external_ip=None, external_port=None)
        self.external_port = None
        self.external_ip = None

    def proxy_key(self):
        """Return unique identifier that can be used ti generate proxy URLs."""
        return hashlib.sha256("{}:{}".format(self.id, zoe_lib.config.get_conf().cookie_secret).encode('utf-8')).hexdigest()



class PortTable(BaseTable):