@Override SinkInfo parse(String... args) throws Exception { if (args.length >= 3) { if (args.length > 4) { throw new IllegalArgumentException("Too many arguments."); } String name = args[0]; Destination destination; switch (args[1]) { case "bucket": destination = BucketDestination.of(args[2]); break; case "dataset": destination = DatasetDestination.of(args[2]); break; case "topic": destination = TopicDestination.of(args[2]); break; default: throw new IllegalArgumentException("Second argument must be bucket|dataset|topic."); } SinkInfo.Builder builder = SinkInfo.newBuilder(name, destination); if (args.length == 4) { builder.setFilter(args[3]); } return builder.build(); } throw new IllegalArgumentException("Missing required sink name, destination or filter."); }
@Test public void testReload() { initializeExpectedSink(2); SinkInfo updatedInfo = SINK_INFO.toBuilder().setFilter(NEW_FILTER).build(); Sink expectedSink = new Sink(serviceMockReturnsOptions, new SinkInfo.BuilderImpl(updatedInfo)); expect(logging.getOptions()).andReturn(mockOptions); expect(logging.getSink(NAME)).andReturn(expectedSink); replay(logging); initializeSink(); Sink updatedSink = sink.reload(); compareSink(expectedSink, updatedSink); }
@Test public void testReloadAsync() throws ExecutionException, InterruptedException { initializeExpectedSink(2); SinkInfo updatedInfo = SINK_INFO.toBuilder().setFilter(NEW_FILTER).build(); Sink expectedSink = new Sink(serviceMockReturnsOptions, new SinkInfo.BuilderImpl(updatedInfo)); expect(logging.getOptions()).andReturn(mockOptions); expect(logging.getSinkAsync(NAME)).andReturn(ApiFutures.immediateFuture(expectedSink)); replay(logging); initializeSink(); Sink updatedSink = sink.reloadAsync().get(); compareSink(expectedSink, updatedSink); }
static SinkInfo fromPb(LogSink sinkPb) { Builder builder = newBuilder(sinkPb.getName(), Destination.fromPb(sinkPb.getDestination())) .setVersionFormat(VersionFormat.fromPb(sinkPb.getOutputVersionFormat())); if (!sinkPb.getFilter().equals("")) { builder.setFilter(sinkPb.getFilter()); } return builder.build(); } }
@Test public void testToBuilder() { compareSinkInfo(BUCKET_SINK_INFO, BUCKET_SINK_INFO.toBuilder().build()); compareSinkInfo(DATASET_SINK_INFO, DATASET_SINK_INFO.toBuilder().build()); compareSinkInfo(TOPIC_SINK_INFO, TOPIC_SINK_INFO.toBuilder().build()); SinkInfo updatedSinkInfo = BUCKET_SINK_INFO .toBuilder() .setDestination(TOPIC_DESTINATION) .setName("newName") .setFilter("logName=projects/my-projectid/logs/syslog") .setVersionFormat(VersionFormat.V2) .build(); assertEquals("newName", updatedSinkInfo.getName()); assertEquals(TOPIC_DESTINATION, updatedSinkInfo.getDestination()); assertEquals("logName=projects/my-projectid/logs/syslog", updatedSinkInfo.getFilter()); assertEquals(VersionFormat.V2, updatedSinkInfo.getVersionFormat()); updatedSinkInfo = BUCKET_SINK_INFO .toBuilder() .setDestination(BUCKET_DESTINATION) .setName(NAME) .setFilter(FILTER) .setVersionFormat(VersionFormat.V1) .build(); assertEquals(BUCKET_SINK_INFO, updatedSinkInfo); }
@Test public void testCreateGetUpdateAndDeleteSink() { String name = formatForTest("test-create-get-update-sink"); SinkInfo sinkInfo = SinkInfo.newBuilder(name, DatasetDestination.of("dataset")) .setFilter("severity>=ERROR") .setVersionFormat(SinkInfo.VersionFormat.V2) .build(); Sink sink = logging().create(sinkInfo); assertEquals(name, sink.getName()); assertEquals(SinkInfo.VersionFormat.V2, sink.getVersionFormat()); assertEquals("severity>=ERROR", sink.getFilter()); DatasetDestination datasetDestination = sink.getDestination(); assertEquals(logging().getOptions().getProjectId(), datasetDestination.getProject()); assertEquals("dataset", datasetDestination.getDataset()); assertEquals(sink, logging().getSink(name)); sink = sink.toBuilder().setFilter("severity<=ERROR").build().update(); assertEquals(name, sink.getName()); assertEquals(SinkInfo.VersionFormat.V2, sink.getVersionFormat()); assertEquals("severity<=ERROR", sink.getFilter()); assertTrue(sink.delete()); assertFalse(sink.delete()); }
/** Example of asynchronously updating a sink. */ // [TARGET updateAsync(SinkInfo)] // [VARIABLE "my_sink_name"] // [VARIABLE "my_dataset"] public Sink updateSinkAsync(String sinkName, String datasetName) throws ExecutionException, InterruptedException { // [START updateSinkAsync] SinkInfo sinkInfo = SinkInfo.newBuilder(sinkName, DatasetDestination.of(datasetName)) .setVersionFormat(SinkInfo.VersionFormat.V2) .setFilter("severity>=ERROR") .build(); Future<Sink> future = logging.updateAsync(sinkInfo); // ... Sink sink = future.get(); // [END updateSinkAsync] return sink; }
public static void main(String... args) throws Exception { // Create a service object // Credentials are inferred from the environment try (Logging logging = LoggingOptions.getDefaultInstance().getService()) { // Create a sink to back log entries to a BigQuery dataset SinkInfo sinkInfo = SinkInfo.newBuilder("test-sink", DatasetDestination.of("test-dataset")) .setFilter("severity >= ERROR") .build(); logging.create(sinkInfo); // List sinks Page<Sink> sinks = logging.listSinks(); for (Sink sink : sinks.iterateAll()) { System.out.println(sink); } } } }
/** Example of updating a sink. */ // [TARGET update(SinkInfo)] // [VARIABLE "my_sink_name"] // [VARIABLE "my_dataset"] public Sink updateSink(String sinkName, String datasetName) { // [START logging_update_sink] SinkInfo sinkInfo = SinkInfo.newBuilder(sinkName, DatasetDestination.of(datasetName)) .setVersionFormat(SinkInfo.VersionFormat.V2) .setFilter("severity>=ERROR") .build(); Sink sink = logging.update(sinkInfo); // [END logging_update_sink] return sink; }
@Test public void testUpdateNonExistingSink() { String name = formatForTest("test-update-non-existing-sink"); SinkInfo sinkInfo = SinkInfo.newBuilder(name, DatasetDestination.of("dataset")) .setFilter("severity>=ERROR") .setVersionFormat(SinkInfo.VersionFormat.V2) .build(); assertNull(logging().getSink(name)); thrown.expect(LoggingException.class); thrown.expectMessage("NOT_FOUND"); logging().update(sinkInfo); }
@Test public void testUpdate() { initializeExpectedSink(2); SinkInfo updatedInfo = SINK_INFO.toBuilder().setFilter(NEW_FILTER).build(); Sink expectedSink = new Sink(serviceMockReturnsOptions, new SinkInfo.BuilderImpl(updatedInfo)); expect(logging.getOptions()).andReturn(mockOptions).times(2); expect(logging.update(expectedSink)).andReturn(expectedSink); replay(logging); initializeSink(); Sink updatedSink = sink.toBuilder().setFilter(NEW_FILTER).build().update(); compareSink(expectedSink, updatedSink); }
static SinkInfo fromPb(LogSink sinkPb) { Builder builder = newBuilder(sinkPb.getName(), Destination.fromPb(sinkPb.getDestination())) .setVersionFormat(VersionFormat.fromPb(sinkPb.getOutputVersionFormat())); if (!sinkPb.getFilter().equals("")) { builder.setFilter(sinkPb.getFilter()); } return builder.build(); } }
@Test public void testUpdateAsync() throws ExecutionException, InterruptedException { initializeExpectedSink(2); SinkInfo updatedInfo = SINK_INFO.toBuilder().setFilter(NEW_FILTER).build(); Sink expectedSink = new Sink(serviceMockReturnsOptions, new SinkInfo.BuilderImpl(updatedInfo)); expect(logging.getOptions()).andReturn(mockOptions).times(2); expect(logging.updateAsync(expectedSink)).andReturn(ApiFutures.immediateFuture(expectedSink)); replay(logging); initializeSink(); Sink updatedSink = sink.toBuilder().setFilter(NEW_FILTER).build().updateAsync().get(); compareSink(expectedSink, updatedSink); }