Skip to content

Commit

Permalink
Implement MonitoredResourceDescriptor methods and tests (#1098)
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard authored Jul 6, 2016
1 parent 0bcca69 commit 79fd788
Show file tree
Hide file tree
Showing 3 changed files with 334 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -136,6 +137,25 @@ public static ListOption pageToken(String pageToken) {
*/
Future<Boolean> 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<MonitoredResourceDescriptor> 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<AsyncPage<MonitoredResourceDescriptor>> listMonitoredResourceDescriptorsAsync(
ListOption... options);

/**
* Creates a new metric.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -118,6 +121,22 @@ public Future<AsyncPage<Sink>> nextPage() {
}
}

private static class MonitoredResourceDescriptorPageFetcher
extends BasePageFetcher<MonitoredResourceDescriptor> {

private static final long serialVersionUID = -2346495771766629195L;

MonitoredResourceDescriptorPageFetcher(LoggingOptions serviceOptions, String cursor,
Map<Option.OptionType, ?> requestOptions) {
super(serviceOptions, cursor, requestOptions);
}

@Override
public Future<AsyncPage<MonitoredResourceDescriptor>> nextPage() {
return listMonitoredResourceDescriptorsAsync(serviceOptions(), requestOptions());
}
}

private static class MetricPageFetcher extends BasePageFetcher<Metric> {

private static final long serialVersionUID = -316783549651771553L;
Expand Down Expand Up @@ -230,6 +249,55 @@ public Future<Boolean> deleteSinkAsync(String sink) {
return lazyTransform(rpc.delete(request), EMPTY_TO_BOOLEAN_FUNCTION);
}

private static ListMonitoredResourceDescriptorsRequest listMonitoredResourceDescriptorsRequest(
Map<Option.OptionType, ?> 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<AsyncPage<MonitoredResourceDescriptor>>
listMonitoredResourceDescriptorsAsync(final LoggingOptions serviceOptions,
final Map<Option.OptionType, ?> options) {
final ListMonitoredResourceDescriptorsRequest request =
listMonitoredResourceDescriptorsRequest(options);
Future<ListMonitoredResourceDescriptorsResponse> list = serviceOptions.rpc().list(request);
return lazyTransform(list, new Function<ListMonitoredResourceDescriptorsResponse,
AsyncPage<MonitoredResourceDescriptor>>() {
@Override
public AsyncPage<MonitoredResourceDescriptor> apply(
ListMonitoredResourceDescriptorsResponse listDescriptorsResponse) {
List<MonitoredResourceDescriptor> descriptors =
listDescriptorsResponse.getResourceDescriptorsList() == null
? ImmutableList.<MonitoredResourceDescriptor>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<MonitoredResourceDescriptor> listMonitoredResourceDescriptors(ListOption... options) {
return get(listMonitoredResourceDescriptorsAsync(options));
}

public Future<AsyncPage<MonitoredResourceDescriptor>> listMonitoredResourceDescriptorsAsync(
ListOption... options) {
return listMonitoredResourceDescriptorsAsync(options(), optionMap(options));
}

@Override
public Metric create(MetricInfo metric) {
return get(createAsync(metric));
Expand Down
Loading

0 comments on commit 79fd788

Please sign in to comment.