Skip to content

Commit 5ed956b

Browse files
mgmt, fix compute MSI sample (Azure#27849)
1 parent e94441d commit 5ed956b

File tree

6 files changed

+1156
-1792
lines changed

6 files changed

+1156
-1792
lines changed

sdk/resourcemanager/azure-resourcemanager-samples/src/main/java/com/azure/resourcemanager/compute/samples/ManageResourceFromMSIEnabledVirtualMachineBelongsToAADGroup.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,23 @@ public static boolean runSample(AzureResourceManager azureResourceManager) {
5151
final String pipName = Utils.randomResourceName(azureResourceManager, "pip1", 15);
5252
final String userName = "tirekicker";
5353
final String sshPublicKey = Utils.sshPublicKey();
54-
final Region region = Region.US_SOUTH_CENTRAL;
54+
final Region region = Region.US_EAST;
5555

5656
final String installScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/main/sdk/resourcemanager/azure-resourcemanager-samples/src/main/resources/create_resources_with_msi.sh";
5757
String installCommand = "bash create_resources_with_msi.sh {stgName} {rgName} {location}";
5858
List<String> fileUris = new ArrayList<>();
5959
fileUris.add(installScript);
6060

61+
ActiveDirectoryGroup activeDirectoryGroup = null;
62+
6163
try {
6264

6365
//=============================================================
6466
// Create a AAD security group
6567

6668
System.out.println("Creating a AAD security group");
6769

68-
ActiveDirectoryGroup activeDirectoryGroup = azureResourceManager.accessManagement()
70+
activeDirectoryGroup = azureResourceManager.accessManagement()
6971
.activeDirectoryGroups()
7072
.define(groupName)
7173
.withEmailAlias(groupName)
@@ -127,11 +129,11 @@ public static boolean runSample(AzureResourceManager azureResourceManager) {
127129

128130
System.out.println("Added virtual machine MSI service principal to the AAD group");
129131

130-
System.out.println("Waiting 15 minutes to MSI extension in the VM to refresh the token");
132+
System.out.println("Waiting 10 minutes to MSI extension in the VM to refresh the token");
131133

132134
ResourceManagerUtils.sleep(Duration.ofMinutes(10));
133135

134-
// Prepare custom script t install az cli that uses MSI to create a storage account
136+
// Prepare custom script to install az cli that uses MSI to create a storage account
135137
//
136138
final String stgName = Utils.randomResourceName(azureResourceManager, "st44", 15);
137139
installCommand = installCommand.replace("{stgName}", stgName)
@@ -145,10 +147,10 @@ public static boolean runSample(AzureResourceManager azureResourceManager) {
145147

146148
virtualMachine
147149
.update()
148-
.defineNewExtension("CustomScriptForLinux")
149-
.withPublisher("Microsoft.OSTCExtensions")
150-
.withType("CustomScriptForLinux")
151-
.withVersion("1.4")
150+
.defineNewExtension("az-create-storage-account")
151+
.withPublisher("Microsoft.Azure.Extensions")
152+
.withType("CustomScript")
153+
.withVersion("2.1")
152154
.withMinorVersionAutoUpgrade()
153155
.withPublicSetting("fileUris", fileUris)
154156
.withPublicSetting("commandToExecute", installCommand)
@@ -164,6 +166,15 @@ public static boolean runSample(AzureResourceManager azureResourceManager) {
164166
Utils.print(storageAccount);
165167
return true;
166168
} finally {
169+
try {
170+
if (activeDirectoryGroup != null) {
171+
System.out.println("Deleting AAD Group: " + activeDirectoryGroup.name());
172+
azureResourceManager.accessManagement().activeDirectoryGroups()
173+
.deleteById(activeDirectoryGroup.id());
174+
}
175+
} catch (Exception e) {
176+
e.printStackTrace();
177+
}
167178
try {
168179
System.out.println("Deleting Resource Group: " + rgName);
169180
azureResourceManager.resourceGroups().beginDeleteByName(rgName);

sdk/resourcemanager/azure-resourcemanager-samples/src/main/java/com/azure/resourcemanager/compute/samples/ManageStorageFromMSIEnabledVirtualMachine.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static boolean runSample(AzureResourceManager azureResourceManager) {
4242
final String pipName = Utils.randomResourceName(azureResourceManager, "pip1", 15);
4343
final String userName = "tirekicker";
4444
final String sshPublicKey = Utils.sshPublicKey();
45-
final Region region = Region.US_WEST_CENTRAL;
45+
final Region region = Region.US_EAST;
4646

4747
final String installScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/main/sdk/resourcemanager/azure-resourcemanager-samples/src/main/resources/create_resources_with_msi.sh";
4848
String installCommand = "bash create_resources_with_msi.sh {stgName} {rgName} {location}";
@@ -74,7 +74,7 @@ public static boolean runSample(AzureResourceManager azureResourceManager) {
7474
System.out.println("Created virtual machine with MSI enabled");
7575
Utils.print(virtualMachine);
7676

77-
// Prepare custom script t install az cli that uses MSI to create a storage account
77+
// Prepare custom script to install az cli that uses MSI to create a storage account
7878
//
7979
final String stgName = Utils.randomResourceName(azureResourceManager, "st44", 15);
8080
installCommand = installCommand.replace("{stgName}", stgName)
@@ -88,10 +88,10 @@ public static boolean runSample(AzureResourceManager azureResourceManager) {
8888

8989
virtualMachine
9090
.update()
91-
.defineNewExtension("CustomScriptForLinux")
92-
.withPublisher("Microsoft.OSTCExtensions")
93-
.withType("CustomScriptForLinux")
94-
.withVersion("1.4")
91+
.defineNewExtension("az-create-storage-account")
92+
.withPublisher("Microsoft.Azure.Extensions")
93+
.withType("CustomScript")
94+
.withVersion("2.1")
9595
.withMinorVersionAutoUpgrade()
9696
.withPublicSetting("fileUris", fileUris)
9797
.withPublicSetting("commandToExecute", installCommand)
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/bin/bash
22

3-
/usr/bin/yes | sudo apt-get update
4-
/usr/bin/yes | sudo apt install python-pip
5-
/usr/bin/yes | sudo pip install --upgrade pip
6-
sudo pip install azure-cli
7-
az login --msi
8-
az storage account create -n $1 -g $2 -l $3 --sku Premium_LRS
3+
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
4+
5+
az login --identity
6+
az storage account create -n $1 -g $2 -l $3 --sku Premium_LRS

sdk/resourcemanager/azure-resourcemanager-samples/src/test/java/com/azure/resourcemanager/samples/ComputeSampleTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,11 @@ public void testManageManagedDisks() {
150150
}
151151

152152
@Test
153-
@Disabled("Skipping for now - looks like a service side issue")
154153
public void testManageStorageFromMSIEnabledVirtualMachine() {
155154
Assertions.assertTrue(ManageStorageFromMSIEnabledVirtualMachine.runSample(azureResourceManager));
156155
}
157156

158157
@Test
159-
@Disabled("Skipping for now - looks like a service side issue")
160158
public void testManageResourceFromMSIEnabledVirtualMachineBelongsToAADGroup() {
161159
Assertions.assertTrue(ManageResourceFromMSIEnabledVirtualMachineBelongsToAADGroup.runSample(azureResourceManager));
162160
}

0 commit comments

Comments
 (0)