Esempio n. 1
0
    def _get_metadata(service: Resource, project: str,
                      instance: str) -> Dict[str, Union[Dict, str]]:
        """Requests metadata from the Cloud SQL Instance
        and returns a dictionary containing the IP addresses and certificate
        authority of the Cloud SQL Instance.

        :type service: googleapiclient.discovery.Resource
        :param service:
            A service object created from the Google Python API client library.
            Must be using the SQL Admin API. For more info check out
            https://github.com/googleapis/google-api-python-client.

        :type project: str
        :param project:
            A string representing the name of the project.

        :type inst_name: str
        :param project: A string representing the name of the instance.

        :rtype: Dict[str: Union[Dict, str]]
        :returns: Returns a dictionary containing a dictionary of all IP
              addresses and their type and a string representing the
              certificate authority.

        :raises TypeError: If any of the arguments are not the specified type.
        """

        if (not isinstance(service, googleapiclient.discovery.Resource)
                or not isinstance(project, str)
                or not isinstance(instance, str)):
            raise TypeError("Arguments must be as follows: " +
                            "service (googleapiclient.discovery.Resource), " +
                            "proj_name (str) and inst_name (str).")

        req = service.instances().get(project=project, instance=instance)
        res = req.execute()

        # Extract server certificate authority
        serverCaCert = res["serverCaCert"]["cert"]

        # Map IP addresses to type.
        ip_map = {ip["type"]: ip["ipAddress"] for ip in res["ipAddresses"]}

        metadata = {"ip_addresses": ip_map, "server_ca_cert": serverCaCert}

        return metadata
Esempio n. 2
0
def get_gcp_instance_responses(project_id: str, zones: Optional[List[Dict]],
                               compute: Resource) -> List[Resource]:
    """
    Return list of GCP instance response objects for a given project and list of zones
    :param project_id: The project ID
    :param zones: The list of zones to query for instances
    :param compute: The compute resource object
    :return: A list of response objects of the form {id: str, items: []} where each item in `items` is a GCP instance
    """
    if not zones:
        # If the Compute Engine API is not enabled for a project, there are no zones and therefore no instances.
        return []
    response_objects: List[Resource] = []
    for zone in zones:
        req = compute.instances().list(project=project_id, zone=zone['name'])
        res = req.execute()
        response_objects.append(res)
    return response_objects