def _get_services(self):
        """use the Google Discovery Build to generate API clients
        for Life Sciences, and use the google storage python client
        for storage.
        """
        from googleapiclient.discovery import build as discovery_build
        from oauth2client.client import (
            GoogleCredentials,
            ApplicationDefaultCredentialsError,
        )
        from google.cloud import storage

        # Credentials must be exported to environment
        try:
            creds = GoogleCredentials.get_application_default()
        except ApplicationDefaultCredentialsError as ex:
            log_verbose_traceback(ex)
            raise ex

        # Discovery clients for Google Cloud Storage and Life Sciences API
        self._storage_cli = discovery_build("storage",
                                            "v1",
                                            credentials=creds,
                                            cache_discovery=False)
        self._compute_cli = discovery_build("compute",
                                            "v1",
                                            credentials=creds,
                                            cache_discovery=False)
        self._api = discovery_build("lifesciences",
                                    "v2beta",
                                    credentials=creds,
                                    cache_discovery=False)
        self._bucket_service = storage.Client()
Example #2
0
    def _get_services(self):
        """
        Use the Google Discovery Build to generate API clients
        for Life Sciences, and use the google storage python client
        for storage.
        """
        from googleapiclient.discovery import build as discovery_build
        from google.cloud import storage
        import google.auth
        import google_auth_httplib2
        import httplib2
        import googleapiclient

        # Credentials must be exported to environment
        try:
            # oauth2client is deprecated, see: https://google-auth.readthedocs.io/en/master/oauth2client-deprecation.html
            # google.auth is replacement
            # not sure about scopes here. this cover all cloud services
            creds, _ = google.auth.default(
                scopes=["https://www.googleapis.com/auth/cloud-platform"])
        except google.auth.DefaultCredentialsError as ex:
            log_verbose_traceback(ex)
            raise ex

        def build_request(http, *args, **kwargs):
            """
            See https://googleapis.github.io/google-api-python-client/docs/thread_safety.html
            """
            new_http = google_auth_httplib2.AuthorizedHttp(
                creds, http=httplib2.Http())
            return googleapiclient.http.HttpRequest(new_http, *args, **kwargs)

        # Discovery clients for Google Cloud Storage and Life Sciences API
        # create authorized http for building services
        authorized_http = google_auth_httplib2.AuthorizedHttp(
            creds, http=httplib2.Http())
        self._storage_cli = discovery_build(
            "storage",
            "v1",
            cache_discovery=False,
            requestBuilder=build_request,
            http=authorized_http,
        )
        self._compute_cli = discovery_build(
            "compute",
            "v1",
            cache_discovery=False,
            requestBuilder=build_request,
            http=authorized_http,
        )
        self._api = discovery_build(
            "lifesciences",
            "v2beta",
            cache_discovery=False,
            requestBuilder=build_request,
            http=authorized_http,
        )
        self._bucket_service = storage.Client()
Example #3
0
    def _get_services(self, version="v1"):
        """get version 1 of the google compute and storage service

        Parameters
        ==========
        version: version to use (default is v1)
        """
        self._bucket_service = storage.Client()
        creds = GoogleCredentials.get_application_default()
        self._storage_service = discovery_build("storage", version, credentials=creds)
        self._compute_service = discovery_build("compute", version, credentials=creds)
Example #4
0
    def _get_services(self, version='v1'):
        '''get version 1 of the google compute and storage service

        Parameters
        ==========
        version: version to use (default is v1)
        '''
        self._bucket_service = storage.Client()
        creds = GoogleCredentials.get_application_default()
        self._storage_service = discovery_build('storage',
                                                version,
                                                credentials=creds)
        self._compute_service = discovery_build('compute',
                                                version,
                                                credentials=creds)
 def build_client(self):
     credentials = self.get_oauth_credentials()
     http = credentials.authorize(httplib2.Http())
     # cache_discovery=False prevents 'ImportError: file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth'
     return discovery_build("storage",
                            "v1",
                            http=http,
                            cache_discovery=False)
Example #6
0
    def __init__(self, user_email=None):
        credentials = service_account.ServiceAccountCredentials\
            .from_json_keyfile_name(ACCOUNT_SERVICE_KEY_FILE,
                                    scopes=self.SCOPES)

        credentials = credentials.create_delegated(user_email)
        self.user_email = user_email
        self.http = credentials.authorize(httplib2.Http(timeout=120))
        self.service = discovery_build(self.API_SERVICE, self.API_VERSION,
                                       http=self.http)
def get_authenticated_service(scope):
    print('Authenticating...')
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
                                   scope=scope,
                                   message=MISSING_CLIENT_SECRETS_MESSAGE)

    credential_storage = CredentialStorage(CREDENTIALS_FILE)
    credentials = credential_storage.get()
    if credentials is None or credentials.invalid:
        credentials = run_oauth2(flow, credential_storage)

    print('Constructing Google Cloud Storage service...')
    http = credentials.authorize(httplib2.Http())
    return discovery_build('storage', 'v1', http=http)
Example #8
0
 def build_client(self):
     credentials = self.get_oauth_credentials()
     http = credentials.authorize(httplib2.Http())
     return discovery_build("storage", "v1", http=http)
Example #9
0
        }],
    }

    return compute.instances().insert(project=project, zone=zone,
                                      body=config).execute()


if __name__ == "__main__":
    project = "k8s-istio-218403"
    zone = "us-east1-b"
    nodes = 4

    # Authenticate with the googles.
    credentials = service_account.Credentials.from_service_account_file(
        "creds/serviceaccount.json")
    compute = discovery_build("compute", "v1", credentials=credentials)

    instances = []
    for i in range(nodes):
        instance_name = "node-" + str(i)

        print("Creating instance {}...".format(instance_name))
        operation = create_instance(compute, project, zone, instance_name)
        wait_for_operation(compute, project, zone, operation["name"])

        ip = get_ip(compute, project, zone, instance_name)
        instances.append({"name": instance_name, "ip": ip})

    for instance in instances:
        print(instance)