Skip to content

Commit be4dff5

Browse files
Merge branch 'main' into compute_snapshot_schedule_attach
2 parents 8cf36fd + 0795de2 commit be4dff5

26 files changed

+1669
-142
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
/speech @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers
8080
/talent @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers
8181
/texttospeech @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers
82-
/translate @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers
82+
/translate @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/cloud-ml-translate-dev
8383
/video @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers
8484
/vision @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers
8585

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2024 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 compute;
18+
19+
// [START compute_instance_create_replicated_boot_disk]
20+
import com.google.cloud.compute.v1.AttachedDisk;
21+
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
22+
import com.google.cloud.compute.v1.Instance;
23+
import com.google.cloud.compute.v1.InstancesClient;
24+
import com.google.cloud.compute.v1.NetworkInterface;
25+
import com.google.cloud.compute.v1.Operation;
26+
import com.google.cloud.compute.v1.Operation.Status;
27+
import java.io.IOException;
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
import java.util.concurrent.ExecutionException;
31+
import java.util.concurrent.TimeUnit;
32+
import java.util.concurrent.TimeoutException;
33+
34+
public class CreateInstanceWithRegionalDiskFromSnapshot {
35+
36+
public static void main(String[] args) throws IOException, ExecutionException,
37+
InterruptedException, TimeoutException {
38+
// TODO(developer): Replace these variables before running the sample
39+
// Project ID or project number of the Cloud project you want to use.
40+
String projectId = "YOUR_PROJECT_ID";
41+
// Name of the zone in which you want to create the instance.
42+
String zone = "us-central1-a";
43+
// Name of the instance you want to create.
44+
String instanceName = "YOUR_INSTANCE_NAME";
45+
// Name for the replicated disk.
46+
String diskName = "YOUR_REPLICATED_DISK_NAME";
47+
String region = zone.substring(0, zone.length() - 2);
48+
// Type of the disk.
49+
String diskType = String.format(
50+
"projects/%s/regions/%s/diskTypes/pd-standard", projectId, region);
51+
// The full path and name of the snapshot that you want to use as the source for the new disk.
52+
String snapshotLink = String.format("projects/%s/global/snapshots/%s", projectId,
53+
"SNAPSHOT_NAME");
54+
// An iterable collection of zone names in which you want to keep
55+
// the new disks' replicas. One of the replica zones of the clone must match
56+
// the zone of the source disk.
57+
List<String> replicaZones = new ArrayList<>();
58+
59+
createInstanceWithRegionalDiskFromSnapshot(projectId, zone, instanceName, diskName, diskType,
60+
snapshotLink, replicaZones);
61+
}
62+
63+
// Creates a new VM instance with regional disk from a snapshot and specifies replica zones.
64+
public static Status createInstanceWithRegionalDiskFromSnapshot(
65+
String projectId, String zone, String instanceName, String diskName,
66+
String diskType, String snapshotLink, List<String> replicaZones)
67+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
68+
// Initialize client that will be used to send requests. This client only needs to be created
69+
// once, and can be reused for multiple requests.
70+
try (InstancesClient instancesClient = InstancesClient.create()) {
71+
AttachedDiskInitializeParams initializeParams = AttachedDiskInitializeParams.newBuilder()
72+
.setSourceSnapshot(snapshotLink)
73+
.setDiskType(diskType)
74+
.setDiskName(diskName)
75+
.addAllReplicaZones(replicaZones)
76+
.build();
77+
78+
// Boot disk configuration
79+
AttachedDisk bootDisk = AttachedDisk.newBuilder()
80+
.setBoot(true)
81+
.setAutoDelete(true) // Optional: Delete disk when instance is deleted.
82+
.setType(AttachedDisk.Type.PERSISTENT.toString())
83+
.setInitializeParams(initializeParams)
84+
.build();
85+
86+
// Network interface configuration (using the default network)
87+
NetworkInterface networkInterface = NetworkInterface.newBuilder()
88+
.setNetwork("global/networks/default")
89+
.build();
90+
91+
// Create the instance resource
92+
Instance instanceResource = Instance.newBuilder()
93+
.setName(instanceName)
94+
.setMachineType(String.format("zones/%s/machineTypes/n1-standard-1", zone))
95+
.addDisks(bootDisk)
96+
.addNetworkInterfaces(networkInterface)
97+
.build();
98+
99+
Operation response = instancesClient.insertAsync(projectId, zone, instanceResource).get(3,
100+
TimeUnit.MINUTES);
101+
102+
if (response.hasError()) {
103+
throw new Error("Error creating instance! " + response.getError());
104+
}
105+
return response.getStatus();
106+
}
107+
}
108+
}
109+
// [END compute_instance_create_replicated_boot_disk]
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2024 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 compute.disks;
18+
19+
// [START compute_disk_create_secondary_regional]
20+
import com.google.cloud.compute.v1.Disk;
21+
import com.google.cloud.compute.v1.DiskAsyncReplication;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.Operation.Status;
24+
import com.google.cloud.compute.v1.RegionDisksClient;
25+
import java.io.IOException;
26+
import java.util.Arrays;
27+
import java.util.List;
28+
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.TimeUnit;
30+
import java.util.concurrent.TimeoutException;
31+
32+
public class CreateDiskSecondaryRegional {
33+
public static void main(String[] args)
34+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
// The project that contains the primary disk.
37+
String primaryProjectId = "PRIMARY_PROJECT_ID";
38+
// The project that contains the secondary disk.
39+
String secondaryProjectId = "SECONDARY_PROJECT_ID";
40+
// Name of the primary disk you want to use.
41+
String primaryDiskName = "PRIMARY_DISK_NAME";
42+
// Name of the disk you want to create.
43+
String secondaryDiskName = "SECONDARY_DISK_NAME";
44+
// Name of the region in which your primary disk is located.
45+
// Learn more about zones and regions:
46+
// https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs
47+
String primaryDiskRegion = "us-central1";
48+
// Name of the region in which you want to create the secondary disk.
49+
String secondaryDiskRegion = "us-east1";
50+
// Size of the new disk in gigabytes.
51+
// Learn more about disk requirements:
52+
// https://cloud.google.com/compute/docs/disks/async-pd/configure?authuser=0#disk_requirements
53+
long diskSizeGb = 30L;
54+
// The type of the disk you want to create. This value uses the following format:
55+
// "projects/{projectId}/zones/{zone}/diskTypes/
56+
// (pd-standard|pd-ssd|pd-balanced|pd-extreme)".
57+
String diskType = String.format(
58+
"projects/%s/regions/%s/diskTypes/pd-balanced", secondaryProjectId, secondaryDiskRegion);
59+
60+
createDiskSecondaryRegional(primaryProjectId, secondaryProjectId, primaryDiskName,
61+
secondaryDiskName, primaryDiskRegion, secondaryDiskRegion, diskSizeGb, diskType);
62+
}
63+
64+
// Creates a secondary disk in a specified region.
65+
public static Status createDiskSecondaryRegional(String projectId,
66+
String secondaryProjectId, String primaryDiskName, String secondaryDiskName,
67+
String primaryDiskRegion, String secondaryDiskRegion, long diskSizeGb, String diskType)
68+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
69+
List<String> replicaZones = Arrays.asList(
70+
String.format("projects/%s/zones/%s-c", secondaryProjectId, secondaryDiskRegion),
71+
String.format("projects/%s/zones/%s-b", secondaryProjectId, secondaryDiskRegion));
72+
73+
String primaryDiskSource = String.format("projects/%s/regions/%s/disks/%s",
74+
projectId, primaryDiskRegion, primaryDiskName);
75+
76+
// Initialize client that will be used to send requests. This client only needs to be created
77+
// once, and can be reused for multiple requests.
78+
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
79+
DiskAsyncReplication asyncReplication = DiskAsyncReplication.newBuilder()
80+
.setDisk(primaryDiskSource)
81+
.build();
82+
83+
Disk disk = Disk.newBuilder()
84+
.addAllReplicaZones(replicaZones)
85+
.setName(secondaryDiskName)
86+
.setSizeGb(diskSizeGb)
87+
.setType(diskType)
88+
.setRegion(secondaryDiskRegion)
89+
.setAsyncPrimaryDisk(asyncReplication)
90+
.build();
91+
92+
// Wait for the create disk operation to complete.
93+
Operation response = disksClient.insertAsync(secondaryProjectId, secondaryDiskRegion, disk)
94+
.get(3, TimeUnit.MINUTES);
95+
96+
if (response.hasError()) {
97+
throw new Error("Error creating secondary disks! " + response.getError());
98+
}
99+
return response.getStatus();
100+
}
101+
}
102+
}
103+
// [END compute_disk_create_secondary_regional]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2024 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 compute.disks.consistencygroup;
18+
19+
// [START compute_consistency_group_regional_stop_replication]
20+
import com.google.cloud.compute.v1.DisksStopGroupAsyncReplicationResource;
21+
import com.google.cloud.compute.v1.Operation;
22+
import com.google.cloud.compute.v1.Operation.Status;
23+
import com.google.cloud.compute.v1.RegionDisksClient;
24+
import com.google.cloud.compute.v1.StopGroupAsyncReplicationRegionDiskRequest;
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 StopRegionalDiskReplicationConsistencyGroup {
31+
32+
public static void main(String[] args)
33+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
// Project ID or project number of the Cloud project that contains the disk.
36+
String project = "YOUR_PROJECT_ID";
37+
// Region of the disk.
38+
String region = "us-central1";
39+
// Name of the consistency group.
40+
String consistencyGroupName = "CONSISTENCY_GROUP";
41+
42+
stopRegionalDiskReplicationConsistencyGroup(project, region, consistencyGroupName);
43+
}
44+
45+
// Stops replication of a consistency group for a project in a given region.
46+
public static Status stopRegionalDiskReplicationConsistencyGroup(
47+
String project, String region, String consistencyGroupName)
48+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
49+
String resourcePolicy = String.format("projects/%s/regions/%s/resourcePolicies/%s",
50+
project, region, consistencyGroupName);
51+
// Initialize client that will be used to send requests. This client only needs to be created
52+
// once, and can be reused for multiple requests.
53+
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
54+
StopGroupAsyncReplicationRegionDiskRequest request =
55+
StopGroupAsyncReplicationRegionDiskRequest.newBuilder()
56+
.setProject(project)
57+
.setRegion(region)
58+
.setDisksStopGroupAsyncReplicationResourceResource(
59+
DisksStopGroupAsyncReplicationResource.newBuilder()
60+
.setResourcePolicy(resourcePolicy).build())
61+
.build();
62+
Operation response = disksClient.stopGroupAsyncReplicationAsync(request)
63+
.get(3, TimeUnit.MINUTES);
64+
65+
if (response.hasError()) {
66+
throw new Error("Error stopping disk replication! " + response.getError());
67+
}
68+
return response.getStatus();
69+
}
70+
}
71+
}
72+
// [END compute_consistency_group_regional_stop_replication]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2024 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 compute.disks.consistencygroup;
18+
19+
// [START compute_consistency_group_stop_replication]
20+
import com.google.cloud.compute.v1.DisksClient;
21+
import com.google.cloud.compute.v1.DisksStopGroupAsyncReplicationResource;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.Operation.Status;
24+
import com.google.cloud.compute.v1.StopGroupAsyncReplicationDiskRequest;
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 StopZonalDiskReplicationConsistencyGroup {
31+
public static void main(String[] args)
32+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
// Project ID or project number of the Cloud project that contains the disk.
35+
String project = "YOUR_PROJECT_ID";
36+
// Zone of the disk.
37+
String zone = "us-central1-a";
38+
// Name of the consistency group.
39+
String consistencyGroupName = "CONSISTENCY_GROUP";
40+
41+
stopZonalDiskReplicationConsistencyGroup(project, zone, consistencyGroupName);
42+
}
43+
44+
// Stops replication of a consistency group for a project in a given zone.
45+
public static Status stopZonalDiskReplicationConsistencyGroup(
46+
String project, String zone, String consistencyGroupName)
47+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
48+
String region = zone.substring(0, zone.lastIndexOf('-'));
49+
50+
String resourcePolicy = String.format("projects/%s/regions/%s/resourcePolicies/%s",
51+
project, region, consistencyGroupName);
52+
// Initialize client that will be used to send requests. This client only needs to be created
53+
// once, and can be reused for multiple requests.
54+
try (DisksClient disksClient = DisksClient.create()) {
55+
StopGroupAsyncReplicationDiskRequest request =
56+
StopGroupAsyncReplicationDiskRequest.newBuilder()
57+
.setProject(project)
58+
.setZone(zone)
59+
.setDisksStopGroupAsyncReplicationResourceResource(
60+
DisksStopGroupAsyncReplicationResource.newBuilder()
61+
.setResourcePolicy(resourcePolicy).build())
62+
.build();
63+
Operation response = disksClient.stopGroupAsyncReplicationAsync(request)
64+
.get(3, TimeUnit.MINUTES);
65+
66+
if (response.hasError()) {
67+
throw new Error("Error stopping disk replication! " + response.getError());
68+
}
69+
return response.getStatus();
70+
}
71+
}
72+
}
73+
// [END compute_consistency_group_stop_replication]

0 commit comments

Comments
 (0)