Skip to content

Commit

Permalink
Add waits to publuc container creation
Browse files Browse the repository at this point in the history
  • Loading branch information
harjain99 committed Aug 12, 2024
1 parent 37abdbf commit 5ff7863
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/main/java/gyro/azure/storage/CloudBlobContainerResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import com.azure.core.http.rest.Response;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobContainerAccessPolicies;
import com.azure.storage.blob.models.PublicAccessType;
import com.azure.storage.blob.options.BlobContainerCreateOptions;
import gyro.azure.AzureResource;
import gyro.azure.Copyable;
import gyro.core.GyroUI;
import gyro.core.Type;
import gyro.core.Wait;
import gyro.core.resource.Id;
import gyro.core.resource.Output;
import gyro.core.resource.Resource;
Expand Down Expand Up @@ -150,20 +154,34 @@ public boolean refresh() {
public void create(GyroUI ui, State state) {
BlobContainerClient blobContainer = blobContainer();

blobContainer.create();

blobContainer = blobContainer();

state.save();
BlobContainerCreateOptions blobContainerCreateOptions = new BlobContainerCreateOptions();

if (getPublicAccess() != null) {
blobContainer.setAccessPolicy(PublicAccessType.fromString(getPublicAccess()), null);
blobContainerCreateOptions.setPublicAccessType(PublicAccessType.fromString(getPublicAccess()));
}

if (!getMetadata().isEmpty()) {
blobContainer.setMetadata(getMetadata());
}
if (!getMetadata().isEmpty()) {
blobContainerCreateOptions.setMetadata(getMetadata());
}

// We add this wait to create as sometimes there is a delay for the StorageAccount to actually become public.
// This results in a 409 error that gets thrown
// Unfortunately th api doesn't really provide a good way to determine if the account is actually public.
Wait.atMost(2, TimeUnit.MINUTES)
.prompt(false)
.checkEvery(10, TimeUnit.SECONDS)
.until(() -> {
Response<Boolean> response =
blobContainer.createIfNotExistsWithResponse(blobContainerCreateOptions, null, null);

if (response.getStatusCode() == 409) {
return false;

} else {
return true;
}
});

copyFrom(blobContainer);
}

Expand Down

0 comments on commit 5ff7863

Please sign in to comment.