From 5abb3aded8301839b5dd223ffb56621272f6decd Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 4 Jul 2016 18:58:02 +0200 Subject: [PATCH] Implement MonitoredResourceDescriptor methods and tests --- .../com/google/cloud/logging/Logging.java | 20 ++ .../com/google/cloud/logging/LoggingImpl.java | 70 ++++- .../google/cloud/logging/LoggingImplTest.java | 245 ++++++++++++++++++ 3 files changed, 334 insertions(+), 1 deletion(-) diff --git a/gcloud-java-logging/src/main/java/com/google/cloud/logging/Logging.java b/gcloud-java-logging/src/main/java/com/google/cloud/logging/Logging.java index 64d42f280ad9..36419676f146 100644 --- a/gcloud-java-logging/src/main/java/com/google/cloud/logging/Logging.java +++ b/gcloud-java-logging/src/main/java/com/google/cloud/logging/Logging.java @@ -17,6 +17,7 @@ package com.google.cloud.logging; import com.google.cloud.AsyncPage; +import com.google.cloud.MonitoredResourceDescriptor; import com.google.cloud.Page; import com.google.cloud.Service; @@ -136,6 +137,25 @@ public static ListOption pageToken(String pageToken) { */ Future deleteSinkAsync(String sink); + /** + * Lists the monitored resource descriptors used by Google Cloud Logging. This method returns a + * {@link Page} object that can be used to consume paginated results. Use {@link ListOption} to + * specify the page size or the page token from which to start listing resource descriptors. + * + * @throws LoggingException upon failure + */ + Page listMonitoredResourceDescriptors(ListOption... options); + + /** + * Sends a request for listing monitored resource descriptors used by Google Cloud Logging. This + * method returns a {@code Future} object to consume the result. {@link Future#get()} returns an + * {@link AsyncPage} object that can be used to asynchronously handle paginated results. Use + * {@link ListOption} to specify the page size or the page token from which to start listing + * resource descriptors. + */ + Future> listMonitoredResourceDescriptorsAsync( + ListOption... options); + /** * Creates a new metric. * diff --git a/gcloud-java-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java b/gcloud-java-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java index d7839d4afd13..f1afd64a176d 100644 --- a/gcloud-java-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java +++ b/gcloud-java-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java @@ -16,14 +16,15 @@ package com.google.cloud.logging; +import static com.google.api.client.util.Preconditions.checkArgument; import static com.google.cloud.logging.Logging.ListOption.OptionType.PAGE_SIZE; import static com.google.cloud.logging.Logging.ListOption.OptionType.PAGE_TOKEN; -import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.util.concurrent.Futures.lazyTransform; import com.google.cloud.AsyncPage; import com.google.cloud.AsyncPageImpl; import com.google.cloud.BaseService; +import com.google.cloud.MonitoredResourceDescriptor; import com.google.cloud.Page; import com.google.cloud.PageImpl; import com.google.cloud.logging.spi.LoggingRpc; @@ -43,6 +44,8 @@ import com.google.logging.v2.GetSinkRequest; import com.google.logging.v2.ListLogMetricsRequest; import com.google.logging.v2.ListLogMetricsResponse; +import com.google.logging.v2.ListMonitoredResourceDescriptorsRequest; +import com.google.logging.v2.ListMonitoredResourceDescriptorsResponse; import com.google.logging.v2.ListSinksRequest; import com.google.logging.v2.ListSinksResponse; import com.google.logging.v2.UpdateLogMetricRequest; @@ -118,6 +121,22 @@ public Future> nextPage() { } } + private static class MonitoredResourceDescriptorPageFetcher + extends BasePageFetcher { + + private static final long serialVersionUID = -2346495771766629195L; + + MonitoredResourceDescriptorPageFetcher(LoggingOptions serviceOptions, String cursor, + Map requestOptions) { + super(serviceOptions, cursor, requestOptions); + } + + @Override + public Future> nextPage() { + return listMonitoredResourceDescriptorsAsync(serviceOptions(), requestOptions()); + } + } + private static class MetricPageFetcher extends BasePageFetcher { private static final long serialVersionUID = -316783549651771553L; @@ -230,6 +249,55 @@ public Future deleteSinkAsync(String sink) { return lazyTransform(rpc.delete(request), EMPTY_TO_BOOLEAN_FUNCTION); } + private static ListMonitoredResourceDescriptorsRequest listMonitoredResourceDescriptorsRequest( + Map options) { + ListMonitoredResourceDescriptorsRequest.Builder builder = + ListMonitoredResourceDescriptorsRequest.newBuilder(); + Integer pageSize = PAGE_SIZE.get(options); + String pageToken = PAGE_TOKEN.get(options); + if (pageSize != null) { + builder.setPageSize(pageSize); + } + if (pageToken != null) { + builder.setPageToken(pageToken); + } + return builder.build(); + } + + private static Future> + listMonitoredResourceDescriptorsAsync(final LoggingOptions serviceOptions, + final Map options) { + final ListMonitoredResourceDescriptorsRequest request = + listMonitoredResourceDescriptorsRequest(options); + Future list = serviceOptions.rpc().list(request); + return lazyTransform(list, new Function>() { + @Override + public AsyncPage apply( + ListMonitoredResourceDescriptorsResponse listDescriptorsResponse) { + List descriptors = + listDescriptorsResponse.getResourceDescriptorsList() == null + ? ImmutableList.of() + : Lists.transform(listDescriptorsResponse.getResourceDescriptorsList(), + MonitoredResourceDescriptor.FROM_PB_FUNCTION); + String cursor = listDescriptorsResponse.getNextPageToken().equals("") ? null + : listDescriptorsResponse.getNextPageToken(); + return new AsyncPageImpl<>( + new MonitoredResourceDescriptorPageFetcher(serviceOptions, cursor, options), cursor, + descriptors); + } + }); + } + + public Page listMonitoredResourceDescriptors(ListOption... options) { + return get(listMonitoredResourceDescriptorsAsync(options)); + } + + public Future> listMonitoredResourceDescriptorsAsync( + ListOption... options) { + return listMonitoredResourceDescriptorsAsync(options(), optionMap(options)); + } + @Override public Metric create(MetricInfo metric) { return get(createAsync(metric)); diff --git a/gcloud-java-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java b/gcloud-java-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java index 87dd9b41ef2e..6d768e61bd67 100644 --- a/gcloud-java-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java +++ b/gcloud-java-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue; import com.google.cloud.AsyncPage; +import com.google.cloud.MonitoredResourceDescriptor; import com.google.cloud.Page; import com.google.cloud.RetryParams; import com.google.cloud.logging.Logging.ListOption; @@ -43,6 +44,8 @@ import com.google.logging.v2.GetSinkRequest; import com.google.logging.v2.ListLogMetricsRequest; import com.google.logging.v2.ListLogMetricsResponse; +import com.google.logging.v2.ListMonitoredResourceDescriptorsRequest; +import com.google.logging.v2.ListMonitoredResourceDescriptorsResponse; import com.google.logging.v2.ListSinksRequest; import com.google.logging.v2.ListSinksResponse; import com.google.logging.v2.LogMetric; @@ -77,6 +80,10 @@ public class LoggingImplTest { private static final MetricInfo METRIC_INFO = MetricInfo.builder(METRIC_NAME, FILTER) .description(DESCRIPTION) .build(); + private static final com.google.api.MonitoredResourceDescriptor DESCRIPTOR_PB = + com.google.api.MonitoredResourceDescriptor.getDefaultInstance(); + private static final MonitoredResourceDescriptor DESCRIPTOR = + MonitoredResourceDescriptor.fromPb(DESCRIPTOR_PB); private static final Function SINK_TO_PB_FUNCTION = new Function() { @Override @@ -91,6 +98,15 @@ public LogMetric apply(MetricInfo metricInfo) { return metricInfo.toPb(); } }; + private static final Function DESCRIPTOR_TO_PB_FUNCTION = + new Function() { + @Override + public com.google.api.MonitoredResourceDescriptor apply( + MonitoredResourceDescriptor descriptor) { + return descriptor.toPb(); + } + }; private LoggingOptions options; private LoggingRpcFactory rpcFactoryMock; @@ -835,4 +851,233 @@ public void testListMetricsWithOptionsAsync() throws ExecutionException, Interru assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.values(), Metric.class)); } + + @Test + public void testListResourceDescriptor() { + String cursor = "cursor"; + EasyMock.replay(rpcFactoryMock); + logging = options.service(); + ListMonitoredResourceDescriptorsRequest request = + ListMonitoredResourceDescriptorsRequest.newBuilder().build(); + List descriptorList = ImmutableList.of(DESCRIPTOR, DESCRIPTOR); + ListMonitoredResourceDescriptorsResponse response = + ListMonitoredResourceDescriptorsResponse.newBuilder() + .setNextPageToken(cursor) + .addAllResourceDescriptors(Lists.transform(descriptorList, DESCRIPTOR_TO_PB_FUNCTION)) + .build(); + Future futureResponse = + Futures.immediateFuture(response); + EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); + EasyMock.replay(loggingRpcMock); + Page page = logging.listMonitoredResourceDescriptors(); + assertEquals(cursor, page.nextPageCursor()); + assertArrayEquals(descriptorList.toArray(), + Iterables.toArray(page.values(), MonitoredResourceDescriptor.class)); + } + + @Test + public void testListResourceDescriptorNextPage() { + String cursor1 = "cursor"; + EasyMock.replay(rpcFactoryMock); + logging = options.service(); + ListMonitoredResourceDescriptorsRequest request1 = + ListMonitoredResourceDescriptorsRequest.newBuilder().build(); + ListMonitoredResourceDescriptorsRequest request2 = + ListMonitoredResourceDescriptorsRequest.newBuilder().setPageToken(cursor1).build(); + List descriptorList1 = ImmutableList.of(DESCRIPTOR, DESCRIPTOR); + List descriptorList2 = ImmutableList.of(DESCRIPTOR); + ListMonitoredResourceDescriptorsResponse response1 = + ListMonitoredResourceDescriptorsResponse.newBuilder() + .setNextPageToken(cursor1) + .addAllResourceDescriptors(Lists.transform(descriptorList1, DESCRIPTOR_TO_PB_FUNCTION)) + .build(); + String cursor2 = "nextCursor"; + ListMonitoredResourceDescriptorsResponse response2 = + ListMonitoredResourceDescriptorsResponse.newBuilder() + .setNextPageToken(cursor2) + .addAllResourceDescriptors(Lists.transform(descriptorList2, DESCRIPTOR_TO_PB_FUNCTION)) + .build(); + Future futureResponse1 = + Futures.immediateFuture(response1); + Future futureResponse2 = + Futures.immediateFuture(response2); + EasyMock.expect(loggingRpcMock.list(request1)).andReturn(futureResponse1); + EasyMock.expect(loggingRpcMock.list(request2)).andReturn(futureResponse2); + EasyMock.replay(loggingRpcMock); + Page page = logging.listMonitoredResourceDescriptors(); + assertEquals(cursor1, page.nextPageCursor()); + assertArrayEquals(descriptorList1.toArray(), Iterables.toArray(page.values(), + MonitoredResourceDescriptor.class)); + page = page.nextPage(); + assertEquals(cursor2, page.nextPageCursor()); + assertArrayEquals(descriptorList2.toArray(), + Iterables.toArray(page.values(), MonitoredResourceDescriptor.class)); + } + + @Test + public void testListResourceDescriptorEmpty() { + EasyMock.replay(rpcFactoryMock); + logging = options.service(); + ListMonitoredResourceDescriptorsRequest request = + ListMonitoredResourceDescriptorsRequest.newBuilder().build(); + List descriptorList = ImmutableList.of(); + ListMonitoredResourceDescriptorsResponse response = + ListMonitoredResourceDescriptorsResponse.newBuilder() + .setNextPageToken("") + .addAllResourceDescriptors(Lists.transform(descriptorList, DESCRIPTOR_TO_PB_FUNCTION)) + .build(); + Future futureResponse = + Futures.immediateFuture(response); + EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); + EasyMock.replay(loggingRpcMock); + Page page = logging.listMonitoredResourceDescriptors(); + assertNull(page.nextPageCursor()); + assertNull(page.nextPage()); + assertArrayEquals(descriptorList.toArray(), + Iterables.toArray(page.values(), MonitoredResourceDescriptor.class)); + } + + @Test + public void testListResourceDescriptorWithOptions() { + String cursor = "cursor"; + EasyMock.replay(rpcFactoryMock); + logging = options.service(); + ListMonitoredResourceDescriptorsRequest request = + ListMonitoredResourceDescriptorsRequest.newBuilder() + .setPageToken(cursor) + .setPageSize(42) + .build(); + List descriptorList = ImmutableList.of(DESCRIPTOR, DESCRIPTOR); + ListMonitoredResourceDescriptorsResponse response = + ListMonitoredResourceDescriptorsResponse.newBuilder() + .setNextPageToken(cursor) + .addAllResourceDescriptors(Lists.transform(descriptorList, DESCRIPTOR_TO_PB_FUNCTION)) + .build(); + Future futureResponse = + Futures.immediateFuture(response); + EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); + EasyMock.replay(loggingRpcMock); + Page page = logging.listMonitoredResourceDescriptors( + ListOption.pageSize(42), ListOption.pageToken(cursor)); + assertEquals(cursor, page.nextPageCursor()); + assertArrayEquals(descriptorList.toArray(), + Iterables.toArray(page.values(), MonitoredResourceDescriptor.class)); + } + + @Test + public void testListResourceDescriptorAsync() throws ExecutionException, InterruptedException { + String cursor = "cursor"; + EasyMock.replay(rpcFactoryMock); + logging = options.service(); + ListMonitoredResourceDescriptorsRequest request = + ListMonitoredResourceDescriptorsRequest.newBuilder().build(); + List descriptorList = ImmutableList.of(DESCRIPTOR, DESCRIPTOR); + ListMonitoredResourceDescriptorsResponse response = + ListMonitoredResourceDescriptorsResponse.newBuilder() + .setNextPageToken(cursor) + .addAllResourceDescriptors(Lists.transform(descriptorList, DESCRIPTOR_TO_PB_FUNCTION)) + .build(); + Future futureResponse = + Futures.immediateFuture(response); + EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); + EasyMock.replay(loggingRpcMock); + AsyncPage page = + logging.listMonitoredResourceDescriptorsAsync().get(); + assertEquals(cursor, page.nextPageCursor()); + assertArrayEquals(descriptorList.toArray(), + Iterables.toArray(page.values(), MonitoredResourceDescriptor.class)); + } + + @Test + public void testListResourceDescriptorAsyncNextPage() throws ExecutionException, InterruptedException { + String cursor1 = "cursor"; + EasyMock.replay(rpcFactoryMock); + logging = options.service(); + ListMonitoredResourceDescriptorsRequest request1 = + ListMonitoredResourceDescriptorsRequest.newBuilder().build(); + ListMonitoredResourceDescriptorsRequest request2 = + ListMonitoredResourceDescriptorsRequest.newBuilder().setPageToken(cursor1).build(); + List descriptorList1 = ImmutableList.of(DESCRIPTOR, DESCRIPTOR); + List descriptorList2 = ImmutableList.of(DESCRIPTOR); + ListMonitoredResourceDescriptorsResponse response1 = + ListMonitoredResourceDescriptorsResponse.newBuilder() + .setNextPageToken(cursor1) + .addAllResourceDescriptors(Lists.transform(descriptorList1, DESCRIPTOR_TO_PB_FUNCTION)) + .build(); + String cursor2 = "nextCursor"; + ListMonitoredResourceDescriptorsResponse response2 = + ListMonitoredResourceDescriptorsResponse.newBuilder() + .setNextPageToken(cursor2) + .addAllResourceDescriptors(Lists.transform(descriptorList2, DESCRIPTOR_TO_PB_FUNCTION)) + .build(); + Future futureResponse1 = + Futures.immediateFuture(response1); + Future futureResponse2 = + Futures.immediateFuture(response2); + EasyMock.expect(loggingRpcMock.list(request1)).andReturn(futureResponse1); + EasyMock.expect(loggingRpcMock.list(request2)).andReturn(futureResponse2); + EasyMock.replay(loggingRpcMock); + AsyncPage page = + logging.listMonitoredResourceDescriptorsAsync().get(); + assertEquals(cursor1, page.nextPageCursor()); + assertArrayEquals(descriptorList1.toArray(), Iterables.toArray(page.values(), + MonitoredResourceDescriptor.class)); + page = page.nextPageAsync().get(); + assertEquals(cursor2, page.nextPageCursor()); + assertArrayEquals(descriptorList2.toArray(), + Iterables.toArray(page.values(), MonitoredResourceDescriptor.class)); + } + + @Test + public void testListResourceDescriptorAsyncEmpty() + throws ExecutionException, InterruptedException { + EasyMock.replay(rpcFactoryMock); + logging = options.service(); + ListMonitoredResourceDescriptorsRequest request = + ListMonitoredResourceDescriptorsRequest.newBuilder().build(); + List descriptorList = ImmutableList.of(); + ListMonitoredResourceDescriptorsResponse response = + ListMonitoredResourceDescriptorsResponse.newBuilder() + .setNextPageToken("") + .addAllResourceDescriptors(Lists.transform(descriptorList, DESCRIPTOR_TO_PB_FUNCTION)) + .build(); + Future futureResponse = + Futures.immediateFuture(response); + EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); + EasyMock.replay(loggingRpcMock); + AsyncPage page = + logging.listMonitoredResourceDescriptorsAsync().get(); + assertNull(page.nextPageCursor()); + assertNull(page.nextPage()); + assertArrayEquals(descriptorList.toArray(), + Iterables.toArray(page.values(), MonitoredResourceDescriptor.class)); + } + + @Test + public void testListResourceDescriptorAsyncWithOptions() + throws ExecutionException, InterruptedException { + String cursor = "cursor"; + EasyMock.replay(rpcFactoryMock); + logging = options.service(); + ListMonitoredResourceDescriptorsRequest request = + ListMonitoredResourceDescriptorsRequest.newBuilder() + .setPageToken(cursor) + .setPageSize(42) + .build(); + List descriptorList = ImmutableList.of(DESCRIPTOR, DESCRIPTOR); + ListMonitoredResourceDescriptorsResponse response = + ListMonitoredResourceDescriptorsResponse.newBuilder() + .setNextPageToken(cursor) + .addAllResourceDescriptors(Lists.transform(descriptorList, DESCRIPTOR_TO_PB_FUNCTION)) + .build(); + Future futureResponse = + Futures.immediateFuture(response); + EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); + EasyMock.replay(loggingRpcMock); + AsyncPage page = logging.listMonitoredResourceDescriptorsAsync( + ListOption.pageSize(42), ListOption.pageToken(cursor)).get(); + assertEquals(cursor, page.nextPageCursor()); + assertArrayEquals(descriptorList.toArray(), + Iterables.toArray(page.values(), MonitoredResourceDescriptor.class)); + } }