Esempio n. 1
0
def main():

    fl = os.path.dirname(os.path.abspath(__file__))
    fn = os.path.join(fl, 'grpc.json')

    with open(fn) as grpc_file:

        item = json.load(grpc_file)

        creds = item["grpc"]["Rollback"]["credentials"]

        credentials = service_account.Credentials.from_service_account_file(
            "{}".format(creds))
        scoped_credentials = credentials.with_scopes(
            ['https://www.googleapis.com/auth/datastore'])
        http_request = google.auth.transport.requests.Request()
        channel = google.auth.transport.grpc.secure_authorized_channel(
            scoped_credentials, http_request, 'firestore.googleapis.com:443')

        stub = firestore_pb2_grpc.FirestoreStub(channel)

        database = item["grpc"]["Rollback"]["database"]

        begin_transaction_request = firestore_pb2.BeginTransactionRequest(
            database=database)
        begin_transaction_response = stub.BeginTransaction(
            begin_transaction_request)

        rollback_request = firestore_pb2.RollbackRequest(
            database=database,
            transaction=begin_transaction_response.transaction)
        rollback_response = stub.Rollback(rollback_request)
        print(rollback_response)
Esempio n. 2
0
def main():

    fl = os.path.dirname(os.path.abspath(__file__))
    fn = os.path.join(fl, 'grpc.json')

    with open(fn) as grpc_file:

        subprocess.call('clear')

        item = json.load(grpc_file)

        creds = item["grpc"]["DeleteDocuments"]["credentials"]

        credentials = service_account.Credentials.from_service_account_file(
            "{}".format(creds))
        scoped_credentials = credentials.with_scopes(
            ['https://www.googleapis.com/auth/datastore'])
        http_request = google.auth.transport.requests.Request()
        channel = google.auth.transport.grpc.secure_authorized_channel(
            scoped_credentials, http_request, 'firestore.googleapis.com:443')

        # name is defined in the grpc.json file
        name = item["grpc"]["DeleteDocuments"]["name"]

        if name == ' ':

            name = raw_input(
                "Please enter the resource name of the document to delete, e.g. project/{project_id}/databases/{database_id}/documents/{document_path}: \n"
            )

        stub = firestore_pb2_grpc.FirestoreStub(channel)

        delete_document_request = firestore_pb2.DeleteDocumentRequest(
            name=name)
        stub.DeleteDocument(delete_document_request)
Esempio n. 3
0
def main():

    fl = os.path.dirname(os.path.abspath(__file__))
    fn = os.path.join(fl, 'grpc.json')

    with open(fn) as grpc_file:

        item = json.load(grpc_file)

        creds = item["grpc"]["UpdateDocument"]["credentials"]

        credentials = service_account.Credentials.from_service_account_file(
            "{}".format(creds))
        scoped_credentials = credentials.with_scopes(
            ['https://www.googleapis.com/auth/datastore'])
        http_request = google.auth.transport.requests.Request()
        channel = google.auth.transport.grpc.secure_authorized_channel(
            scoped_credentials, http_request, 'firestore.googleapis.com:443')

        stub = firestore_pb2_grpc.FirestoreStub(channel)

        now = time.time()
        seconds = int(now)
        timestamp = timestamp_pb2.Timestamp(seconds=seconds)

        # name is set in the grpc.json file
        name = item["grpc"]["UpdateDocument"]["name"]

        if name == ' ':
            name = raw_input(
                "Please provide the resource name of the document to be updated: \n"
            )

        field = raw_input(
            'Please provide the field to be updated, e.g. "foo" ')
        current_value = raw_input(
            'Please provide the current value of the field to be updated, e.g. "bar"  '
        )

        update_mask = common_pb2.DocumentMask(
            field_paths=[field, current_value])

        value = raw_input(
            "Please provide the new value of the field to update using the following syntax, e.g. 'foo_boo' \n"
        )

        value_ = document_pb2.Value(string_value=value)

        document = document_pb2.Document(name=name, fields={field: value_})

        update_document_request = firestore_pb2.UpdateDocumentRequest(
            document=document,
            update_mask=common_pb2.DocumentMask(field_paths=[field]))
        update_document_response = stub.UpdateDocument(update_document_request)

        print(update_document_response)
