def asgs_for_cluster(cluster): """ Given a named cluster, get all ASGs in the cluster. Arguments: cluster(str): The name of the asgard cluster. Returns: list(dict): List of ASGs. Raises: RateLimitedException: When we are being rate limited by AWS. """ LOG.debug("URL: {}".format(CLUSTER_INFO_URL.format(cluster))) url = CLUSTER_INFO_URL.format(cluster) response = requests.get(url, params=ASGARD_API_TOKEN, timeout=REQUESTS_TIMEOUT) LOG.debug("ASGs for Cluster: {}".format(response.text)) asgs_json = _parse_asgard_json_response(url, response) if len(asgs_json) < 1: msg = "Expected a list of dicts with an 'autoScalingGroupName' attribute. " \ "Got: {}".format(asgs_json) raise BackendDataError(msg) return asgs_json
def elbs_for_asg(asg): """ Return the ELB(s) which are directing traffic to a particular ASG. Arguments: asg(str): The name of the asg. Returns: list(str): List of the ELB names. Raises: BackendDataError: If unexpected response from Asgard. RateLimitedException: When we are being rate limited by AWS. """ url = ASG_INFO_URL.format(asg) response = requests.get(url, params=ASGARD_API_TOKEN, timeout=REQUESTS_TIMEOUT) resp_json = _parse_asgard_json_response(url, response) try: elbs = resp_json['group']['loadBalancerNames'] except (KeyError, TypeError): msg = "Expected a dict with path ['group']['loadbalancerNames']. " \ "Got: {}".format(resp_json) raise BackendDataError(msg) return elbs
def clusters_for_asgs(asgs): """ An autoscaling group can belong to multiple clusters potentially. This function finds all asgard clusters for a list of ASGs. eg. get all clusters that have the 'edxapp' cluster tag.. Arguments:: asgs(iterable): A iterable of ASGs we care about. eg. [ u'test-edx-edxapp-v007', u'test-edx-worker-v007', ] Returns: dict: A mapping of cluster names to asgs in the cluster. eg. { u'test-edx-edxapp': [ u'test-edx-edxapp-v007', ], u'test-edx-worker': [ u'test-edx-worker-v004', ] } Raises: BackendDataError: We got bad data from the backend. We can't get cluster information from it. """ request = requests.Request('GET', CLUSTER_LIST_URL, params=ASGARD_API_TOKEN) url = request.prepare().url LOG.debug("Getting Cluster List from: {}".format(url)) response = requests.get(CLUSTER_LIST_URL, params=ASGARD_API_TOKEN, timeout=REQUESTS_TIMEOUT) cluster_json = _parse_json(url, response) relevant_clusters = {} for cluster in cluster_json: if "autoScalingGroups" not in cluster or "cluster" not in cluster: msg = "Expected 'cluster' and 'autoScalingGroups' keys in dict: {}".format(cluster) raise BackendDataError(msg) for asg in cluster['autoScalingGroups']: LOG.debug("Membership: {} in {}: {}".format(asg, asgs, asg in asgs)) if asg in asgs: relevant_clusters[cluster['cluster']] = cluster['autoScalingGroups'] # A cluster can have multiple relevant ASGs. # We don't need to check them all. break # The inner for loop return relevant_clusters