Skip to content

Commit 7c45e8b

Browse files
authored
Creating game servers samples. (GoogleCloudPlatform#1516)
* Game servers samples. * Feedback updates: 1. Catch exceptions in samples instead of throwing. 2. Use poll responses from Create/Update calls in output. 3. Paginate List responses. Other changes: 1. Fixed non-functional Update calls. 2. Removed GKE_CLUSTER from kokoro config (added to secrets file in bigstore). * Formatting feedback on gameservers.
1 parent c0210e2 commit 7c45e8b

37 files changed

+2573
-0
lines changed

gameservices/v1alpha/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!--
2+
Copyright 2019 Google LLC
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
-->
13+
14+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
15+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
16+
<modelVersion>4.0.0</modelVersion>
17+
<groupId>com.google.gameservices.snippets</groupId>
18+
<artifactId>gameservices</artifactId>
19+
<packaging>jar</packaging>
20+
<version>1.0</version>
21+
<name>gameservices</name>
22+
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not affect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.11</version>
31+
</parent>
32+
33+
<properties>
34+
<maven.compiler.target>1.8</maven.compiler.target>
35+
<maven.compiler.source>1.8</maven.compiler.source>
36+
</properties>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>com.google.cloud</groupId>
41+
<artifactId>google-cloud-gameservices</artifactId>
42+
<version>0.1.0</version>
43+
</dependency>
44+
45+
<!-- Test dependencies -->
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>4.12</version>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.google.truth</groupId>
54+
<artifactId>truth</artifactId>
55+
<version>0.40</version>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2019 Google LLC
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+
17+
package com.google.cloud.gameservices.samples.allocationpolicies;
18+
19+
// [START cloud_game_servers_allocation_policy_create]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.gaming.v1alpha.AllocationPoliciesServiceClient;
23+
import com.google.cloud.gaming.v1alpha.AllocationPolicy;
24+
import com.google.cloud.gaming.v1alpha.CreateAllocationPolicyRequest;
25+
import com.google.protobuf.Empty;
26+
import com.google.protobuf.Int32Value;
27+
28+
import java.io.IOException;
29+
import java.util.concurrent.ExecutionException;
30+
import java.util.concurrent.TimeUnit;
31+
import java.util.concurrent.TimeoutException;
32+
33+
public class CreateAllocationPolicy {
34+
public static void createAllocationPolicy(String projectId, String policyId) {
35+
// String projectId = "your-project-id";
36+
// String policyId = "your-policy-id";
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests. After completing all of your requests, call
39+
// the "close" method on the client to safely clean up any remaining background resources.
40+
try (AllocationPoliciesServiceClient client = AllocationPoliciesServiceClient.create()) {
41+
String parent = String.format("projects/%s/locations/global", projectId);
42+
String policyName = String.format("%s/allocationPolicies/%s", parent, policyId);
43+
44+
AllocationPolicy policy = AllocationPolicy
45+
.newBuilder()
46+
.setName(policyName)
47+
.setPriority(Int32Value.newBuilder().setValue(1))
48+
.build();
49+
50+
CreateAllocationPolicyRequest request = CreateAllocationPolicyRequest
51+
.newBuilder()
52+
.setParent(parent)
53+
.setAllocationPolicyId(policyId)
54+
.setAllocationPolicy(policy)
55+
.build();
56+
57+
OperationFuture<AllocationPolicy, Empty> call = client.createAllocationPolicyAsync(request);
58+
59+
AllocationPolicy result = call.get(1, TimeUnit.MINUTES);
60+
System.out.println("Allocation Policy created: " + result.getName());
61+
} catch (IOException | InterruptedException | ExecutionException | TimeoutException e) {
62+
System.err.println("Allocation Policy create request unsuccessful.");
63+
e.printStackTrace(System.err);
64+
}
65+
}
66+
}
67+
// [END cloud_game_servers_allocation_policy_create]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2019 Google LLC
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+
17+
package com.google.cloud.gameservices.samples.allocationpolicies;
18+
19+
// [START cloud_game_servers_allocation_policy_delete]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.gaming.v1alpha.AllocationPoliciesServiceClient;
23+
import com.google.protobuf.Empty;
24+
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.concurrent.TimeoutException;
29+
30+
public class DeleteAllocationPolicy {
31+
32+
public static void deleteAllocationPolicy(String projectId, String policyId) {
33+
// String projectId = "your-project-id";
34+
// String policyId = "your-policy-id";
35+
// Initialize client that will be used to send requests. This client only needs to be created
36+
// once, and can be reused for multiple requests. After completing all of your requests, call
37+
// the "close" method on the client to safely clean up any remaining background resources.
38+
try (AllocationPoliciesServiceClient client = AllocationPoliciesServiceClient.create()) {
39+
String parent = String.format("projects/%s/locations/global", projectId);
40+
String policyName = String.format("%s/allocationPolicies/%s", parent, policyId);
41+
42+
OperationFuture<Empty, Empty> call = client.deleteAllocationPolicyAsync(policyName);
43+
44+
call.get(1, TimeUnit.MINUTES);
45+
System.out.println("Allocation Policy deleted: " + policyName);
46+
} catch (IOException | InterruptedException | ExecutionException | TimeoutException e) {
47+
System.err.println("Allocation Policy delete request unsuccessful.");
48+
e.printStackTrace(System.err);
49+
}
50+
}
51+
}
52+
// [END cloud_game_servers_allocation_policy_delete]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2019 Google LLC
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+
17+
package com.google.cloud.gameservices.samples.allocationpolicies;
18+
19+
// [START cloud_game_servers_allocation_policy_get]
20+
21+
import com.google.cloud.gaming.v1alpha.AllocationPoliciesServiceClient;
22+
import com.google.cloud.gaming.v1alpha.AllocationPolicy;
23+
24+
import java.io.IOException;
25+
26+
public class GetAllocationPolicy {
27+
public static void getAllocationPolicy(String projectId, String policyId) {
28+
// String projectId = "your-project-id";
29+
// String policyId = "your-policy-id";
30+
// Initialize client that will be used to send requests. This client only needs to be created
31+
// once, and can be reused for multiple requests. After completing all of your requests, call
32+
// the "close" method on the client to safely clean up any remaining background resources.
33+
try (AllocationPoliciesServiceClient client = AllocationPoliciesServiceClient.create()) {
34+
String policyName = String.format(
35+
"projects/%s/locations/global/allocationPolicies/%s", projectId, policyId);
36+
37+
AllocationPolicy allocationPolicy = client.getAllocationPolicy(policyName);
38+
39+
System.out.println("Allocation Policy found: " + allocationPolicy.getName());
40+
} catch (IOException e) {
41+
e.printStackTrace(System.err);
42+
}
43+
}
44+
}
45+
// [END cloud_game_servers_allocation_policy_get]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2019 Google LLC
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+
17+
package com.google.cloud.gameservices.samples.allocationpolicies;
18+
19+
// [START cloud_game_servers_allocation_policy_list]
20+
21+
import com.google.cloud.gaming.v1alpha.AllocationPoliciesServiceClient;
22+
import com.google.cloud.gaming.v1alpha.AllocationPoliciesServiceClient.ListAllocationPoliciesPagedResponse;
23+
import com.google.cloud.gaming.v1alpha.AllocationPolicy;
24+
import com.google.cloud.gaming.v1alpha.ListAllocationPoliciesRequest;
25+
import com.google.common.base.Strings;
26+
27+
import java.io.IOException;
28+
29+
public class ListAllocationPolicies {
30+
public static void listAllocationPolicies(String projectId) {
31+
// String projectId = "your-project-id";
32+
// Initialize client that will be used to send requests. This client only needs to be created
33+
// once, and can be reused for multiple requests. After completing all of your requests, call
34+
// the "close" method on the client to safely clean up any remaining background resources.
35+
try (AllocationPoliciesServiceClient client = AllocationPoliciesServiceClient.create()) {
36+
String parent = String.format("projects/%s/locations/global", projectId);
37+
38+
ListAllocationPoliciesPagedResponse response = client.listAllocationPolicies(parent);
39+
for (AllocationPolicy policy : response.iterateAll()) {
40+
System.out.println("Allocation Policy found: " + policy.getName());
41+
}
42+
43+
while (!Strings.isNullOrEmpty(response.getNextPageToken())) {
44+
ListAllocationPoliciesRequest request = ListAllocationPoliciesRequest
45+
.newBuilder()
46+
.setParent(parent)
47+
.setPageToken(response.getNextPageToken())
48+
.build();
49+
response = client.listAllocationPolicies(request);
50+
for (AllocationPolicy policy : response.iterateAll()) {
51+
System.out.println("Allocation Policy found: " + policy.getName());
52+
}
53+
}
54+
} catch (IOException e) {
55+
e.printStackTrace(System.err);
56+
}
57+
}
58+
}
59+
// [END cloud_game_servers_allocation_policy_list]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2019 Google LLC
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+
17+
package com.google.cloud.gameservices.samples.allocationpolicies;
18+
19+
// [START cloud_game_servers_allocation_policy_update]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.gaming.v1alpha.AllocationPoliciesServiceClient;
23+
import com.google.cloud.gaming.v1alpha.AllocationPolicy;
24+
import com.google.protobuf.Empty;
25+
import com.google.protobuf.FieldMask;
26+
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.TimeUnit;
30+
import java.util.concurrent.TimeoutException;
31+
32+
public class UpdateAllocationPolicy {
33+
public static void updateAllocationPolicy(String projectId, String policyId) {
34+
// String projectId = "your-project-id";
35+
// String policyId = "your-policy-id";
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests. After completing all of your requests, call
38+
// the "close" method on the client to safely clean up any remaining background resources.
39+
try (AllocationPoliciesServiceClient client = AllocationPoliciesServiceClient.create()) {
40+
String policyName = String.format(
41+
"projects/%s/locations/global/allocationPolicies/%s", projectId, policyId);
42+
43+
AllocationPolicy policy = client
44+
.getAllocationPolicy(policyName)
45+
.toBuilder()
46+
.setWeight(5)
47+
.build();
48+
49+
FieldMask fieldMask = FieldMask.newBuilder().addPaths("weight").build();
50+
51+
OperationFuture<AllocationPolicy, Empty> call = client.updateAllocationPolicyAsync(
52+
policy, fieldMask);
53+
54+
AllocationPolicy updated = call.get(1, TimeUnit.MINUTES);
55+
System.out.println("Allocation Policy updated: " + updated.getName());
56+
} catch (IOException | InterruptedException | ExecutionException | TimeoutException e) {
57+
System.err.println("Allocation Policy update request unsuccessful.");
58+
e.printStackTrace(System.err);
59+
}
60+
}
61+
}
62+
// [END cloud_game_servers_allocation_policy_update]

0 commit comments

Comments
 (0)