-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
328a47b
commit 5a1a8b4
Showing
4 changed files
with
1,671 additions
and
1,431 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,31 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for | ||
* license information. | ||
*/ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.resourcemanager.compute.samples; | ||
|
||
import com.azure.core.credential.TokenCredential; | ||
import com.azure.core.http.policy.HttpLogDetailLevel; | ||
import com.azure.core.http.rest.PagedIterable; | ||
import com.azure.core.management.AzureEnvironment; | ||
import com.azure.identity.DefaultAzureCredentialBuilder; | ||
import com.azure.resourcemanager.AzureResourceManager; | ||
import com.azure.resourcemanager.compute.models.CachingTypes; | ||
import com.azure.resourcemanager.compute.models.ImageReference; | ||
import com.azure.resourcemanager.compute.models.StorageAccountTypes; | ||
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSetVM; | ||
import com.azure.resourcemanager.network.models.LoadBalancerInboundNatRule; | ||
import com.azure.resourcemanager.network.models.Network; | ||
import com.azure.resourcemanager.network.models.PublicIpAddress; | ||
import com.azure.resourcemanager.network.models.LoadBalancer; | ||
import com.azure.resourcemanager.network.models.TransportProtocol; | ||
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSet; | ||
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSetSkuTypes; | ||
import com.azure.resourcemanager.network.models.VirtualMachineScaleSetNetworkInterface; | ||
import com.azure.resourcemanager.network.models.VirtualMachineScaleSetNicIpConfiguration; | ||
import com.azure.core.management.Region; | ||
import com.azure.core.management.profile.AzureProfile; | ||
import com.azure.resourcemanager.samples.Utils; | ||
|
||
package com.microsoft.azure.management.compute.samples; | ||
|
||
import com.microsoft.azure.PagedList; | ||
import com.microsoft.azure.management.Azure; | ||
import com.microsoft.azure.management.compute.CachingTypes; | ||
import com.microsoft.azure.management.compute.ImageReference; | ||
import com.microsoft.azure.management.compute.StorageAccountTypes; | ||
import com.microsoft.azure.management.compute.VirtualMachineScaleSetVM; | ||
import com.microsoft.azure.management.network.LoadBalancerInboundNatRule; | ||
import com.microsoft.azure.management.network.Network; | ||
import com.microsoft.azure.management.network.PublicIPAddress; | ||
import com.microsoft.azure.management.network.LoadBalancer; | ||
import com.microsoft.azure.management.network.TransportProtocol; | ||
import com.microsoft.azure.management.compute.VirtualMachineScaleSet; | ||
import com.microsoft.azure.management.compute.VirtualMachineScaleSetSkuTypes; | ||
import com.microsoft.azure.management.network.VirtualMachineScaleSetNetworkInterface; | ||
import com.microsoft.azure.management.network.VirtualMachineScaleSetNicIPConfiguration; | ||
import com.microsoft.azure.management.resources.fluentcore.arm.Region; | ||
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext; | ||
import com.microsoft.azure.management.samples.Utils; | ||
import com.microsoft.rest.LogLevel; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
@@ -47,14 +46,14 @@ public final class ManageVirtualMachineScaleSet { | |
|
||
/** | ||
* Main function which runs the actual sample. | ||
* @param azure instance of the azure client | ||
* @param azureResourceManager instance of the azure client | ||
* @return true if sample runs successfully | ||
*/ | ||
public static boolean runSample(Azure azure) { | ||
public static boolean runSample(AzureResourceManager azureResourceManager) { | ||
final Region region = Region.US_EAST2; | ||
final String rgName = SdkContext.randomResourceName("rgCOVS", 15); | ||
final String vnetName = SdkContext.randomResourceName("vnet", 24); | ||
final String loadBalancerName1 = SdkContext.randomResourceName("intlb" + "-", 18); | ||
final String rgName = Utils.randomResourceName(azureResourceManager, "rgCOVS", 15); | ||
final String vnetName = Utils.randomResourceName(azureResourceManager, "vnet", 24); | ||
final String loadBalancerName1 = Utils.randomResourceName(azureResourceManager, "intlb" + "-", 18); | ||
final String publicIpName = "pip-" + loadBalancerName1; | ||
final String frontendName = loadBalancerName1 + "-FE1"; | ||
final String backendPoolName1 = loadBalancerName1 + "-BAP1"; | ||
|
@@ -66,12 +65,12 @@ public static boolean runSample(Azure azure) { | |
final String httpsLoadBalancingRule = "httpsRule"; | ||
final String natPool50XXto22 = "natPool50XXto22"; | ||
final String natPool60XXto23 = "natPool60XXto23"; | ||
final String vmssName = SdkContext.randomResourceName("vmss", 24); | ||
final String vmssName = Utils.randomResourceName(azureResourceManager, "vmss", 24); | ||
|
||
final String userName = "tirekicker"; | ||
final String sshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD [email protected]"; | ||
|
||
final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-libraries-for-java/master/azure-samples/src/main/resources/install_apache.sh"; | ||
final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/resourcemanager/azure-resourcemanager-samples/src/main/resources/install_apache.sh"; | ||
final String installCommand = "bash install_apache.sh"; | ||
List<String> fileUris = new ArrayList<>(); | ||
fileUris.add(apacheInstallScript); | ||
|
@@ -81,7 +80,7 @@ public static boolean runSample(Azure azure) { | |
// Create a virtual network with a frontend subnet | ||
System.out.println("Creating virtual network with a frontend subnet ..."); | ||
|
||
Network network = azure.networks().define(vnetName) | ||
Network network = azureResourceManager.networks().define(vnetName) | ||
.withRegion(region) | ||
.withNewResourceGroup(rgName) | ||
.withAddressSpace("172.16.0.0/16") | ||
|
@@ -98,7 +97,7 @@ public static boolean runSample(Azure azure) { | |
// Create a public IP address | ||
System.out.println("Creating a public IP address..."); | ||
|
||
PublicIPAddress publicIPAddress = azure.publicIPAddresses().define(publicIpName) | ||
PublicIpAddress publicIPAddress = azureResourceManager.publicIpAddresses().define(publicIpName) | ||
.withRegion(region) | ||
.withExistingResourceGroup(rgName) | ||
.withLeafDomainLabel(publicIpName) | ||
|
@@ -133,7 +132,7 @@ public static boolean runSample(Azure azure) { | |
+ " balancer to a port for a specific virtual machine in the backend address pool\n" | ||
+ " - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23"); | ||
|
||
LoadBalancer loadBalancer1 = azure.loadBalancers().define(loadBalancerName1) | ||
LoadBalancer loadBalancer1 = azureResourceManager.loadBalancers().define(loadBalancerName1) | ||
.withRegion(region) | ||
.withExistingResourceGroup(rgName) | ||
// Add two rules that uses above backend and probe | ||
|
@@ -169,7 +168,7 @@ public static boolean runSample(Azure azure) { | |
|
||
// Explicitly define the frontend | ||
.definePublicFrontend(frontendName) | ||
.withExistingPublicIPAddress(publicIPAddress) | ||
.withExistingPublicIpAddress(publicIPAddress) | ||
.attach() | ||
|
||
// Add two probes one per rule | ||
|
@@ -204,7 +203,7 @@ public static boolean runSample(Azure azure) { | |
.withSku("18.04-LTS") | ||
.withVersion("latest"); | ||
|
||
VirtualMachineScaleSet virtualMachineScaleSet = azure.virtualMachineScaleSets().define(vmssName) | ||
VirtualMachineScaleSet virtualMachineScaleSet = azureResourceManager.virtualMachineScaleSets().define(vmssName) | ||
.withRegion(region) | ||
.withExistingResourceGroup(rgName) | ||
.withSku(VirtualMachineScaleSetSkuTypes.STANDARD_D3_V2) | ||
|
@@ -238,13 +237,13 @@ public static boolean runSample(Azure azure) { | |
System.out.println(); | ||
|
||
// Print virtual machine scale set details | ||
// Utils.print(virtualMachineScaleSet); | ||
// ResourceManagerUtils.print(virtualMachineScaleSet); | ||
|
||
//============================================================= | ||
// List virtual machine scale set network interfaces | ||
|
||
System.out.println("Listing scale set network interfaces ..."); | ||
PagedList<VirtualMachineScaleSetNetworkInterface> vmssNics = virtualMachineScaleSet.listNetworkInterfaces(); | ||
PagedIterable<VirtualMachineScaleSetNetworkInterface> vmssNics = virtualMachineScaleSet.listNetworkInterfaces(); | ||
for (VirtualMachineScaleSetNetworkInterface vmssNic : vmssNics) { | ||
System.out.println(vmssNic.id()); | ||
} | ||
|
@@ -256,10 +255,10 @@ public static boolean runSample(Azure azure) { | |
for (VirtualMachineScaleSetVM instance : virtualMachineScaleSet.virtualMachines().list()) { | ||
System.out.println("Scale set virtual machine instance #" + instance.instanceId()); | ||
System.out.println(instance.id()); | ||
PagedList<VirtualMachineScaleSetNetworkInterface> networkInterfaces = instance.listNetworkInterfaces(); | ||
PagedIterable<VirtualMachineScaleSetNetworkInterface> networkInterfaces = instance.listNetworkInterfaces(); | ||
// Pick the first NIC | ||
VirtualMachineScaleSetNetworkInterface networkInterface = networkInterfaces.get(0); | ||
for (VirtualMachineScaleSetNicIPConfiguration ipConfig :networkInterface.ipConfigurations().values()) { | ||
VirtualMachineScaleSetNetworkInterface networkInterface = networkInterfaces.iterator().next(); | ||
for (VirtualMachineScaleSetNicIpConfiguration ipConfig :networkInterface.ipConfigurations().values()) { | ||
if (ipConfig.isPrimary()) { | ||
List<LoadBalancerInboundNatRule> natRules = ipConfig.listAssociatedLoadBalancerInboundNatRules(); | ||
for (LoadBalancerInboundNatRule natRule : natRules) { | ||
|
@@ -329,23 +328,17 @@ public static boolean runSample(Azure azure) { | |
System.out.println("re-started virtual machine scale set"); | ||
|
||
return true; | ||
} catch (Exception f) { | ||
|
||
System.out.println(f.getMessage()); | ||
f.printStackTrace(); | ||
|
||
} finally { | ||
try { | ||
System.out.println("Deleting Resource Group: " + rgName); | ||
azure.resourceGroups().deleteByName(rgName); | ||
azureResourceManager.resourceGroups().beginDeleteByName(rgName); | ||
System.out.println("Deleted Resource Group: " + rgName); | ||
} catch (NullPointerException npe) { | ||
System.out.println("Did not create any resources in Azure. No clean up is necessary"); | ||
} catch (Exception g) { | ||
g.printStackTrace(); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
|
@@ -359,18 +352,21 @@ public static void main(String[] args) { | |
//============================================================= | ||
// Authenticate | ||
|
||
System.out.println(System.getenv("AZURE_AUTH_LOCATION")); | ||
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION")); | ||
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); | ||
final TokenCredential credential = new DefaultAzureCredentialBuilder() | ||
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint()) | ||
.build(); | ||
|
||
Azure azure = Azure.configure() | ||
.withLogLevel(LogLevel.BASIC) | ||
.authenticate(credFile) | ||
.withDefaultSubscription(); | ||
AzureResourceManager azureResourceManager = AzureResourceManager | ||
.configure() | ||
.withLogLevel(HttpLogDetailLevel.BASIC) | ||
.authenticate(credential, profile) | ||
.withDefaultSubscription(); | ||
|
||
// Print selected subscription | ||
System.out.println("Selected subscription: " + azure.subscriptionId()); | ||
System.out.println("Selected subscription: " + azureResourceManager.subscriptionId()); | ||
|
||
runSample(azure); | ||
runSample(azureResourceManager); | ||
|
||
} catch (Exception e) { | ||
System.out.println(e.getMessage()); | ||
|
Oops, something went wrong.