Skip to content

Commit

Permalink
Resolved merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
TetyanaYahodska committed Dec 23, 2024
2 parents 9b3b124 + 7afa0d2 commit 187c50b
Show file tree
Hide file tree
Showing 18 changed files with 1,225 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
/speech @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers
/talent @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers
/texttospeech @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers
/translate @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers
/translate @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/cloud-ml-translate-dev
/video @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers
/vision @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2024 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 compute;

// [START compute_instance_create_replicated_boot_disk]
import com.google.cloud.compute.v1.AttachedDisk;
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
import com.google.cloud.compute.v1.Instance;
import com.google.cloud.compute.v1.InstancesClient;
import com.google.cloud.compute.v1.NetworkInterface;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateInstanceWithRegionalDiskFromSnapshot {

public static void main(String[] args) throws IOException, ExecutionException,
InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample
// Project ID or project number of the Cloud project you want to use.
String projectId = "YOUR_PROJECT_ID";
// Name of the zone in which you want to create the instance.
String zone = "us-central1-a";
// Name of the instance you want to create.
String instanceName = "YOUR_INSTANCE_NAME";
// Name for the replicated disk.
String diskName = "YOUR_REPLICATED_DISK_NAME";
String region = zone.substring(0, zone.length() - 2);
// Type of the disk.
String diskType = String.format(
"projects/%s/regions/%s/diskTypes/pd-standard", projectId, region);
// The full path and name of the snapshot that you want to use as the source for the new disk.
String snapshotLink = String.format("projects/%s/global/snapshots/%s", projectId,
"SNAPSHOT_NAME");
// An iterable collection of zone names in which you want to keep
// the new disks' replicas. One of the replica zones of the clone must match
// the zone of the source disk.
List<String> replicaZones = new ArrayList<>();

createInstanceWithRegionalDiskFromSnapshot(projectId, zone, instanceName, diskName, diskType,
snapshotLink, replicaZones);
}

// Creates a new VM instance with regional disk from a snapshot and specifies replica zones.
public static Status createInstanceWithRegionalDiskFromSnapshot(
String projectId, String zone, String instanceName, String diskName,
String diskType, String snapshotLink, List<String> replicaZones)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (InstancesClient instancesClient = InstancesClient.create()) {
AttachedDiskInitializeParams initializeParams = AttachedDiskInitializeParams.newBuilder()
.setSourceSnapshot(snapshotLink)
.setDiskType(diskType)
.setDiskName(diskName)
.addAllReplicaZones(replicaZones)
.build();

// Boot disk configuration
AttachedDisk bootDisk = AttachedDisk.newBuilder()
.setBoot(true)
.setAutoDelete(true) // Optional: Delete disk when instance is deleted.
.setType(AttachedDisk.Type.PERSISTENT.toString())
.setInitializeParams(initializeParams)
.build();

// Network interface configuration (using the default network)
NetworkInterface networkInterface = NetworkInterface.newBuilder()
.setNetwork("global/networks/default")
.build();

// Create the instance resource
Instance instanceResource = Instance.newBuilder()
.setName(instanceName)
.setMachineType(String.format("zones/%s/machineTypes/n1-standard-1", zone))
.addDisks(bootDisk)
.addNetworkInterfaces(networkInterface)
.build();

Operation response = instancesClient.insertAsync(projectId, zone, instanceResource).get(3,
TimeUnit.MINUTES);

if (response.hasError()) {
throw new Error("Error creating instance! " + response.getError());
}
return response.getStatus();
}
}
}
// [END compute_instance_create_replicated_boot_disk]
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2024 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 compute.disks.consistencygroup;

// [START compute_consistency_group_regional_stop_replication]
import com.google.cloud.compute.v1.DisksStopGroupAsyncReplicationResource;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import com.google.cloud.compute.v1.RegionDisksClient;
import com.google.cloud.compute.v1.StopGroupAsyncReplicationRegionDiskRequest;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class StopRegionalDiskReplicationConsistencyGroup {

public static void main(String[] args)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project that contains the disk.
String project = "YOUR_PROJECT_ID";
// Region of the disk.
String region = "us-central1";
// Name of the consistency group.
String consistencyGroupName = "CONSISTENCY_GROUP";

stopRegionalDiskReplicationConsistencyGroup(project, region, consistencyGroupName);
}

