1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.cloud.asset.v1p5beta1; 18 19 import com.google.api.core.ApiFuture; 20 import com.google.api.core.ApiFutures; 21 import com.google.api.core.BetaApi; 22 import com.google.api.gax.core.BackgroundResource; 23 import com.google.api.gax.paging.AbstractFixedSizeCollection; 24 import com.google.api.gax.paging.AbstractPage; 25 import com.google.api.gax.paging.AbstractPagedListResponse; 26 import com.google.api.gax.rpc.PageContext; 27 import com.google.api.gax.rpc.UnaryCallable; 28 import com.google.api.resourcenames.ResourceName; 29 import com.google.cloud.asset.v1p5beta1.stub.AssetServiceStub; 30 import com.google.cloud.asset.v1p5beta1.stub.AssetServiceStubSettings; 31 import com.google.common.util.concurrent.MoreExecutors; 32 import java.io.IOException; 33 import java.util.List; 34 import java.util.concurrent.TimeUnit; 35 import javax.annotation.Generated; 36 37 // AUTO-GENERATED DOCUMENTATION AND CLASS. 38 /** 39 * Service Description: Asset service definition. 40 * 41 * <p>This class provides the ability to make remote calls to the backing service through method 42 * calls that map to API methods. Sample code to get started: 43 * 44 * <pre>{@code 45 * // This snippet has been automatically generated and should be regarded as a code template only. 46 * // It will require modifications to work: 47 * // - It may require correct/in-range values for request initialization. 48 * // - It may require specifying regional endpoints when creating the service client as shown in 49 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 50 * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) { 51 * ResourceName parent = 52 * new ResourceName() { 53 * {@literal @}Override 54 * public Map<String, String> getFieldValuesMap() { 55 * Map<String, String> fieldValuesMap = new HashMap<>(); 56 * fieldValuesMap.put("parent", "parent-4715/parent-4715"); 57 * return fieldValuesMap; 58 * } 59 * 60 * {@literal @}Override 61 * public String getFieldValue(String fieldName) { 62 * return getFieldValuesMap().get(fieldName); 63 * } 64 * 65 * {@literal @}Override 66 * public String toString() { 67 * return "parent-4715/parent-4715"; 68 * } 69 * }; 70 * for (Asset element : assetServiceClient.listAssets(parent).iterateAll()) { 71 * // doThingsWith(element); 72 * } 73 * } 74 * }</pre> 75 * 76 * <p>Note: close() needs to be called on the AssetServiceClient object to clean up resources such 77 * as threads. In the example above, try-with-resources is used, which automatically calls close(). 78 * 79 * <p>The surface of this class includes several types of Java methods for each of the API's 80 * methods: 81 * 82 * <ol> 83 * <li>A "flattened" method. With this type of method, the fields of the request type have been 84 * converted into function parameters. It may be the case that not all fields are available as 85 * parameters, and not every API method will have a flattened method entry point. 86 * <li>A "request object" method. This type of method only takes one parameter, a request object, 87 * which must be constructed before the call. Not every API method will have a request object 88 * method. 89 * <li>A "callable" method. This type of method takes no parameters and returns an immutable API 90 * callable object, which can be used to initiate calls to the service. 91 * </ol> 92 * 93 * <p>See the individual methods for example code. 94 * 95 * <p>Many parameters require resource names to be formatted in a particular way. To assist with 96 * these names, this class includes a format method for each type of name, and additionally a parse 97 * method to extract the individual identifiers contained within names that are returned. 98 * 99 * <p>This class can be customized by passing in a custom instance of AssetServiceSettings to 100 * create(). For example: 101 * 102 * <p>To customize credentials: 103 * 104 * <pre>{@code 105 * // This snippet has been automatically generated and should be regarded as a code template only. 106 * // It will require modifications to work: 107 * // - It may require correct/in-range values for request initialization. 108 * // - It may require specifying regional endpoints when creating the service client as shown in 109 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 110 * AssetServiceSettings assetServiceSettings = 111 * AssetServiceSettings.newBuilder() 112 * .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) 113 * .build(); 114 * AssetServiceClient assetServiceClient = AssetServiceClient.create(assetServiceSettings); 115 * }</pre> 116 * 117 * <p>To customize the endpoint: 118 * 119 * <pre>{@code 120 * // This snippet has been automatically generated and should be regarded as a code template only. 121 * // It will require modifications to work: 122 * // - It may require correct/in-range values for request initialization. 123 * // - It may require specifying regional endpoints when creating the service client as shown in 124 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 125 * AssetServiceSettings assetServiceSettings = 126 * AssetServiceSettings.newBuilder().setEndpoint(myEndpoint).build(); 127 * AssetServiceClient assetServiceClient = AssetServiceClient.create(assetServiceSettings); 128 * }</pre> 129 * 130 * <p>To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over 131 * the wire: 132 * 133 * <pre>{@code 134 * // This snippet has been automatically generated and should be regarded as a code template only. 135 * // It will require modifications to work: 136 * // - It may require correct/in-range values for request initialization. 137 * // - It may require specifying regional endpoints when creating the service client as shown in 138 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 139 * AssetServiceSettings assetServiceSettings = AssetServiceSettings.newHttpJsonBuilder().build(); 140 * AssetServiceClient assetServiceClient = AssetServiceClient.create(assetServiceSettings); 141 * }</pre> 142 * 143 * <p>Please refer to the GitHub repository's samples for more quickstart code snippets. 144 */ 145 @BetaApi 146 @Generated("by gapic-generator-java") 147 public class AssetServiceClient implements BackgroundResource { 148 private final AssetServiceSettings settings; 149 private final AssetServiceStub stub; 150 151 /** Constructs an instance of AssetServiceClient with default settings. */ create()152 public static final AssetServiceClient create() throws IOException { 153 return create(AssetServiceSettings.newBuilder().build()); 154 } 155 156 /** 157 * Constructs an instance of AssetServiceClient, using the given settings. The channels are 158 * created based on the settings passed in, or defaults for any settings that are not set. 159 */ create(AssetServiceSettings settings)160 public static final AssetServiceClient create(AssetServiceSettings settings) throws IOException { 161 return new AssetServiceClient(settings); 162 } 163 164 /** 165 * Constructs an instance of AssetServiceClient, using the given stub for making calls. This is 166 * for advanced usage - prefer using create(AssetServiceSettings). 167 */ create(AssetServiceStub stub)168 public static final AssetServiceClient create(AssetServiceStub stub) { 169 return new AssetServiceClient(stub); 170 } 171 172 /** 173 * Constructs an instance of AssetServiceClient, using the given settings. This is protected so 174 * that it is easy to make a subclass, but otherwise, the static factory methods should be 175 * preferred. 176 */ AssetServiceClient(AssetServiceSettings settings)177 protected AssetServiceClient(AssetServiceSettings settings) throws IOException { 178 this.settings = settings; 179 this.stub = ((AssetServiceStubSettings) settings.getStubSettings()).createStub(); 180 } 181 AssetServiceClient(AssetServiceStub stub)182 protected AssetServiceClient(AssetServiceStub stub) { 183 this.settings = null; 184 this.stub = stub; 185 } 186 getSettings()187 public final AssetServiceSettings getSettings() { 188 return settings; 189 } 190 getStub()191 public AssetServiceStub getStub() { 192 return stub; 193 } 194 195 // AUTO-GENERATED DOCUMENTATION AND METHOD. 196 /** 197 * Lists assets with time and resource types and returns paged results in response. 198 * 199 * <p>Sample code: 200 * 201 * <pre>{@code 202 * // This snippet has been automatically generated and should be regarded as a code template only. 203 * // It will require modifications to work: 204 * // - It may require correct/in-range values for request initialization. 205 * // - It may require specifying regional endpoints when creating the service client as shown in 206 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 207 * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) { 208 * ResourceName parent = 209 * new ResourceName() { 210 * {@literal @}Override 211 * public Map<String, String> getFieldValuesMap() { 212 * Map<String, String> fieldValuesMap = new HashMap<>(); 213 * fieldValuesMap.put("parent", "parent-4715/parent-4715"); 214 * return fieldValuesMap; 215 * } 216 * 217 * {@literal @}Override 218 * public String getFieldValue(String fieldName) { 219 * return getFieldValuesMap().get(fieldName); 220 * } 221 * 222 * {@literal @}Override 223 * public String toString() { 224 * return "parent-4715/parent-4715"; 225 * } 226 * }; 227 * for (Asset element : assetServiceClient.listAssets(parent).iterateAll()) { 228 * // doThingsWith(element); 229 * } 230 * } 231 * }</pre> 232 * 233 * @param parent Required. Name of the organization or project the assets belong to. Format: 234 * "organizations/[organization-number]" (such as "organizations/123"), 235 * "projects/[project-id]" (such as "projects/my-project-id"), or "projects/[project-number]" 236 * (such as "projects/12345"). 237 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 238 */ listAssets(ResourceName parent)239 public final ListAssetsPagedResponse listAssets(ResourceName parent) { 240 ListAssetsRequest request = 241 ListAssetsRequest.newBuilder().setParent(parent == null ? null : parent.toString()).build(); 242 return listAssets(request); 243 } 244 245 // AUTO-GENERATED DOCUMENTATION AND METHOD. 246 /** 247 * Lists assets with time and resource types and returns paged results in response. 248 * 249 * <p>Sample code: 250 * 251 * <pre>{@code 252 * // This snippet has been automatically generated and should be regarded as a code template only. 253 * // It will require modifications to work: 254 * // - It may require correct/in-range values for request initialization. 255 * // - It may require specifying regional endpoints when creating the service client as shown in 256 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 257 * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) { 258 * String parent = 259 * new ResourceName() { 260 * {@literal @}Override 261 * public Map<String, String> getFieldValuesMap() { 262 * Map<String, String> fieldValuesMap = new HashMap<>(); 263 * fieldValuesMap.put("parent", "parent-995424086"); 264 * return fieldValuesMap; 265 * } 266 * 267 * {@literal @}Override 268 * public String getFieldValue(String fieldName) { 269 * return getFieldValuesMap().get(fieldName); 270 * } 271 * 272 * {@literal @}Override 273 * public String toString() { 274 * return "parent-995424086"; 275 * } 276 * }.toString(); 277 * for (Asset element : assetServiceClient.listAssets(parent).iterateAll()) { 278 * // doThingsWith(element); 279 * } 280 * } 281 * }</pre> 282 * 283 * @param parent Required. Name of the organization or project the assets belong to. Format: 284 * "organizations/[organization-number]" (such as "organizations/123"), 285 * "projects/[project-id]" (such as "projects/my-project-id"), or "projects/[project-number]" 286 * (such as "projects/12345"). 287 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 288 */ listAssets(String parent)289 public final ListAssetsPagedResponse listAssets(String parent) { 290 ListAssetsRequest request = ListAssetsRequest.newBuilder().setParent(parent).build(); 291 return listAssets(request); 292 } 293 294 // AUTO-GENERATED DOCUMENTATION AND METHOD. 295 /** 296 * Lists assets with time and resource types and returns paged results in response. 297 * 298 * <p>Sample code: 299 * 300 * <pre>{@code 301 * // This snippet has been automatically generated and should be regarded as a code template only. 302 * // It will require modifications to work: 303 * // - It may require correct/in-range values for request initialization. 304 * // - It may require specifying regional endpoints when creating the service client as shown in 305 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 306 * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) { 307 * ListAssetsRequest request = 308 * ListAssetsRequest.newBuilder() 309 * .setParent("ListAssetsRequest-221586066".toString()) 310 * .setReadTime(Timestamp.newBuilder().build()) 311 * .addAllAssetTypes(new ArrayList<String>()) 312 * .setContentType(ContentType.forNumber(0)) 313 * .setPageSize(883849137) 314 * .setPageToken("pageToken873572522") 315 * .build(); 316 * for (Asset element : assetServiceClient.listAssets(request).iterateAll()) { 317 * // doThingsWith(element); 318 * } 319 * } 320 * }</pre> 321 * 322 * @param request The request object containing all of the parameters for the API call. 323 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 324 */ listAssets(ListAssetsRequest request)325 public final ListAssetsPagedResponse listAssets(ListAssetsRequest request) { 326 return listAssetsPagedCallable().call(request); 327 } 328 329 // AUTO-GENERATED DOCUMENTATION AND METHOD. 330 /** 331 * Lists assets with time and resource types and returns paged results in response. 332 * 333 * <p>Sample code: 334 * 335 * <pre>{@code 336 * // This snippet has been automatically generated and should be regarded as a code template only. 337 * // It will require modifications to work: 338 * // - It may require correct/in-range values for request initialization. 339 * // - It may require specifying regional endpoints when creating the service client as shown in 340 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 341 * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) { 342 * ListAssetsRequest request = 343 * ListAssetsRequest.newBuilder() 344 * .setParent("ListAssetsRequest-221586066".toString()) 345 * .setReadTime(Timestamp.newBuilder().build()) 346 * .addAllAssetTypes(new ArrayList<String>()) 347 * .setContentType(ContentType.forNumber(0)) 348 * .setPageSize(883849137) 349 * .setPageToken("pageToken873572522") 350 * .build(); 351 * ApiFuture<Asset> future = assetServiceClient.listAssetsPagedCallable().futureCall(request); 352 * // Do something. 353 * for (Asset element : future.get().iterateAll()) { 354 * // doThingsWith(element); 355 * } 356 * } 357 * }</pre> 358 */ listAssetsPagedCallable()359 public final UnaryCallable<ListAssetsRequest, ListAssetsPagedResponse> listAssetsPagedCallable() { 360 return stub.listAssetsPagedCallable(); 361 } 362 363 // AUTO-GENERATED DOCUMENTATION AND METHOD. 364 /** 365 * Lists assets with time and resource types and returns paged results in response. 366 * 367 * <p>Sample code: 368 * 369 * <pre>{@code 370 * // This snippet has been automatically generated and should be regarded as a code template only. 371 * // It will require modifications to work: 372 * // - It may require correct/in-range values for request initialization. 373 * // - It may require specifying regional endpoints when creating the service client as shown in 374 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 375 * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) { 376 * ListAssetsRequest request = 377 * ListAssetsRequest.newBuilder() 378 * .setParent("ListAssetsRequest-221586066".toString()) 379 * .setReadTime(Timestamp.newBuilder().build()) 380 * .addAllAssetTypes(new ArrayList<String>()) 381 * .setContentType(ContentType.forNumber(0)) 382 * .setPageSize(883849137) 383 * .setPageToken("pageToken873572522") 384 * .build(); 385 * while (true) { 386 * ListAssetsResponse response = assetServiceClient.listAssetsCallable().call(request); 387 * for (Asset element : response.getAssetsList()) { 388 * // doThingsWith(element); 389 * } 390 * String nextPageToken = response.getNextPageToken(); 391 * if (!Strings.isNullOrEmpty(nextPageToken)) { 392 * request = request.toBuilder().setPageToken(nextPageToken).build(); 393 * } else { 394 * break; 395 * } 396 * } 397 * } 398 * }</pre> 399 */ listAssetsCallable()400 public final UnaryCallable<ListAssetsRequest, ListAssetsResponse> listAssetsCallable() { 401 return stub.listAssetsCallable(); 402 } 403 404 @Override close()405 public final void close() { 406 stub.close(); 407 } 408 409 @Override shutdown()410 public void shutdown() { 411 stub.shutdown(); 412 } 413 414 @Override isShutdown()415 public boolean isShutdown() { 416 return stub.isShutdown(); 417 } 418 419 @Override isTerminated()420 public boolean isTerminated() { 421 return stub.isTerminated(); 422 } 423 424 @Override shutdownNow()425 public void shutdownNow() { 426 stub.shutdownNow(); 427 } 428 429 @Override awaitTermination(long duration, TimeUnit unit)430 public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { 431 return stub.awaitTermination(duration, unit); 432 } 433 434 public static class ListAssetsPagedResponse 435 extends AbstractPagedListResponse< 436 ListAssetsRequest, 437 ListAssetsResponse, 438 Asset, 439 ListAssetsPage, 440 ListAssetsFixedSizeCollection> { 441 createAsync( PageContext<ListAssetsRequest, ListAssetsResponse, Asset> context, ApiFuture<ListAssetsResponse> futureResponse)442 public static ApiFuture<ListAssetsPagedResponse> createAsync( 443 PageContext<ListAssetsRequest, ListAssetsResponse, Asset> context, 444 ApiFuture<ListAssetsResponse> futureResponse) { 445 ApiFuture<ListAssetsPage> futurePage = 446 ListAssetsPage.createEmptyPage().createPageAsync(context, futureResponse); 447 return ApiFutures.transform( 448 futurePage, input -> new ListAssetsPagedResponse(input), MoreExecutors.directExecutor()); 449 } 450 ListAssetsPagedResponse(ListAssetsPage page)451 private ListAssetsPagedResponse(ListAssetsPage page) { 452 super(page, ListAssetsFixedSizeCollection.createEmptyCollection()); 453 } 454 } 455 456 public static class ListAssetsPage 457 extends AbstractPage<ListAssetsRequest, ListAssetsResponse, Asset, ListAssetsPage> { 458 ListAssetsPage( PageContext<ListAssetsRequest, ListAssetsResponse, Asset> context, ListAssetsResponse response)459 private ListAssetsPage( 460 PageContext<ListAssetsRequest, ListAssetsResponse, Asset> context, 461 ListAssetsResponse response) { 462 super(context, response); 463 } 464 createEmptyPage()465 private static ListAssetsPage createEmptyPage() { 466 return new ListAssetsPage(null, null); 467 } 468 469 @Override createPage( PageContext<ListAssetsRequest, ListAssetsResponse, Asset> context, ListAssetsResponse response)470 protected ListAssetsPage createPage( 471 PageContext<ListAssetsRequest, ListAssetsResponse, Asset> context, 472 ListAssetsResponse response) { 473 return new ListAssetsPage(context, response); 474 } 475 476 @Override createPageAsync( PageContext<ListAssetsRequest, ListAssetsResponse, Asset> context, ApiFuture<ListAssetsResponse> futureResponse)477 public ApiFuture<ListAssetsPage> createPageAsync( 478 PageContext<ListAssetsRequest, ListAssetsResponse, Asset> context, 479 ApiFuture<ListAssetsResponse> futureResponse) { 480 return super.createPageAsync(context, futureResponse); 481 } 482 } 483 484 public static class ListAssetsFixedSizeCollection 485 extends AbstractFixedSizeCollection< 486 ListAssetsRequest, 487 ListAssetsResponse, 488 Asset, 489 ListAssetsPage, 490 ListAssetsFixedSizeCollection> { 491 ListAssetsFixedSizeCollection(List<ListAssetsPage> pages, int collectionSize)492 private ListAssetsFixedSizeCollection(List<ListAssetsPage> pages, int collectionSize) { 493 super(pages, collectionSize); 494 } 495 createEmptyCollection()496 private static ListAssetsFixedSizeCollection createEmptyCollection() { 497 return new ListAssetsFixedSizeCollection(null, 0); 498 } 499 500 @Override createCollection( List<ListAssetsPage> pages, int collectionSize)501 protected ListAssetsFixedSizeCollection createCollection( 502 List<ListAssetsPage> pages, int collectionSize) { 503 return new ListAssetsFixedSizeCollection(pages, collectionSize); 504 } 505 } 506 } 507