Skip to content

Commit 21dc731

Browse files
committed
Merge pull request #162 from GoogleCloudPlatform/compute-sample
Migrate sample from compute-getting-started-java repo.
2 parents 735a4cf + c414e36 commit 21dc731

File tree

6 files changed

+514
-0
lines changed

6 files changed

+514
-0
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
3+
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
4+
<listEntry value="/compute/cmdline/src/main/java/ComputeEngineSample.java"/>
5+
</listAttribute>
6+
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
7+
<listEntry value="1"/>
8+
</listAttribute>
9+
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
10+
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="ComputeEngineSample"/>
11+
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="compute-engine-cmdline-sample"/>
12+
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
13+
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Djava.util.logging.config.file=${project_loc:compute-engine-cmdline-sample}/logging.properties"/>
14+
</launchConfiguration>

compute/cmdline/README.md

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Compute Engine - Getting started with Java
2+
3+
This sample command line application demonstrates how to access the Google
4+
Compute Engine API using the Google Java API Client Library.
5+
6+
When the sample is setup and run as instructed below, it will list all the
7+
VM instances in a Google Cloud Platform project for a specific
8+
[zone](https://cloud.google.com/compute/docs/zones).
9+
The sample also checks for the existence of a VM instance named
10+
"my-sample-instance". If the VM instance doesn't exist, the sample
11+
will create a new VM instance named "my-sample-instance". If the VM instance
12+
does exist, running the sample will delete the instance. The instance create
13+
and delete actions are implemented as
14+
"[zone specific operations](https://cloud.google.com/compute/docs/reference/latest/zoneOperations#resource)".
15+
The sample demonstrates how to poll the status of these operations to
16+
determine when and if they successfully complete.
17+
18+
## Products
19+
- [Compute Engine][1]
20+
21+
## Language
22+
- [Java][2]
23+
24+
## APIs
25+
- [Google Compute Engine][3]
26+
27+
## Setup Instructions
28+
1. Create or select a project in the Google Cloud Console:
29+
1. Visit the [Cloud Console][4]
30+
1. If this is your first time then click "Create Project," otherwise you can
31+
reuse an existing project by clicking on it.
32+
1. Note: You will need to enable billing for the project to use Compute
33+
Engine.
34+
1. Click "Overview" in the left-side navigation menu and copy your Project ID
35+
for use in step 3.3 below.
36+
37+
1. Authentication instructions to run the sample (on your local machine or on a Compute Engine VM):
38+
* Running the sample locally on your development machine:
39+
1. Install [Google Cloud SDK](https://cloud.google.com/sdk/)
40+
1. Run the following command to authorize the Cloud SDK and configure your project:
41+
<pre>gcloud init</pre>
42+
* Running the sample on a Google Compute Engine VM using Default Application
43+
Credentials:
44+
1. Create a Compute Engine VM Instance.
45+
1. In the [Cloud Console](https://console.cloud.google.com/project)
46+
go to the Compute > Compute Engine section.
47+
1. Click the "Create instance" button.
48+
1. For the 'Boot Disk' select a Linux machine image like Debian or Ubuntu.
49+
1. Click the "Management, disk, networking, access & security options"
50+
section to expand it.
51+
1. Select the "Access and Security" subsection and then select the
52+
"Compute" drop-down menu to set its scope.
53+
* Set the "Compute" access scope to be "Read/Write".
54+
1. Click the "Create" button.
55+
1. Once the VM is created click the VM instance's "SSH" button to ssh
56+
in to the newly created VM instance.
57+
58+
1. Code checkout instructions:
59+
1. Prerequisites: install [Java 7 or Java 8 JDK][2], [Git][7], and [Maven][8].
60+
You may need to set your `JAVA_HOME` environment variable as well.
61+
* To install these prerequisites on a Linux (Debian or Ubuntu) based Compute Engine VM
62+
instance, run these commands:
63+
<pre>
64+
sudo apt-get update
65+
sudo apt-get install git maven openjdk-7-jdk -y
66+
</pre>
67+
1. Download the sample code by running the following commands:
68+
<pre>mkdir some_directory
69+
cd some_directory
70+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
71+
cd java-docs-samples/compute/cmdline</pre>
72+
In a text editor open the `ComputeEngineSample.java` file. For example, to edit the file with nano:
73+
<pre>nano src/main/java/ComputeEngineSample.java</pre>
74+
75+
1. In your text editor update the `YOUR_PROJECT_ID` value in
76+
[`src/main/java/ComputeEngineSample.java`][5]
77+
so that the following line is updated. Not performing this step will result
78+
in an error; specifically, "Parameter 'project' must conform to the pattern...".
79+
For more information see setting your [Project ID][6].
80+
<pre>private static final String projectId = "YOUR_PROJECT_ID"</pre>
81+
1. Specify an 'Application Name' for your app by updating the following line of code:
82+
<pre>private static final String APPLICATION_NAME = "";</pre>
83+
1. Save the changes to the file and exit the text editor.
84+
85+
1. Compile and run the sample:
86+
1. Compile the sample code using Maven by running the following command:
87+
<pre>mvn compile</pre>
88+
1. Execute the sample code using Maven by running the following command:
89+
<pre>mvn -q exec:java</pre>
90+
1. Running the sample will list all Google Compute Engine VM instances found in
91+
the zone you specified. The sample will also check for the existence of a VM instance
92+
named "my-sample-instance". If the "my-sample-instance" VM does not exist, running the
93+
sample will create it. If the "my-sample-instance" VM does already exist then running the
94+
sample will delete it.
95+
96+
You can verify the list of VM instances by running the command
97+
`gcloud compute instances list`. VM instances can be deleted with the 'gcloud compute instance delete'
98+
command. For example, this command will delete the 'my-sample-instance' VM instance
99+
in the zone 'us-central1-f':
100+
101+
`gcloud compute instances delete my-sample-instance --zone us-central1-f`
102+
103+
1. Importing the code into Eclipse and running it from there:
104+
1. Prerequisites: install [Eclipse][9] and the [Maven plugin for Eclipse][10].
105+
1. Download code as specified above.
106+
1. File -> Import -> Maven -> Existing Maven Projects -> Next.
107+
1. Select your project directory as your "Root Directory," and click "Finish."
108+
1. Right-click on project compute-engine-cmdline-sample.
109+
1. Run As > Java Application.
110+
1. If asked, type or select "ComputeEngineSample" and click OK.
111+
1. Application output will display in the Eclipse Console.
112+
113+
[1]: https://cloud.google.com/compute/
114+
[2]: http://java.com/en/download/faq/develop.xml
115+
[3]: https://cloud.google.com/compute/
116+
[4]: https://console.cloud.google.com/project
117+
[5]: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/compute/cmdline/src/main/java/ComputeEngineSample.java#L54
118+
[6]: https://support.google.com/cloud/answer/6158840
119+
[7]: http://git-scm.com/downloads
120+
[8]: http://maven.apache.org/download.html
121+
[9]: http://www.eclipse.org/downloads/
122+
[10]: http://download.eclipse.org/technology/m2e/releases/

compute/cmdline/logging.properties

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Properties file which configures the operation of the JDK logging facility.
2+
# The system will look for this config file to be specified as a system property:
3+
# -Djava.util.logging.config.file=${project_loc:compute-engine-cmdline-sample}/logging.properties
4+
5+
# Set up the console handler (uncomment "level" to show more fine-grained messages)
6+
handlers = java.util.logging.ConsoleHandler
7+
#java.util.logging.ConsoleHandler.level = CONFIG
8+
9+
# Set up logging of HTTP requests and responses (uncomment "level" to show)
10+
#com.google.api.client.http.level = CONFIG

compute/cmdline/pom.xml

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<!--
2+
Copyright 2016 Google Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<groupId>com.google.cloud.samples</groupId>
19+
<artifactId>compute-cmdline</artifactId>
20+
<version>1</version>
21+
22+
<!-- Parent POM defines common plugins and properties. -->
23+
<parent>
24+
<groupId>com.google.cloud</groupId>
25+
<artifactId>doc-samples</artifactId>
26+
<version>1.0.0</version>
27+
<relativePath>../..</relativePath>
28+
</parent>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<artifactId>maven-compiler-plugin</artifactId>
34+
<version>3.5.1</version>
35+
<configuration>
36+
<source>1.7</source>
37+
<target>1.7</target>
38+
</configuration>
39+
</plugin>
40+
<plugin>
41+
<groupId>org.codehaus.mojo</groupId>
42+
<artifactId>exec-maven-plugin</artifactId>
43+
<version>1.4.0</version>
44+
<executions>
45+
<execution>
46+
<goals>
47+
<goal>java</goal>
48+
</goals>
49+
</execution>
50+
</executions>
51+
<configuration>
52+
<mainClass>ComputeEngineSample</mainClass>
53+
<systemProperties>
54+
<systemProperty>
55+
<key>java.util.logging.config.file</key>
56+
<value>logging.properties</value>
57+
</systemProperty>
58+
</systemProperties>
59+
</configuration>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.codehaus.mojo</groupId>
63+
<artifactId>findbugs-maven-plugin</artifactId>
64+
<version>3.0.3</version>
65+
<configuration>
66+
<failOnError>false</failOnError>
67+
</configuration>
68+
<executions>
69+
<execution>
70+
<goals>
71+
<goal>check</goal>
72+
</goals>
73+
</execution>
74+
</executions>
75+
</plugin>
76+
</plugins>
77+
<finalName>${project.artifactId}-${project.version}</finalName>
78+
</build>
79+
<dependencies>
80+
<dependency>
81+
<groupId>com.google.api-client</groupId>
82+
<artifactId>google-api-client</artifactId>
83+
<version>1.21.0</version>
84+
</dependency>
85+
<dependency>
86+
<groupId>com.google.apis</groupId>
87+
<artifactId>google-api-services-compute</artifactId>
88+
<version>${project.compute.version}</version>
89+
</dependency>
90+
</dependencies>
91+
<properties>
92+
<project.compute.version>v1-rev105-1.21.0</project.compute.version>
93+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
94+
</properties>
95+
</project>

0 commit comments

Comments
 (0)