def getServer(self, id, detail=False):
        try:
            response_data = self.getResource("servers/" + id, "GET")
        except requests.exceptions.HTTPError as ex:
            raise Exception("error on retrieving the server info (id=%r)"
                            ": %s" % (id, ex.response.json()))

        server = None

        if response_data:
            server_data = response_data["server"]

            server = Server()
            server.setId(server_data["id"])
            server.setName(server_data["name"])
            server.setKeyName(server_data["key_name"])
            server.setMetadata(server_data["metadata"])
            server.setState(server_data["OS-EXT-STS:vm_state"])
            server.setUserId(server_data["user_id"])
            server.setProjectId(server_data["tenant_id"])
            server.setCreatedAt(server_data["created"])
            server.setUpdatedAt(server_data.get("updated", None))
            server.setLaunchedAt(
                server_data.get("OS-SRV-USG:launched_at", None))
            server.setTerminatedAt(
                server_data.get("OS-SRV-USG:terminated_at", None))

            if "user_data" in server_data:
                user_data = server_data["user_data"]
                server.setUserData(utils.decodeBase64(user_data))

            if detail:
                server.setFlavor(self.getFlavor(server_data["flavor"]["id"]))

        return server
    def getServer(self, id, detail=False):
        try:
            response_data = self.getResource("servers/" + id, "GET")
        except requests.exceptions.HTTPError as ex:
            raise SynergyError("error on retrieving the server info (id=%r)"
                               ": %s" % (id, ex.response.json()))

        server = None

        if response_data:
            server_data = response_data["server"]

            server = Server()
            server.setId(server_data["id"])
            server.setName(server_data["name"])
            server.setKeyName(server_data["key_name"])
            server.setMetadata(server_data["metadata"])
            server.setUserData(server_data.get("OS-EXT-SRV-ATTR:user_data",
                                               None))
            server.setType()
            server.setState(server_data["OS-EXT-STS:vm_state"])
            server.setUserId(server_data["user_id"])
            server.setProjectId(server_data["tenant_id"])
            server.setCreatedAt(server_data["created"])
            server.setUpdatedAt(server_data.get("updated", None))
            server.setLaunchedAt(
                server_data.get("OS-SRV-USG:launched_at", None))
            server.setTerminatedAt(
                server_data.get("OS-SRV-USG:terminated_at", None))

            if detail:
                server.setFlavor(self.getFlavor(server_data["flavor"]["id"]))

        return server
    def getExpiredServers(self, prj_id, server_ids, TTL):
        servers = []
        connection = self.db_engine.connect()

        try:
            # retrieve all expired instances for the specified
            # project and expiration time
            ids = ""

            if server_ids:
                ids = "uuid in ('%s') and " % "', '".join(server_ids)

            QUERY = """select uuid, vcpus, memory_mb, root_gb, \
vm_state, user_data from nova.instances where project_id = \
'%(project_id)s' and deleted_at is NULL and (vm_state='error' or \
(%(server_ids)s vm_state='active' and terminated_at is NULL \
and timestampdiff(minute, launched_at, utc_timestamp()) >= %(expiration)s))\
""" % {"project_id": prj_id, "server_ids": ids, "expiration": TTL}

            LOG.debug("getExpiredServers query: %s" % QUERY)

            result = connection.execute(QUERY)

            for row in result.fetchall():
                flavor = Flavor()
                flavor.setVCPUs(row[1])
                flavor.setMemory(row[2])
                flavor.setStorage(row[3])

                server = Server()
                server.setId(row[0])
                server.setState(row[4])
                server.setUserData(row[5])
                server.setFlavor(flavor)

                QUERY = """select `key`, value from nova.instance_metadata \
where instance_uuid='%(id)s' and deleted_at is NULL""" % {"id": server.getId()}

                LOG.debug("getExpiredServers query: %s" % QUERY)

                result = connection.execute(QUERY)
                metadata = {}

                for row in result.fetchall():
                    metadata[row[0]] = row[1]

                server.setMetadata(metadata)
                server.setType()

                servers.append(server)
        except SQLAlchemyError as ex:
            raise SynergyError(ex.message)
        finally:
            connection.close()

        return servers
    def getProjectServers(self, prj_id):
        connection = self.db_engine.connect()
        servers = []

        try:
            # retrieve the amount of resources in terms of cores and memory
            QUERY = """select a.uuid, a.vcpus, a.memory_mb, a.root_gb, \
a.vm_state, a.user_data from nova.instances as a WHERE a.project_id=\
'%(project_id)s'and a.vm_state in ('active', 'building', 'error') and \
a.deleted_at is NULL and a.terminated_at is NULL""" % {
                "project_id": prj_id
            }

            LOG.debug("getProjectServers query: %s" % QUERY)

            result = connection.execute(QUERY)

            for row in result.fetchall():
                flavor = Flavor()
                flavor.setVCPUs(row[1])
                flavor.setMemory(row[2])
                flavor.setStorage(row[3])

                server = Server()
                server.setId(row[0])
                server.setState(row[4])
                server.setUserData(row[5])
                server.setFlavor(flavor)

                QUERY = """select `key`, value from nova.instance_metadata \
where instance_uuid='%(id)s' and deleted_at is NULL""" % {
                    "id": server.getId()
                }

                LOG.debug("getProjectServers query: %s" % QUERY)

                result = connection.execute(QUERY)
                metadata = {}

                for row in result.fetchall():
                    metadata[row[0]] = row[1]

                server.setMetadata(metadata)
                server.setType()

                servers.append(server)
        except SQLAlchemyError as ex:
            raise SynergyError(ex.message)
        finally:
            connection.close()

        return servers
    def getProjectServers(self, prj_id):
        connection = self.db_engine.connect()
        servers = []

        try:
            # retrieve the amount of resources in terms of cores and memory
            QUERY = """select a.uuid, a.vcpus, a.memory_mb, a.root_gb, \
a.vm_state, a.user_data from nova.instances as a WHERE a.project_id=\
'%(project_id)s'and a.vm_state in ('active', 'building', 'error') and \
a.deleted_at is NULL and a.terminated_at is NULL""" % {"project_id": prj_id}

            LOG.debug("getProjectServers query: %s" % QUERY)

            result = connection.execute(QUERY)

            for row in result.fetchall():
                flavor = Flavor()
                flavor.setVCPUs(row[1])
                flavor.setMemory(row[2])
                flavor.setStorage(row[3])

                server = Server()
                server.setId(row[0])
                server.setState(row[4])
                server.setUserData(row[5])
                server.setFlavor(flavor)

                QUERY = """select `key`, value from nova.instance_metadata \
where instance_uuid='%(id)s' and deleted_at is NULL""" % {"id": server.getId()}

                LOG.debug("getProjectServers query: %s" % QUERY)

                result = connection.execute(QUERY)
                metadata = {}

                for row in result.fetchall():
                    metadata[row[0]] = row[1]

                server.setMetadata(metadata)
                server.setType()

                servers.append(server)
        except SQLAlchemyError as ex:
            raise SynergyError(ex.message)
        finally:
            connection.close()

        return servers
    def getServers(self, detail=False, status=None):
        params = {}
        if status:
            params["status"] = status

        url = "servers/detail"

        try:
            response_data = self.getResource(url, "GET", params)
        except requests.exceptions.HTTPError as ex:
            response = ex.response.json()
            raise Exception("error on retrieving the servers list"
                            ": %s" % (id, response))

        servers = []

        if response_data:
            for server_data in response_data["servers"]:
                server = Server()
                server.setId(server_data["id"])
                server.setName(server_data["name"])
                server.setKeyName(server_data["key_name"])
                server.setMetadata(server_data["metadata"])
                server.setState(server_data["OS-EXT-STS:vm_state"])
                server.setUserId(server_data["user_id"])
                server.setProjectId(server_data["tenant_id"])
                server.setCreatedAt(server_data["created"])
                server.setUpdatedAt(server_data.get("updated", None))
                server.setLaunchedAt(
                    server_data.get("OS-SRV-USG:launched_at", None))
                server.setTerminatedAt(
                    server_data.get("OS-SRV-USG:terminated_at", None))

                if "user_data" in server_data:
                    user_data = server_data["user_data"]
                    server.setUserData(utils.decodeBase64(user_data))

                if detail:
                    server.setFlavor(
                        self.getFlavor(server_data["flavor"]["id"]))

                servers.append(server)

        return servers
    def getServers(self, detail=False, status=None):
        params = {}
        if status:
            params["status"] = status

        url = "servers/detail"

        try:
            response_data = self.getResource(url, "GET", params)
        except requests.exceptions.HTTPError as ex:
            response = ex.response.json()
            raise SynergyError("error on retrieving the servers list"
                               ": %s" % (id, response))

        servers = []

        if response_data:
            for server_data in response_data["servers"]:
                server = Server()
                server.setId(server_data["id"])
                server.setName(server_data["name"])
                server.setKeyName(server_data["key_name"])
                server.setMetadata(server_data["metadata"])
                server.setUserData(server_data.get("OS-EXT-SRV-ATTR:user_data",
                                                   None))
                server.setType()
                server.setState(server_data["OS-EXT-STS:vm_state"])
                server.setUserId(server_data["user_id"])
                server.setProjectId(server_data["tenant_id"])
                server.setCreatedAt(server_data["created"])
                server.setUpdatedAt(server_data.get("updated", None))
                server.setLaunchedAt(
                    server_data.get("OS-SRV-USG:launched_at", None))
                server.setTerminatedAt(
                    server_data.get("OS-SRV-USG:terminated_at", None))

                if detail:
                    server.setFlavor(self.getFlavor(
                        server_data["flavor"]["id"]))

                servers.append(server)

        return servers
    def getExpiredServers(self, prj_id, server_ids, TTL):
        servers = []
        connection = self.db_engine.connect()

        try:
            # retrieve all expired instances for the specified
            # project and expiration time
            ids = ""

            if server_ids:
                ids = "uuid in ('%s') and " % "', '".join(server_ids)

            QUERY = """select uuid, vcpus, memory_mb, root_gb, \
vm_state, user_data from nova.instances where project_id = \
'%(project_id)s' and deleted_at is NULL and (vm_state='error' or \
(%(server_ids)s vm_state='active' and terminated_at is NULL \
and timestampdiff(minute, launched_at, utc_timestamp()) >= %(expiration)s))\
""" % {
                "project_id": prj_id,
                "server_ids": ids,
                "expiration": TTL
            }

            LOG.debug("getExpiredServers query: %s" % QUERY)

            result = connection.execute(QUERY)

            for row in result.fetchall():
                flavor = Flavor()
                flavor.setVCPUs(row[1])
                flavor.setMemory(row[2])
                flavor.setStorage(row[3])

                server = Server()
                server.setId(row[0])
                server.setState(row[4])
                server.setUserData(row[5])
                server.setFlavor(flavor)

                QUERY = """select `key`, value from nova.instance_metadata \
where instance_uuid='%(id)s' and deleted_at is NULL""" % {
                    "id": server.getId()
                }

                LOG.debug("getExpiredServers query: %s" % QUERY)

                result = connection.execute(QUERY)
                metadata = {}

                for row in result.fetchall():
                    metadata[row[0]] = row[1]

                server.setMetadata(metadata)
                server.setType()

                servers.append(server)
        except SQLAlchemyError as ex:
            raise SynergyError(ex.message)
        finally:
            connection.close()

        return servers