diff --git a/.kokoro/nightly/integration.cfg b/.kokoro/nightly/integration.cfg index 0048c8ec..e670cd7a 100644 --- a/.kokoro/nightly/integration.cfg +++ b/.kokoro/nightly/integration.cfg @@ -10,6 +10,7 @@ env_vars: { key: "JOB_TYPE" value: "integration" } + # TODO: remove this after we've migrated all tests and scripts env_vars: { key: "GCLOUD_PROJECT" diff --git a/.kokoro/presubmit/integration.cfg b/.kokoro/presubmit/integration.cfg index dded67a9..8212ad4f 100644 --- a/.kokoro/presubmit/integration.cfg +++ b/.kokoro/presubmit/integration.cfg @@ -13,12 +13,12 @@ env_vars: { # TODO: remove this after we've migrated all tests and scripts env_vars: { - key: "GCLOUD_PROJECT" + key: "GOOGLE_CLOUD_PROJECT" value: "gcloud-devel" } env_vars: { - key: "GOOGLE_CLOUD_PROJECT" + key: "GCLOUD_PROJECT" value: "gcloud-devel" } diff --git a/.kokoro/presubmit/samples.cfg b/.kokoro/presubmit/samples.cfg index 01e09600..4082da61 100644 --- a/.kokoro/presubmit/samples.cfg +++ b/.kokoro/presubmit/samples.cfg @@ -13,12 +13,12 @@ env_vars: { # TODO: remove this after we've migrated all tests and scripts env_vars: { - key: "GCLOUD_PROJECT" + key: "GOOGLE_CLOUD_PROJECT" value: "java-docs-samples-testing" } env_vars: { - key: "GOOGLE_CLOUD_PROJECT" + key: "GCLOUD_PROJECT" value: "java-docs-samples-testing" } diff --git a/pom.xml b/pom.xml index 317f3cfb..f987161b 100644 --- a/pom.xml +++ b/pom.xml @@ -205,4 +205,13 @@ + + + + enable-samples + + samples + + + diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index dc2ea102..efe06039 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -2,10 +2,10 @@ 4.0.0 com.google.cloud - game-servers-install-without-bom + gameservices-install-without-bom jar - Google Cloud Gaming Install Without Bom - https://github.com/googleapis/java-game-servers + Google Cloud Game Services Install Without BOM Sample + https://github.com/googleapis/java-gameservices @@ -32,6 +32,11 @@ 0.21.0 + + com.google.code.gson + gson + 2.8.6 + junit @@ -46,6 +51,7 @@ test + diff --git a/samples/pom.xml b/samples/pom.xml index dc594ee8..c69e7259 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -34,6 +34,23 @@ + + + + org.apache.maven.plugins + maven-dependency-plugin + + true + + + + org.codehaus.mojo + build-helper-maven-plugin + 3.0.0 + + + + org.apache.maven.plugins @@ -53,4 +70,4 @@ - + diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index 8ac3cda9..f34979e5 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -31,6 +31,11 @@ 0.21.1-SNAPSHOT + + com.google.code.gson + gson + 2.8.6 + junit @@ -80,4 +85,4 @@ - \ No newline at end of file + diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 84526385..d790f179 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -37,11 +37,18 @@ + com.google.cloud google-cloud-game-servers 0.21.0 + + + com.google.code.gson + gson + 2.8.6 + junit junit diff --git a/samples/snippets/src/main/java/com/example/gameservices/clusters/CreateCluster.java b/samples/snippets/src/main/java/com/example/gameservices/clusters/CreateCluster.java new file mode 100644 index 00000000..d372d24c --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/clusters/CreateCluster.java @@ -0,0 +1,76 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.clusters; + +// [START cloud_game_servers_cluster_create] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.gaming.v1.CreateGameServerClusterRequest; +import com.google.cloud.gaming.v1.GameServerCluster; +import com.google.cloud.gaming.v1.GameServerClusterConnectionInfo; +import com.google.cloud.gaming.v1.GameServerClustersServiceClient; +import com.google.cloud.gaming.v1.GkeClusterReference; +import com.google.cloud.gaming.v1.OperationMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class CreateCluster { + + public static void createGameServerCluster( + String projectId, String regionId, String realmId, String clusterId, String gkeName) + throws InterruptedException, ExecutionException, TimeoutException, IOException { + // String projectId = "your-project-id"; + // String regionId = "us-central1-f"; + // String realmId = "your-realm-id"; + // String clusterId = "your-game-server-cluster-id"; + // String gkeName = "projects/your-project-id/locations/us-central1/clusters/test"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (GameServerClustersServiceClient client = GameServerClustersServiceClient.create()) { + String parent = + String.format("projects/%s/locations/%s/realms/%s", projectId, regionId, realmId); + String clusterName = String.format("%s/gameServerClusters/%s", parent, clusterId); + + GameServerCluster gameServerCluster = + GameServerCluster.newBuilder() + .setName(clusterName) + .setConnectionInfo( + GameServerClusterConnectionInfo.newBuilder() + .setGkeClusterReference( + GkeClusterReference.newBuilder().setCluster(gkeName).build()) + .setNamespace("default")) + .build(); + + CreateGameServerClusterRequest request = + CreateGameServerClusterRequest.newBuilder() + .setParent(parent) + .setGameServerClusterId(clusterId) + .setGameServerCluster(gameServerCluster) + .build(); + + OperationFuture call = + client.createGameServerClusterAsync(request); + + GameServerCluster created = call.get(1, TimeUnit.MINUTES); + System.out.println("Game Server Cluster created: " + created.getName()); + } + } +} +// [END cloud_game_servers_cluster_create] diff --git a/samples/snippets/src/main/java/com/example/gameservices/clusters/DeleteCluster.java b/samples/snippets/src/main/java/com/example/gameservices/clusters/DeleteCluster.java new file mode 100644 index 00000000..ea42621b --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/clusters/DeleteCluster.java @@ -0,0 +1,54 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.clusters; + +// [START cloud_game_servers_cluster_delete] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.gaming.v1.GameServerClustersServiceClient; +import com.google.cloud.gaming.v1.OperationMetadata; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class DeleteCluster { + + public static void deleteGameServerCluster( + String projectId, String regionId, String realmId, String clusterId) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + // String projectId = "your-project-id"; + // String regionId = "us-central1-f"; + // String clusterId = "your-game-server-cluster-id"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (GameServerClustersServiceClient client = GameServerClustersServiceClient.create()) { + String parent = + String.format("projects/%s/locations/%s/realms/%s", projectId, regionId, realmId); + String clusterName = String.format("%s/gameServerClusters/%s", parent, clusterId); + + OperationFuture call = + client.deleteGameServerClusterAsync(clusterName); + + call.get(1, TimeUnit.MINUTES); + System.out.println("Game Server Cluster deleted: " + clusterName); + } + } +} +// [END cloud_game_servers_cluster_delete] diff --git a/samples/snippets/src/main/java/com/example/gameservices/clusters/GetCluster.java b/samples/snippets/src/main/java/com/example/gameservices/clusters/GetCluster.java new file mode 100644 index 00000000..9fd65621 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/clusters/GetCluster.java @@ -0,0 +1,47 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.clusters; + +// [START cloud_game_servers_cluster_get] + +import com.google.cloud.gaming.v1.GameServerCluster; +import com.google.cloud.gaming.v1.GameServerClustersServiceClient; +import java.io.IOException; + +public class GetCluster { + + public static void getGameServerCluster( + String projectId, String regionId, String realmId, String clusterId) throws IOException { + // String projectId = "your-project-id"; + // String regionId = "us-central1-f"; + // String realmId = "your-realm-id"; + // String clusterId = "your-game-server-cluster-id"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (GameServerClustersServiceClient client = GameServerClustersServiceClient.create()) { + String parent = + String.format("projects/%s/locations/%s/realms/%s", projectId, regionId, realmId); + String clusterName = String.format("%s/gameServerClusters/%s", parent, clusterId); + + GameServerCluster cluster = client.getGameServerCluster(clusterName); + + System.out.println("Game Server Cluster found: " + cluster.getName()); + } + } +} +// [END cloud_game_servers_cluster_get] diff --git a/samples/snippets/src/main/java/com/example/gameservices/clusters/ListClusters.java b/samples/snippets/src/main/java/com/example/gameservices/clusters/ListClusters.java new file mode 100644 index 00000000..ec5af806 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/clusters/ListClusters.java @@ -0,0 +1,60 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.clusters; + +// [START cloud_game_servers_cluster_list] + +import com.google.cloud.gaming.v1.GameServerCluster; +import com.google.cloud.gaming.v1.GameServerClustersServiceClient; +import com.google.cloud.gaming.v1.GameServerClustersServiceClient.ListGameServerClustersPagedResponse; +import com.google.cloud.gaming.v1.ListGameServerClustersRequest; +import com.google.common.base.Strings; +import java.io.IOException; + +public class ListClusters { + + public static void listGameServerClusters(String projectId, String regionId, String realmId) + throws IOException { + // String projectId = "your-project-id"; + // String regionId = "us-central1-f"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (GameServerClustersServiceClient client = GameServerClustersServiceClient.create()) { + String parent = + String.format("projects/%s/locations/%s/realms/%s", projectId, regionId, realmId); + + ListGameServerClustersPagedResponse response = client.listGameServerClusters(parent); + for (GameServerCluster cluster : response.iterateAll()) { + System.out.println("Game Server Cluster found: " + cluster.getName()); + } + + while (!Strings.isNullOrEmpty(response.getNextPageToken())) { + ListGameServerClustersRequest request = + ListGameServerClustersRequest.newBuilder() + .setParent(parent) + .setPageToken(response.getNextPageToken()) + .build(); + response = client.listGameServerClusters(request); + for (GameServerCluster cluster : response.iterateAll()) { + System.out.println("Game Server Cluster found: " + cluster.getName()); + } + } + } + } +} +// [END cloud_game_servers_cluster_list] diff --git a/samples/snippets/src/main/java/com/example/gameservices/clusters/UpdateCluster.java b/samples/snippets/src/main/java/com/example/gameservices/clusters/UpdateCluster.java new file mode 100644 index 00000000..23efd356 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/clusters/UpdateCluster.java @@ -0,0 +1,59 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.clusters; + +// [START cloud_game_servers_cluster_update] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.gaming.v1.GameServerCluster; +import com.google.cloud.gaming.v1.GameServerClustersServiceClient; +import com.google.cloud.gaming.v1.OperationMetadata; +import com.google.protobuf.FieldMask; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class UpdateCluster { + public static void updateGameServerCluster( + String projectId, String regionId, String realmId, String clusterId) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + // String projectId = "your-project-id"; + // String regionId = "us-central1-f"; + // String realmId = "your-realm-id"; + // String clusterId = "your-game-server-cluster-id"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (GameServerClustersServiceClient client = GameServerClustersServiceClient.create()) { + String parent = + String.format("projects/%s/locations/%s/realms/%s", projectId, regionId, realmId); + String clusterName = String.format("%s/gameServerClusters/%s", parent, clusterId); + + GameServerCluster cluster = + GameServerCluster.newBuilder().setName(clusterName).putLabels("key", "value").build(); + + FieldMask fieldMask = FieldMask.newBuilder().addPaths("labels").build(); + OperationFuture call = + client.updateGameServerClusterAsync(cluster, fieldMask); + + GameServerCluster updated = call.get(1, TimeUnit.MINUTES); + System.out.println("Game Server Cluster updated: " + updated.getName()); + } + } +} +// [END cloud_game_servers_cluster_update] diff --git a/samples/snippets/src/main/java/com/example/gameservices/deployments/CreateDeployment.java b/samples/snippets/src/main/java/com/example/gameservices/deployments/CreateDeployment.java new file mode 100644 index 00000000..a731e249 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/deployments/CreateDeployment.java @@ -0,0 +1,88 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.deployments; + +// [START cloud_game_servers_deployment_create] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.gaming.v1.CreateGameServerDeploymentRequest; +import com.google.cloud.gaming.v1.GameServerDeployment; +import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient; +import com.google.cloud.gaming.v1.OperationMetadata; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class CreateDeployment { + public static void createGameServerDeployment(String projectId, String deploymentId) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + // String projectId = "your-project-id"; + // String deploymentId = "your-game-server-deployment-id"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (GameServerDeploymentsServiceClient client = GameServerDeploymentsServiceClient.create()) { + + // Build a spec as shown at https://agones.dev/site/docs/reference/gameserver/ + JsonObject container = new JsonObject(); + container.addProperty("name", "default"); + container.addProperty("image", "gcr.io/agones-images/default:1.0"); + + JsonArray containers = new JsonArray(); + containers.add(container); + + JsonObject spec = new JsonObject(); + spec.add("containers", containers); + + JsonObject template = new JsonObject(); + template.add("spec", spec); + + JsonObject port = new JsonObject(); + port.addProperty("name", "default"); + + JsonArray ports = new JsonArray(); + ports.add(port); + + JsonObject specObject = new JsonObject(); + specObject.add("ports", ports); + specObject.add("template", template); + + String parent = String.format("projects/%s/locations/global", projectId); + String deploymentName = String.format("%s/gameServerDeployments/%s", parent, deploymentId); + + GameServerDeployment gameServerDeployment = + GameServerDeployment.newBuilder().setName(deploymentName).build(); + + CreateGameServerDeploymentRequest request = + CreateGameServerDeploymentRequest.newBuilder() + .setParent(parent) + .setDeploymentId(deploymentId) + .setGameServerDeployment(gameServerDeployment) + .build(); + + OperationFuture call = + client.createGameServerDeploymentAsync(request); + + GameServerDeployment created = call.get(1, TimeUnit.MINUTES); + System.out.println("Game Server Deployment created: " + created.getName()); + } + } +} +// [END cloud_game_servers_deployment_create] diff --git a/samples/snippets/src/main/java/com/example/gameservices/deployments/DeleteDeployment.java b/samples/snippets/src/main/java/com/example/gameservices/deployments/DeleteDeployment.java new file mode 100644 index 00000000..427df569 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/deployments/DeleteDeployment.java @@ -0,0 +1,49 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.deployments; + +// [START cloud_game_servers_deployment_delete] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient; +import com.google.cloud.gaming.v1.OperationMetadata; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class DeleteDeployment { + public static void deleteGameServerDeployment(String projectId, String deploymentId) + throws InterruptedException, ExecutionException, TimeoutException, IOException { + // String projectId = "your-project-id"; + // String deploymentId = "your-game-server-deployment-id"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (GameServerDeploymentsServiceClient client = GameServerDeploymentsServiceClient.create()) { + String parent = String.format("projects/%s/locations/global", projectId); + String deploymentName = String.format("%s/gameServerDeployments/%s", parent, deploymentId); + + OperationFuture call = + client.deleteGameServerDeploymentAsync(deploymentName); + call.get(1, TimeUnit.MINUTES); + System.out.println("Game Server Deployment deleted: " + deploymentName); + } + } +} +// [END cloud_game_servers_deployment_delete] diff --git a/samples/snippets/src/main/java/com/example/gameservices/deployments/GetDeployment.java b/samples/snippets/src/main/java/com/example/gameservices/deployments/GetDeployment.java new file mode 100644 index 00000000..58cb301f --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/deployments/GetDeployment.java @@ -0,0 +1,44 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.deployments; + +// [START cloud_game_servers_deployment_get] + +import com.google.cloud.gaming.v1.GameServerDeployment; +import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient; +import java.io.IOException; + +public class GetDeployment { + public static void getGameServerDeployment(String projectId, String deploymentId) + throws IOException { + // String projectId = "your-project-id"; + // String deploymentId = "your-game-server-deployment-id"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (GameServerDeploymentsServiceClient client = GameServerDeploymentsServiceClient.create()) { + String deploymentName = + String.format( + "projects/%s/locations/global/gameServerDeployments/%s", projectId, deploymentId); + + GameServerDeployment deployment = client.getGameServerDeployment(deploymentName); + + System.out.println("Game Server Deployment found: " + deployment.getName()); + } + } +} +// [END cloud_game_servers_deployment_get] diff --git a/samples/snippets/src/main/java/com/example/gameservices/deployments/ListDeployments.java b/samples/snippets/src/main/java/com/example/gameservices/deployments/ListDeployments.java new file mode 100644 index 00000000..a4237901 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/deployments/ListDeployments.java @@ -0,0 +1,56 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.deployments; + +// [START cloud_game_servers_deployment_list] + +import com.google.cloud.gaming.v1.GameServerDeployment; +import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient; +import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient.ListGameServerDeploymentsPagedResponse; +import com.google.cloud.gaming.v1.ListGameServerDeploymentsRequest; +import com.google.common.base.Strings; +import java.io.IOException; + +public class ListDeployments { + public static void listGameServerDeployments(String projectId) throws IOException { + // String projectId = "your-project-id"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (GameServerDeploymentsServiceClient client = GameServerDeploymentsServiceClient.create()) { + String parent = String.format("projects/%s/locations/global", projectId); + + ListGameServerDeploymentsPagedResponse response = client.listGameServerDeployments(parent); + for (GameServerDeployment deployment : response.iterateAll()) { + System.out.println("Game Server Deployment found: " + deployment.getName()); + } + + while (!Strings.isNullOrEmpty(response.getNextPageToken())) { + ListGameServerDeploymentsRequest request = + ListGameServerDeploymentsRequest.newBuilder() + .setParent(parent) + .setPageToken(response.getNextPageToken()) + .build(); + response = client.listGameServerDeployments(request); + for (GameServerDeployment deployment : response.iterateAll()) { + System.out.println("Game Server Deployment found: " + deployment.getName()); + } + } + } + } +} +// [END cloud_game_servers_deployment_list] diff --git a/samples/snippets/src/main/java/com/example/gameservices/deployments/UpdateDeployment.java b/samples/snippets/src/main/java/com/example/gameservices/deployments/UpdateDeployment.java new file mode 100644 index 00000000..5d651b99 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/deployments/UpdateDeployment.java @@ -0,0 +1,60 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.deployments; + +// [START cloud_game_servers_deployment_update] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.gaming.v1.GameServerDeployment; +import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient; +import com.google.cloud.gaming.v1.OperationMetadata; +import com.google.protobuf.FieldMask; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class UpdateDeployment { + public static void updateGameServerDeployment(String projectId, String deploymentId) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + // String projectId = "your-project-id"; + // String deploymentId = "your-game-server-deployment-id"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (GameServerDeploymentsServiceClient client = GameServerDeploymentsServiceClient.create()) { + String deploymentName = + String.format( + "projects/%s/locations/global/gameServerDeployments/%s", projectId, deploymentId); + + GameServerDeployment deployment = + GameServerDeployment.newBuilder() + .setName(deploymentName) + .putLabels("key", "value") + .build(); + + FieldMask fieldMask = FieldMask.newBuilder().addPaths("labels").build(); + + OperationFuture call = + client.updateGameServerDeploymentAsync(deployment, fieldMask); + + GameServerDeployment updated = call.get(1, TimeUnit.MINUTES); + System.out.println("Game Server Deployment updated: " + updated.getName()); + } + } +} +// [END cloud_game_servers_deployment_update] diff --git a/samples/snippets/src/main/java/com/example/gameservices/realms/CreateRealm.java b/samples/snippets/src/main/java/com/example/gameservices/realms/CreateRealm.java new file mode 100644 index 00000000..f7cf59bf --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/realms/CreateRealm.java @@ -0,0 +1,61 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.realms; + +// [START cloud_game_servers_realm_create] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.gaming.v1.CreateRealmRequest; +import com.google.cloud.gaming.v1.OperationMetadata; +import com.google.cloud.gaming.v1.Realm; +import com.google.cloud.gaming.v1.RealmsServiceClient; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class CreateRealm { + public static void createRealm(String projectId, String regionId, String realmId) + throws InterruptedException, ExecutionException, TimeoutException, IOException { + // String projectId = "your-project-id"; + // String regionId = "us-central1-f"; + // String realmId = "your-realm-id"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (RealmsServiceClient client = RealmsServiceClient.create()) { + String parent = String.format("projects/%s/locations/%s", projectId, regionId); + String realmName = String.format("%s/realms/%s", parent, realmId); + + Realm realm = + Realm.newBuilder().setName(realmName).setTimeZone("America/Los_Angeles").build(); + + CreateRealmRequest request = + CreateRealmRequest.newBuilder() + .setParent(parent) + .setRealmId(realmId) + .setRealm(realm) + .build(); + + OperationFuture call = client.createRealmAsync(request); + Realm result = call.get(1, TimeUnit.MINUTES); + + System.out.println("Realm created: " + result.getName()); + } + } +} +// [END cloud_game_servers_realm_create] diff --git a/samples/snippets/src/main/java/com/example/gameservices/realms/DeleteRealm.java b/samples/snippets/src/main/java/com/example/gameservices/realms/DeleteRealm.java new file mode 100644 index 00000000..24581a4f --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/realms/DeleteRealm.java @@ -0,0 +1,50 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.realms; + +// [START cloud_game_servers_realm_delete] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.gaming.v1.OperationMetadata; +import com.google.cloud.gaming.v1.RealmsServiceClient; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class DeleteRealm { + public static void deleteRealm(String projectId, String regionId, String realmId) + throws InterruptedException, ExecutionException, TimeoutException, IOException { + // String projectId = "your-project-id"; + // String regionId = "us-central1-f"; + // String realmId = "your-realm-id"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (RealmsServiceClient client = RealmsServiceClient.create()) { + String parent = String.format("projects/%s/locations/%s", projectId, regionId); + String realmName = String.format("%s/realms/%s", parent, realmId); + + OperationFuture call = client.deleteRealmAsync(realmName); + + call.get(1, TimeUnit.MINUTES); + System.out.println("Realm deleted: " + realmName); + } + } +} +// [END cloud_game_servers_realm_delete] diff --git a/samples/snippets/src/main/java/com/example/gameservices/realms/GetRealm.java b/samples/snippets/src/main/java/com/example/gameservices/realms/GetRealm.java new file mode 100644 index 00000000..b6d55eca --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/realms/GetRealm.java @@ -0,0 +1,44 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.realms; + +// [START cloud_game_servers_realm_get] + +import com.google.cloud.gaming.v1.Realm; +import com.google.cloud.gaming.v1.RealmsServiceClient; +import java.io.IOException; + +public class GetRealm { + public static void getRealm(String projectId, String regionId, String realmId) + throws IOException { + // String projectId = "your-project-id"; + // String regionId = "us-central1-f"; + // String realmId = "your-realm-id"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (RealmsServiceClient client = RealmsServiceClient.create()) { + String realmName = + String.format("projects/%s/locations/%s/realms/%s", projectId, regionId, realmId); + + Realm allocationPolicy = client.getRealm(realmName); + + System.out.println("Realm found: " + allocationPolicy.getName()); + } + } +} +// [END cloud_game_servers_realm_get] diff --git a/samples/snippets/src/main/java/com/example/gameservices/realms/ListRealms.java b/samples/snippets/src/main/java/com/example/gameservices/realms/ListRealms.java new file mode 100644 index 00000000..87ca8d13 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/realms/ListRealms.java @@ -0,0 +1,57 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.realms; + +// [START cloud_game_servers_realm_list] + +import com.google.cloud.gaming.v1.ListRealmsRequest; +import com.google.cloud.gaming.v1.Realm; +import com.google.cloud.gaming.v1.RealmsServiceClient; +import com.google.cloud.gaming.v1.RealmsServiceClient.ListRealmsPagedResponse; +import com.google.common.base.Strings; +import java.io.IOException; + +public class ListRealms { + public static void listRealms(String projectId, String regionId) throws IOException { + // String projectId = "your-project-id"; + // String regionId = "us-central1-f"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (RealmsServiceClient client = RealmsServiceClient.create()) { + String parent = String.format("projects/%s/locations/%s", projectId, regionId); + + ListRealmsPagedResponse response = client.listRealms(parent); + for (Realm realm : response.iterateAll()) { + System.out.println("Realm found: " + realm.getName()); + } + + while (!Strings.isNullOrEmpty(response.getNextPageToken())) { + ListRealmsRequest request = + ListRealmsRequest.newBuilder() + .setParent(parent) + .setPageToken(response.getNextPageToken()) + .build(); + response = client.listRealms(request); + for (Realm realm : response.iterateAll()) { + System.out.println("Realm found: " + realm.getName()); + } + } + } + } +} +// [END cloud_game_servers_realm_list] diff --git a/samples/snippets/src/main/java/com/example/gameservices/realms/UpdateRealm.java b/samples/snippets/src/main/java/com/example/gameservices/realms/UpdateRealm.java new file mode 100644 index 00000000..9e6f6891 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/gameservices/realms/UpdateRealm.java @@ -0,0 +1,55 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices.realms; + +// [START cloud_game_servers_realm_update] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.gaming.v1.OperationMetadata; +import com.google.cloud.gaming.v1.Realm; +import com.google.cloud.gaming.v1.RealmsServiceClient; +import com.google.protobuf.FieldMask; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class UpdateRealm { + public static void updateRealm(String projectId, String regionId, String realmId) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + // String projectId = "your-project-id"; + // String regionId = "us-central1-f"; + // String realmId = "your-realm-id"; + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (RealmsServiceClient client = RealmsServiceClient.create()) { + String parent = String.format("projects/%s/locations/%s", projectId, regionId); + String realmName = String.format("%s/realms/%s", parent, realmId); + + Realm realm = Realm.newBuilder().setName(realmName).setTimeZone("America/New_York").build(); + + FieldMask fieldMask = FieldMask.newBuilder().addPaths("time_zone").build(); + + OperationFuture call = client.updateRealmAsync(realm, fieldMask); + + Realm updated = call.get(1, TimeUnit.MINUTES); + System.out.println("Realm updated: " + updated.getName()); + } + } +} +// [END cloud_game_servers_realm_update] diff --git a/samples/snippets/src/test/java/com/example/gameservices/ClusterTests.java b/samples/snippets/src/test/java/com/example/gameservices/ClusterTests.java new file mode 100644 index 00000000..da0e0e77 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/gameservices/ClusterTests.java @@ -0,0 +1,131 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices; + +import static org.junit.Assert.assertTrue; + +import com.example.gameservices.clusters.CreateCluster; +import com.example.gameservices.clusters.DeleteCluster; +import com.example.gameservices.clusters.GetCluster; +import com.example.gameservices.clusters.ListClusters; +import com.example.gameservices.clusters.UpdateCluster; +import com.example.gameservices.realms.CreateRealm; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ClusterTests { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String REGION_ID = "us-central1"; + + private static String parentName = + String.format("projects/%s/locations/%s", PROJECT_ID, REGION_ID); + + private static String realmId = "realm-1"; + private static String realmName = String.format("%s/realms/%s", parentName, realmId); + + private static String clusterId = "cluster-1"; + private static String clusterName = + String.format("%s/gameServerClusters/%s", realmName, clusterId); + + private static final String DEFAULT_GKE_CLUSTER = + String.format("projects/%s/locations/us-central1-a/clusters/standard-cluster-1", PROJECT_ID); + private static String gkeClusterName; + + static { + gkeClusterName = System.getenv("GKE_CLUSTER"); + if (gkeClusterName == null) { + gkeClusterName = DEFAULT_GKE_CLUSTER; + } + } + + private final PrintStream originalOut = System.out; + private ByteArrayOutputStream bout; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @BeforeClass + public static void init() + throws InterruptedException, ExecutionException, TimeoutException, IOException { + // Delete all existing clusters and realms. + GameServicesTestUtil.deleteExistingClusters(realmName); + GameServicesTestUtil.deleteExistingRealms(parentName); + CreateRealm.createRealm(PROJECT_ID, REGION_ID, realmId); + CreateCluster.createGameServerCluster( + PROJECT_ID, REGION_ID, realmId, clusterId, gkeClusterName); + } + + @After + public void tearDown() { + System.setOut(originalOut); + bout.reset(); + } + + @AfterClass + public static void tearDownClass() { + GameServicesTestUtil.deleteExistingClusters(realmName); + GameServicesTestUtil.deleteExistingRealms(parentName); + } + + @Test + public void createDeleteClusterTest() + throws InterruptedException, ExecutionException, TimeoutException, IOException { + String newClusterId = "cluster-2"; + String newClusterName = String.format("%s/gameServerClusters/%s", realmName, newClusterId); + CreateCluster.createGameServerCluster( + PROJECT_ID, REGION_ID, realmId, newClusterId, gkeClusterName); + DeleteCluster.deleteGameServerCluster(PROJECT_ID, REGION_ID, realmId, newClusterId); + assertTrue(bout.toString().contains("Game Server Cluster created: " + newClusterName)); + assertTrue(bout.toString().contains("Game Server Cluster deleted: " + newClusterName)); + } + + @Test + public void getClusterTest() throws IOException { + GetCluster.getGameServerCluster(PROJECT_ID, REGION_ID, realmId, clusterId); + + assertTrue(bout.toString().contains("Game Server Cluster found: " + clusterName)); + } + + @Test + public void listClustersTest() throws IOException { + ListClusters.listGameServerClusters(PROJECT_ID, REGION_ID, realmId); + + assertTrue(bout.toString().contains("Game Server Cluster found: " + clusterName)); + } + + @Test + public void updateClusterTest() + throws InterruptedException, ExecutionException, TimeoutException, IOException { + UpdateCluster.updateGameServerCluster(PROJECT_ID, REGION_ID, realmId, clusterId); + + assertTrue(bout.toString().contains("Game Server Cluster updated: " + clusterName)); + } +} diff --git a/samples/snippets/src/test/java/com/example/gameservices/DeploymentTests.java b/samples/snippets/src/test/java/com/example/gameservices/DeploymentTests.java new file mode 100644 index 00000000..fa4dafff --- /dev/null +++ b/samples/snippets/src/test/java/com/example/gameservices/DeploymentTests.java @@ -0,0 +1,107 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices; + +import static org.junit.Assert.assertTrue; + +import com.example.gameservices.deployments.CreateDeployment; +import com.example.gameservices.deployments.DeleteDeployment; +import com.example.gameservices.deployments.GetDeployment; +import com.example.gameservices.deployments.ListDeployments; +import com.example.gameservices.deployments.UpdateDeployment; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class DeploymentTests { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private static String parentName = String.format("projects/%s/locations/global", PROJECT_ID); + private static String deploymentId = "deployment-1"; + private static String deploymentName = + String.format("%s/gameServerDeployments/%s", parentName, deploymentId); + + private final PrintStream originalOut = System.out; + private ByteArrayOutputStream bout; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @BeforeClass + public static void init() + throws InterruptedException, ExecutionException, TimeoutException, IOException { + GameServicesTestUtil.deleteExistingDeployments(parentName); + CreateDeployment.createGameServerDeployment(PROJECT_ID, deploymentId); + } + + @After + public void tearDown() { + System.setOut(originalOut); + bout.reset(); + } + + @AfterClass + public static void tearDownClass() { + GameServicesTestUtil.deleteExistingDeployments(parentName); + } + + @Test + public void createDeleteGameServerDeploymentTest() + throws InterruptedException, ExecutionException, TimeoutException, IOException { + String newDeploymentId = "deployment-2"; + String newDeploymentName = + String.format("%s/gameServerDeployments/%s", parentName, newDeploymentId); + CreateDeployment.createGameServerDeployment(PROJECT_ID, newDeploymentId); + DeleteDeployment.deleteGameServerDeployment(PROJECT_ID, newDeploymentId); + assertTrue(bout.toString().contains("Game Server Deployment created: " + newDeploymentName)); + assertTrue(bout.toString().contains("Game Server Deployment deleted: " + newDeploymentName)); + } + + @Test + public void getGameServerDeploymentTest() throws IOException { + GetDeployment.getGameServerDeployment(PROJECT_ID, deploymentId); + + assertTrue(bout.toString().contains("Game Server Deployment found: " + deploymentName)); + } + + @Test + public void listGameServerDeploymentsTest() throws IOException { + ListDeployments.listGameServerDeployments(PROJECT_ID); + + assertTrue(bout.toString().contains("Game Server Deployment found: " + deploymentName)); + } + + @Test + public void updateGameServerDeploymentTest() + throws InterruptedException, ExecutionException, TimeoutException, IOException { + UpdateDeployment.updateGameServerDeployment(PROJECT_ID, deploymentId); + assertTrue(bout.toString().contains("Game Server Deployment updated: " + deploymentName)); + } +} diff --git a/samples/snippets/src/test/java/com/example/gameservices/GameServicesTestUtil.java b/samples/snippets/src/test/java/com/example/gameservices/GameServicesTestUtil.java new file mode 100644 index 00000000..6096ad2c --- /dev/null +++ b/samples/snippets/src/test/java/com/example/gameservices/GameServicesTestUtil.java @@ -0,0 +1,102 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.gaming.v1.GameServerCluster; +import com.google.cloud.gaming.v1.GameServerClustersServiceClient; +import com.google.cloud.gaming.v1.GameServerClustersServiceClient.ListGameServerClustersPagedResponse; +import com.google.cloud.gaming.v1.GameServerDeployment; +import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient; +import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient.ListGameServerDeploymentsPagedResponse; +import com.google.cloud.gaming.v1.Realm; +import com.google.cloud.gaming.v1.RealmsServiceClient; +import com.google.cloud.gaming.v1.RealmsServiceClient.ListRealmsPagedResponse; +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +class GameServicesTestUtil { + private static GameServerClustersServiceClient clustersClient; + private static GameServerDeploymentsServiceClient deploymentsClient; + private static RealmsServiceClient realmsClient; + + private static GameServerClustersServiceClient getClustersClient() throws IOException { + if (clustersClient == null) { + clustersClient = GameServerClustersServiceClient.create(); + } + return clustersClient; + } + + private static GameServerDeploymentsServiceClient getDeploymentsClient() throws IOException { + if (deploymentsClient == null) { + deploymentsClient = GameServerDeploymentsServiceClient.create(); + } + return deploymentsClient; + } + + private static RealmsServiceClient getRealmsClient() throws IOException { + if (realmsClient == null) { + realmsClient = RealmsServiceClient.create(); + } + return realmsClient; + } + + public static void deleteExistingClusters(String parent) { + try { + ListGameServerClustersPagedResponse response = + getClustersClient().listGameServerClusters(parent); + + for (GameServerCluster cluster : response.iterateAll()) { + System.out.println("Deleting game cluster " + cluster.getName()); + OperationFuture poll = getClustersClient().deleteGameServerClusterAsync(cluster.getName()); + poll.get(1, TimeUnit.MINUTES); + } + } catch (Exception e) { + e.printStackTrace(System.err); + } + } + + public static void deleteExistingDeployments(String parent) { + try { + ListGameServerDeploymentsPagedResponse response = + getDeploymentsClient().listGameServerDeployments(parent); + + for (GameServerDeployment deployment : response.iterateAll()) { + System.out.println("Deleting game server deployment " + deployment.getName()); + OperationFuture poll = + getDeploymentsClient().deleteGameServerDeploymentAsync(deployment.getName()); + poll.get(1, TimeUnit.MINUTES); + } + } catch (Exception e) { + e.printStackTrace(System.err); + } + } + + public static void deleteExistingRealms(String parent) { + try { + ListRealmsPagedResponse response = getRealmsClient().listRealms(parent); + + for (Realm realm : response.iterateAll()) { + System.out.println("Deleting realm " + realm.getName()); + OperationFuture poll = getRealmsClient().deleteRealmAsync(realm.getName()); + poll.get(1, TimeUnit.MINUTES); + } + } catch (Exception e) { + e.printStackTrace(System.err); + } + } +} diff --git a/samples/snippets/src/test/java/com/example/gameservices/RealmTests.java b/samples/snippets/src/test/java/com/example/gameservices/RealmTests.java new file mode 100644 index 00000000..9c3274fe --- /dev/null +++ b/samples/snippets/src/test/java/com/example/gameservices/RealmTests.java @@ -0,0 +1,111 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.gameservices; + +import static org.junit.Assert.assertTrue; + +import com.example.gameservices.realms.CreateRealm; +import com.example.gameservices.realms.DeleteRealm; +import com.example.gameservices.realms.GetRealm; +import com.example.gameservices.realms.ListRealms; +import com.example.gameservices.realms.UpdateRealm; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class RealmTests { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String REGION_ID = "us-central1"; + + private static String parentName = + String.format("projects/%s/locations/%s", PROJECT_ID, REGION_ID); + + private static String realmId = "realm-1"; + private static String realmName = String.format("%s/realms/%s", parentName, realmId); + + private final PrintStream originalOut = System.out; + private ByteArrayOutputStream bout; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @BeforeClass + public static void init() + throws InterruptedException, ExecutionException, TimeoutException, IOException { + GameServicesTestUtil.deleteExistingRealms(parentName); + CreateRealm.createRealm(PROJECT_ID, REGION_ID, realmId); + } + + @After + public void tearDown() { + System.setOut(originalOut); + bout.reset(); + } + + @AfterClass + public static void tearDownClass() { + GameServicesTestUtil.deleteExistingRealms(parentName); + } + + @Test + public void createDeleteRealmTest() + throws InterruptedException, ExecutionException, TimeoutException, IOException { + String newRealmId = "realm-2"; + String newRealmName = + String.format("projects/%s/locations/%s/realms/%s", PROJECT_ID, REGION_ID, newRealmId); + CreateRealm.createRealm(PROJECT_ID, REGION_ID, newRealmId); + DeleteRealm.deleteRealm(PROJECT_ID, REGION_ID, newRealmId); + String output = bout.toString(); + assertTrue(output.contains("Realm created: " + newRealmName)); + assertTrue(output.contains("Realm deleted: " + newRealmName)); + } + + @Test + public void getRealmTest() throws IOException { + GetRealm.getRealm(PROJECT_ID, REGION_ID, realmId); + + assertTrue(bout.toString().contains("Realm found: " + realmName)); + } + + @Test + public void listRealmsTest() throws IOException { + ListRealms.listRealms(PROJECT_ID, REGION_ID); + + assertTrue(bout.toString().contains("Realm found: " + realmName)); + } + + @Test + public void updateRealmTest() + throws InterruptedException, ExecutionException, TimeoutException, IOException { + UpdateRealm.updateRealm(PROJECT_ID, REGION_ID, realmId); + + assertTrue(bout.toString().contains("Realm updated: " + realmName)); + } +}