Skip to content

Commit

Permalink
mgmt, container group, add beginCreate (#42356)
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft authored Oct 16, 2024
1 parent 253a830 commit d7aba01
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

### Features Added

### Breaking Changes

### Bugs Fixed

### Other Changes
- Supported `beginCreate` method for `ContainerGroup`.

## 2.44.0-beta.1 (2024-10-08)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "java",
"TagPrefix": "java/resourcemanager/azure-resourcemanager-containerinstance",
"Tag": "java/resourcemanager/azure-resourcemanager-containerinstance_b66810ce1f"
"Tag": "java/resourcemanager/azure-resourcemanager-containerinstance_67378ea46f"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package com.azure.resourcemanager.containerinstance.implementation;

import com.azure.core.management.Resource;
import com.azure.core.util.Context;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.logging.ClientLogger;
import com.azure.resourcemanager.authorization.utils.RoleAssignmentHelper;
import com.azure.resourcemanager.authorization.models.BuiltInRole;
import com.azure.resourcemanager.containerinstance.ContainerInstanceManager;
Expand Down Expand Up @@ -36,7 +39,10 @@
import com.azure.resourcemanager.network.models.Subnet;
import com.azure.resourcemanager.resources.fluentcore.arm.ResourceUtils;
import com.azure.resourcemanager.resources.fluentcore.arm.models.implementation.GroupableParentResourceImpl;
import com.azure.resourcemanager.resources.fluentcore.model.Accepted;
import com.azure.resourcemanager.resources.fluentcore.model.Creatable;
import com.azure.resourcemanager.resources.fluentcore.model.Indexable;
import com.azure.resourcemanager.resources.fluentcore.model.implementation.AcceptedImpl;
import com.azure.resourcemanager.resources.fluentcore.utils.ResourceManagerUtils;
import com.azure.resourcemanager.storage.models.StorageAccount;
import com.azure.storage.file.share.ShareServiceAsyncClient;
Expand All @@ -60,6 +66,8 @@ public class ContainerGroupImpl
ContainerGroup, ContainerGroupInner, ContainerGroupImpl, ContainerInstanceManager>
implements ContainerGroup, ContainerGroup.Definition, ContainerGroup.Update {

private final ClientLogger logger = new ClientLogger(ContainerGroupImpl.class);

private String creatableStorageAccountKey;
private Creatable<Network> creatableVirtualNetwork;
private Map<String, String> newFileShares;
Expand Down Expand Up @@ -133,10 +141,58 @@ protected Mono<ContainerGroupInner> createInner() {
}
}

private static class VolumeParameters {
private String volumeName;
private String fileShareName;
private String storageAccountKey;
@Override
public Accepted<ContainerGroup> beginCreate() {
return AcceptedImpl
.<ContainerGroup, ContainerGroupInner>newAccepted(
logger,
this.manager().serviceClient().getHttpPipeline(),
this.manager().serviceClient().getDefaultPollInterval(),
() ->
this
.manager()
.serviceClient()
.getContainerGroups()
.createOrUpdateWithResponseAsync(resourceGroupName(), name(), innerModel())
.block(),
inner ->
new ContainerGroupImpl(
inner.name(),
inner,
this.manager()),
ContainerGroupInner.class,
() -> {
Flux<Indexable> dependencyTasksAsync =
taskGroup().invokeDependencyAsync(taskGroup().newInvocationContext());
dependencyTasksAsync.blockLast();

// same as createInner
beforeCreation().block();
// msi
this.containerGroupMsiHandler.processCreatedExternalIdentities();
this.containerGroupMsiHandler.handleExternalIdentities();
// storage account
if (!(newFileShares == null || creatableStorageAccountKey == null)) {
final StorageAccount storageAccount = this.taskResult(this.creatableStorageAccountKey);
List<VolumeParameters> volumeParametersList = createFileShareAsync(storageAccount).collectList().block();
if (!CoreUtils.isNullOrEmpty(volumeParametersList)) {
for (VolumeParameters volumeParameters : volumeParametersList) {
this.defineVolume(volumeParameters.volumeName)
.withExistingReadWriteAzureFileShare(volumeParameters.fileShareName)
.withStorageAccountName(storageAccount.name())
.withStorageAccountKey(volumeParameters.storageAccountKey)
.attach();
}
}
}
},
Context.NONE);
}

private static final class VolumeParameters {
private final String volumeName;
private final String fileShareName;
private final String storageAccountKey;

VolumeParameters(String volumeName, String fileShareName, String storageAccountKey) {
this.volumeName = volumeName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.azure.resourcemanager.network.models.Subnet;
import com.azure.resourcemanager.resources.fluentcore.arm.models.GroupableResource;
import com.azure.resourcemanager.resources.fluentcore.arm.models.Resource;
import com.azure.resourcemanager.resources.fluentcore.model.Accepted;
import com.azure.resourcemanager.resources.fluentcore.model.Appliable;
import com.azure.resourcemanager.resources.fluentcore.model.Attachable;
import com.azure.resourcemanager.resources.fluentcore.model.Creatable;
Expand All @@ -20,6 +21,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;

import reactor.core.publisher.Mono;

/** An immutable client-side representation of an Azure Container Group. */
Expand Down Expand Up @@ -1309,6 +1311,13 @@ interface WithCreate
WithLogAnalytics,
Creatable<ContainerGroup>,
Resource.DefinitionWithTags<WithCreate> {

/**
* Begins creating the deployment resource.
*
* @return the accepted create operation
*/
Accepted<ContainerGroup> beginCreate();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.azure.resourcemanager.containerinstance.models.ContainerState;
import com.azure.resourcemanager.containerinstance.models.Scheme;
import com.azure.resourcemanager.network.models.Network;
import com.azure.resourcemanager.resources.fluentcore.model.Accepted;
import com.azure.resourcemanager.resources.fluentcore.utils.ResourceManagerUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -26,7 +27,7 @@ public class ContainerGroupTest extends ContainerInstanceManagementTest {
@Test
public void testContainerGroupWithVirtualNetwork() {
String containerGroupName = generateRandomResourceName("container", 20);
Region region = Region.US_EAST;
Region region = Region.US_WEST3;

ContainerGroup containerGroup =
containerInstanceManager
Expand Down Expand Up @@ -180,4 +181,56 @@ public void testCreateWithLivenessAndReadiness() {
Assertions.assertTrue(durationBeforeRestart.compareTo(Duration.ofSeconds(healthySeconds + probePeriodSeconds * (failureThreshold - 1))) > 0);
Assertions.assertTrue(durationBeforeRestart.compareTo(Duration.ofSeconds(healthySeconds + probePeriodSeconds * failureThreshold)) <= 0);
}

@Test
public void testBeginCreate() {
final String succeededState = "Succeeded";
String containerGroupName = generateRandomResourceName("container", 20);
Region region = Region.US_WEST3;

// create resource group and virtual network, but no storage account, before create container group
Accepted<ContainerGroup> acceptedContainerGroup =
containerInstanceManager
.containerGroups()
.define(containerGroupName)
.withRegion(region)
.withNewResourceGroup(rgName)
.withLinux()
.withPublicImageRegistryOnly()
.withoutVolume()
.withContainerInstance("nginx", 80)
.withNewVirtualNetwork("10.0.0.0/24")
.beginCreate();

ContainerGroup createdContainerGroup = acceptedContainerGroup.getActivationResponse().getValue();
Assertions.assertNotEquals(succeededState, createdContainerGroup.provisioningState());

ContainerGroup containerGroup = acceptedContainerGroup.getSyncPoller().getFinalResult();
Assertions.assertEquals(succeededState, containerGroup.provisioningState());
}

// test contains a data-plane call
@DoNotRecord(skipInPlayback = true)
@Test
public void testBeginCreateWithFileShareVolume() {
String containerGroupName = generateRandomResourceName("container", 20);
Region region = Region.US_WEST3;

// create storage account (and virtual network), before create container group
Accepted<ContainerGroup> acceptedContainerGroup =
containerInstanceManager
.containerGroups()
.define(containerGroupName)
.withRegion(region)
.withNewResourceGroup(rgName)
.withLinux()
.withPublicImageRegistryOnly()
// definition step only allow creating one file share volume
.withNewAzureFileShareVolume("vol1", "share1")
.withContainerInstance("nginx", 80)
.withNewVirtualNetwork("10.0.0.0/24")
.beginCreate();
ContainerGroup containerGroup = acceptedContainerGroup.getSyncPoller().getFinalResult();
Assertions.assertEquals(1, containerGroup.volumes().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,8 @@ private void addSanitizers() {
new TestProxySanitizer("$..passwords[*].value", null, REDACTED_VALUE, TestProxySanitizerType.BODY_KEY),
// ContainerService
new TestProxySanitizer("$..secret", null, REDACTED_VALUE, TestProxySanitizerType.BODY_KEY),
// ContainerInstance
new TestProxySanitizer("$..storageAccountKey", null, REDACTED_VALUE, TestProxySanitizerType.BODY_KEY),
// AppService
new TestProxySanitizer("$.properties.siteConfig.machineKey.decryptionKey", null, REDACTED_VALUE, TestProxySanitizerType.BODY_KEY),
// Replace "AccountKey=<accountKey>;" with "AccountKey=REDACTED;"
Expand Down

0 comments on commit d7aba01

Please sign in to comment.