// Stops replication of a consistency group for a project in a given region.
public static Status stopRegionalDiskReplicationConsistencyGroup(
String project, String region, String consistencyGroupName)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
String resourcePolicy = String.format("projects/%s/regions/%s/resourcePolicies/%s",
project, region, consistencyGroupName);
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
StopGroupAsyncReplicationRegionDiskRequest request =
StopGroupAsyncReplicationRegionDiskRequest.newBuilder()
.setProject(project)
.setRegion(region)
.setDisksStopGroupAsyncReplicationResourceResource(
DisksStopGroupAsyncReplicationResource.newBuilder()
.setResourcePolicy(resourcePolicy).build())
.build();
Operation response = disksClient.stopGroupAsyncReplicationAsync(request)
.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
throw new Error("Error stopping disk replication! " + response.getError());
}
return response.getStatus();
}
}
}
// [END compute_consistency_group_regional_stop_replication]
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2024 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 compute.disks.consistencygroup;

// [START compute_consistency_group_stop_replication]
import com.google.cloud.compute.v1.DisksClient;
import com.google.cloud.compute.v1.DisksStopGroupAsyncReplicationResource;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import com.google.cloud.compute.v1.StopGroupAsyncReplicationDiskRequest;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class StopZonalDiskReplicationConsistencyGroup {
public static void main(String[] args)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project that contains the disk.
String project = "YOUR_PROJECT_ID";
// Zone of the disk.
String zone = "us-central1-a";
// Name of the consistency group.
String consistencyGroupName = "CONSISTENCY_GROUP";

stopZonalDiskReplicationConsistencyGroup(project, zone, consistencyGroupName);
}

// Stops replication of a consistency group for a project in a given zone.
public static Status stopZonalDiskReplicationConsistencyGroup(
String project, String zone, String consistencyGroupName)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
String region = zone.substring(0, zone.lastIndexOf('-'));

String resourcePolicy = String.format("projects/%s/regions/%s/resourcePolicies/%s",
project, region, consistencyGroupName);
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (DisksClient disksClient = DisksClient.create()) {
StopGroupAsyncReplicationDiskRequest request =
StopGroupAsyncReplicationDiskRequest.newBuilder()
.setProject(project)
.setZone(zone)
.setDisksStopGroupAsyncReplicationResourceResource(
DisksStopGroupAsyncReplicationResource.newBuilder()
.setResourcePolicy(resourcePolicy).build())
.build();
Operation response = disksClient.stopGroupAsyncReplicationAsync(request)
.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
throw new Error("Error stopping disk replication! " + response.getError());
}
return response.getStatus();
}
}
}
// [END compute_consistency_group_stop_replication]
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

// [START compute_reservation_create_shared]
import com.google.cloud.compute.v1.AllocationSpecificSKUReservation;
import com.google.cloud.compute.v1.InsertReservationRequest;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import com.google.cloud.compute.v1.Reservation;
import com.google.cloud.compute.v1.ReservationsClient;
import com.google.cloud.compute.v1.ShareSettings;
Expand All @@ -29,12 +31,6 @@
import java.util.concurrent.TimeoutException;

public class CreateSharedReservation {
private final ReservationsClient reservationsClient;

// Constructor to inject the ReservationsClient
public CreateSharedReservation(ReservationsClient reservationsClient) {
this.reservationsClient = reservationsClient;
}

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
Expand All @@ -48,62 +44,66 @@ public static void main(String[] args)
// For more information visit this page:
// https://cloud.google.com/compute/docs/instances/reservations-shared#shared_reservation_constraint
String projectId = "YOUR_PROJECT_ID";
// Zone in which the reservation resides.
// Zone in which to reserve resources.
String zone = "us-central1-a";
// Name of the reservation to be created.
String reservationName = "YOUR_RESERVATION_NAME";
// The URI of the global instance template to be used for creating the reservation.
String instanceTemplateUri = String.format(
"projects/%s/global/instanceTemplates/YOUR_INSTANCE_TEMPLATE_NAME", projectId);
"projects/%s/global/instanceTemplates/%s", projectId, "YOUR_INSTANCE_TEMPLATE_NAME");
// Number of instances for which capacity needs to be reserved.
int vmCount = 3;
// In your main method, create ReservationsClient
ReservationsClient client = ReservationsClient.create();
// Create an instance of your class, passing in the client
CreateSharedReservation creator = new CreateSharedReservation(client);