Esempio n. 4
0
def main():

    subprocess.call('clear')

    fl = os.path.dirname(os.path.abspath(__file__))
    fn = os.path.join(fl, 'grpc.json')

    with open(fn) as grpc_file:

        item = json.load(grpc_file)

        creds = item["grpc"]["GetDocument"]["credentials"]

        credentials = service_account.Credentials.from_service_account_file(
            "{}".format(creds))
        scoped_credentials = credentials.with_scopes(
            ['https://www.googleapis.com/auth/datastore'])
        http_request = google.auth.transport.requests.Request()
        channel = google.auth.transport.grpc.secure_authorized_channel(
            scoped_credentials, http_request, 'firestore.googleapis.com:443')

        stub = firestore_pb2_grpc.FirestoreStub(channel)

        now = time.time()
        seconds = int(now)
        timestamp = timestamp_pb2.Timestamp(seconds=seconds)

        field_paths = {}

        # field_paths is set in the grpc.json file
        field_paths = item["grpc"]["GetDocument"]["document_mask_field_path"]

        mask = common_pb2.DocumentMask(field_paths=[field_paths])

        # name is set in the grpc.json file
        name = item["grpc"]["GetDocument"]["name"]

        if name == ' ':

            name = raw_input(
                "Please enter the resource name of the Document to get, e.g. projects/{project_id}/databases/{database_id}/documents/{document_path}: \n"
            )

        get_document_request = firestore_pb2.GetDocumentRequest(name=name,
                                                                mask=mask)
        get_document_response = stub.GetDocument(get_document_request)

        print(get_document_response)
Esempio n. 5
0
def _execute_probe(api, use_extension=False):
    """Execute a probe function given certain Cloud api and probe name.

  Args:
    api: the name of the api provider, e.g. "spanner", "firestore".
    use_extension: option to use grpc-gcp extension when creating channel.

  Raises:
    NotImplementedError: An error occurred when api does not match any records.
  """
    util = StackdriverUtil(api)

    if api == 'spanner':
        channel = _get_stub_channel(_SPANNER_TARGET, use_extension)
        stub = spanner_pb2_grpc.SpannerStub(channel)
        probe_functions = spanner_probes.PROBE_FUNCTIONS
    elif api == 'firestore':
        channel = _get_stub_channel(_FIRESTORE_TARGET)
        stub = firestore_pb2_grpc.FirestoreStub(channel)
        probe_functions = firestore_probes.PROBE_FUNCTIONS
    else:
        raise NotImplementedError('gRPC prober is not implemented for %s !' %
                                  api)

    total = len(probe_functions)
    success = 0
    metrics = {}

    # Execute all probes for given api
    for probe_name in probe_functions:
        probe_function = probe_functions[probe_name]
        try:
            probe_function(stub, metrics)
            success += 1
        except Exception:  # pylint: disable=broad-except
            # report any kind of exception to Stackdriver
            util.report_error(traceback.format_exc())

    if success == total:
        util.set_success(True)

    # Summarize metrics
    util.add_metrics_dict(metrics)
    util.output_metrics()

    # Fail this probe if any function fails
    if success != total:
        sys.exit(1)
Esempio n. 6
0
def main():

    subprocess.call('clear')

    fl = os.path.dirname(os.path.abspath(__file__))
    fn = os.path.join(fl, 'grpc.json')

    with open(fn) as grpc_file:

        item = json.load(grpc_file)

        creds = item["grpc"]["CreateDocument"]["credentials"]

        credentials = service_account.Credentials.from_service_account_file(
            "{}".format(creds))
        scoped_credentials = credentials.with_scopes(
            ['https://www.googleapis.com/auth/datastore'])
        http_request = google.auth.transport.requests.Request()
        channel = google.auth.transport.grpc.secure_authorized_channel(
            scoped_credentials, http_request, 'firestore.googleapis.com:443')

        stub = firestore_pb2_grpc.FirestoreStub(channel)

        # parent defined in the grpc.json file
        parent = item["grpc"]["CreateDocument"]["parent"]

        # collection_id defined in the grpc.json
        collection_id = item["grpc"]["CreateDocument"]["collection_id"]

        document_id = item["grpc"]["CreateDocument"]["document_id"]

        if document_id == ' ':

            document_id = raw_input("Please provde a document_id: \n")

        document = {}

        create_document_request = firestore_pb2.CreateDocumentRequest(
            parent=parent,
            collection_id=collection_id,
            document_id=document_id,
            document=document)
        document = stub.CreateDocument(create_document_request)
        print(document)
