def test_export_assets(self): # Setup Expected Response expected_response = {} expected_response = asset_service_pb2.ExportAssetsResponse( **expected_response) operation = operations_pb2.Operation( name='operations/test_export_assets', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = asset_v1beta1.AssetServiceClient(channel=channel) # Setup Request parent = client.project_path('[PROJECT]') output_config = {} response = client.export_assets(parent, output_config) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = asset_service_pb2.ExportAssetsRequest( parent=parent, output_config=output_config) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_batch_get_assets_history(self): # Setup Expected Response expected_response = {} expected_response = asset_service_pb2.BatchGetAssetsHistoryResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = asset_v1beta1.AssetServiceClient(channel=channel) # Setup Request parent = client.project_path('[PROJECT]') content_type = enums.ContentType.CONTENT_TYPE_UNSPECIFIED read_time_window = {} response = client.batch_get_assets_history(parent, content_type, read_time_window) assert expected_response == response assert len(channel.requests) == 1 expected_request = asset_service_pb2.BatchGetAssetsHistoryRequest( parent=parent, content_type=content_type, read_time_window=read_time_window) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_batch_get_assets_history(self): # Setup Expected Response expected_response = {} expected_response = asset_service_pb2.BatchGetAssetsHistoryResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = asset_v1beta1.AssetServiceClient() # Setup Request parent = client.project_path("[PROJECT]") content_type = enums.ContentType.CONTENT_TYPE_UNSPECIFIED read_time_window = {} response = client.batch_get_assets_history(parent, content_type, read_time_window) assert expected_response == response assert len(channel.requests) == 1 expected_request = asset_service_pb2.BatchGetAssetsHistoryRequest( parent=parent, content_type=content_type, read_time_window=read_time_window) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_export_assets(self): # Setup Expected Response expected_response = {} expected_response = asset_service_pb2.ExportAssetsResponse( **expected_response) operation = operations_pb2.Operation( name="operations/test_export_assets", done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = asset_v1beta1.AssetServiceClient() # Setup Request parent = client.project_path("[PROJECT]") output_config = {} response = client.export_assets(parent, output_config) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = asset_service_pb2.ExportAssetsRequest( parent=parent, output_config=output_config) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_batch_get_assets_history_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = asset_v1beta1.AssetServiceClient(channel=channel) # Setup request parent = client.project_path('[PROJECT]') content_type = enums.ContentType.CONTENT_TYPE_UNSPECIFIED read_time_window = {} with pytest.raises(CustomException): client.batch_get_assets_history(parent, content_type, read_time_window)
def export_assets(project_id, dump_file_path): # [START asset_quickstart_exportassets] from google.cloud import asset_v1beta1 from google.cloud.asset_v1beta1.proto import asset_service_pb2 # TODO project_id = 'Your Google Cloud Project ID' # TODO dump_file_path = 'Your asset dump file path' client = asset_v1beta1.AssetServiceClient() parent = client.project_path(project_id) output_config = asset_service_pb2.OutputConfig() output_config.gcs_destination.uri = dump_file_path response = client.export_assets(parent, output_config) print(response.result())
def main(request): request_json = request.get_json() if 'key' not in request.args or request.args.get('key') != KEY: return "Unauthorized export" if request.args and 'fileName' in request.args: project_id = request.args.get('fileName') dump_file_path = DESTINATION_PATH client = asset_v1beta1.AssetServiceClient() parent = "organizations/" + ORGANIZATION_ID output_config = asset_service_pb2.OutputConfig() output_config.gcs_destination.uri = dump_file_path response = client.export_assets(parent, output_config) return "Export requestsuccess" else: raise ValueError("Please specify a fileName")
def test_batch_get_assets_history_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = asset_v1beta1.AssetServiceClient() # Setup request parent = client.project_path("[PROJECT]") content_type = enums.ContentType.CONTENT_TYPE_UNSPECIFIED read_time_window = {} with pytest.raises(CustomException): client.batch_get_assets_history(parent, content_type, read_time_window)
def test_export_assets_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_export_assets_exception', done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) client = asset_v1beta1.AssetServiceClient(channel=channel) # Setup Request parent = client.project_path('[PROJECT]') output_config = {} response = client.export_assets(parent, output_config) exception = response.exception() assert exception.errors[0] == error
def batch_get_assets_history(project_id, asset_names): # [START asset_quickstart_batch_get_assets_history] from google.cloud import asset_v1beta1 from google.cloud.asset_v1beta1.proto import assets_pb2 from google.cloud.asset_v1beta1 import enums # TODO project_id = 'Your Google Cloud Project ID' # TODO asset_names = 'Your asset names list, e.g.: # ["//storage.googleapis.com/[BUCKET_NAME]",]' client = asset_v1beta1.AssetServiceClient() parent = client.project_path(project_id) content_type = enums.ContentType.RESOURCE read_time_window = assets_pb2.TimeWindow() read_time_window.start_time.GetCurrentTime() response = client.batch_get_assets_history(parent, content_type, read_time_window, asset_names) print('assets: {}'.format(response.assets))
def main(request): request_json = request.get_json() if 'key' not in request.args or request.args.get('key') != KEY: return "Unauthorized export" client = asset_v1beta1.AssetServiceClient() parent = "organizations/" + ORGANIZATION_ID output_config = asset_service_pb2.OutputConfig() output_config.gcs_destination.uri = DESTINATION_PATH response = client.export_assets(parent, output_config, None, None, "IAM_POLICY") logging.info(response.result()) logging.info("isdone:") logging.info(response.done()) logging.info("errors:") try: logging.info(response.error()) except: logging.info("no errors") return "Export request success"
def test_export_assets_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_export_assets_exception", done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = asset_v1beta1.AssetServiceClient() # Setup Request parent = client.project_path("[PROJECT]") output_config = {} response = client.export_assets(parent, output_config) exception = response.exception() assert exception.errors[0] == error
def cloudasset(cls): if cls._cloudasset: return cls._cloudasset cls._cloudasset = asset_v1beta1.AssetServiceClient() return cls._cloudasset