-
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.
Merge pull request #1 from anudeepsharma/UpdatedSample
Updated sample
- Loading branch information
Showing
7 changed files
with
918 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
*.class | ||
|
||
# Auth filed | ||
*.auth | ||
*.azureauth | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.ear | ||
|
||
# Azure Tooling # | ||
node_modules | ||
packages | ||
|
||
# Eclipse # | ||
*.pydevproject | ||
.project | ||
.metadata | ||
bin/** | ||
tmp/** | ||
tmp/**/* | ||
*.tmp | ||
*.bak | ||
*.swp | ||
*~.nib | ||
local.properties | ||
.classpath | ||
.settings/ | ||
.loadpath | ||
|
||
# Other Tooling # | ||
.classpath | ||
.project | ||
target | ||
.idea | ||
*.iml | ||
|
||
# Mac OS # | ||
.DS_Store | ||
.DS_Store? | ||
|
||
# Windows # | ||
Thumbs.db | ||
|
||
# reduced pom files should not be included | ||
dependency-reduced-pom.xml |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Contributing to Azure samples | ||
|
||
Thank you for your interest in contributing to Azure samples! | ||
|
||
## Ways to contribute | ||
|
||
You can contribute to [Azure samples](https://azure.microsoft.com/documentation/samples/) in a few different ways: | ||
|
||
- Submit feedback on [this sample page](https://azure.microsoft.com/documentation/samples/compute-java-manage-virtual-machines-in-parallel/) whether it was helpful or not. | ||
- Submit issues through [issue tracker](https://github.com/Azure-Samples/compute-java-manage-virtual-machines-in-parallel/issues) on GitHub. We are actively monitoring the issues and improving our samples. | ||
- If you wish to make code changes to samples, or contribute something new, please follow the [GitHub Forks / Pull requests model](https://help.github.com/articles/fork-a-repo/): Fork the sample repo, make the change and propose it back by submitting a pull request. |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Microsoft Corporation | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 +1,34 @@ | ||
# compute-java-manage-virtual-machines-in-parallel | ||
--- | ||
services: Compute | ||
platforms: java | ||
author: anuchandy | ||
--- | ||
|
||
#Getting Started with Compute - Manage Virtual Machines In Parallel - in Java # | ||
|
||
|
||
Azure Compute sample for managing virtual machines - | ||
- Create N virtual machines in parallel. | ||
|
||
|
||
## Running this Sample ## | ||
|
||
To run this sample: | ||
|
||
Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an auth file. See [how to create an auth file](https://github.com/Azure/azure-sdk-for-java/blob/master/AUTH.md). | ||
|
||
git clone https://github.com/Azure-Samples/compute-java-manage-virtual-machines-in-parallel.git | ||
|
||
cd compute-java-manage-virtual-machines-in-parallel | ||
|
||
mvn clean compile exec:java | ||
|
||
## More information ## | ||
|
||
[http://azure.com/java] (http://azure.com/java) | ||
|
||
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212) | ||
|
||
--- | ||
|
||
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. |
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.microsoft.azure</groupId> | ||
<artifactId>compute-java-manage-virtual-machines-in-parallel</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>ManageVirtualMachinesInParallel</name> | ||
<description></description> | ||
<url>https://github.com/Azure/compute-java-manage-virtual-machines-in-parallel</url> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.microsoft.azure</groupId> | ||
<artifactId>azure</artifactId> | ||
<version>1.0.0-beta3</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<sourceDirectory>src</sourceDirectory> | ||
<resources> | ||
<resource> | ||
<directory>resources</directory> | ||
</resource> | ||
</resources> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.4.0</version> | ||
<configuration> | ||
<mainClass>com.microsoft.azure.management.compute.samples.ManageVirtualMachinesInParallel</mainClass> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.0</version> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<!-- Generate a fully packaged executable jar with dependencies --> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>attached</goal> | ||
</goals> | ||
<phase>package</phase> | ||
<configuration> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
<archive> | ||
<manifest> | ||
<mainClass>com.microsoft.azure.management.compute.samples.ManageVirtualMachinesInParallel</mainClass> | ||
</manifest> | ||
</archive> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
140 changes: 140 additions & 0 deletions
140
.../java/com/microsoft/azure/management/compute/samples/ManageVirtualMachinesInParallel.java
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 |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/** | ||
* | ||
* 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.management.Azure; | ||
import com.microsoft.azure.management.compute.KnownLinuxVirtualMachineImage; | ||
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.ResourceGroup; | ||
import com.microsoft.azure.management.resources.fluentcore.arm.Region; | ||
import com.microsoft.azure.management.resources.fluentcore.model.Creatable; | ||
import com.microsoft.azure.management.resources.fluentcore.model.CreatedResources; | ||
import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer; | ||
import com.microsoft.azure.management.storage.StorageAccount; | ||
import okhttp3.logging.HttpLoggingInterceptor; | ||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
/** | ||
* Azure Compute sample for managing virtual machines - | ||
* - Create N virtual machines in parallel. | ||
*/ | ||
public final class ManageVirtualMachinesInParallel { | ||
|
||
/** | ||
* Main entry point. | ||
* @param args the parameters | ||
*/ | ||
public static void main(String[] args) { | ||
|
||
final int vmCount = 10; | ||
final String rgName = ResourceNamer.randomResourceName("rgCOPP", 24); | ||
final String networkName = ResourceNamer.randomResourceName("vnetCOMV", 24); | ||
final String storageAccountName = ResourceNamer.randomResourceName("stgCOMV", 20); | ||
final String userName = "tirekicker"; | ||
final String password = "12NewPA$$w0rd!"; | ||
|
||
try { | ||
|
||
//============================================================= | ||
// Authenticate | ||
|
||
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION")); | ||
|
||
Azure azure = Azure | ||
.configure() | ||
.withLogLevel(HttpLoggingInterceptor.Level.BASIC) | ||
.authenticate(credFile) | ||
.withDefaultSubscription(); | ||
|
||
// Print selected subscription | ||
System.out.println("Selected subscription: " + azure.subscriptionId()); | ||
|
||
try { | ||
// Create a resource group [Where all resources gets created] | ||
ResourceGroup resourceGroup = azure.resourceGroups() | ||
.define(rgName) | ||
.withRegion(Region.US_EAST) | ||
.create(); | ||
|
||
// Prepare Creatable Network definition [Where all the virtual machines get added to] | ||
Network.DefinitionStages.WithCreate creatableNetwork = azure.networks() | ||
.define(networkName) | ||
.withRegion(Region.US_EAST) | ||
.withExistingResourceGroup(resourceGroup) | ||
.withAddressSpace("172.16.0.0/16"); | ||
|
||
// Prepare Creatable Storage account definition [For storing VMs disk] | ||
StorageAccount.DefinitionStages.WithCreate creatableStorageAccount = azure.storageAccounts() | ||
.define(storageAccountName) | ||
.withRegion(Region.US_EAST) | ||
.withExistingResourceGroup(resourceGroup); | ||
|
||
// Prepare a batch of Creatable Virtual Machines definitions | ||
List<Creatable<VirtualMachine>> creatableVirtualMachines = new ArrayList<>(); | ||
|
||
for (int i = 0; i < vmCount; i++) { | ||
VirtualMachine.DefinitionStages.WithCreate creatableVirtualMachine = azure.virtualMachines() | ||
.define("VM-" + i) | ||
.withRegion(Region.US_EAST) | ||
.withExistingResourceGroup(resourceGroup) | ||
.withNewPrimaryNetwork(creatableNetwork) | ||
.withPrimaryPrivateIpAddressDynamic() | ||
.withoutPrimaryPublicIpAddress() | ||
.withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS) | ||
.withRootUserName(userName) | ||
.withPassword(password) | ||
.withSize(VirtualMachineSizeTypes.STANDARD_DS3_V2) | ||
.withNewStorageAccount(creatableStorageAccount); | ||
creatableVirtualMachines.add(creatableVirtualMachine); | ||
} | ||
|
||
Date t1 = new Date(); | ||
System.out.println("Creating the virtual machines"); | ||
|
||
CreatedResources<VirtualMachine> virtualMachines = azure.virtualMachines().create(creatableVirtualMachines); | ||
|
||
Date t2 = new Date(); | ||
System.out.println("Created virtual machines"); | ||
|
||
for (VirtualMachine virtualMachine : virtualMachines) { | ||
System.out.println(virtualMachine.id()); | ||
} | ||
|
||
System.out.println("Virtual Machines create: (took " + ((t2.getTime() - t1.getTime()) / 1000) + " seconds) "); | ||
} catch (Exception f) { | ||
|
||
System.out.println(f.getMessage()); | ||
f.printStackTrace(); | ||
|
||
} finally { | ||
|
||
try { | ||
System.out.println("Deleting Resource Group: " + rgName); | ||
azure.resourceGroups().delete(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(); | ||
} | ||
|
||
} | ||
} catch (Exception e) { | ||
System.out.println(e.getMessage()); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private ManageVirtualMachinesInParallel() { | ||
} | ||
} |
Oops, something went wrong.