|
16 | 16 |
|
17 | 17 | package com.google.cloud.logging; |
18 | 18 |
|
| 19 | +import static com.google.api.client.util.Preconditions.checkArgument; |
19 | 20 | import static com.google.cloud.logging.Logging.ListOption.OptionType.PAGE_SIZE; |
20 | 21 | import static com.google.cloud.logging.Logging.ListOption.OptionType.PAGE_TOKEN; |
21 | | -import static com.google.common.base.Preconditions.checkArgument; |
22 | 22 | import static com.google.common.util.concurrent.Futures.lazyTransform; |
23 | 23 |
|
24 | 24 | import com.google.cloud.AsyncPage; |
25 | 25 | import com.google.cloud.AsyncPageImpl; |
26 | 26 | import com.google.cloud.BaseService; |
| 27 | +import com.google.cloud.MonitoredResourceDescriptor; |
27 | 28 | import com.google.cloud.Page; |
28 | 29 | import com.google.cloud.PageImpl; |
29 | 30 | import com.google.cloud.logging.spi.LoggingRpc; |
|
43 | 44 | import com.google.logging.v2.GetSinkRequest; |
44 | 45 | import com.google.logging.v2.ListLogMetricsRequest; |
45 | 46 | import com.google.logging.v2.ListLogMetricsResponse; |
| 47 | +import com.google.logging.v2.ListMonitoredResourceDescriptorsRequest; |
| 48 | +import com.google.logging.v2.ListMonitoredResourceDescriptorsResponse; |
46 | 49 | import com.google.logging.v2.ListSinksRequest; |
47 | 50 | import com.google.logging.v2.ListSinksResponse; |
48 | 51 | import com.google.logging.v2.UpdateLogMetricRequest; |
@@ -118,6 +121,22 @@ public Future<AsyncPage<Sink>> nextPage() { |
118 | 121 | } |
119 | 122 | } |
120 | 123 |
|
| 124 | + private static class MonitoredResourceDescriptorPageFetcher |
| 125 | + extends BasePageFetcher<MonitoredResourceDescriptor> { |
| 126 | + |
| 127 | + private static final long serialVersionUID = -2346495771766629195L; |
| 128 | + |
| 129 | + MonitoredResourceDescriptorPageFetcher(LoggingOptions serviceOptions, String cursor, |
| 130 | + Map<Option.OptionType, ?> requestOptions) { |
| 131 | + super(serviceOptions, cursor, requestOptions); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public Future<AsyncPage<MonitoredResourceDescriptor>> nextPage() { |
| 136 | + return listMonitoredResourceDescriptorsAsync(serviceOptions(), requestOptions()); |
| 137 | + } |
| 138 | + } |
| 139 | + |
121 | 140 | private static class MetricPageFetcher extends BasePageFetcher<Metric> { |
122 | 141 |
|
123 | 142 | private static final long serialVersionUID = -316783549651771553L; |
@@ -230,6 +249,55 @@ public Future<Boolean> deleteSinkAsync(String sink) { |
230 | 249 | return lazyTransform(rpc.delete(request), EMPTY_TO_BOOLEAN_FUNCTION); |
231 | 250 | } |
232 | 251 |
|
| 252 | + private static ListMonitoredResourceDescriptorsRequest listMonitoredResourceDescriptorsRequest( |
| 253 | + Map<Option.OptionType, ?> options) { |
| 254 | + ListMonitoredResourceDescriptorsRequest.Builder builder = |
| 255 | + ListMonitoredResourceDescriptorsRequest.newBuilder(); |
| 256 | + Integer pageSize = PAGE_SIZE.get(options); |
| 257 | + String pageToken = PAGE_TOKEN.get(options); |
| 258 | + if (pageSize != null) { |
| 259 | + builder.setPageSize(pageSize); |
| 260 | + } |
| 261 | + if (pageToken != null) { |
| 262 | + builder.setPageToken(pageToken); |
| 263 | + } |
| 264 | + return builder.build(); |
| 265 | + } |
| 266 | + |
| 267 | + private static Future<AsyncPage<MonitoredResourceDescriptor>> |
| 268 | + listMonitoredResourceDescriptorsAsync(final LoggingOptions serviceOptions, |
| 269 | + final Map<Option.OptionType, ?> options) { |
| 270 | + final ListMonitoredResourceDescriptorsRequest request = |
| 271 | + listMonitoredResourceDescriptorsRequest(options); |
| 272 | + Future<ListMonitoredResourceDescriptorsResponse> list = serviceOptions.rpc().list(request); |
| 273 | + return lazyTransform(list, new Function<ListMonitoredResourceDescriptorsResponse, |
| 274 | + AsyncPage<MonitoredResourceDescriptor>>() { |
| 275 | + @Override |
| 276 | + public AsyncPage<MonitoredResourceDescriptor> apply( |
| 277 | + ListMonitoredResourceDescriptorsResponse listDescriptorsResponse) { |
| 278 | + List<MonitoredResourceDescriptor> descriptors = |
| 279 | + listDescriptorsResponse.getResourceDescriptorsList() == null |
| 280 | + ? ImmutableList.<MonitoredResourceDescriptor>of() |
| 281 | + : Lists.transform(listDescriptorsResponse.getResourceDescriptorsList(), |
| 282 | + MonitoredResourceDescriptor.FROM_PB_FUNCTION); |
| 283 | + String cursor = listDescriptorsResponse.getNextPageToken().equals("") ? null |
| 284 | + : listDescriptorsResponse.getNextPageToken(); |
| 285 | + return new AsyncPageImpl<>( |
| 286 | + new MonitoredResourceDescriptorPageFetcher(serviceOptions, cursor, options), cursor, |
| 287 | + descriptors); |
| 288 | + } |
| 289 | + }); |
| 290 | + } |
| 291 | + |
| 292 | + public Page<MonitoredResourceDescriptor> listMonitoredResourceDescriptors(ListOption... options) { |
| 293 | + return get(listMonitoredResourceDescriptorsAsync(options)); |
| 294 | + } |
| 295 | + |
| 296 | + public Future<AsyncPage<MonitoredResourceDescriptor>> listMonitoredResourceDescriptorsAsync( |
| 297 | + ListOption... options) { |
| 298 | + return listMonitoredResourceDescriptorsAsync(options(), optionMap(options)); |
| 299 | + } |
| 300 | + |
233 | 301 | @Override |
234 | 302 | public Metric create(MetricInfo metric) { |
235 | 303 | return get(createAsync(metric)); |
|
0 commit comments