def SayHello(self, request, context): request_in_flight = datetime.datetime.now() - \ request.request_initiation.ToDatetime() request_duration = duration_pb2.Duration() request_duration.FromTimedelta(request_in_flight) return helloworld_pb2.HelloReply( message='Hello, %s!' % request.name, request_duration=request_duration, )
def test_max_age_gc_rule_to_pb(): import datetime from google.protobuf import duration_pb2 max_age = datetime.timedelta(seconds=1) duration = duration_pb2.Duration(seconds=1) gc_rule = _make_max_age_gc_rule(max_age=max_age) pb_val = gc_rule.to_pb() assert pb_val == _GcRulePB(max_age=duration)
def test_to_pb(self): import datetime from google.protobuf import duration_pb2 max_age = datetime.timedelta(seconds=1) duration = duration_pb2.Duration(seconds=1) gc_rule = self._make_one(max_age=max_age) pb_val = gc_rule.to_pb() self.assertEqual(pb_val, _GcRulePB(max_age=duration))
def test_duration_write_pb2(): class Foo(proto.Message): ttl = proto.Field(proto.MESSAGE, number=1, message=duration_pb2.Duration,) foo = Foo() foo.ttl = duration_pb2.Duration(seconds=120) assert isinstance(foo.ttl, timedelta) assert isinstance(Foo.pb(foo).ttl, duration_pb2.Duration) assert foo.ttl.seconds == 120 assert Foo.pb(foo).ttl.seconds == 120
def testInvalidDuration(self): message = duration_pb2.Duration() self.assertRaisesRegexp( well_known_types.ParseError, 'Duration must end with letter "s": 1.', message.FromJsonString, '1') self.assertRaisesRegexp( well_known_types.ParseError, 'Couldn\'t parse duration: 1...2s.', message.FromJsonString, '1...2s')
def test_duration_read(): class Foo(proto.Message): ttl = proto.Field(proto.MESSAGE, number=1, message=duration_pb2.Duration,) foo = Foo(ttl=duration_pb2.Duration(seconds=60, nanos=1000)) assert isinstance(foo.ttl, timedelta) assert isinstance(Foo.pb(foo).ttl, duration_pb2.Duration) assert foo.ttl.days == 0 assert foo.ttl.seconds == 60 assert foo.ttl.microseconds == 1
def test_to_pb(self): import datetime from google.protobuf import duration_pb2 from gcloud.bigtable._generated import (bigtable_table_data_pb2 as data_pb2) max_age = datetime.timedelta(seconds=1) duration = duration_pb2.Duration(seconds=1) gc_rule = self._makeOne(max_age=max_age) pb_val = gc_rule.to_pb() self.assertEqual(pb_val, data_pb2.GcRule(max_age=duration))
def Duration(start, end, duration_proto=None): """ Take in two start and end times from time.time() and makes them into Duration proto. If the duration_proto is passed in then it fills in the fields and returns it. """ if duration_proto is None: duration_proto = duration_pb2.Duration() diff = end - start seconds = int(diff) nanos = int((diff - seconds) * 10**9) duration_proto.seconds = seconds duration_proto.nanos = nanos return duration_proto
def testTimedeltaConversion(self): message = duration_pb2.Duration() message.FromNanoseconds(1999999999) td = message.ToTimedelta() self.assertEqual(1, td.seconds) self.assertEqual(999999, td.microseconds) message.FromNanoseconds(-1999999999) td = message.ToTimedelta() self.assertEqual(-1, td.days) self.assertEqual(86398, td.seconds) self.assertEqual(1, td.microseconds) message.FromMicroseconds(-1) td = message.ToTimedelta() self.assertEqual(-1, td.days) self.assertEqual(86399, td.seconds) self.assertEqual(999999, td.microseconds) converted_message = duration_pb2.Duration() converted_message.FromTimedelta(td) self.assertEqual(message, converted_message)
def send(self, request=b"csutil.py", timeout=duration_proto.Duration(seconds=5), read_only=False, pre_execute=False, correlation_id="csutil-cid"): req = request_proto.Request(request=request, timeout=timeout, read_only=read_only, pre_execute=pre_execute, correlation_id=correlation_id) return self.request_stub.Send(req)
def test_it(self): import datetime from google.protobuf import duration_pb2 seconds = microseconds = 1 duration_pb = duration_pb2.Duration(seconds=seconds, nanos=1000 * microseconds) timedelta_val = datetime.timedelta(seconds=seconds, microseconds=microseconds) result = self._call_fut(duration_pb) self.assertIsInstance(result, datetime.timedelta) self.assertEqual(result, timedelta_val)
def test_create_test_completed_event(self, succeeded_count, failed_count, conditions, expected_status): job = _job_from_dict({ 'metadata': { 'name': 'job-name', 'namespace': 'namespace', 'labels': { 'benchmarkId': 'test-job', }, }, 'status': { 'startTime': _START_TIME, 'succeeded': succeeded_count, 'failed': failed_count, 'conditions': [ { 'status': True, 'reason': reason, 'type': cond_type, 'lastTransitionTime': _END_TIME, } for cond_type, reason in conditions ] } }) actual_event = event_publisher.create_test_completed_event( job, model_output_bucket='gs://fake-bucket', cluster_name='cluster-name', cluster_location='cluster-location', project='project-id' ) start_time = timestamp_pb2.Timestamp() start_time.FromDatetime(_START_TIME) duration = duration_pb2.Duration() duration.FromTimedelta(_END_TIME - _START_TIME) expected_event = metrics_pb2.TestCompletedEvent( benchmark_id='test-job', output_path='gs://fake-bucket/job-name', status=metrics_pb2.TestCompletedEvent.TestStatus.Value(expected_status), num_attempts=succeeded_count + failed_count, start_time=start_time, duration=duration, labels={'benchmarkId': 'test-job'}, debug_info=metrics_pb2.DebugInfo( logs_link='https://console.cloud.google.com/logs?project=project-id&advancedFilter=resource.type%3Dk8s_container%0Aresource.labels.project_id%3Dproject-id%0Aresource.labels.cluster_name%3Dcluster-name%0Aresource.labels.namespace_name%3Dnamespace%0Aresource.labels.pod_name%3Ajob-name%0Aresource.labels.location%3Acluster-location%0A', details_link=f'https://console.cloud.google.com/kubernetes/job/cluster-location/cluster-name/namespace/job-name?project=project-id' ), metric_collection_config=metrics_pb2.MetricCollectionConfig(), ) self.assertProtoEqual(expected_event, actual_event)
def create_key_hsm(project_id, location_id, key_ring_id, id): """ Creates a new key in Cloud KMS backed by Cloud HSM. Args: project_id (string): Google Cloud project ID (e.g. 'my-project'). location_id (string): Cloud KMS location (e.g. 'us-east1'). key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring'). id (string): ID of the key to create (e.g. 'my-hsm-key'). Returns: CryptoKey: Cloud KMS key. """ # Import the client library. from google.cloud import kms from google.protobuf import duration_pb2 import datetime # Create the client. client = kms.KeyManagementServiceClient() # Build the parent key ring name. key_ring_name = client.key_ring_path(project_id, location_id, key_ring_id) # Build the key. purpose = kms.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT algorithm = kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION protection_level = kms.ProtectionLevel.HSM key = { 'purpose': purpose, 'version_template': { 'algorithm': algorithm, 'protection_level': protection_level }, # Optional: customize how long key versions should be kept before # destroying. 'destroy_scheduled_duration': duration_pb2.Duration().FromTimedelta(datetime.timedelta(days=1)) } # Call the API. created_key = client.create_crypto_key(request={ 'parent': key_ring_name, 'crypto_key_id': id, 'crypto_key': key }) print('Created hsm key: {}'.format(created_key.name)) return created_key
def create_certificate_csr( project_id: str, location: str, ca_pool_name: str, ca_name: str, certificate_name: str, certificate_lifetime: int, pem_csr: str, ) -> None: """ Create a Certificate which is issued by the specified Certificate Authority (CA). The certificate details and the public key is provided as a Certificate Signing Request (CSR). Args: project_id: project ID or project number of the Cloud project you want to use. location: location you want to use. For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations. ca_pool_name: set a unique name for the CA pool. ca_name: the name of the certificate authority to sign the CSR. certificate_name: set a unique name for the certificate. certificate_lifetime: the validity of the certificate in seconds. pem_csr: set the Certificate Issuing Request in the pem encoded format. """ ca_service_client = privateca_v1.CertificateAuthorityServiceClient() # The public key used to sign the certificate can be generated using any crypto library/framework. # Also you can use Cloud KMS to retrieve an already created public key. # For more info, see: https://cloud.google.com/kms/docs/retrieve-public-key. # Create certificate with CSR. # The pem_csr contains the public key and the domain details required. certificate = privateca_v1.Certificate( pem_csr=pem_csr, lifetime=duration_pb2.Duration(seconds=certificate_lifetime), ) # Create the Certificate Request. # Set the CA which is responsible for creating the certificate with the provided CSR. request = privateca_v1.CreateCertificateRequest( parent=ca_service_client.ca_pool_path(project_id, location, ca_pool_name), certificate_id=certificate_name, certificate=certificate, issuing_certificate_authority_id=ca_name, ) response = ca_service_client.create_certificate(request=request) print(f"Certificate created successfully: {response.name}") # Get the signed certificate and the issuer chain list. print(f"Signed certificate: {response.pem_certificate}") print(f"Issuer chain list: {response.pem_certificate_chain}")
def test_create_scc_params_durations(mocker, mock_datetime, duration): # given client = scc.Client() expected = { 'query' : 'attribute.asset_type = "PROJECT"', 'compare_duration' : duration_pb2.Duration(seconds=24192000) } # when with mocker.patch('client.helpers.now', return_value=mock_datetime): result = client.create_scc_params( where='attribute.asset_type = "PROJECT"', duration=duration) # then assert expected == result, 'The result should be equals: {}'.format(str(expected))
def delayed_bundle_application(self, op, deferred_remainder): # TODO(SDF): For non-root nodes, need main_input_coder + residual_coder. ((element_and_restriction, output_watermark), deferred_watermark) = deferred_remainder if deferred_watermark: assert isinstance(deferred_watermark, timestamp.Duration) proto_deferred_watermark = duration_pb2.Duration() proto_deferred_watermark.FromMicroseconds(deferred_watermark.micros) else: proto_deferred_watermark = None return beam_fn_api_pb2.DelayedBundleApplication( requested_time_delay=proto_deferred_watermark, application=self.construct_bundle_application( op, output_watermark, element_and_restriction))
def create_key_asymmetric_sign(project_id, location_id, key_ring_id, id): """ Creates a new asymmetric signing key in Cloud KMS. Args: project_id (string): Google Cloud project ID (e.g. 'my-project'). location_id (string): Cloud KMS location (e.g. 'us-east1'). key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring'). id (string): ID of the key to create (e.g. 'my-asymmetric-signing-key'). Returns: CryptoKey: Cloud KMS key. """ # Import the client library. from google.cloud import kms from google.protobuf import duration_pb2 import datetime # Create the client. client = kms.KeyManagementServiceClient() # Build the parent key ring name. key_ring_name = client.key_ring_path(project_id, location_id, key_ring_id) # Build the key. purpose = kms.CryptoKey.CryptoKeyPurpose.ASYMMETRIC_SIGN algorithm = kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.RSA_SIGN_PKCS1_2048_SHA256 key = { 'purpose': purpose, 'version_template': { 'algorithm': algorithm, }, # Optional: customize how long key versions should be kept before # destroying. 'destroy_scheduled_duration': duration_pb2.Duration().FromTimedelta(datetime.timedelta(days=1)) } # Call the API. created_key = client.create_crypto_key(request={ 'parent': key_ring_name, 'crypto_key_id': id, 'crypto_key': key }) print('Created asymmetric signing key: {}'.format(created_key.name)) return created_key
def testDurationIntegerConversion(self): message = duration_pb2.Duration() message.FromNanoseconds(1) self.assertEqual('0.000000001s', message.ToJsonString()) self.assertEqual(1, message.ToNanoseconds()) message.FromNanoseconds(-1) self.assertEqual('-0.000000001s', message.ToJsonString()) self.assertEqual(-1, message.ToNanoseconds()) message.FromMicroseconds(1) self.assertEqual('0.000001s', message.ToJsonString()) self.assertEqual(1, message.ToMicroseconds()) message.FromMicroseconds(-1) self.assertEqual('-0.000001s', message.ToJsonString()) self.assertEqual(-1, message.ToMicroseconds()) message.FromMilliseconds(1) self.assertEqual('0.001s', message.ToJsonString()) self.assertEqual(1, message.ToMilliseconds()) message.FromMilliseconds(-1) self.assertEqual('-0.001s', message.ToJsonString()) self.assertEqual(-1, message.ToMilliseconds()) message.FromSeconds(1) self.assertEqual('1s', message.ToJsonString()) self.assertEqual(1, message.ToSeconds()) message.FromSeconds(-1) self.assertEqual('-1s', message.ToJsonString()) self.assertEqual(-1, message.ToSeconds()) # Test truncation behavior. message.FromNanoseconds(1999) self.assertEqual(1, message.ToMicroseconds()) # For negative values, Duration will be rounded towards 0. message.FromNanoseconds(-1999) self.assertEqual(-1, message.ToMicroseconds())
def test_create_scc_params_ref_timestamp(): # given client = scc.Client() expected = { 'query' : 'attribute.asset_type = "PROJECT"', 'compare_duration' : duration_pb2.Duration(seconds=24192000), 'reference_time' : timestamp_pb2.Timestamp(seconds=1527811200) } # when result = client.create_scc_params( where='attribute.asset_type = "PROJECT"', duration='40w', reference_time='2018-06-01T00:00:00+0000', reference_time_type='TIMESTAMP') # then assert expected == result, 'The result should be equals: {}'.format(str(expected))
def test_create_scc_params_ref_from_now(mocker, mock_datetime): # given client = scc.Client() expected = { 'query' : 'attribute.asset_type = "PROJECT"', 'compare_duration' : duration_pb2.Duration(seconds=24192000), 'reference_time' : timestamp_pb2.Timestamp(seconds=1527807600) } # when with mocker.patch('client.helpers.now', return_value=mock_datetime): result = client.create_scc_params( where='attribute.asset_type = "PROJECT"', duration='40w', reference_time='1h', reference_time_type='FROM_NOW') # then assert expected == result, 'The result should be equals: {}'.format(str(expected))
def _timedelta_to_duration_pb(timedelta_val): """Convert a Python timedelta object to a duration protobuf. .. note:: The Python timedelta has a granularity of microseconds while the protobuf duration type has a duration of nanoseconds. :type timedelta_val: :class:`datetime.timedelta` :param timedelta_val: A timedelta object. :rtype: :class:`google.protobuf.duration_pb2.Duration` :returns: A duration object equivalent to the time delta. """ duration_pb = duration_pb2.Duration() duration_pb.FromTimedelta(timedelta_val) return duration_pb
def add_to_queue( client, dataset, table, limit, project, queue, location, url, service_account_email, in_seconds=None, task_name=None, ): payload = dict(dataset=dataset, table=table, limit=None) # Construct the fully qualified queue name. parent = client.queue_path(project, location, queue) # Construct the request body. task = { "http_request": { # Specify the type of request. "http_method": tasks_v2.HttpMethod.POST, "url": url, "oidc_token": {"service_account_email": service_account_email}, }, "dispatch_deadline": duration_pb2.Duration( seconds=60 * 30 ), # 30m is maximum :/ # "name": f"{dataset}-{table}", } if payload is not None: # The API expects a payload of type bytes. converted_payload = json.dumps(payload).encode() # Add the payload to the request. task["http_request"]["body"] = converted_payload task["http_request"]["headers"] = {"Content-type": "application/json"} # Use the client to build and send the task. response = client.create_task(request={"parent": parent, "task": task}) print("Created task {}".format(response.name)) print(f"Dataset: {dataset} Table: {table}")
def _collect_and_upload_profile(self, profile): """Collects a profile and uploads to the profiler server.""" try: profile_type = profile['profileType'] if profile_type not in self._profilers: logger.warning('Unexpected profile type: %s', profile_type) return duration = duration_pb2.Duration() profile_duration = profile['duration'] json_format.Parse(json.dumps(profile_duration), duration) duration_ns = duration.seconds * _NANOS_PER_SEC + duration.nanos profile_bytes = self._profilers[profile_type].profile(duration_ns) profile['profileBytes'] = base64.b64encode(profile_bytes).decode('UTF-8') logger.debug('Starting to upload profile') self._profiler_service.patch(name=profile['name'], body=profile).execute() except BaseException as e: logger.error('Failed to collect and upload profile: %s', str(e))
def main(args): # the host where collectd-grpc is running #host = args[1] if args else 'localhost' host = "where.collectd-grpc.is.running" #host = "localhost" # Change port with the port collectd-grpc is running on GRPC_PORT = 1234 channel = grpc.insecure_channel('{}:{}'.format(host, GRPC_PORT)) stub = collectd_pb2.CollectdStub(channel) # stub.QueryValues(collectd_pb2.QueryValuesRequest()) time1 = timestamp_pb2.Timestamp() time1.GetCurrentTime() time.sleep(SLEEP) # Dummy metric that just increments from 0 to NB_ITERATION for metric in range(NB_ITERATION): time2 = timestamp_pb2.Timestamp() time2.GetCurrentTime() duration = duration_pb2.Duration() duration.seconds = time2.seconds - time1.seconds duration.nanos = time2.nanos - time1.nanos data_metric = types_pb2.ValueList( values=[types_pb2.Value(gauge=metric)], time=time2, interval=duration, identifier=types_pb2.Identifier(host='collectd', plugin='grpc', plugin_instance='grpc1', type='gauge', type_instance='metric_name')) payload = [collectd_pb2.PutValuesRequest(value_list=data_metric)] time1 = timestamp_pb2.Timestamp() time1.GetCurrentTime() time.sleep(SLEEP) def _get_iterator(): for data in payload: yield data data_iterator = _get_iterator() stub.PutValues(data_iterator)
def test_to_pb(self): import datetime from google.protobuf import duration_pb2 from google.cloud.bigtable.column_family import MaxAgeGCRule from google.cloud.bigtable.column_family import MaxVersionsGCRule max_num_versions = 42 rule1 = MaxVersionsGCRule(max_num_versions) pb_rule1 = _GcRulePB(max_num_versions=max_num_versions) max_age = datetime.timedelta(seconds=1) rule2 = MaxAgeGCRule(max_age) pb_rule2 = _GcRulePB(max_age=duration_pb2.Duration(seconds=1)) rule3 = self._make_one(rules=[rule1, rule2]) pb_rule3 = _GcRulePB(union=_GcRuleUnionPB(rules=[pb_rule1, pb_rule2])) gc_rule_pb = rule3.to_pb() self.assertEqual(gc_rule_pb, pb_rule3)
def testInvalidDuration(self): message = duration_pb2.Duration() self.assertRaisesRegexp( ValueError, 'Duration must end with letter "s": 1.', message.FromJsonString, '1') self.assertRaisesRegexp( ValueError, 'Couldn\'t parse duration: 1...2s.', message.FromJsonString, '1...2s') text = '-315576000001.000000000s' self.assertRaisesRegexp( ValueError, r'Duration is not valid\: Seconds -315576000001 must be in range' r' \[-315576000000\, 315576000000\].', message.FromJsonString, text) text = '315576000001.000000000s' self.assertRaisesRegexp( ValueError, r'Duration is not valid\: Seconds 315576000001 must be in range' r' \[-315576000000\, 315576000000\].', message.FromJsonString, text) message.seconds = -315576000001 message.nanos = 0 self.assertRaisesRegexp( ValueError, r'Duration is not valid\: Seconds -315576000001 must be in range' r' \[-315576000000\, 315576000000\].', message.ToJsonString) message.seconds = 0 message.nanos = 999999999 + 1 self.assertRaisesRegexp( ValueError, r'Duration is not valid\: Nanos 1000000000 must be in range' r' \[-999999999\, 999999999\].', message.ToJsonString) message.seconds = -1 message.nanos = 1 self.assertRaisesRegexp( ValueError, r'Duration is not valid\: Sign mismatch.', message.ToJsonString)
def test_gc_rule_intersection_to_pb(): import datetime from google.protobuf import duration_pb2 from google.cloud.bigtable.column_family import MaxAgeGCRule from google.cloud.bigtable.column_family import MaxVersionsGCRule max_num_versions = 42 rule1 = MaxVersionsGCRule(max_num_versions) pb_rule1 = _GcRulePB(max_num_versions=max_num_versions) max_age = datetime.timedelta(seconds=1) rule2 = MaxAgeGCRule(max_age) pb_rule2 = _GcRulePB(max_age=duration_pb2.Duration(seconds=1)) rule3 = _make_gc_rule_intersection(rules=[rule1, rule2]) pb_rule3 = _GcRulePB(intersection=_GcRuleIntersectionPB( rules=[pb_rule1, pb_rule2])) gc_rule_pb = rule3.to_pb() assert gc_rule_pb == pb_rule3
def PermittedActions(self, request, _context): """Returns a set of permitted actions for the requested resources.""" logging.debug('Received request from %s for: %s', auth.get_current_identity(), request) if request.resource_kind != 'bucket': return access_pb2.PermittedActionsResponse() bucket_ids = dict( utils.async_apply(request.resource_ids, api_common.to_bucket_id_async)) roles = dict( utils.async_apply(bucket_ids.itervalues(), user.get_role_async)) permitted = { rid: create_resource_permissions(roles[bucket_ids[rid]]) for rid in request.resource_ids } logging.debug('Permitted: %s', permitted) return access_pb2.PermittedActionsResponse( permitted=permitted, validity_duration=duration_pb2.Duration(seconds=10), )
def create_scc_params(where=None, duration=None, reference_time=None, reference_time_type=None, timeout=None): """Create params need to make a query on SCC""" query_args = {} if where: query_args['query'] = where if duration: query_args['compare_duration'] = duration_pb2.Duration( seconds=duration_to_seconds(duration)) if reference_time: if reference_time_type == 'TIMESTAMP': query_args['reference_time'] = timestamp_pb2.Timestamp( seconds=fixed_timestamp_to_seconds(reference_time)) if reference_time_type == 'FROM_NOW': query_args['reference_time'] = timestamp_pb2.Timestamp( seconds=from_now_to_seconds(reference_time)) if timeout: query_args['timeout'] = int(timeout) return query_args
def test_to_pb(self): import datetime from google.protobuf import duration_pb2 from gcloud.bigtable._generated import (bigtable_table_data_pb2 as data_pb2) from gcloud.bigtable.column_family import MaxAgeGCRule from gcloud.bigtable.column_family import MaxVersionsGCRule max_num_versions = 42 rule1 = MaxVersionsGCRule(max_num_versions) pb_rule1 = data_pb2.GcRule(max_num_versions=max_num_versions) max_age = datetime.timedelta(seconds=1) rule2 = MaxAgeGCRule(max_age) pb_rule2 = data_pb2.GcRule(max_age=duration_pb2.Duration(seconds=1)) rule3 = self._makeOne(rules=[rule1, rule2]) pb_rule3 = data_pb2.GcRule(intersection=data_pb2.GcRule.Intersection( rules=[pb_rule1, pb_rule2])) gc_rule_pb = rule3.to_pb() self.assertEqual(gc_rule_pb, pb_rule3)