Created
December 21, 2017 07:04
-
-
Save Walter-Shui/73a827fb81eb15788c60a11bb5dc097e to your computer and use it in GitHub Desktop.
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
/** | |
* Copyright (c) Microsoft Corporation. All rights reserved. | |
* Licensed under the MIT License. See License.txt in the project root for | |
* license information. | |
*/ | |
package com.microsoft.azure.management.compute.samples; | |
import com.microsoft.azure.AzureEnvironment; | |
import com.microsoft.azure.credentials.ApplicationTokenCredentials; | |
import com.microsoft.azure.management.Azure; | |
import com.microsoft.azure.management.Azure.Authenticated; | |
import com.microsoft.azure.management.compute.Disk; | |
import com.microsoft.azure.management.compute.KnownLinuxVirtualMachineImage; | |
import com.microsoft.azure.management.compute.KnownWindowsVirtualMachineImage; | |
import com.microsoft.azure.management.compute.VirtualMachine; | |
import com.microsoft.azure.management.compute.VirtualMachineSizeTypes; | |
import com.microsoft.azure.management.network.Network; | |
import com.microsoft.azure.management.resources.fluentcore.arm.Region; | |
import com.microsoft.azure.management.resources.fluentcore.model.Creatable; | |
import com.microsoft.azure.management.samples.Utils; | |
import com.microsoft.rest.LogLevel; | |
import java.io.File; | |
import java.util.Date; | |
/** | |
* Azure Compute sample for managing virtual machines - | |
* - Create a virtual machine with managed OS Disk | |
* - Start a virtual machine | |
* - Stop a virtual machine | |
* - Restart a virtual machine | |
* - Update a virtual machine | |
* - Tag a virtual machine (there are many possible variations here) | |
* - Attach data disks | |
* - Detach data disks | |
* - List virtual machines | |
* - Delete a virtual machine. | |
*/ | |
public final class ManageVirtualMachine { | |
/** | |
* Main function which runs the actual sample. | |
* @param azure instance of the azure client | |
* @return true if sample runs successfully | |
*/ | |
public static boolean runSample(Azure azure) { | |
final Region region = Region.US_WEST_CENTRAL; | |
final String windowsVMName = Utils.createRandomName("wVM"); | |
final String linuxVMName = Utils.createRandomName("lVM"); | |
final String rgName = Utils.createRandomName("rgCOMV"); | |
final String userName = "tirekicker"; | |
final String password = "12NewPA$$w0rd!"; | |
try { | |
//============================================================= | |
// Create a Windows virtual machine | |
// Prepare a creatable data disk for VM | |
// | |
Creatable<Disk> dataDiskCreatable = azure.disks().define(Utils.createRandomName("dsk-")) | |
.withRegion(region) | |
.withExistingResourceGroup(rgName) | |
.withData() | |
.withSizeInGB(100); | |
// Create a data disk to attach to VM | |
// | |
Disk dataDisk = azure.disks() | |
.define(Utils.createRandomName("dsk-")) | |
.withRegion(region) | |
.withNewResourceGroup(rgName) | |
.withData() | |
.withSizeInGB(50) | |
.create(); | |
System.out.println("Creating a Windows VM"); | |
Date t1 = new Date(); | |
VirtualMachine windowsVM = azure.virtualMachines() | |
.define(windowsVMName) | |
.withRegion(region) | |
.withNewResourceGroup(rgName) | |
.withNewPrimaryNetwork("10.0.0.0/28") | |
.withPrimaryPrivateIPAddressDynamic() | |
.withoutPrimaryPublicIPAddress() | |
.withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_R2_DATACENTER) | |
.withAdminUsername(userName) | |
.withAdminPassword(password) | |
.withNewDataDisk(10) | |
.withNewDataDisk(dataDiskCreatable) | |
.withExistingDataDisk(dataDisk) | |
.withSize(VirtualMachineSizeTypes.STANDARD_D3_V2) | |
.create(); | |
Date t2 = new Date(); | |
System.out.println("Created VM: (took " + ((t2.getTime() - t1.getTime()) / 1000) + " seconds) " + windowsVM.id()); | |
// Print virtual machine details | |
Utils.print(windowsVM); | |
windowsVM.update() | |
.defineNewExtension("shuitest") | |
.withPublisher("Microsoft.Compute") | |
.withType("CustomScriptExtension") | |
.withVersion("1.9") | |
.withMinorVersionAutoUpgrade() | |
.withPublicSetting("commandToExecute", "netsh advfirewall firewall add rule name=\"Open Port 8077\" dir=in action=allow protocol=TCP localport=8077") | |
.attach() | |
.apply(); | |
return true; | |
} catch (Exception f) { | |
System.out.println(f.getMessage()); | |
f.printStackTrace(); | |
} finally { | |
try { | |
System.out.println("Deleting Resource Group: " + rgName); | |
//delete resource group | |
// azure.resourceGroups().deleteByName(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; | |
} | |
/** | |
* Main entry point. | |
* @param args the parameters | |
*/ | |
public static void main(String[] args) { | |
try { | |
//============================================================= | |
// Authenticate | |
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials( | |
"clientid", | |
"tennatid", | |
"secrted id", | |
AzureEnvironment.AZURE); | |
Azure azure = Azure.configure() | |
.withLogLevel(LogLevel.BASIC) | |
.authenticate(credentials) | |
.withSubscription("3b4d41fa-e91d-4bc7-bc11-13d221b3b77d"); | |
// Print selected subscription | |
System.out.println("Selected subscription: " + azure.subscriptionId()); | |
runSample(azure); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
e.printStackTrace(); | |
} | |
} | |
private ManageVirtualMachine() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment