-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate sample from compute-getting-started-java repo.
Only made minimal changes to follow style guide and update links.
- Loading branch information
Showing
6 changed files
with
514 additions
and
0 deletions.
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,14 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication"> | ||
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS"> | ||
<listEntry value="/compute/cmdline/src/main/java/ComputeEngineSample.java"/> | ||
</listAttribute> | ||
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES"> | ||
<listEntry value="1"/> | ||
</listAttribute> | ||
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/> | ||
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="ComputeEngineSample"/> | ||
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="compute-engine-cmdline-sample"/> | ||
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/> | ||
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Djava.util.logging.config.file=${project_loc:compute-engine-cmdline-sample}/logging.properties"/> | ||
</launchConfiguration> |
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,122 @@ | ||
# Compute Engine - Getting started with Java | ||
|
||
This sample command line application demonstrates how to access the Google | ||
Compute Engine API using the Google Java API Client Library. | ||
|
||
When the sample is setup and run as instructed below, it will list all the | ||
VM instances in a Google Cloud Platform project for a specific | ||
[zone](https://cloud.google.com/compute/docs/zones). | ||
The sample also checks for the existence of a VM instance named | ||
"my-sample-instance". If the VM instance doesn't exist, the sample | ||
will create a new VM instance named "my-sample-instance". If the VM instance | ||
does exist, running the sample will delete the instance. The instance create | ||
and delete actions are implemented as | ||
"[zone specific operations](https://cloud.google.com/compute/docs/reference/latest/zoneOperations#resource)". | ||
The sample demonstrates how to poll the status of these operations to | ||
determine when and if they successfully complete. | ||
|
||
## Products | ||
- [Compute Engine][1] | ||
|
||
## Language | ||
- [Java][2] | ||
|
||
## APIs | ||
- [Google Compute Engine][3] | ||
|
||
## Setup Instructions | ||
1. Create or select a project in the Google Cloud Console: | ||
1. Visit the [Cloud Console][4] | ||
1. If this is your first time then click "Create Project," otherwise you can | ||
reuse an existing project by clicking on it. | ||
1. Note: You will need to enable billing for the project to use Compute | ||
Engine. | ||
1. Click "Overview" in the left-side navigation menu and copy your Project ID | ||
for use in step 3.3 below. | ||
|
||
1. Authentication instructions to run the sample (on your local machine or on a Compute Engine VM): | ||
* Running the sample locally on your development machine: | ||
1. Install [Google Cloud SDK](https://cloud.google.com/sdk/) | ||
1. Run the following command to authorize the Cloud SDK and configure your project: | ||
<pre>gcloud init</pre> | ||
* Running the sample on a Google Compute Engine VM using Default Application | ||
Credentials: | ||
1. Create a Compute Engine VM Instance. | ||
1. In the [Cloud Console](https://console.cloud.google.com/project) | ||
go to the Compute > Compute Engine section. | ||
1. Click the "Create instance" button. | ||
1. For the 'Boot Disk' select a Linux machine image like Debian or Ubuntu. | ||
1. Click the "Management, disk, networking, access & security options" | ||
section to expand it. | ||
1. Select the "Access and Security" subsection and then select the | ||
"Compute" drop-down menu to set its scope. | ||
* Set the "Compute" access scope to be "Read/Write". | ||
1. Click the "Create" button. | ||
1. Once the VM is created click the VM instance's "SSH" button to ssh | ||
in to the newly created VM instance. | ||
|
||
1. Code checkout instructions: | ||
1. Prerequisites: install [Java 7 or Java 8 JDK][2], [Git][7], and [Maven][8]. | ||
You may need to set your `JAVA_HOME` environment variable as well. | ||
* To install these prerequisites on a Linux (Debian or Ubuntu) based Compute Engine VM | ||
instance, run these commands: | ||
<pre> | ||
sudo apt-get update | ||
sudo apt-get install git maven openjdk-7-jdk -y | ||
</pre> | ||
1. Download the sample code by running the following commands: | ||
<pre>mkdir some_directory | ||
cd some_directory | ||
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git | ||
cd java-docs-samples/compute/cmdline</pre> | ||
In a text editor open the `ComputeEngineSample.java` file. For example, to edit the file with nano: | ||
<pre>nano src/main/java/ComputeEngineSample.java</pre> | ||
|
||
1. In your text editor update the `YOUR_PROJECT_ID` value in | ||
[`src/main/java/ComputeEngineSample.java`][5] | ||
so that the following line is updated. Not performing this step will result | ||
in an error; specifically, "Parameter 'project' must conform to the pattern...". | ||
For more information see setting your [Project ID][6]. | ||
<pre>private static final String projectId = "YOUR_PROJECT_ID"</pre> | ||
1. Specify an 'Application Name' for your app by updating the following line of code: | ||
<pre>private static final String APPLICATION_NAME = "";</pre> | ||
1. Save the changes to the file and exit the text editor. | ||
|
||
1. Compile and run the sample: | ||
1. Compile the sample code using Maven by running the following command: | ||
<pre>mvn compile</pre> | ||
1. Execute the sample code using Maven by running the following command: | ||
<pre>mvn -q exec:java</pre> | ||
1. Running the sample will list all Google Compute Engine VM instances found in | ||
the zone you specified. The sample will also check for the existence of a VM instance | ||
named "my-sample-instance". If the "my-sample-instance" VM does not exist, running the | ||
sample will create it. If the "my-sample-instance" VM does already exist then running the | ||
sample will delete it. | ||
|
||
You can verify the list of VM instances by running the command | ||
`gcloud compute instances list`. VM instances can be deleted with the 'gcloud compute instance delete' | ||
command. For example, this command will delete the 'my-sample-instance' VM instance | ||
in the zone 'us-central1-f': | ||
|
||
`gcloud compute instances delete my-sample-instance --zone us-central1-f` | ||
|
||
1. Importing the code into Eclipse and running it from there: | ||
1. Prerequisites: install [Eclipse][9] and the [Maven plugin for Eclipse][10]. | ||
1. Download code as specified above. | ||
1. File -> Import -> Maven -> Existing Maven Projects -> Next. | ||
1. Select your project directory as your "Root Directory," and click "Finish." | ||
1. Right-click on project compute-engine-cmdline-sample. | ||
1. Run As > Java Application. | ||
1. If asked, type or select "ComputeEngineSample" and click OK. | ||
1. Application output will display in the Eclipse Console. | ||
|
||
[1]: https://cloud.google.com/compute/ | ||
[2]: http://java.com/en/download/faq/develop.xml | ||
[3]: https://cloud.google.com/compute/ | ||
[4]: https://console.cloud.google.com/project | ||
[5]: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/compute/cmdline/src/main/java/ComputeEngineSample.java#L54 | ||
[6]: https://support.google.com/cloud/answer/6158840 | ||
[7]: http://git-scm.com/downloads | ||
[8]: http://maven.apache.org/download.html | ||
[9]: http://www.eclipse.org/downloads/ | ||
[10]: http://download.eclipse.org/technology/m2e/releases/ |
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,10 @@ | ||
# Properties file which configures the operation of the JDK logging facility. | ||
# The system will look for this config file to be specified as a system property: | ||
# -Djava.util.logging.config.file=${project_loc:compute-engine-cmdline-sample}/logging.properties | ||
|
||
# Set up the console handler (uncomment "level" to show more fine-grained messages) | ||
handlers = java.util.logging.ConsoleHandler | ||
#java.util.logging.ConsoleHandler.level = CONFIG | ||
|
||
# Set up logging of HTTP requests and responses (uncomment "level" to show) | ||
#com.google.api.client.http.level = CONFIG |
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,95 @@ | ||
<!-- | ||
Copyright 2016 Google Inc. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>compute-cmdline</artifactId> | ||
<version>1</version> | ||
|
||
<!-- Parent POM defines common plugins and properties. --> | ||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>doc-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../..</relativePath> | ||
</parent> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.5.1</version> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.4.0</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>java</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<mainClass>ComputeEngineSample</mainClass> | ||
<systemProperties> | ||
<systemProperty> | ||
<key>java.util.logging.config.file</key> | ||
<value>logging.properties</value> | ||
</systemProperty> | ||
</systemProperties> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>findbugs-maven-plugin</artifactId> | ||
<version>3.0.3</version> | ||
<configuration> | ||
<failOnError>false</failOnError> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>check</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
<finalName>${project.artifactId}-${project.version}</finalName> | ||
</build> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.api-client</groupId> | ||
<artifactId>google-api-client</artifactId> | ||
<version>1.21.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.apis</groupId> | ||
<artifactId>google-api-services-compute</artifactId> | ||
<version>${project.compute.version}</version> | ||
</dependency> | ||
</dependencies> | ||
<properties> | ||
<project.compute.version>v1-rev105-1.21.0</project.compute.version> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
</project> |
Oops, something went wrong.