Skip to content

Commit 43e4808

Browse files
committed
Updated sample
1 parent a9a4d8f commit 43e4808

File tree

7 files changed

+918
-1
lines changed

7 files changed

+918
-1
lines changed

Diff for: .gitignore

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
*.class
2+
3+
# Auth filed
4+
*.auth
5+
*.azureauth
6+
7+
# Mobile Tools for Java (J2ME)
8+
.mtj.tmp/
9+
10+
# Package Files #
11+
*.jar
12+
*.war
13+
*.ear
14+
15+
# Azure Tooling #
16+
node_modules
17+
packages
18+
19+
# Eclipse #
20+
*.pydevproject
21+
.project
22+
.metadata
23+
bin/**
24+
tmp/**
25+
tmp/**/*
26+
*.tmp
27+
*.bak
28+
*.swp
29+
*~.nib
30+
local.properties
31+
.classpath
32+
.settings/
33+
.loadpath
34+
35+
# Other Tooling #
36+
.classpath
37+
.project
38+
target
39+
.idea
40+
*.iml
41+
42+
# Mac OS #
43+
.DS_Store
44+
.DS_Store?
45+
46+
# Windows #
47+
Thumbs.db
48+
49+
# reduced pom files should not be included
50+
dependency-reduced-pom.xml

Diff for: CONTRIBUTING.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Contributing to Azure samples
2+
3+
Thank you for your interest in contributing to Azure samples!
4+
5+
## Ways to contribute
6+
7+
You can contribute to [Azure samples](https://azure.microsoft.com/documentation/samples/) in a few different ways:
8+
9+
- 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.
10+
- 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.
11+
- 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.

Diff for: LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Microsoft Corporation
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Diff for: README.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
1-
# compute-java-manage-virtual-machines-in-parallel
1+
---
2+
services: Compute
3+
platforms: java
4+
author: anuchandy
5+
---
6+
7+
#Getting Started with Compute - Manage Virtual Machines In Parallel - in Java #
8+
9+
10+
Azure Compute sample for managing virtual machines -
11+
- Create N virtual machines in parallel.
12+
13+
14+
## Running this Sample ##
15+
16+
To run this sample:
17+
18+
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).
19+
20+
git clone https://github.com/Azure-Samples/compute-java-manage-virtual-machines-in-parallel.git
21+
22+
cd compute-java-manage-virtual-machines-in-parallel
23+
24+
mvn clean compile exec:java
25+
26+
## More information ##
27+
28+
[http://azure.com/java] (http://azure.com/java)
29+
30+
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212)
31+
32+
---
33+
34+
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 [[email protected]](mailto:[email protected]) with any additional questions or comments.

