|
| 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 | + * http://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.example.optimizationai; |
| 18 | + |
| 19 | +// [START cloudoptimization_get_operation] |
| 20 | +import com.google.cloud.optimization.v1.FleetRoutingClient; |
| 21 | +import com.google.longrunning.Operation; |
| 22 | +import java.io.IOException; |
| 23 | + |
| 24 | +class GetOperation { |
| 25 | + |
| 26 | + static void getOperation() throws IOException { |
| 27 | + // TODO(developer): Replace these variables before running the sample. |
| 28 | + String operationFullId = "projects/[projectId]/operations/[operationId]"; |
| 29 | + getOperation(operationFullId); |
| 30 | + } |
| 31 | + |
| 32 | + // Get the status of an operation |
| 33 | + static void getOperation(String operationFullId) throws IOException { |
| 34 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 35 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 36 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 37 | + try (FleetRoutingClient client = FleetRoutingClient.create()) { |
| 38 | + // Get the latest state of a long-running operation. |
| 39 | + Operation operation = client.getOperationsClient().getOperation(operationFullId); |
| 40 | + |
| 41 | + // Display operation details. |
| 42 | + System.out.println("Operation details:"); |
| 43 | + System.out.format("\tName: %s\n", operation.getName()); |
| 44 | + System.out.format("\tMetadata Type Url: %s\n", operation.getMetadata().getTypeUrl()); |
| 45 | + System.out.format("\tDone: %s\n", operation.getDone()); |
| 46 | + if (operation.hasResponse()) { |
| 47 | + System.out.format("\tResponse Type Url: %s\n", operation.getResponse().getTypeUrl()); |
| 48 | + } |
| 49 | + if (operation.hasError()) { |
| 50 | + System.out.println("\tResponse:"); |
| 51 | + System.out.format("\t\tError code: %s\n", operation.getError().getCode()); |
| 52 | + System.out.format("\t\tError message: %s\n", operation.getError().getMessage()); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | +// [END cloudoptimization_get_operation] |
0 commit comments