Skip to content

Commit

Permalink
mgmt, Accepted interface, handle LRO succeeded without poll (Azure#14868
Browse files Browse the repository at this point in the history
)
  • Loading branch information
weidongxu-microsoft authored Sep 7, 2020
1 parent 464f387 commit 53dee37
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ public void canCreateVirtualMachine() throws Exception {

@Test
public void canCreateVirtualMachineSyncPoll() throws Exception {
final long defaultDelayInMillis = 10 * 1000;

Accepted<VirtualMachine> acceptedVirtualMachine = computeManager
.virtualMachines()
.define(vmName)
Expand All @@ -215,17 +217,17 @@ public void canCreateVirtualMachineSyncPoll() throws Exception {
Assertions.assertNotEquals("Succeeded", createdVirtualMachine.provisioningState());

LongRunningOperationStatus pollStatus = acceptedVirtualMachine.getActivationResponse().getStatus();
int delayInMills = acceptedVirtualMachine.getActivationResponse().getRetryAfter() == null
? 0
: (int) acceptedVirtualMachine.getActivationResponse().getRetryAfter().toMillis();
long delayInMills = acceptedVirtualMachine.getActivationResponse().getRetryAfter() == null
? defaultDelayInMillis
: acceptedVirtualMachine.getActivationResponse().getRetryAfter().toMillis();
while (!pollStatus.isComplete()) {
SdkContext.sleep(delayInMills);

PollResponse<?> pollResponse = acceptedVirtualMachine.getSyncPoller().poll();
pollStatus = pollResponse.getStatus();
delayInMills = pollResponse.getRetryAfter() == null
? 10000
: (int) pollResponse.getRetryAfter().toMillis();
? defaultDelayInMillis
: pollResponse.getRetryAfter().toMillis();
}
Assertions.assertEquals(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, pollStatus);
VirtualMachine virtualMachine = acceptedVirtualMachine.getFinalResult();
Expand All @@ -236,7 +238,7 @@ public void canCreateVirtualMachineSyncPoll() throws Exception {

pollStatus = acceptedDelete.getActivationResponse().getStatus();
delayInMills = acceptedDelete.getActivationResponse().getRetryAfter() == null
? 0
? defaultDelayInMillis
: (int) acceptedDelete.getActivationResponse().getRetryAfter().toMillis();

while (!pollStatus.isComplete()) {
Expand All @@ -245,7 +247,7 @@ public void canCreateVirtualMachineSyncPoll() throws Exception {
PollResponse<?> pollResponse = acceptedDelete.getSyncPoller().poll();
pollStatus = pollResponse.getStatus();
delayInMills = pollResponse.getRetryAfter() == null
? 10000
? defaultDelayInMillis
: (int) pollResponse.getRetryAfter().toMillis();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.azure.core.management.exception.ManagementException;
import com.azure.core.management.polling.PollResult;
import com.azure.core.management.polling.PollerFactory;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.FluxUtil;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.util.polling.LongRunningOperationStatus;
Expand All @@ -24,6 +25,7 @@
import com.azure.resourcemanager.resources.fluentcore.model.HasInner;
import com.azure.resourcemanager.resources.fluentcore.rest.ActivationResponse;
import com.azure.resourcemanager.resources.fluentcore.utils.SdkContext;
import com.fasterxml.jackson.annotation.JsonProperty;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -79,23 +81,13 @@ public ActivationResponse<T> getActivationResponse() {
Duration retryAfter = getRetryAfter(activationResponse.getHeaders());
return new ActivationResponse<>(activationResponse.getRequest(), activationResponse.getStatusCode(),
activationResponse.getHeaders(), value,
LongRunningOperationStatus.IN_PROGRESS, retryAfter);
getActivationResponseStatus(), retryAfter);
} catch (IOException e) {
throw logger.logExceptionAsError(
new IllegalStateException("Failed to deserialize activation response body", e));
}
}

private static Duration getRetryAfter(HttpHeaders headers) {
if (headers != null) {
final String value = headers.getValue("Retry-After");
if (value != null) {
return Duration.ofSeconds(Long.parseLong(value));
}
}
return null;
}

@Override
public SyncPoller<Void, T> getSyncPoller() {
if (syncPoller == null) {
Expand Down Expand Up @@ -159,6 +151,60 @@ public T getFinalResult() {
return this.getSyncPoller().getFinalResult();
}

private LongRunningOperationStatus getActivationResponseStatus() {
String responseBody = new String(getResponse(), StandardCharsets.UTF_8);
String provisioningState = null;
// try get "provisioningState" property.
if (!CoreUtils.isNullOrEmpty(responseBody)) {
try {
ResourceWithProvisioningState resource = serializerAdapter.deserialize(responseBody,
ResourceWithProvisioningState.class, SerializerEncoding.JSON);
provisioningState = resource != null
? resource.getProvisioningState()
: null;
} catch (IOException ignored) {

}
}

// get LRO status, default is IN_PROGRESS
LongRunningOperationStatus status = LongRunningOperationStatus.IN_PROGRESS;
if (!CoreUtils.isNullOrEmpty(provisioningState)) {
// LRO status based on provisioningState.
status = toLongRunningOperationStatus(provisioningState);
} else {
// LRO status based on status code.
int statusCode = activationResponse.getStatusCode();
if (statusCode == 200 || statusCode == 201 || statusCode == 204) {
status = LongRunningOperationStatus.SUCCESSFULLY_COMPLETED;
}
}
return status;
}

private static LongRunningOperationStatus toLongRunningOperationStatus(String value) {
if (ProvisioningState.SUCCEEDED.equalsIgnoreCase(value)) {
return LongRunningOperationStatus.SUCCESSFULLY_COMPLETED;
} else if (ProvisioningState.FAILED.equalsIgnoreCase(value)) {
return LongRunningOperationStatus.FAILED;
} else if (ProvisioningState.CANCELED.equalsIgnoreCase(value)) {
return LongRunningOperationStatus.USER_CANCELLED;
} else if (ProvisioningState.IN_PROGRESS.equalsIgnoreCase(value)) {
return LongRunningOperationStatus.IN_PROGRESS;
}
return LongRunningOperationStatus.fromString(value, false);
}

private static Duration getRetryAfter(HttpHeaders headers) {
if (headers != null) {
final String value = headers.getValue("Retry-After");
if (value != null) {
return Duration.ofSeconds(Long.parseLong(value));
}
}
return null;
}

private byte[] getResponse() {
if (responseBytes == null) {
responseBytes = FluxUtil.collectBytesInByteBufferStream(activationResponse.getValue()).block();
Expand Down Expand Up @@ -238,6 +284,31 @@ private PollResponse<Void> voidResponse(PollResponse<PollResult<InnerT>> pollRes
}
}

private static class ResourceWithProvisioningState {
@JsonProperty(value = "properties")
private Properties properties;

private String getProvisioningState() {
if (this.properties != null) {
return this.properties.provisioningState;
} else {
return null;
}
}

private static class Properties {
@JsonProperty(value = "provisioningState")
private String provisioningState;
}
}

private static class ProvisioningState {
static final String IN_PROGRESS = "InProgress";
static final String SUCCEEDED = "Succeeded";
static final String FAILED = "Failed";
static final String CANCELED = "Canceled";
}

public static <T, InnerT> Accepted<T> newAccepted(
ClientLogger logger,
AzureServiceClient client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ public void canUpdateVirtualNetworkDeployment() throws Exception {

@Test
public void canDeployVirtualNetworkSyncPoll() throws Exception {
final long defaultDelayInMillis = 10 * 1000;

final String dp = "dpD" + testId;

// Begin create
Expand All @@ -250,17 +252,17 @@ public void canDeployVirtualNetworkSyncPoll() throws Exception {
Assertions.assertNotEquals("Succeeded", createdDeployment.provisioningState());

LongRunningOperationStatus pollStatus = acceptedDeployment.getActivationResponse().getStatus();
int delayInMills = acceptedDeployment.getActivationResponse().getRetryAfter() == null
? 0
: (int) acceptedDeployment.getActivationResponse().getRetryAfter().toMillis();
long delayInMills = acceptedDeployment.getActivationResponse().getRetryAfter() == null
? defaultDelayInMillis
: acceptedDeployment.getActivationResponse().getRetryAfter().toMillis();
while (!pollStatus.isComplete()) {
SdkContext.sleep(delayInMills);

PollResponse<?> pollResponse = acceptedDeployment.getSyncPoller().poll();
pollStatus = pollResponse.getStatus();
delayInMills = pollResponse.getRetryAfter() == null
? 10000
: (int) pollResponse.getRetryAfter().toMillis();
? defaultDelayInMillis
: pollResponse.getRetryAfter().toMillis();
}
Assertions.assertEquals(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, pollStatus);
Deployment deployment = acceptedDeployment.getFinalResult();
Expand All @@ -269,6 +271,8 @@ public void canDeployVirtualNetworkSyncPoll() throws Exception {

@Test
public void canDeployVirtualNetworkSyncPollWithFailure() throws Exception {
final long defaultDelayInMillis = 10 * 1000;

final String templateJson = "{ \"$schema\": \"https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#\", \"contentVersion\": \"1.0.0.0\", \"resources\": [ { \"type\": \"Microsoft.Storage/storageAccounts\", \"apiVersion\": \"2019-04-01\", \"name\": \"satestnameconflict\", \"location\": \"eastus\", \"sku\": { \"name\": \"Standard_LRS\" }, \"kind\": \"StorageV2\", \"properties\": { \"supportsHttpsTrafficOnly\": true } } ] }";

final String dp = "dpE" + testId;
Expand All @@ -284,17 +288,17 @@ public void canDeployVirtualNetworkSyncPollWithFailure() throws Exception {
Assertions.assertNotEquals("Succeeded", createdDeployment.provisioningState());

LongRunningOperationStatus pollStatus = acceptedDeployment.getActivationResponse().getStatus();
int delayInMills = acceptedDeployment.getActivationResponse().getRetryAfter() == null
? 0
: (int) acceptedDeployment.getActivationResponse().getRetryAfter().toMillis();
long delayInMills = acceptedDeployment.getActivationResponse().getRetryAfter() == null
? defaultDelayInMillis
: acceptedDeployment.getActivationResponse().getRetryAfter().toMillis();
while (!pollStatus.isComplete()) {
SdkContext.sleep(delayInMills);

PollResponse<?> pollResponse = acceptedDeployment.getSyncPoller().poll();
pollStatus = pollResponse.getStatus();
delayInMills = pollResponse.getRetryAfter() == null
? 10000
: (int) pollResponse.getRetryAfter().toMillis();
? defaultDelayInMillis
: pollResponse.getRetryAfter().toMillis();
}
Assertions.assertEquals(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, pollStatus);
Deployment deployment = acceptedDeployment.getFinalResult();
Expand All @@ -317,15 +321,15 @@ public void canDeployVirtualNetworkSyncPollWithFailure() throws Exception {

pollStatus = acceptedDeployment.getActivationResponse().getStatus();
delayInMills = acceptedDeployment.getActivationResponse().getRetryAfter() == null
? 0
? defaultDelayInMillis
: (int) acceptedDeployment.getActivationResponse().getRetryAfter().toMillis();
while (!pollStatus.isComplete()) {
SdkContext.sleep(delayInMills);

PollResponse<?> pollResponse = acceptedDeployment.getSyncPoller().poll();
pollStatus = pollResponse.getStatus();
delayInMills = pollResponse.getRetryAfter() == null
? 10000
? defaultDelayInMillis
: (int) pollResponse.getRetryAfter().toMillis();
}
Assertions.assertEquals(LongRunningOperationStatus.FAILED, pollStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public void canCreateUpdateMoveResource() throws Exception {

@Test
public void canCreateDeleteResourceSyncPoll() throws Exception {
final long defaultDelayInMillis = 10 * 1000;

final String resourceName = "rs" + testId;
// Create
Accepted<GenericResource> acceptedResource = genericResources.define(resourceName)
Expand All @@ -110,17 +112,17 @@ public void canCreateDeleteResourceSyncPoll() throws Exception {
.beginCreate();

LongRunningOperationStatus pollStatus = acceptedResource.getActivationResponse().getStatus();
int delayInMills = acceptedResource.getActivationResponse().getRetryAfter() == null
? 0
: (int) acceptedResource.getActivationResponse().getRetryAfter().toMillis();
long delayInMills = acceptedResource.getActivationResponse().getRetryAfter() == null
? defaultDelayInMillis
: acceptedResource.getActivationResponse().getRetryAfter().toMillis();
while (!pollStatus.isComplete()) {
SdkContext.sleep(delayInMills);

PollResponse<?> pollResponse = acceptedResource.getSyncPoller().poll();
pollStatus = pollResponse.getStatus();
delayInMills = pollResponse.getRetryAfter() == null
? 10000
: (int) pollResponse.getRetryAfter().toMillis();
? defaultDelayInMillis
: pollResponse.getRetryAfter().toMillis();
}
Assertions.assertEquals(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, pollStatus);
GenericResource resource = acceptedResource.getFinalResult();
Expand Down
Loading

0 comments on commit 53dee37

Please sign in to comment.