-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update READMEs for Compute #3388
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
40d53a7
update READMEs
andreamlin 0112804
move ComputeExamples to v1 and deprecated dirs
andreamlin 0f7ca42
no star imporst
andreamlin 5c941ad
clean up ComputeExample.java
andreamlin 12dbd0e
googleJavaFormat on ComputeExample.javag
andreamlin ef13166
alphabetaize
andreamlin 5f57f8f
regen from toolkit and discovery-artifact-manager master
andreamlin 6ca651d
Merge branch 'master' of github.com:GoogleCloudPlatform/google-cloud-…
andreamlin ba38575
Merge branch 'master' of github.com:GoogleCloudPlatform/google-cloud-…
andreamlin 5531d59
Merge branch 'master' into compute_docs
andreamlin ba43efa
updated README.md with examples
andreamlin 167fc3d
merge maste
andreamlin 7ca2fef
merge master
andreamlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
144 changes: 144 additions & 0 deletions
144
google-cloud-examples/src/main/java/com/google/cloud/examples/compute/v1/ComputeExample.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,144 @@ | ||
package com.google.cloud.examples.compute.v1; | ||
|
||
import com.google.api.core.ApiFuture; | ||
import com.google.api.gax.core.FixedCredentialsProvider; | ||
import com.google.auth.Credentials; | ||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.cloud.compute.v1.Address; | ||
import com.google.cloud.compute.v1.AddressClient; | ||
import com.google.cloud.compute.v1.AddressSettings; | ||
import com.google.cloud.compute.v1.InsertAddressHttpRequest; | ||
import com.google.cloud.compute.v1.ListAddressesHttpRequest; | ||
import com.google.cloud.compute.v1.Operation; | ||
import com.google.cloud.compute.v1.ProjectRegionAddressName; | ||
import com.google.cloud.compute.v1.ProjectRegionName; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** Working example code to make live calls on Addresses resources in a GCP Compute project. */ | ||
public class ComputeExample { | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
// Replace the following String values with your Project and Region ids. | ||
private static String PROJECT_NAME = "my-project-id"; | ||
private static String REGION = "us-central1"; | ||
|
||
/** | ||
* List addresses, Insert an address, and then delete the address. Use ResourceNames in the | ||
* request objects. | ||
*/ | ||
public static void main(String[] args) throws IOException { | ||
AddressClient client = createCredentialedClient(); | ||
|
||
System.out.println("Running with Gapic Client."); | ||
AddressClient.ListAddressesPagedResponse listResponse = listAddresses(client); | ||
verifyListAddressWithGets(client, listResponse); | ||
|
||
System.out.println("Running with Gapic Client and Resource Name."); | ||
String newAddressName = "new_address_name"; | ||
System.out.println("Inserting address:"); | ||
|
||
insertNewAddressJustClient(client, newAddressName); | ||
|
||
listAddresses(client); | ||
|
||
System.out.println("Deleting address:"); | ||
Operation deleteResponse = | ||
client.deleteAddress(ProjectRegionAddressName.of(newAddressName, PROJECT_NAME, REGION)); | ||
System.out.format("Result of delete: %s\n", deleteResponse.toString()); | ||
int sleepTimeInSeconds = 3; | ||
System.out.format("Waiting %d seconds for server to update...\n", sleepTimeInSeconds); | ||
// Wait for the delete operation to finish on the server. | ||
try { | ||
TimeUnit.SECONDS.sleep(sleepTimeInSeconds); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
listAddresses(client); | ||
} | ||
|
||
private static AddressClient createCredentialedClient() throws IOException { | ||
Credentials myCredentials = GoogleCredentials.getApplicationDefault(); | ||
String myEndpoint = AddressSettings.getDefaultEndpoint(); | ||
|
||
AddressSettings addressSettings = | ||
AddressSettings.newBuilder() | ||
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) | ||
.setTransportChannelProvider( | ||
AddressSettings.defaultHttpJsonTransportProviderBuilder() | ||
.setEndpoint(myEndpoint) | ||
.build()) | ||
.build(); | ||
return AddressClient.create(addressSettings); | ||
} | ||
|
||
private static void insertNewAddressJustClient(AddressClient client, String newAddressName) { | ||
// Begin samplegen code for insertAddress(). | ||
Address newAddress = Address.newBuilder().setName(newAddressName).build(); | ||
ProjectRegionName region = ProjectRegionName.of(PROJECT_NAME, REGION); | ||
Operation response = client.insertAddress(region, newAddress); | ||
// End samplegen code for insertAddress(). | ||
System.out.format("Result of insert: %s\n", response.toString()); | ||
} | ||
|
||
/** Use an InsertAddressHttpRequest object to make an addresses.insert method call. */ | ||
private static void insertNewAddressUsingRequest(AddressClient client, String newAddressName) | ||
throws InterruptedException, ExecutionException { | ||
// Begin samplegen code for insertAddress(). | ||
ProjectRegionName region = ProjectRegionName.of(PROJECT_NAME, REGION); | ||
Address address = Address.newBuilder().build(); | ||
InsertAddressHttpRequest request = | ||
InsertAddressHttpRequest.newBuilder() | ||
.setRegion(region.toString()) | ||
.setAddressResource(address) | ||
.build(); | ||
// Do something | ||
Operation response = client.insertAddress(request); | ||
|
||
// End samplegen code for insertAddress(). | ||
System.out.format("Result of insert: %s\n", response.toString()); | ||
} | ||
|
||
/** Use an callable object to make an addresses.insert method call. */ | ||
private static void insertAddressUsingCallable(AddressClient client, String newAddressName) | ||
throws InterruptedException, ExecutionException { | ||
// Begin samplegen code for insertAddress(). | ||
ProjectRegionName region = ProjectRegionName.of(PROJECT_NAME, REGION); | ||
Address address = Address.newBuilder().build(); | ||
InsertAddressHttpRequest request = | ||
InsertAddressHttpRequest.newBuilder() | ||
.setRegion(region.toString()) | ||
.setAddressResource(address) | ||
.build(); | ||
ApiFuture<Operation> future = client.insertAddressCallable().futureCall(request); | ||
// Do something | ||
Operation response = future.get(); | ||
|
||
// End samplegen code for insertAddress(). | ||
System.out.format("Result of insert: %s\n", response.toString()); | ||
} | ||
|
||
/** List Addresses in the under the project PROJECT_NAME and region REGION. */ | ||
private static AddressClient.ListAddressesPagedResponse listAddresses(AddressClient client) { | ||
System.out.println("Listing addresses:"); | ||
ProjectRegionName regionName = | ||
ProjectRegionName.newBuilder().setRegion(REGION).setProject(PROJECT_NAME).build(); | ||
ListAddressesHttpRequest listRequest = | ||
ListAddressesHttpRequest.newBuilder().setRegion(regionName.toString()).build(); | ||
AddressClient.ListAddressesPagedResponse response = client.listAddresses(listRequest); | ||
for (Address address : response.iterateAll()) { | ||
System.out.println("\t - " + address.toString()); | ||
} | ||
return response; | ||
} | ||
|
||
private static void verifyListAddressWithGets( | ||
AddressClient client, AddressClient.ListAddressesPagedResponse listResponse) { | ||
for (Address address : listResponse.iterateAll()) { | ||
System.out.format("Making get request for address \"%s\"...\n", address.getName()); | ||
Address fetchedAddress = | ||
client.getAddress(ProjectRegionAddressName.of(address.getName(), PROJECT_NAME, REGION)); | ||
System.out.format("addresses.get returns \n\t%s\n\n", fetchedAddress.toString()); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.