creator.createSharedReservation(projectId, zone, reservationName, instanceTemplateUri, vmCount);
createSharedReservation(projectId, zone, reservationName, instanceTemplateUri, vmCount);
}

// Creates a shared reservation with the given name in the given zone.
public void createSharedReservation(
String projectId, String zone,
String reservationName, String instanceTemplateUri, int vmCount)
throws ExecutionException, InterruptedException, TimeoutException {
public static Status createSharedReservation(
String projectId, String zone,
String reservationName, String instanceTemplateUri, int vmCount)
throws ExecutionException, InterruptedException, TimeoutException, IOException {

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (ReservationsClient reservationsClient = ReservationsClient.create()) {
ShareSettings shareSettings = ShareSettings.newBuilder()
.setShareType(String.valueOf(ShareSettings.ShareType.SPECIFIC_PROJECTS))
// The IDs of projects that can consume this reservation. You can include up to
// 100 consumer projects. These projects must be in the same organization as
// the owner project. Don't include the owner project.
// By default, it is already allowed to consume the reservation.
.putProjectMap("CONSUMER_PROJECT_1", ShareSettingsProjectConfig.newBuilder().build())
.putProjectMap("CONSUMER_PROJECT_2", ShareSettingsProjectConfig.newBuilder().build())
.build();

ShareSettings shareSettings = ShareSettings.newBuilder()
.setShareType(String.valueOf(ShareSettings.ShareType.SPECIFIC_PROJECTS))
// The IDs of projects that can consume this reservation. You can include up to 100
// consumer projects. These projects must be in the same organization as
// the owner project. Don't include the owner project. By default, it is already allowed
// to consume the reservation.
.putProjectMap("CONSUMER_PROJECT_ID_1", ShareSettingsProjectConfig.newBuilder().build())
.putProjectMap("CONSUMER_PROJECT_ID_2", ShareSettingsProjectConfig.newBuilder().build())
.build();
Reservation reservationResource =
Reservation.newBuilder()
.setName(reservationName)
.setZone(zone)
.setSpecificReservationRequired(true)
.setShareSettings(shareSettings)
.setSpecificReservation(
AllocationSpecificSKUReservation.newBuilder()
.setCount(vmCount)
.setSourceInstanceTemplate(instanceTemplateUri)
.build())
.build();

// Create the reservation.
Reservation reservation =
Reservation.newBuilder()
.setName(reservationName)
.setZone(zone)
.setSpecificReservationRequired(true)
.setShareSettings(shareSettings)
.setSpecificReservation(
AllocationSpecificSKUReservation.newBuilder()
.setCount(vmCount)
.setSourceInstanceTemplate(instanceTemplateUri)
.build())
.build();
InsertReservationRequest request =
InsertReservationRequest.newBuilder()
.setProject(projectId)
.setZone(zone)
.setReservationResource(reservationResource)
.build();

// Wait for the create reservation operation to complete.
Operation response =
this.reservationsClient.insertAsync(projectId, zone, reservation).get(3, TimeUnit.MINUTES);
Operation response = reservationsClient.insertAsync(request)
.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
System.out.println("Reservation creation failed!" + response);
return;
if (response.hasError()) {
throw new Error("Reservation creation failed!!" + response);
}
return response.getStatus();
}
System.out.println("Reservation created. Operation Status: " + response.getStatus());
}
}
// [END compute_reservation_create_shared]
Loading

0 comments on commit 187c50b

Please sign in to comment.