Esempio n. 7
0
def main():

    subprocess.call('clear')

    fl = os.path.dirname(os.path.abspath(__file__))
    fn = os.path.join(fl, 'grpc.json')

    with open(fn) as grpc_file:

        item = json.load(grpc_file)

        # credentials file location defined in grpc.json file
        creds = item["grpc"]["BeginTransaction"]["credentials"]

        credentials = service_account.Credentials.from_service_account_file(
            "{}".format(creds))
        scoped_credentials = credentials.with_scopes(
            ['https://www.googleapis.com/auth/datastore'])
        http_request = google.auth.transport.requests.Request()
        channel = google.auth.transport.grpc.secure_authorized_channel(
            scoped_credentials, http_request, 'firestore.googleapis.com:443')

        stub = firestore_pb2_grpc.FirestoreStub(channel)

        now = time.time()
        seconds = int(now)
        timestamp = timestamp_pb2.Timestamp(seconds=seconds)

        # database definded in grpc.json file
        database = item["grpc"]["BeginTransaction"]["database"]

        options = common_pb2.TransactionOptions(
            read_only=common_pb2.TransactionOptions.ReadOnly(
                read_time=timestamp))

        begin_transaction_request = firestore_pb2.BeginTransactionRequest(
            database=database, options=options)

        begin_transaction_response = stub.BeginTransaction(
            begin_transaction_request)

        print(begin_transaction_response)
Esempio n. 8
0
def main():

    fl = os.path.dirname(os.path.abspath(__file__))
    fn = os.path.join(fl, 'grpc.json')

    with open(fn) as grpc_file:

        item = json.load(grpc_file)

        creds = item["grpc"]["Write"]["credentials"]

        credentials = service_account.Credentials.from_service_account_file(
            "{}".format(creds))
        scoped_credentials = credentials.with_scopes(
            ['https://www.googleapis.com/auth/datastore'])
        http_request = google.auth.transport.requests.Request()
        channel = google.auth.transport.grpc.secure_authorized_channel(
            scoped_credentials, http_request, 'firestore.googleapis.com:443')

        stub = firestore_pb2_grpc.FirestoreStub(channel)

        database = item["grpc"]["Write"]["database"]
        name = item["grpc"]["Write"]["name"]
        first_write = write_pb2.Write()

        responses = stub.Write(first_message(database, first_write))
        for response in responses:
            print("Received message %s" % (response.stream_id))
            print(response.stream_token)

        value_ = document_pb2.Value(string_value="foo_boo")
        update = document_pb2.Document(name=name, fields={"foo": value_})
        writes = write_pb2.Write(
            update_mask=common_pb2.DocumentMask(field_paths=["foo"]),
            update=update)
        r2 = stub.Write(
            generate_messages(database, writes, response.stream_id,
                              response.stream_token))
        for r in r2:
            print(r.write_results)
Esempio n. 9
0
def main():

    fl = os.path.dirname(os.path.abspath(__file__))
    fn = os.path.join(fl, 'grpc.json')

    with open(fn) as grpc_file:

        item = json.load(grpc_file)

        creds = item["grpc"]["RunQuery"]["credentials"]

        credentials = service_account.Credentials.from_service_account_file(
            "{}".format(creds))
        scoped_credentials = credentials.with_scopes(
            ['https://www.googleapis.com/auth/datastore'])
        http_request = google.auth.transport.requests.Request()
        channel = google.auth.transport.grpc.secure_authorized_channel(
            scoped_credentials, http_request, 'firestore.googleapis.com:443')

        stub = firestore_pb2_grpc.FirestoreStub(channel)

        parent = item["grpc"]["RunQuery"]["parent"]
        field_path = item["grpc"]["RunQuery"]["field_path"]

        fields = {}
        fields = query_pb2.StructuredQuery.FieldReference(
            field_path=field_path)
        select = query_pb2.StructuredQuery.Projection(fields=[fields])

        structured_query = query_pb2.StructuredQuery(select=select)

        run_query_request = firestore_pb2.RunQueryRequest(
            parent=parent, structured_query=structured_query)
        run_query_response = stub.RunQuery(run_query_request)

        print('starting read from batch: ', type(run_query_response))
        for runquery in run_query_response:
            print(runquery)
def main():

  subprocess.call('clear')

  fl = os.path.dirname(os.path.abspath(__file__))
  fn = os.path.join(fl, 'grpc.json')

  with open(fn) as grpc_file:
         
     item = json.load(grpc_file)

     creds = item["grpc"]["BatchGetDocuments"]["credentials"]

     credentials = service_account.Credentials.from_service_account_file("{}".format(creds))
     scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/datastore'])
     http_request = google.auth.transport.requests.Request()
     channel = google.auth.transport.grpc.secure_authorized_channel(scoped_credentials, http_request, 'firestore.googleapis.com:443')

     stub = firestore_pb2_grpc.FirestoreStub(channel)
     
     # database and documents defined in the grpc.json file
     database = item["grpc"]["BatchGetDocuments"]["database"]
     documents = item["grpc"]["BatchGetDocuments"]["documents"]

     if documents == ' ':

        print("Please enter the document Id's you would like to retrieve, e.g.\n")
        print("projects/geoff-python/databases/(default)/documents/users/alovelace \n") 
      
        documents = raw_input(":")

     batch_get_document_request = firestore_pb2.BatchGetDocumentsRequest(database = database, documents = [documents])
     batch_get_document_response = stub.BatchGetDocuments(batch_get_document_request)
                     
     print('staring read from batch: ', type(batch_get_document_response))
     for get_document in batch_get_document_response:
       	    print(get_document)