Diff for: pom.xml

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.microsoft.azure</groupId>
4+
<artifactId>compute-java-manage-virtual-machines-in-parallel</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<name>ManageVirtualMachinesInParallel</name>
7+
<description></description>
8+
<url>https://github.com/Azure/compute-java-manage-virtual-machines-in-parallel</url>
9+
<dependencies>
10+
<dependency>
11+
<groupId>com.microsoft.azure</groupId>
12+
<artifactId>azure</artifactId>
13+
<version>1.0.0-beta3</version>
14+
</dependency>
15+
</dependencies>
16+
<build>
17+
<sourceDirectory>src</sourceDirectory>
18+
<resources>
19+
<resource>
20+
<directory>resources</directory>
21+
</resource>
22+
</resources>
23+
<plugins>
24+
<plugin>
25+
<groupId>org.codehaus.mojo</groupId>
26+
<artifactId>exec-maven-plugin</artifactId>
27+
<version>1.4.0</version>
28+
<configuration>
29+
<mainClass>com.microsoft.azure.management.compute.samples.ManageVirtualMachinesInParallel</mainClass>
30+
</configuration>
31+
</plugin>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-compiler-plugin</artifactId>
35+
<version>3.0</version>
36+
<configuration>
37+
<source>1.7</source>
38+
<target>1.7</target>
39+
</configuration>
40+
</plugin>
41+
<plugin>
42+
<!-- Generate a fully packaged executable jar with dependencies -->
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-assembly-plugin</artifactId>
45+
<executions>
46+
<execution>
47+
<goals>
48+
<goal>attached</goal>
49+
</goals>
50+
<phase>package</phase>
51+
<configuration>
52+
<descriptorRefs>
53+
<descriptorRef>jar-with-dependencies</descriptorRef>
54+
</descriptorRefs>
55+
<archive>
56+
<manifest>
57+
<mainClass>com.microsoft.azure.management.compute.samples.ManageVirtualMachinesInParallel</mainClass>
58+
</manifest>
59+
</archive>
60+
</configuration>
61+
</execution>
62+
</executions>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/**
2+
*
3+
* Copyright (c) Microsoft Corporation. All rights reserved.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*
6+
*/
7+
8+
package com.microsoft.azure.management.compute.samples;
9+
10+
import com.microsoft.azure.management.Azure;
11+
import com.microsoft.azure.management.compute.KnownLinuxVirtualMachineImage;
12+
import com.microsoft.azure.management.compute.VirtualMachine;
13+
import com.microsoft.azure.management.compute.VirtualMachineSizeTypes;
14+
import com.microsoft.azure.management.network.Network;
15+
import com.microsoft.azure.management.resources.ResourceGroup;
16+
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
17+
import com.microsoft.azure.management.resources.fluentcore.model.Creatable;
18+
import com.microsoft.azure.management.resources.fluentcore.model.CreatedResources;
19+
import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer;
20+
import com.microsoft.azure.management.storage.StorageAccount;
21+
import okhttp3.logging.HttpLoggingInterceptor;
22+
import java.io.File;
23+
import java.util.ArrayList;
24+
import java.util.Date;
25+
import java.util.List;
26+
27+
/**
28+
* Azure Compute sample for managing virtual machines -
29+
* - Create N virtual machines in parallel.
30+
*/
31+
public final class ManageVirtualMachinesInParallel {
32+
33+
/**
34+
* Main entry point.
35+
* @param args the parameters
36+
*/
37+
public static void main(String[] args) {
38+
39+
final int vmCount = 10;
40+
final String rgName = ResourceNamer.randomResourceName("rgCOPP", 24);
41+
final String networkName = ResourceNamer.randomResourceName("vnetCOMV", 24);
42+
final String storageAccountName = ResourceNamer.randomResourceName("stgCOMV", 20);
43+
final String userName = "tirekicker";
44+
final String password = "12NewPA$$w0rd!";
45+
46+
try {
47+
48+
//=============================================================
49+
// Authenticate
50+
51+
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
52+
53+
Azure azure = Azure
54+
.configure()
55+
.withLogLevel(HttpLoggingInterceptor.Level.BASIC)
56+
.authenticate(credFile)
57+
.withDefaultSubscription();
58+
59+
// Print selected subscription
60+
System.out.println("Selected subscription: " + azure.subscriptionId());
61+
62+
try {
63+
// Create a resource group [Where all resources gets created]
64+
ResourceGroup resourceGroup = azure.resourceGroups()
65+
.define(rgName)
66+
.withRegion(Region.US_EAST)
67+
.create();
68+
69+
// Prepare Creatable Network definition [Where all the virtual machines get added to]
70+
Network.DefinitionStages.WithCreate creatableNetwork = azure.networks()
71+
.define(networkName)
72+
.withRegion(Region.US_EAST)
73+
.withExistingResourceGroup(resourceGroup)
74+
.withAddressSpace("172.16.0.0/16");
75+
76+
// Prepare Creatable Storage account definition [For storing VMs disk]
77+
StorageAccount.DefinitionStages.WithCreate creatableStorageAccount = azure.storageAccounts()
78+
.define(storageAccountName)
79+
.withRegion(Region.US_EAST)
80+
.withExistingResourceGroup(resourceGroup);
81+
82+
// Prepare a batch of Creatable Virtual Machines definitions
83+
List<Creatable<VirtualMachine>> creatableVirtualMachines = new ArrayList<>();
84+
85+
for (int i = 0; i < vmCount; i++) {
86+
VirtualMachine.DefinitionStages.WithCreate creatableVirtualMachine = azure.virtualMachines()
87+
.define("VM-" + i)
88+
.withRegion(Region.US_EAST)
89+
.withExistingResourceGroup(resourceGroup)
90+
.withNewPrimaryNetwork(creatableNetwork)
91+
.withPrimaryPrivateIpAddressDynamic()
92+
.withoutPrimaryPublicIpAddress()
93+
.withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
94+
.withRootUserName(userName)
95+
.withPassword(password)
96+
.withSize(VirtualMachineSizeTypes.STANDARD_DS3_V2)
97+
.withNewStorageAccount(creatableStorageAccount);
98+
creatableVirtualMachines.add(creatableVirtualMachine);
99+
}
100+
101+
Date t1 = new Date();
102+
System.out.println("Creating the virtual machines");
103+
104+
CreatedResources<VirtualMachine> virtualMachines = azure.virtualMachines().create(creatableVirtualMachines);
105+
106+
Date t2 = new Date();
107+
System.out.println("Created virtual machines");
108+
109+
for (VirtualMachine virtualMachine : virtualMachines) {
110+
System.out.println(virtualMachine.id());
111+
}
112+
113+
System.out.println("Virtual Machines create: (took " + ((t2.getTime() - t1.getTime()) / 1000) + " seconds) ");
114+
} catch (Exception f) {
115+
116+
System.out.println(f.getMessage());
117+
f.printStackTrace();
118+
119+
} finally {
120+
121+
try {
122+
System.out.println("Deleting Resource Group: " + rgName);
123+
azure.resourceGroups().delete(rgName);
124+
System.out.println("Deleted Resource Group: " + rgName);
125+
} catch (NullPointerException npe) {
126+
System.out.println("Did not create any resources in Azure. No clean up is necessary");
127+
} catch (Exception g) {
128+
g.printStackTrace();
129+
}
130+
131+
}
132+
} catch (Exception e) {
133+
System.out.println(e.getMessage());
134+
e.printStackTrace();
135+
}
136+
}
137+
138+
private ManageVirtualMachinesInParallel() {
139+
}
140+
}

0 commit comments

Comments
 (0)