Skip to content
This repository was archived by the owner on Jun 15, 2026. It is now read-only.

Commit 2841425

Browse files
authored
Merge pull request #1 from selvasingh/dev
Sample for listing virtual machine images
2 parents fee076f + eb71ef2 commit 2841425

7 files changed

Lines changed: 589 additions & 0 deletions

File tree

.gitignore

Lines changed: 50 additions & 0 deletions
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

CONTRIBUTING.md

Lines changed: 11 additions & 0 deletions
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-list-vm-images/) whether it was helpful or not.
10+
- Submit issues through [issue tracker](https://github.com/Azure-Samples/compute-java-list-vm-images/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.

LICENSE

Lines changed: 21 additions & 0 deletions
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.

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
services: compute
3+
platforms: java
4+
author: selvasingh
5+
---
6+
7+
#Getting Started with Compute - List Virtual Machine Images - in Java #
8+
9+
Compute Manage Virtual Machine Sample (for 1.0.0-beta2) - demonstrates how to perform common tasks using the Microsoft Azure Compute service.
10+
11+
12+
- List all virtual machine image publishers and
13+
- List all virtual machine images published by Canonical, Red Hat and SUSE by browsing through locations, publishers, offers, SKUs and images.
14+
15+
16+
## Running this Sample ##
17+
18+
To run this sample:
19+
20+
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).
21+
22+
git clone https://github.com/Azure-Samples/compute-java-list-vm-images.git
23+
24+
cd compute-java-list-vm-images
25+
26+
mvn clean compile exec:java
27+
28+
## More information ##
29+
30+
[http://azure.com/java] (http://azure.com/java)
31+
32+
[Virtual Machines](https://azure.microsoft.com/en-us/services/virtual-machines/)
33+
34+
[Virtual Machines - Learning Path](https://azure.microsoft.com/en-us/documentation/learning-paths/virtual-machines/)
35+
36+
[Virtual Machines Marketplace](https://azure.microsoft.com/en-us/marketplace/virtual-machines/)
37+
38+
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212)
39+
40+
---
41+
42+
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.

pom.xml

Lines changed: 66 additions & 0 deletions
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-list-vm-images</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<name>ListVirtualMachineImages</name>
7+
<description></description>
8+
<url>https://github.com/Azure/compute-java-list-vm-images</url>
9+
<dependencies>
10+
<dependency>
11+
<groupId>com.microsoft.azure</groupId>
12+
<artifactId>azure</artifactId>
13+
<version>1.0.0-SNAPSHOT</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.ListVirtualMachineImages</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.8</source>
38+
<target>1.8</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.ListVirtualMachineImages</mainClass>
58+
</manifest>
59+
</archive>
60+
</configuration>
61+
</execution>
62+
</executions>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
</project>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*
5+
*/
6+
7+
package com.microsoft.azure.management.compute.samples;
8+
9+
import com.microsoft.azure.Azure;
10+
import com.microsoft.azure.management.compute.VirtualMachineOffer;
11+
import com.microsoft.azure.management.compute.VirtualMachinePublisher;
12+
import com.microsoft.azure.management.compute.VirtualMachineImage;
13+
import com.microsoft.azure.management.compute.VirtualMachineSku;
14+
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
15+
import okhttp3.logging.HttpLoggingInterceptor;
16+
17+
import java.io.File;
18+
import java.util.List;
19+
20+
/**
21+
* List all virtual machine image publishers and
22+
* list all virtual machine images published by Canonical, Red Hat and
23+
* SUSE by browsing through locations, publishers, offers, SKUs and images.
24+
*/
25+
public final class ListVirtualMachineImages {
26+
/**
27+
* The main entry point.
28+
*
29+
* @param args the parameters
30+
*/
31+
public static void main(String[] args) {
32+
try {
33+
34+
35+
//=================================================================
36+
// Authenticate
37+
38+
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
39+
40+
Azure azure = Azure
41+
.configure()
42+
.withLogLevel(HttpLoggingInterceptor.Level.NONE)
43+
.authenticate(credFile)
44+
.withDefaultSubscription();
45+
46+
47+
//=================================================================
48+
// List all virtual machine image publishers and
49+
// list all virtual machine images
50+
// published by Canonical, Red Hat and SUSE
51+
// by browsing through locations, publishers, offers, SKUs and images
52+
53+
List<VirtualMachinePublisher> publishers = azure
54+
.virtualMachineImages()
55+
.publishers()
56+
.listByRegion(Region.US_EAST);
57+
58+
VirtualMachinePublisher chosenPublisher;
59+
60+
System.out.println("US East data center: printing list of \n"
61+
+ "a) Publishers and\n"
62+
+ "b) Images published by Canonical, Red Hat and Suse");
63+
System.out.println("=======================================================");
64+
System.out.println("\n");
65+
66+
for (VirtualMachinePublisher publisher : publishers) {
67+
68+
System.out.println("Publisher - " + publisher.name());
69+
70+
if (publisher.name().equalsIgnoreCase("Canonical")
71+
| publisher.name().equalsIgnoreCase("Suse")
72+
| publisher.name().equalsIgnoreCase("RedHat")) {
73+
74+
chosenPublisher = publisher;
75+
System.out.print("\n\n");
76+
System.out.println("=======================================================");
77+
System.out.println("Located " + chosenPublisher.name());
78+
System.out.println("=======================================================");
79+
System.out.println("Printing entries as publisher/offer/sku/image.version()");
80+
81+
for (VirtualMachineOffer offer : chosenPublisher.offers().list()) {
82+
for (VirtualMachineSku sku: offer.skus().list()) {
83+
for (VirtualMachineImage image : sku.images().list()) {
84+
System.out.println("Image - " + chosenPublisher.name() + "/"
85+
+ offer.name() + "/"
86+
+ sku.name() + "/" + image.version());
87+
}
88+
}
89+
90+
}
91+
92+
System.out.print("\n\n");
93+
94+
}
95+
}
96+
} catch (Exception e) {
97+
System.out.println(e.getMessage());
98+
e.printStackTrace();
99+
}
100+
}
101+
102+
private ListVirtualMachineImages() {
103+
}
104+
}

0 commit comments

Comments
 (0)