Esempio n. 11
0
def main():


   fl = os.path.dirname(os.path.abspath(__file__))
   fn = os.path.join(fl, 'grpc.json')

   with open(fn) as grpc_file:
          
            item = json.load(grpc_file)

            creds = item["grpc"]["ListDocuments"]["credentials"]

            credentials = service_account.Credentials.from_service_account_file("{}".format(creds))
            scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/datastore'])
            http_request = google.auth.transport.requests.Request()
            channel = google.auth.transport.grpc.secure_authorized_channel(scoped_credentials, http_request, 'firestore.googleapis.com:443')

            stub = firestore_pb2_grpc.FirestoreStub(channel)

            parent = item["grpc"]["ListDocuments"]["parent"]

            list_document_request = firestore_pb2.ListDocumentsRequest(parent = 'projects/geoff-python/databases/(default)/documents')
            list_document_response = stub.ListDocuments(list_document_request)
            print(list_document_response)
def main():

   subprocess.call('clear')

   fl = os.path.dirname(os.path.abspath(__file__))
   fn = os.path.join(fl, 'grpc.json')

   with open(fn) as grpc_file:
          
            item = json.load(grpc_file)

            creds = item["grpc"]["ListCollectionIds"]["credentials"]

            credentials = service_account.Credentials.from_service_account_file("{}".format(creds))
            scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/datastore'])
            http_request = google.auth.transport.requests.Request()
            channel = google.auth.transport.grpc.secure_authorized_channel(scoped_credentials, http_request, 'firestore.googleapis.com:443')

            stub = firestore_pb2_grpc.FirestoreStub(channel)

            parent = item["grpc"]["ListCollectionIds"]["parent"]
            list_collectionIds_request = firestore_pb2.ListCollectionIdsRequest(parent = parent)
            list_collectionIds_response = stub.ListCollectionIds(list_collectionIds_request)
            print(list_collectionIds_response)
Esempio n. 13
0
def main():

    subprocess.call('clear')

    fl = os.path.dirname(os.path.abspath(__file__))
    fn = os.path.join(fl, 'grpc.json')

    with open(fn) as grpc_file:

        item = json.load(grpc_file)

        creds = item["grpc"]["Commit"]["credentials"]

        credentials = service_account.Credentials.from_service_account_file(
            "{}".format(creds))
        scoped_credentials = credentials.with_scopes(
            ['https://www.googleapis.com/auth/datastore'])
        http_request = google.auth.transport.requests.Request()
        channel = google.auth.transport.grpc.secure_authorized_channel(
            scoped_credentials, http_request, 'firestore.googleapis.com:443')

        stub = firestore_pb2_grpc.FirestoreStub(channel)

        now = time.time()
        seconds = int(now)
        timestamp = timestamp_pb2.Timestamp(seconds=seconds)

        # database defined in the grpc.json file
        database = item["grpc"]["Commit"]["database"]

        options = common_pb2.TransactionOptions(
            read_write=common_pb2.TransactionOptions.ReadWrite())
        begin_transaction_request = firestore_pb2.BeginTransactionRequest(
            database=database, options=options)
        begin_transaction_response = stub.BeginTransaction(
            begin_transaction_request)
        transaction = begin_transaction_response.transaction

        stub = firestore_pb2_grpc.FirestoreStub(channel)

        now = time.time()
        seconds = int(now)
        timestamp = timestamp_pb2.Timestamp(seconds=seconds)

        field_paths = {}
        # document mask field_path is defined in the grpc.json file
        field_paths = item["grpc"]["Commit"]["field_paths"]
        update_mask = common_pb2.DocumentMask(field_paths=[field_paths])

        # document_fileds is defined in the grpc.json file
        fields = item["grpc"]["Commit"]["fields"]

        # document_name is defined in the grpc.json file
        name = item["grpc"]["Commit"]["name"]

        update = document_pb2.Document(name=name,
                                       fields=fields,
                                       create_time=timestamp,
                                       update_time=timestamp)

        writes = {}
        database = item["grpc"]["Commit"]["database"]
        writes = write_pb2.Write(update_mask=update_mask, update=update)

        commit_request = firestore_pb2.CommitRequest(database=database,
                                                     writes=[writes],
                                                     transaction=transaction)
        commit_response = stub.Commit(commit_request)

        print(commit_response)