|
| 1 | +// Code generated from OpenAPI specs by Databricks SDK Generator. DO NOT EDIT. |
| 2 | +package com.databricks.sdk.service.networking; |
| 3 | + |
| 4 | +import com.databricks.sdk.core.DatabricksException; |
| 5 | +import com.databricks.sdk.core.logging.Logger; |
| 6 | +import com.databricks.sdk.core.logging.LoggerFactory; |
| 7 | +import com.databricks.sdk.core.utils.SerDeUtils; |
| 8 | +import com.databricks.sdk.service.common.lro.LroOptions; |
| 9 | +import com.databricks.sdk.support.Generated; |
| 10 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 11 | +import com.fasterxml.jackson.databind.JsonNode; |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | +import java.time.Duration; |
| 14 | +import java.util.Optional; |
| 15 | +import java.util.concurrent.TimeoutException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Wrapper for interacting with a long-running createPrivateNetworkGateway operation. Provides |
| 19 | + * methods to wait for completion, check status, cancel, and access metadata. |
| 20 | + */ |
| 21 | +@Generated |
| 22 | +public class CreatePrivateNetworkGatewayOperation { |
| 23 | + private static final Logger LOG = |
| 24 | + LoggerFactory.getLogger(CreatePrivateNetworkGatewayOperation.class); |
| 25 | + |
| 26 | + private final PrivateNetworkGatewaysService impl; |
| 27 | + private Operation operation; |
| 28 | + private final ObjectMapper objectMapper; |
| 29 | + |
| 30 | + public CreatePrivateNetworkGatewayOperation( |
| 31 | + PrivateNetworkGatewaysService impl, Operation operation) { |
| 32 | + this.impl = impl; |
| 33 | + this.operation = operation; |
| 34 | + this.objectMapper = SerDeUtils.createMapper(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Wait for the operation to complete and return the resulting PrivateNetworkGateway. Waits |
| 39 | + * indefinitely if no timeout is specified. |
| 40 | + * |
| 41 | + * @return the created PrivateNetworkGateway |
| 42 | + * @throws TimeoutException if the operation doesn't complete within the timeout |
| 43 | + * @throws DatabricksException if the operation fails |
| 44 | + */ |
| 45 | + public PrivateNetworkGateway waitForCompletion() throws TimeoutException { |
| 46 | + return waitForCompletion(Optional.empty()); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Wait for the operation to complete and return the resulting PrivateNetworkGateway. |
| 51 | + * |
| 52 | + * @param options the options for configuring the wait behavior, can be empty for defaults |
| 53 | + * @return the created PrivateNetworkGateway |
| 54 | + * @throws TimeoutException if the operation doesn't complete within the timeout |
| 55 | + * @throws DatabricksException if the operation fails |
| 56 | + */ |
| 57 | + public PrivateNetworkGateway waitForCompletion(Optional<LroOptions> options) |
| 58 | + throws TimeoutException { |
| 59 | + Optional<Duration> timeout = options.flatMap(LroOptions::getTimeout); |
| 60 | + long deadline = |
| 61 | + timeout.isPresent() |
| 62 | + ? System.currentTimeMillis() + timeout.get().toMillis() |
| 63 | + : Long.MAX_VALUE; |
| 64 | + String statusMessage = "polling operation..."; |
| 65 | + int attempt = 1; |
| 66 | + |
| 67 | + while (System.currentTimeMillis() < deadline) { |
| 68 | + // Refresh the operation state |
| 69 | + refreshOperation(); |
| 70 | + |
| 71 | + if (operation.getDone() != null && operation.getDone()) { |
| 72 | + // Operation completed, check for success or failure |
| 73 | + if (operation.getError() != null) { |
| 74 | + String errorMsg = "unknown error"; |
| 75 | + if (operation.getError().getMessage() != null |
| 76 | + && !operation.getError().getMessage().isEmpty()) { |
| 77 | + errorMsg = operation.getError().getMessage(); |
| 78 | + } |
| 79 | + |
| 80 | + if (operation.getError().getErrorCode() != null) { |
| 81 | + errorMsg = String.format("[%s] %s", operation.getError().getErrorCode(), errorMsg); |
| 82 | + } |
| 83 | + |
| 84 | + throw new DatabricksException("Operation failed: " + errorMsg); |
| 85 | + } |
| 86 | + |
| 87 | + // Operation completed successfully, unmarshal response |
| 88 | + if (operation.getResponse() == null) { |
| 89 | + throw new DatabricksException("Operation completed but no response available"); |
| 90 | + } |
| 91 | + |
| 92 | + try { |
| 93 | + JsonNode responseJson = objectMapper.valueToTree(operation.getResponse()); |
| 94 | + return objectMapper.treeToValue(responseJson, PrivateNetworkGateway.class); |
| 95 | + } catch (JsonProcessingException e) { |
| 96 | + throw new DatabricksException( |
| 97 | + "Failed to unmarshal privateNetworkGateway response: " + e.getMessage(), e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // Operation still in progress, wait before polling again |
| 102 | + String prefix = String.format("operation=%s", operation.getName()); |
| 103 | + int sleep = Math.min(attempt, 10); // sleep 10s max per attempt |
| 104 | + LOG.info("{}: operation in progress (sleeping ~{}s)", prefix, sleep); |
| 105 | + |
| 106 | + try { |
| 107 | + Thread.sleep((long) (sleep * 1000L + Math.random() * 1000)); |
| 108 | + } catch (InterruptedException e) { |
| 109 | + Thread.currentThread().interrupt(); |
| 110 | + throw new DatabricksException("Current thread was interrupted", e); |
| 111 | + } |
| 112 | + attempt++; |
| 113 | + } |
| 114 | + |
| 115 | + String timeoutMessage = |
| 116 | + timeout.isPresent() |
| 117 | + ? String.format("Operation timed out after %s: %s", timeout.get(), statusMessage) |
| 118 | + : String.format("Operation timed out: %s", statusMessage); |
| 119 | + throw new TimeoutException(timeoutMessage); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Get the operation name. |
| 124 | + * |
| 125 | + * @return the operation name |
| 126 | + */ |
| 127 | + public String getName() { |
| 128 | + return operation.getName(); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Get the operation metadata. |
| 133 | + * |
| 134 | + * @return the operation metadata, or null if not available |
| 135 | + * @throws DatabricksException if the metadata cannot be deserialized |
| 136 | + */ |
| 137 | + public PrivateNetworkGatewayOperationMetadata getMetadata() { |
| 138 | + if (operation.getMetadata() == null) { |
| 139 | + return null; |
| 140 | + } |
| 141 | + |
| 142 | + try { |
| 143 | + JsonNode metadataJson = objectMapper.valueToTree(operation.getMetadata()); |
| 144 | + return objectMapper.treeToValue(metadataJson, PrivateNetworkGatewayOperationMetadata.class); |
| 145 | + } catch (JsonProcessingException e) { |
| 146 | + throw new DatabricksException("Failed to unmarshal operation metadata: " + e.getMessage(), e); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Check if the operation is done. This method refreshes the operation state before checking. |
| 152 | + * |
| 153 | + * @return true if the operation is complete, false otherwise |
| 154 | + * @throws DatabricksException if the status check fails |
| 155 | + */ |
| 156 | + public boolean isDone() { |
| 157 | + refreshOperation(); |
| 158 | + return operation.getDone() != null && operation.getDone(); |
| 159 | + } |
| 160 | + |
| 161 | + /** Refresh the operation state by polling the server. */ |
| 162 | + private void refreshOperation() { |
| 163 | + operation = |
| 164 | + impl.getPrivateNetworkGatewayOperation( |
| 165 | + new GetOperationRequest().setName(operation.getName())); |
| 166 | + } |
| 167 | +} |
0 commit comments