def on_get(self, req, resp): """ Handles GET requests for Clusters. :param req: Request instance that will be passed through. :type req: falcon.Request :param resp: Response instance that will be passed through. :type resp: falcon.Response """ req.context['model'] = None try: clusters = Clusters.retrieve() except: self.logger.warn( 'Etcd does not have any clusters. Returning [] and 404.') resp.status = falcon.HTTP_404 return if clusters.clusters == []: self.logger.debug( 'Etcd has a clusters directory but no content.') resp.status = falcon.HTTP_200 return req.context['model'] = clusters
def on_delete(self, req, resp, address): """ Handles the Deletion of a Host. :param req: Request instance that will be passed through. :type req: falcon.Request :param resp: Response instance that will be passed through. :type resp: falcon.Response :param address: The address of the Host being requested. :type address: str """ resp.body = "{}" try: Host.delete(address) resp.status = falcon.HTTP_200 except: resp.status = falcon.HTTP_404 # Also remove the host from all clusters. # Note: We've done all we need to for the host deletion, # so if an error occurs from here just log it and # return. try: clusters = Clusters.retrieve() except: self.logger.warn("Etcd does not have any clusters") return try: for cluster_name in clusters.clusters: self.logger.debug("Checking cluster {0}".format(cluster_name)) cluster = Cluster.retrieve(cluster_name) if address in cluster.hostset: self.logger.info("Removing {0} from cluster {1}".format(address, cluster_name)) cluster.hostset.remove(address) cluster.save(cluster_name) self.logger.info("{0} has been removed from cluster {1}".format(address, cluster_name)) except: self.logger.warn("Failed to remove {0} from cluster {1}".format(address, cluster_name))