From 77d892e4697b96caa6da6c80913436b6dff8dccf Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Wed, 27 Nov 2024 14:59:50 +0100 Subject: [PATCH 01/15] Implemented compute_snapshot_schedule_delete and compute_snapshot_schedule_create samples, created test --- .../snapshot/CreateSnapshotSchedule.java | 135 ++++++++++++++++++ .../snapshot/DeleteSnapshotSchedule.java | 51 +++++++ .../java/compute/snapshot/SnapshotIT.java | 86 +++++++++++ 3 files changed, 272 insertions(+) create mode 100644 compute/cloud-client/src/main/java/compute/snapshot/CreateSnapshotSchedule.java create mode 100644 compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java create mode 100644 compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java diff --git a/compute/cloud-client/src/main/java/compute/snapshot/CreateSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshot/CreateSnapshotSchedule.java new file mode 100644 index 00000000000..4627b3a95f7 --- /dev/null +++ b/compute/cloud-client/src/main/java/compute/snapshot/CreateSnapshotSchedule.java @@ -0,0 +1,135 @@ +/* + * 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.snapshot; + +// [START compute_snapshot_schedule_create] +import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.ResourcePoliciesClient; +import com.google.cloud.compute.v1.ResourcePolicy; +import com.google.cloud.compute.v1.ResourcePolicyHourlyCycle; +import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicy; +import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicyRetentionPolicy; +import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySchedule; +import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySnapshotProperties; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class CreateSnapshotSchedule { + 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 region in which you want to create the snapshot schedule. + String region = "us-central1"; + // Name of the snapshot schedule you want to create. + String scheduleName = "YOUR_SCHEDULE_NAME"; + // Description of the snapshot schedule. + String scheduleDescription = "YOUR_SCHEDULE_DESCRIPTION"; + // Maximum number of days to retain snapshots. + int maxRetentionDays = 10; + // Storage location for the snapshots. + // More about storage locations: + // https://cloud.google.com/compute/docs/disks/snapshots?authuser=0#selecting_a_storage_location + String storageLocation = "US"; + // Determines what happens to your snapshots if the source disk is deleted. + String onSourceDiskDelete = "KEEP_AUTO_SNAPSHOTS"; + + createSnapshotSchedule(projectId, region, scheduleName, scheduleDescription, maxRetentionDays, + storageLocation, onSourceDiskDelete); + } + + // Creates a snapshot schedule policy. + public static Operation.Status createSnapshotSchedule(String projectId, String region, + String scheduleName, String scheduleDescription, int maxRetentionDays, + String storageLocation, String onSourceDiskDelete) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + String startTime = "08:00"; + ResourcePolicySnapshotSchedulePolicySnapshotProperties snapshotProperties = + ResourcePolicySnapshotSchedulePolicySnapshotProperties.newBuilder() + .addStorageLocations(storageLocation) + .build(); + + // Define the hourly schedule: + int snapshotInterval = 10; // Create a snapshot every 10 hours + ResourcePolicyHourlyCycle hourlyCycle = ResourcePolicyHourlyCycle.newBuilder() + .setHoursInCycle(snapshotInterval) + .setStartTime(startTime) + .build(); + + // Define the daily schedule. + // ResourcePolicyDailyCycle dailySchedule = + // ResourcePolicyDailyCycle.newBuilder() + // .setDaysInCycle(1) // Every day + // .setStartTime(startTime) + // .build(); + + // Define the weekly schedule. + // List dayOfWeeks = new ArrayList<>(); + // ResourcePolicyWeeklyCycleDayOfWeek tuesdaySchedule = + // ResourcePolicyWeeklyCycleDayOfWeek.newBuilder() + // .setDay(ResourcePolicyWeeklyCycleDayOfWeek.Day.TUESDAY.toString()) + // .setStartTime(startTime) + // .build(); + // dayOfWeeks.add(tuesdaySchedule); + // + // ResourcePolicyWeeklyCycle weeklyCycle = ResourcePolicyWeeklyCycle.newBuilder() + // .addAllDayOfWeeks(dayOfWeeks) + // .build(); + + ResourcePolicySnapshotSchedulePolicyRetentionPolicy retentionPolicy = + ResourcePolicySnapshotSchedulePolicyRetentionPolicy.newBuilder() + .setMaxRetentionDays(maxRetentionDays) + .setOnSourceDiskDelete(onSourceDiskDelete) + .build(); + + ResourcePolicySnapshotSchedulePolicy snapshotSchedulePolicy = + ResourcePolicySnapshotSchedulePolicy.newBuilder() + .setRetentionPolicy(retentionPolicy) + .setSchedule(ResourcePolicySnapshotSchedulePolicySchedule.newBuilder() + // You can set only one of the following options: + .setHourlySchedule(hourlyCycle) //Set Hourly Schedule + // .setDailySchedule(dailySchedule) //Set Daily Schedule + // .setWeeklySchedule(weeklyCycle) // Set Weekly Schedule + .build()) + .setSnapshotProperties(snapshotProperties) + .build(); + + ResourcePolicy resourcePolicy = ResourcePolicy.newBuilder() + .setName(scheduleName) + .setDescription(scheduleDescription) + .setSnapshotSchedulePolicy(snapshotSchedulePolicy) + .build(); + + // 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 (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { + + Operation response = resourcePoliciesClient.insertAsync(projectId, region, resourcePolicy) + .get(3, TimeUnit.MINUTES); + + if (response.hasError()) { + System.out.printf("Snapshot schedule creation failed: %s%n", response.getError()); + return null; + } + return response.getStatus(); + } + } +} +// [END compute_snapshot_schedule_create] \ No newline at end of file diff --git a/compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java new file mode 100644 index 00000000000..242094209b4 --- /dev/null +++ b/compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java @@ -0,0 +1,51 @@ +/* + * 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.snapshot; + +// [START compute_snapshot_schedule_delete] +import com.google.cloud.compute.v1.ResourcePoliciesClient; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class DeleteSnapshotSchedule { + 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 region where your snapshot schedule is located. + String region = "us-central1"; + // Name of the snapshot schedule you want to delete. + String scheduleName = "YOUR_SCHEDULE_NAME"; + + deleteSnapshotSchedule(projectId, region, scheduleName); + } + + // Deletes a snapshot schedule policy. + public static void deleteSnapshotSchedule(String projectId, String region, String scheduleName) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + + try (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { + resourcePoliciesClient.deleteAsync(projectId, region, scheduleName).get(3, TimeUnit.MINUTES); + + System.out.println("Snapshot schedule deleted successfully: " + scheduleName); + } + } +} +// [END compute_snapshot_schedule_delete] \ No newline at end of file diff --git a/compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java b/compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java new file mode 100644 index 00000000000..c5f5ab12ac9 --- /dev/null +++ b/compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java @@ -0,0 +1,86 @@ +/* + * 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.snapshot; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; + +import com.google.cloud.compute.v1.Operation; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@Timeout(value = 6, unit = TimeUnit.MINUTES) +public class SnapshotIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String ZONE = "asia-south1-a"; + private static final String REGION = ZONE.substring(0, ZONE.lastIndexOf('-')); + static String templateUUID = UUID.randomUUID().toString(); + private static final String SCHEDULE_NAME = "test-schedule-" + templateUUID; + private static final String SCHEDULE_DESCRIPTION = "Test hourly snapshot schedule"; + private static final int MAX_RETENTION_DAYS = 2; + private static final String STORAGE_LOCATION = "US"; + private static final String ON_SOURCE_DISK_DELETE = "KEEP_AUTO_SNAPSHOTS"; + + // Check if the required environment variables are set. + public static void requireEnvVar(String envVarName) { + assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) + .that(System.getenv(envVarName)).isNotEmpty(); + } + + @BeforeAll + public static void setUp() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @AfterAll + public static void cleanup() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + + // Delete snapshot schedule created for testing. + DeleteSnapshotSchedule.deleteSnapshotSchedule(PROJECT_ID, REGION, SCHEDULE_NAME); + + assertThat(stdOut.toString()) + .contains("Snapshot schedule deleted successfully: " + SCHEDULE_NAME); + + stdOut.close(); + } + + @Test + public void testCreateSnapshotScheduleHourly() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + Operation.Status status = CreateSnapshotSchedule.createSnapshotSchedule( + PROJECT_ID, REGION, SCHEDULE_NAME, SCHEDULE_DESCRIPTION, + MAX_RETENTION_DAYS, STORAGE_LOCATION, ON_SOURCE_DISK_DELETE); + assertThat(status).isEqualTo(Operation.Status.DONE); + } +} From 9520be38080d068ae3ace8c93270496afa94daf6 Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Sun, 1 Dec 2024 17:16:43 +0100 Subject: [PATCH 02/15] Fixed test --- .../compute/snapshot/DeleteSnapshotSchedule.java | 16 ++++++++++++---- .../test/java/compute/snapshot/SnapshotIT.java | 14 ++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java index 242094209b4..db7cd2bde0f 100644 --- a/compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java +++ b/compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java @@ -17,6 +17,7 @@ package compute.snapshot; // [START compute_snapshot_schedule_delete] +import com.google.cloud.compute.v1.Operation; import com.google.cloud.compute.v1.ResourcePoliciesClient; import java.io.IOException; import java.util.concurrent.ExecutionException; @@ -38,13 +39,20 @@ public static void main(String[] args) } // Deletes a snapshot schedule policy. - public static void deleteSnapshotSchedule(String projectId, String region, String scheduleName) + public static Operation.Status deleteSnapshotSchedule( + String projectId, String region, String scheduleName) 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 (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { - resourcePoliciesClient.deleteAsync(projectId, region, scheduleName).get(3, TimeUnit.MINUTES); + Operation response = resourcePoliciesClient.deleteAsync(projectId, region, scheduleName) + .get(3, TimeUnit.MINUTES); - System.out.println("Snapshot schedule deleted successfully: " + scheduleName); + if (response.hasError()) { + System.out.printf("Snapshot schedule deletion failed: %s%n", response.getError()); + return null; + } + return response.getStatus(); } } } diff --git a/compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java b/compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java index c5f5ab12ac9..f4298f17ab0 100644 --- a/compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java +++ b/compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java @@ -20,9 +20,7 @@ import static com.google.common.truth.Truth.assertWithMessage; import com.google.cloud.compute.v1.Operation; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.PrintStream; import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -63,16 +61,11 @@ public static void setUp() @AfterAll public static void cleanup() throws IOException, ExecutionException, InterruptedException, TimeoutException { - ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); - System.setOut(new PrintStream(stdOut)); - // Delete snapshot schedule created for testing. - DeleteSnapshotSchedule.deleteSnapshotSchedule(PROJECT_ID, REGION, SCHEDULE_NAME); - - assertThat(stdOut.toString()) - .contains("Snapshot schedule deleted successfully: " + SCHEDULE_NAME); + Operation.Status status = DeleteSnapshotSchedule + .deleteSnapshotSchedule(PROJECT_ID, REGION, SCHEDULE_NAME); - stdOut.close(); + assertThat(status).isEqualTo(Operation.Status.DONE); } @Test @@ -81,6 +74,7 @@ public void testCreateSnapshotScheduleHourly() Operation.Status status = CreateSnapshotSchedule.createSnapshotSchedule( PROJECT_ID, REGION, SCHEDULE_NAME, SCHEDULE_DESCRIPTION, MAX_RETENTION_DAYS, STORAGE_LOCATION, ON_SOURCE_DISK_DELETE); + assertThat(status).isEqualTo(Operation.Status.DONE); } } From 361f09fb86a05fe48a911d9bc8417a47a96314a3 Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Mon, 2 Dec 2024 11:01:53 +0100 Subject: [PATCH 03/15] Added compute_snapshot_schedule_get sample, created test --- .../compute/snapshot/GetSnapshotSchedule.java | 54 +++++++++++++++++++ .../java/compute/snapshot/SnapshotIT.java | 36 +++++++++---- 2 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 compute/cloud-client/src/main/java/compute/snapshot/GetSnapshotSchedule.java diff --git a/compute/cloud-client/src/main/java/compute/snapshot/GetSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshot/GetSnapshotSchedule.java new file mode 100644 index 00000000000..9fac68f07b6 --- /dev/null +++ b/compute/cloud-client/src/main/java/compute/snapshot/GetSnapshotSchedule.java @@ -0,0 +1,54 @@ +/* + * 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.snapshot; + +// [START compute_snapshot_schedule_get] +import com.google.cloud.compute.v1.GetResourcePolicyRequest; +import com.google.cloud.compute.v1.ResourcePoliciesClient; +import com.google.cloud.compute.v1.ResourcePolicy; +import java.io.IOException; + +public class GetSnapshotSchedule { + + public static void main(String[] args) throws IOException { + // 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 region in which your snapshot schedule is located. + String region = "us-central1"; + // Name of your snapshot schedule. + String scheduleName = "YOUR_SCHEDULE_NAME"; + + getSnapshotSchedule(projectId, region, scheduleName); + } + + // Retrieves the details of a snapshot schedule. + public static ResourcePolicy getSnapshotSchedule( + String projectId, String region, String scheduleName) throws 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 (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { + GetResourcePolicyRequest request = GetResourcePolicyRequest.newBuilder() + .setProject(projectId) + .setRegion(region) + .setResourcePolicy(scheduleName) + .build(); + return resourcePoliciesClient.get(request); + } + } +} +// [END compute_snapshot_schedule_get] diff --git a/compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java b/compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java index f4298f17ab0..f514b9996b3 100644 --- a/compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java +++ b/compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java @@ -18,22 +18,27 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; +import static org.junit.jupiter.api.Assertions.assertNotNull; import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.ResourcePolicy; import java.io.IOException; import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) @Timeout(value = 6, unit = TimeUnit.MINUTES) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class SnapshotIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String ZONE = "asia-south1-a"; @@ -58,22 +63,33 @@ public static void setUp() requireEnvVar("GOOGLE_CLOUD_PROJECT"); } - @AfterAll - public static void cleanup() + @Test + @Order(1) + public void testCreateSnapshotScheduleHourly() throws IOException, ExecutionException, InterruptedException, TimeoutException { - // Delete snapshot schedule created for testing. - Operation.Status status = DeleteSnapshotSchedule - .deleteSnapshotSchedule(PROJECT_ID, REGION, SCHEDULE_NAME); + Operation.Status status = CreateSnapshotSchedule.createSnapshotSchedule( + PROJECT_ID, REGION, SCHEDULE_NAME, SCHEDULE_DESCRIPTION, + MAX_RETENTION_DAYS, STORAGE_LOCATION, ON_SOURCE_DISK_DELETE); assertThat(status).isEqualTo(Operation.Status.DONE); } @Test - public void testCreateSnapshotScheduleHourly() + @Order(2) + public void testGetSnapshotSchedule() throws IOException { + + ResourcePolicy resourcePolicy = GetSnapshotSchedule.getSnapshotSchedule( + PROJECT_ID, REGION, SCHEDULE_NAME); + assertNotNull(resourcePolicy); + assertThat(resourcePolicy.getName()).isEqualTo(SCHEDULE_NAME); + } + + @Test + @Order(3) + public void testDeleteSnapshotSchedule() throws IOException, ExecutionException, InterruptedException, TimeoutException { - Operation.Status status = CreateSnapshotSchedule.createSnapshotSchedule( - PROJECT_ID, REGION, SCHEDULE_NAME, SCHEDULE_DESCRIPTION, - MAX_RETENTION_DAYS, STORAGE_LOCATION, ON_SOURCE_DISK_DELETE); + Operation.Status status = DeleteSnapshotSchedule + .deleteSnapshotSchedule(PROJECT_ID, REGION, SCHEDULE_NAME); assertThat(status).isEqualTo(Operation.Status.DONE); } From 7df8f9dbbbd3808ce67cfbbb954193adb393a696 Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Mon, 2 Dec 2024 11:04:21 +0100 Subject: [PATCH 04/15] Fixed naming --- .../main/java/compute/snapshot/GetSnapshotSchedule.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/compute/cloud-client/src/main/java/compute/snapshot/GetSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshot/GetSnapshotSchedule.java index 9fac68f07b6..54ef69b7369 100644 --- a/compute/cloud-client/src/main/java/compute/snapshot/GetSnapshotSchedule.java +++ b/compute/cloud-client/src/main/java/compute/snapshot/GetSnapshotSchedule.java @@ -31,22 +31,23 @@ public static void main(String[] args) throws IOException { // Name of the region in which your snapshot schedule is located. String region = "us-central1"; // Name of your snapshot schedule. - String scheduleName = "YOUR_SCHEDULE_NAME"; + String snapshotScheduleName = "YOUR_SCHEDULE_NAME"; - getSnapshotSchedule(projectId, region, scheduleName); + getSnapshotSchedule(projectId, region, snapshotScheduleName); } // Retrieves the details of a snapshot schedule. public static ResourcePolicy getSnapshotSchedule( - String projectId, String region, String scheduleName) throws IOException { + String projectId, String region, String snapshotScheduleName) throws 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 (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { GetResourcePolicyRequest request = GetResourcePolicyRequest.newBuilder() .setProject(projectId) .setRegion(region) - .setResourcePolicy(scheduleName) + .setResourcePolicy(snapshotScheduleName) .build(); + return resourcePoliciesClient.get(request); } } From 4f97e7eca9ad86ad2577067514cb5a118c098b7b Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Wed, 4 Dec 2024 14:35:39 +0100 Subject: [PATCH 05/15] Implemented compute_snapshot_schedule_edit, created test --- .../CreateSnapshotSchedule.java | 5 +- .../DeleteSnapshotSchedule.java | 5 +- .../EditSnapshotSchedule.java | 113 ++++++++++++++++++ .../GetSnapshotSchedule.java | 2 +- .../SnapshotScheduleIT.java} | 19 ++- 5 files changed, 132 insertions(+), 12 deletions(-) rename compute/cloud-client/src/main/java/compute/{snapshot => snapshotschedule}/CreateSnapshotSchedule.java (97%) rename compute/cloud-client/src/main/java/compute/{snapshot => snapshotschedule}/DeleteSnapshotSchedule.java (94%) create mode 100644 compute/cloud-client/src/main/java/compute/snapshotschedule/EditSnapshotSchedule.java rename compute/cloud-client/src/main/java/compute/{snapshot => snapshotschedule}/GetSnapshotSchedule.java (98%) rename compute/cloud-client/src/test/java/compute/{snapshot/SnapshotIT.java => snapshotschedule/SnapshotScheduleIT.java} (89%) diff --git a/compute/cloud-client/src/main/java/compute/snapshot/CreateSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java similarity index 97% rename from compute/cloud-client/src/main/java/compute/snapshot/CreateSnapshotSchedule.java rename to compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java index 4627b3a95f7..0c3ab3dd02a 100644 --- a/compute/cloud-client/src/main/java/compute/snapshot/CreateSnapshotSchedule.java +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package compute.snapshot; +package compute.snapshotschedule; // [START compute_snapshot_schedule_create] import com.google.cloud.compute.v1.Operation; @@ -125,8 +125,7 @@ public static Operation.Status createSnapshotSchedule(String projectId, String r .get(3, TimeUnit.MINUTES); if (response.hasError()) { - System.out.printf("Snapshot schedule creation failed: %s%n", response.getError()); - return null; + throw new Error("Snapshot schedule creation failed! " + response.getError()); } return response.getStatus(); } diff --git a/compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java similarity index 94% rename from compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java rename to compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java index db7cd2bde0f..9c767a28abd 100644 --- a/compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package compute.snapshot; +package compute.snapshotschedule; // [START compute_snapshot_schedule_delete] import com.google.cloud.compute.v1.Operation; @@ -49,8 +49,7 @@ public static Operation.Status deleteSnapshotSchedule( .get(3, TimeUnit.MINUTES); if (response.hasError()) { - System.out.printf("Snapshot schedule deletion failed: %s%n", response.getError()); - return null; + throw new Error("Snapshot schedule deletion failed! " + response.getError()); } return response.getStatus(); } diff --git a/compute/cloud-client/src/main/java/compute/snapshotschedule/EditSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/EditSnapshotSchedule.java new file mode 100644 index 00000000000..1755c4bcef7 --- /dev/null +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/EditSnapshotSchedule.java @@ -0,0 +1,113 @@ +/* + * 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.snapshotschedule; + +// [START compute_snapshot_schedule_edit] +import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.PatchResourcePolicyRequest; +import com.google.cloud.compute.v1.ResourcePoliciesClient; +import com.google.cloud.compute.v1.ResourcePolicy; +import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicyRetentionPolicy; +import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySchedule; +import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySnapshotProperties; +import com.google.cloud.compute.v1.ResourcePolicyWeeklyCycle; +import com.google.cloud.compute.v1.ResourcePolicyWeeklyCycleDayOfWeek; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class EditSnapshotSchedule { + + public static void main(String[] args) throws Exception { + // 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 region where your snapshot schedule is located. + String region = "us-central1"; + // Name of the snapshot schedule you want to update. + String snapshotScheduleName = "YOUR_SCHEDULE_NAME"; + + editSnapshotSchedule(projectId, region, snapshotScheduleName); + } + + public static Operation.Status editSnapshotSchedule( + String projectId, String region, String snapshotScheduleName) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + String description = "Updated description11"; + Map snapshotLabels = new HashMap<>(); + snapshotLabels.put("key", "value"); + ResourcePolicyWeeklyCycleDayOfWeek dayOfWeek = ResourcePolicyWeeklyCycleDayOfWeek.newBuilder() + .setDay("Tuesday") + .setStartTime("09:00") + .build(); + ResourcePolicyWeeklyCycle weeklySchedule = ResourcePolicyWeeklyCycle.newBuilder() + .addDayOfWeeks(dayOfWeek) + .build(); + String onSourceDiskDelete = "apply-retention-policy"; + int maxRetentionDays = 3; + + // 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 (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { + ResourcePolicy existingSchedule = resourcePoliciesClient + .get(projectId, region, snapshotScheduleName); + + ResourcePolicySnapshotSchedulePolicySnapshotProperties.Builder snapshotProperties = + existingSchedule.getSnapshotSchedulePolicy().getSnapshotProperties().toBuilder(); + snapshotProperties.putAllLabels(snapshotLabels); + + ResourcePolicySnapshotSchedulePolicySchedule.Builder scheduler = + existingSchedule.getSnapshotSchedulePolicy().getSchedule().toBuilder(); + scheduler.clearDailySchedule().clearHourlySchedule(); + scheduler.setWeeklySchedule(weeklySchedule); + + ResourcePolicySnapshotSchedulePolicyRetentionPolicy.Builder retentionPolicy = + existingSchedule.getSnapshotSchedulePolicy().getRetentionPolicy().toBuilder(); + retentionPolicy.setOnSourceDiskDelete(onSourceDiskDelete); + retentionPolicy.setMaxRetentionDays(maxRetentionDays); + + ResourcePolicy updatedSchedule = ResourcePolicy.newBuilder() + .setName(existingSchedule.getName()) + .setDescription(description) + .setSnapshotSchedulePolicy( + existingSchedule.getSnapshotSchedulePolicy().toBuilder() + .setSchedule(scheduler) + .setSnapshotProperties(snapshotProperties) + .setRetentionPolicy(retentionPolicy.build()) + .build()) + .build(); + + PatchResourcePolicyRequest request = PatchResourcePolicyRequest.newBuilder() + .setProject(projectId) + .setRegion(region) + .setResourcePolicy(snapshotScheduleName) + .setResourcePolicyResource(updatedSchedule) + .build(); + + Operation response = resourcePoliciesClient.patchAsync(request).get(3, TimeUnit.MINUTES); + + if (response.hasError()) { + throw new Error("Failed to update snapshot schedule! " + response.getError()); + } + return response.getStatus(); + } + } +} +// [END compute_snapshot_schedule_edit] \ No newline at end of file diff --git a/compute/cloud-client/src/main/java/compute/snapshot/GetSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/GetSnapshotSchedule.java similarity index 98% rename from compute/cloud-client/src/main/java/compute/snapshot/GetSnapshotSchedule.java rename to compute/cloud-client/src/main/java/compute/snapshotschedule/GetSnapshotSchedule.java index 54ef69b7369..5e526193535 100644 --- a/compute/cloud-client/src/main/java/compute/snapshot/GetSnapshotSchedule.java +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/GetSnapshotSchedule.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package compute.snapshot; +package compute.snapshotschedule; // [START compute_snapshot_schedule_get] import com.google.cloud.compute.v1.GetResourcePolicyRequest; diff --git a/compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java similarity index 89% rename from compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java rename to compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java index f514b9996b3..3c698f38702 100644 --- a/compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java +++ b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package compute.snapshot; +package compute.snapshotschedule; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; @@ -39,12 +39,11 @@ @RunWith(JUnit4.class) @Timeout(value = 6, unit = TimeUnit.MINUTES) @TestMethodOrder(MethodOrderer.OrderAnnotation.class) -public class SnapshotIT { +public class SnapshotScheduleIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String ZONE = "asia-south1-a"; private static final String REGION = ZONE.substring(0, ZONE.lastIndexOf('-')); - static String templateUUID = UUID.randomUUID().toString(); - private static final String SCHEDULE_NAME = "test-schedule-" + templateUUID; + private static final String SCHEDULE_NAME = "test-schedule-" + UUID.randomUUID(); private static final String SCHEDULE_DESCRIPTION = "Test hourly snapshot schedule"; private static final int MAX_RETENTION_DAYS = 2; private static final String STORAGE_LOCATION = "US"; @@ -77,13 +76,23 @@ public void testCreateSnapshotScheduleHourly() @Test @Order(2) public void testGetSnapshotSchedule() throws IOException { - ResourcePolicy resourcePolicy = GetSnapshotSchedule.getSnapshotSchedule( PROJECT_ID, REGION, SCHEDULE_NAME); + assertNotNull(resourcePolicy); assertThat(resourcePolicy.getName()).isEqualTo(SCHEDULE_NAME); } + @Test + @Order(2) + public void testEditSnapshotSchedule() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + Operation.Status status = EditSnapshotSchedule.editSnapshotSchedule( + PROJECT_ID, REGION, SCHEDULE_NAME); + + assertThat(status).isEqualTo(Operation.Status.DONE); + } + @Test @Order(3) public void testDeleteSnapshotSchedule() From 569c77acf0282886b4e9832fbacda35b6d2031ba Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Wed, 4 Dec 2024 15:23:18 +0100 Subject: [PATCH 06/15] Fixed naming --- .../snapshotschedule/CreateSnapshotSchedule.java | 10 +++++----- .../snapshotschedule/DeleteSnapshotSchedule.java | 9 +++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java index 0c3ab3dd02a..25089f09de9 100644 --- a/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java @@ -39,7 +39,7 @@ public static void main(String[] args) // Name of the region in which you want to create the snapshot schedule. String region = "us-central1"; // Name of the snapshot schedule you want to create. - String scheduleName = "YOUR_SCHEDULE_NAME"; + String snapshotScheduleName = "YOUR_SCHEDULE_NAME"; // Description of the snapshot schedule. String scheduleDescription = "YOUR_SCHEDULE_DESCRIPTION"; // Maximum number of days to retain snapshots. @@ -51,13 +51,13 @@ public static void main(String[] args) // Determines what happens to your snapshots if the source disk is deleted. String onSourceDiskDelete = "KEEP_AUTO_SNAPSHOTS"; - createSnapshotSchedule(projectId, region, scheduleName, scheduleDescription, maxRetentionDays, - storageLocation, onSourceDiskDelete); + createSnapshotSchedule(projectId, region, snapshotScheduleName, scheduleDescription, + maxRetentionDays, storageLocation, onSourceDiskDelete); } // Creates a snapshot schedule policy. public static Operation.Status createSnapshotSchedule(String projectId, String region, - String scheduleName, String scheduleDescription, int maxRetentionDays, + String snapshotScheduleName, String scheduleDescription, int maxRetentionDays, String storageLocation, String onSourceDiskDelete) throws IOException, ExecutionException, InterruptedException, TimeoutException { String startTime = "08:00"; @@ -112,7 +112,7 @@ public static Operation.Status createSnapshotSchedule(String projectId, String r .build(); ResourcePolicy resourcePolicy = ResourcePolicy.newBuilder() - .setName(scheduleName) + .setName(snapshotScheduleName) .setDescription(scheduleDescription) .setSnapshotSchedulePolicy(snapshotSchedulePolicy) .build(); diff --git a/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java index 9c767a28abd..1310a65a9b4 100644 --- a/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java @@ -33,19 +33,20 @@ public static void main(String[] args) // Name of the region where your snapshot schedule is located. String region = "us-central1"; // Name of the snapshot schedule you want to delete. - String scheduleName = "YOUR_SCHEDULE_NAME"; + String snapshotScheduleName = "YOUR_SCHEDULE_NAME"; - deleteSnapshotSchedule(projectId, region, scheduleName); + deleteSnapshotSchedule(projectId, region, snapshotScheduleName); } // Deletes a snapshot schedule policy. public static Operation.Status deleteSnapshotSchedule( - String projectId, String region, String scheduleName) + String projectId, String region, String snapshotScheduleName) 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 (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { - Operation response = resourcePoliciesClient.deleteAsync(projectId, region, scheduleName) + Operation response = resourcePoliciesClient + .deleteAsync(projectId, region, snapshotScheduleName) .get(3, TimeUnit.MINUTES); if (response.hasError()) { From 8f5fd1661d69b43d93bcb8c9a54dbdf4895e3bfd Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Fri, 6 Dec 2024 13:37:36 +0100 Subject: [PATCH 07/15] Implemented compute_snapshot_schedule_list sample, created test --- .../ListSnapshotSchedules.java | 63 +++++++++++++++++++ .../snapshotschedule/SnapshotScheduleIT.java | 14 ++++- 2 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 compute/cloud-client/src/main/java/compute/snapshotschedule/ListSnapshotSchedules.java diff --git a/compute/cloud-client/src/main/java/compute/snapshotschedule/ListSnapshotSchedules.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/ListSnapshotSchedules.java new file mode 100644 index 00000000000..60b64ad8fc4 --- /dev/null +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/ListSnapshotSchedules.java @@ -0,0 +1,63 @@ +/* + * 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.snapshotschedule; + +// [START compute_snapshot_schedule_list] +import com.google.cloud.compute.v1.ListResourcePoliciesRequest; +import com.google.cloud.compute.v1.ResourcePoliciesClient; +import com.google.cloud.compute.v1.ResourcePolicy; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class ListSnapshotSchedules { + + public static void main(String[] args) throws IOException { + // 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 region you want to list snapshot schedules from. + String region = "us-central1"; + + listSnapshotSchedules(projectId, region); + } + + // Lists snapshot schedules in a specified region, optionally filtered. + public static List listSnapshotSchedules( + String projectId, String region) throws IOException { + String filter = String.format( + "https://www.googleapis.com/compute/v1/projects/%s/regions/%s", projectId, region); + List list = new ArrayList<>(); + // 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 (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { + + ListResourcePoliciesRequest request = ListResourcePoliciesRequest.newBuilder() + .setProject(projectId) + .setRegion(region) + .build(); + + for (ResourcePolicy resourcePolicy : resourcePoliciesClient.list(request).iterateAll()) { + if (resourcePolicy.getRegion().equals(filter)) { + list.add(resourcePolicy); + } + } + return list; + } + } +} +// [END compute_snapshot_schedule_list] \ No newline at end of file diff --git a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java index 3c698f38702..cb0646c08cd 100644 --- a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java +++ b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java @@ -23,6 +23,7 @@ import com.google.cloud.compute.v1.Operation; import com.google.cloud.compute.v1.ResourcePolicy; import java.io.IOException; +import java.util.List; import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -41,8 +42,7 @@ @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class SnapshotScheduleIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String ZONE = "asia-south1-a"; - private static final String REGION = ZONE.substring(0, ZONE.lastIndexOf('-')); + private static final String REGION = "us-central1"; private static final String SCHEDULE_NAME = "test-schedule-" + UUID.randomUUID(); private static final String SCHEDULE_DESCRIPTION = "Test hourly snapshot schedule"; private static final int MAX_RETENTION_DAYS = 2; @@ -93,6 +93,16 @@ public void testEditSnapshotSchedule() assertThat(status).isEqualTo(Operation.Status.DONE); } + @Test + @Order(2) + public void testListSnapshotSchedules() throws IOException { + List list = ListSnapshotSchedules.listSnapshotSchedules( + PROJECT_ID, REGION); + + assertThat(list.size()).isEqualTo(1); + assertThat(list.get(0).getName()).isEqualTo(SCHEDULE_NAME); + } + @Test @Order(3) public void testDeleteSnapshotSchedule() From 91669abcfd38bddf348c70653a72cb49baab9afd Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Fri, 6 Dec 2024 14:19:34 +0100 Subject: [PATCH 08/15] Cleaned resources --- .../java/compute/snapshotschedule/SnapshotScheduleIT.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java index cb0646c08cd..36e86c701b2 100644 --- a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java +++ b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java @@ -60,6 +60,7 @@ public static void setUp() throws IOException, ExecutionException, InterruptedException, TimeoutException { requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } @Test @@ -95,12 +96,15 @@ public void testEditSnapshotSchedule() @Test @Order(2) - public void testListSnapshotSchedules() throws IOException { + public void testListSnapshotSchedules() throws IOException, ExecutionException, InterruptedException, TimeoutException { List list = ListSnapshotSchedules.listSnapshotSchedules( PROJECT_ID, REGION); assertThat(list.size()).isEqualTo(1); assertThat(list.get(0).getName()).isEqualTo(SCHEDULE_NAME); + for ( ResourcePolicy item: list) { + DeleteSnapshotSchedule.deleteSnapshotSchedule(PROJECT_ID, REGION, item.getName()); + } } @Test From 84fc30726467ca97626f8896bca2a123b36db927 Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Fri, 6 Dec 2024 14:20:54 +0100 Subject: [PATCH 09/15] Cleaned resources --- .../java/compute/snapshotschedule/SnapshotScheduleIT.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java index 36e86c701b2..2f980f5a7e1 100644 --- a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java +++ b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java @@ -96,14 +96,15 @@ public void testEditSnapshotSchedule() @Test @Order(2) - public void testListSnapshotSchedules() throws IOException, ExecutionException, InterruptedException, TimeoutException { + public void testListSnapshotSchedules() + throws IOException, ExecutionException, InterruptedException, TimeoutException { List list = ListSnapshotSchedules.listSnapshotSchedules( PROJECT_ID, REGION); assertThat(list.size()).isEqualTo(1); assertThat(list.get(0).getName()).isEqualTo(SCHEDULE_NAME); - for ( ResourcePolicy item: list) { - DeleteSnapshotSchedule.deleteSnapshotSchedule(PROJECT_ID, REGION, item.getName()); + for (ResourcePolicy item : list) { + DeleteSnapshotSchedule.deleteSnapshotSchedule(PROJECT_ID, REGION, item.getName()); } } From aee7e526469be2c16bb14d7c2ea95c2288fb5b9b Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Fri, 6 Dec 2024 14:23:19 +0100 Subject: [PATCH 10/15] Cleaned resources --- .../snapshotschedule/SnapshotScheduleIT.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java index 2f980f5a7e1..71ced08c2f5 100644 --- a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java +++ b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java @@ -29,6 +29,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; @@ -60,9 +61,14 @@ public static void setUp() throws IOException, ExecutionException, InterruptedException, TimeoutException { requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("GOOGLE_CLOUD_PROJECT"); - + List list = ListSnapshotSchedules.listSnapshotSchedules( + PROJECT_ID, REGION); + for (ResourcePolicy item : list) { + DeleteSnapshotSchedule.deleteSnapshotSchedule(PROJECT_ID, REGION, item.getName()); + } } + @Disabled @Test @Order(1) public void testCreateSnapshotScheduleHourly() @@ -74,6 +80,7 @@ public void testCreateSnapshotScheduleHourly() assertThat(status).isEqualTo(Operation.Status.DONE); } + @Disabled @Test @Order(2) public void testGetSnapshotSchedule() throws IOException { @@ -84,6 +91,7 @@ public void testGetSnapshotSchedule() throws IOException { assertThat(resourcePolicy.getName()).isEqualTo(SCHEDULE_NAME); } + @Disabled @Test @Order(2) public void testEditSnapshotSchedule() @@ -94,20 +102,18 @@ public void testEditSnapshotSchedule() assertThat(status).isEqualTo(Operation.Status.DONE); } + @Disabled @Test @Order(2) - public void testListSnapshotSchedules() - throws IOException, ExecutionException, InterruptedException, TimeoutException { + public void testListSnapshotSchedules() throws IOException { List list = ListSnapshotSchedules.listSnapshotSchedules( PROJECT_ID, REGION); assertThat(list.size()).isEqualTo(1); assertThat(list.get(0).getName()).isEqualTo(SCHEDULE_NAME); - for (ResourcePolicy item : list) { - DeleteSnapshotSchedule.deleteSnapshotSchedule(PROJECT_ID, REGION, item.getName()); - } } + @Disabled @Test @Order(3) public void testDeleteSnapshotSchedule() From 3fa52d8dbf1d4547f5d7c3bba6a3adfb2e453ef8 Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Fri, 6 Dec 2024 14:54:02 +0100 Subject: [PATCH 11/15] Cleaned resources --- .../test/java/compute/snapshotschedule/SnapshotScheduleIT.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java index 71ced08c2f5..c332c79d2cc 100644 --- a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java +++ b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java @@ -22,6 +22,7 @@ import com.google.cloud.compute.v1.Operation; import com.google.cloud.compute.v1.ResourcePolicy; +import compute.disks.RegionalDelete; import java.io.IOException; import java.util.List; import java.util.UUID; @@ -61,6 +62,8 @@ public static void setUp() throws IOException, ExecutionException, InterruptedException, TimeoutException { requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("GOOGLE_CLOUD_PROJECT"); + RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, "test-disk-for-consistency-727ee5d7"); + List list = ListSnapshotSchedules.listSnapshotSchedules( PROJECT_ID, REGION); for (ResourcePolicy item : list) { From a0558f5cca097d55de8d9c6473709e55a0f25bb8 Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Fri, 6 Dec 2024 17:19:02 +0100 Subject: [PATCH 12/15] Fixed test --- .../snapshotschedule/SnapshotScheduleIT.java | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java index c332c79d2cc..cb0646c08cd 100644 --- a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java +++ b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java @@ -22,7 +22,6 @@ import com.google.cloud.compute.v1.Operation; import com.google.cloud.compute.v1.ResourcePolicy; -import compute.disks.RegionalDelete; import java.io.IOException; import java.util.List; import java.util.UUID; @@ -30,7 +29,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; @@ -62,16 +60,8 @@ public static void setUp() throws IOException, ExecutionException, InterruptedException, TimeoutException { requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("GOOGLE_CLOUD_PROJECT"); - RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, "test-disk-for-consistency-727ee5d7"); - - List list = ListSnapshotSchedules.listSnapshotSchedules( - PROJECT_ID, REGION); - for (ResourcePolicy item : list) { - DeleteSnapshotSchedule.deleteSnapshotSchedule(PROJECT_ID, REGION, item.getName()); - } } - @Disabled @Test @Order(1) public void testCreateSnapshotScheduleHourly() @@ -83,7 +73,6 @@ public void testCreateSnapshotScheduleHourly() assertThat(status).isEqualTo(Operation.Status.DONE); } - @Disabled @Test @Order(2) public void testGetSnapshotSchedule() throws IOException { @@ -94,7 +83,6 @@ public void testGetSnapshotSchedule() throws IOException { assertThat(resourcePolicy.getName()).isEqualTo(SCHEDULE_NAME); } - @Disabled @Test @Order(2) public void testEditSnapshotSchedule() @@ -105,7 +93,6 @@ public void testEditSnapshotSchedule() assertThat(status).isEqualTo(Operation.Status.DONE); } - @Disabled @Test @Order(2) public void testListSnapshotSchedules() throws IOException { @@ -116,7 +103,6 @@ public void testListSnapshotSchedules() throws IOException { assertThat(list.get(0).getName()).isEqualTo(SCHEDULE_NAME); } - @Disabled @Test @Order(3) public void testDeleteSnapshotSchedule() From 195d67fb1bdd2bf2a33e5d912692897faf2cee94 Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Mon, 9 Dec 2024 10:29:30 +0100 Subject: [PATCH 13/15] Added comment --- .../java/compute/snapshotschedule/ListSnapshotSchedules.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compute/cloud-client/src/main/java/compute/snapshotschedule/ListSnapshotSchedules.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/ListSnapshotSchedules.java index 60b64ad8fc4..f8db1aa9a58 100644 --- a/compute/cloud-client/src/main/java/compute/snapshotschedule/ListSnapshotSchedules.java +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/ListSnapshotSchedules.java @@ -50,7 +50,8 @@ public static List listSnapshotSchedules( .setProject(projectId) .setRegion(region) .build(); - + // Filtering must be done manually for now, since list filtering + // inside resourcePoliciesClient.list is not supported yet. for (ResourcePolicy resourcePolicy : resourcePoliciesClient.list(request).iterateAll()) { if (resourcePolicy.getRegion().equals(filter)) { list.add(resourcePolicy); From 6e2f4f3bce4fb26baffc9de763cde55f75a677db Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Mon, 9 Dec 2024 21:50:14 +0100 Subject: [PATCH 14/15] Fixed tests --- .../CreateSnapshotSchedule.java | 8 +- .../DeleteSnapshotSchedule.java | 10 +- .../ListSnapshotSchedules.java | 19 ++- .../snapshotschedule/SnapshotScheduleIT.java | 128 +++++++++++++----- 4 files changed, 116 insertions(+), 49 deletions(-) diff --git a/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java index 25089f09de9..d8ae82c319b 100644 --- a/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java @@ -17,6 +17,7 @@ package compute.snapshotschedule; // [START compute_snapshot_schedule_create] +import com.google.cloud.compute.v1.InsertResourcePolicyRequest; import com.google.cloud.compute.v1.Operation; import com.google.cloud.compute.v1.ResourcePoliciesClient; import com.google.cloud.compute.v1.ResourcePolicy; @@ -120,8 +121,13 @@ public static Operation.Status createSnapshotSchedule(String projectId, String r // 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 (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { + InsertResourcePolicyRequest request = InsertResourcePolicyRequest.newBuilder() + .setProject(projectId) + .setRegion(region) + .setResourcePolicyResource(resourcePolicy) + .build(); - Operation response = resourcePoliciesClient.insertAsync(projectId, region, resourcePolicy) + Operation response = resourcePoliciesClient.insertAsync(request) .get(3, TimeUnit.MINUTES); if (response.hasError()) { diff --git a/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java index 1310a65a9b4..fe34de37e3c 100644 --- a/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java @@ -17,6 +17,7 @@ package compute.snapshotschedule; // [START compute_snapshot_schedule_delete] +import com.google.cloud.compute.v1.DeleteResourcePolicyRequest; import com.google.cloud.compute.v1.Operation; import com.google.cloud.compute.v1.ResourcePoliciesClient; import java.io.IOException; @@ -45,9 +46,12 @@ public static Operation.Status deleteSnapshotSchedule( // 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 (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { - Operation response = resourcePoliciesClient - .deleteAsync(projectId, region, snapshotScheduleName) - .get(3, TimeUnit.MINUTES); + DeleteResourcePolicyRequest request = DeleteResourcePolicyRequest.newBuilder() + .setProject(projectId) + .setRegion(region) + .setResourcePolicy(snapshotScheduleName) + .build(); + Operation response = resourcePoliciesClient.deleteAsync(request).get(3, TimeUnit.MINUTES); if (response.hasError()) { throw new Error("Snapshot schedule deletion failed! " + response.getError()); diff --git a/compute/cloud-client/src/main/java/compute/snapshotschedule/ListSnapshotSchedules.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/ListSnapshotSchedules.java index f8db1aa9a58..e1befbb31f4 100644 --- a/compute/cloud-client/src/main/java/compute/snapshotschedule/ListSnapshotSchedules.java +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/ListSnapshotSchedules.java @@ -32,15 +32,16 @@ public static void main(String[] args) throws IOException { String projectId = "YOUR_PROJECT_ID"; // Name of the region you want to list snapshot schedules from. String region = "us-central1"; + // Name of the snapshot schedule you want to list. + String snapshotScheduleName = "YOUR_SCHEDULE_NAME"; - listSnapshotSchedules(projectId, region); + listSnapshotSchedules(projectId, region, snapshotScheduleName); } // Lists snapshot schedules in a specified region, optionally filtered. public static List listSnapshotSchedules( - String projectId, String region) throws IOException { - String filter = String.format( - "https://www.googleapis.com/compute/v1/projects/%s/regions/%s", projectId, region); + String projectId, String region, String snapshotScheduleName) throws IOException { + String filter = String.format("name = %s", snapshotScheduleName); List list = new ArrayList<>(); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. @@ -49,16 +50,14 @@ public static List listSnapshotSchedules( ListResourcePoliciesRequest request = ListResourcePoliciesRequest.newBuilder() .setProject(projectId) .setRegion(region) + .setFilter(filter) .build(); - // Filtering must be done manually for now, since list filtering - // inside resourcePoliciesClient.list is not supported yet. + for (ResourcePolicy resourcePolicy : resourcePoliciesClient.list(request).iterateAll()) { - if (resourcePolicy.getRegion().equals(filter)) { - list.add(resourcePolicy); - } + list.add(resourcePolicy); } - return list; } + return list; } } // [END compute_snapshot_schedule_list] \ No newline at end of file diff --git a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java index cb0646c08cd..5660d962e0e 100644 --- a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java +++ b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java @@ -18,9 +18,21 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; -import static org.junit.jupiter.api.Assertions.assertNotNull; - +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.compute.v1.DeleteResourcePolicyRequest; +import com.google.cloud.compute.v1.GetResourcePolicyRequest; +import com.google.cloud.compute.v1.InsertResourcePolicyRequest; import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.ResourcePoliciesClient; import com.google.cloud.compute.v1.ResourcePolicy; import java.io.IOException; import java.util.List; @@ -28,26 +40,24 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.MethodOrderer; -import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +import org.mockito.MockedStatic; @RunWith(JUnit4.class) -@Timeout(value = 6, unit = TimeUnit.MINUTES) -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@Timeout(value = 2, unit = TimeUnit.MINUTES) public class SnapshotScheduleIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String REGION = "us-central1"; private static final String SCHEDULE_NAME = "test-schedule-" + UUID.randomUUID(); - private static final String SCHEDULE_DESCRIPTION = "Test hourly snapshot schedule"; + private static final String SCHEDULE_DESCRIPTION = "Test hourly snapshot schedule"; private static final int MAX_RETENTION_DAYS = 2; private static final String STORAGE_LOCATION = "US"; - private static final String ON_SOURCE_DISK_DELETE = "KEEP_AUTO_SNAPSHOTS"; + private static final String ON_SOURCE_DISK_DELETE = "KEEP_AUTO_SNAPSHOTS"; // Check if the required environment variables are set. public static void requireEnvVar(String envVarName) { @@ -60,31 +70,18 @@ public static void setUp() throws IOException, ExecutionException, InterruptedException, TimeoutException { requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } - - @Test - @Order(1) - public void testCreateSnapshotScheduleHourly() - throws IOException, ExecutionException, InterruptedException, TimeoutException { - Operation.Status status = CreateSnapshotSchedule.createSnapshotSchedule( + CreateSnapshotSchedule.createSnapshotSchedule( PROJECT_ID, REGION, SCHEDULE_NAME, SCHEDULE_DESCRIPTION, MAX_RETENTION_DAYS, STORAGE_LOCATION, ON_SOURCE_DISK_DELETE); - - assertThat(status).isEqualTo(Operation.Status.DONE); } - @Test - @Order(2) - public void testGetSnapshotSchedule() throws IOException { - ResourcePolicy resourcePolicy = GetSnapshotSchedule.getSnapshotSchedule( - PROJECT_ID, REGION, SCHEDULE_NAME); - - assertNotNull(resourcePolicy); - assertThat(resourcePolicy.getName()).isEqualTo(SCHEDULE_NAME); + @AfterAll + public static void cleanup() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + DeleteSnapshotSchedule.deleteSnapshotSchedule(PROJECT_ID, REGION, SCHEDULE_NAME); } @Test - @Order(2) public void testEditSnapshotSchedule() throws IOException, ExecutionException, InterruptedException, TimeoutException { Operation.Status status = EditSnapshotSchedule.editSnapshotSchedule( @@ -94,22 +91,83 @@ public void testEditSnapshotSchedule() } @Test - @Order(2) public void testListSnapshotSchedules() throws IOException { List list = ListSnapshotSchedules.listSnapshotSchedules( - PROJECT_ID, REGION); + PROJECT_ID, REGION, SCHEDULE_NAME); assertThat(list.size()).isEqualTo(1); assertThat(list.get(0).getName()).isEqualTo(SCHEDULE_NAME); } @Test - @Order(3) - public void testDeleteSnapshotSchedule() + public void testCreateSnapshotScheduleHourly() throws IOException, ExecutionException, InterruptedException, TimeoutException { - Operation.Status status = DeleteSnapshotSchedule - .deleteSnapshotSchedule(PROJECT_ID, REGION, SCHEDULE_NAME); + try (MockedStatic mockedResourcePoliciesClient = + mockStatic(ResourcePoliciesClient.class)) { + Operation operation = mock(Operation.class); + ResourcePoliciesClient mockClient = mock(ResourcePoliciesClient.class); + OperationFuture mockFuture = mock(OperationFuture.class); + + mockedResourcePoliciesClient.when(ResourcePoliciesClient::create).thenReturn(mockClient); + when(mockClient.insertAsync(any(InsertResourcePolicyRequest.class))) + .thenReturn(mockFuture); + when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation); + when(operation.getStatus()).thenReturn(Operation.Status.DONE); + + Operation.Status status = CreateSnapshotSchedule.createSnapshotSchedule( + PROJECT_ID, REGION, SCHEDULE_NAME, SCHEDULE_DESCRIPTION, + MAX_RETENTION_DAYS, STORAGE_LOCATION, ON_SOURCE_DISK_DELETE); + + verify(mockClient, times(1)) + .insertAsync(any(InsertResourcePolicyRequest.class)); + verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class)); + assertEquals(Operation.Status.DONE, status); + } + } - assertThat(status).isEqualTo(Operation.Status.DONE); + @Test + public void testGetSnapshotSchedule() throws IOException { + try (MockedStatic mockedResourcePoliciesClient = + mockStatic(ResourcePoliciesClient.class)) { + Operation operation = mock(Operation.class); + ResourcePoliciesClient mockClient = mock(ResourcePoliciesClient.class); + ResourcePolicy mockResourcePolicy = mock(ResourcePolicy.class); + + mockedResourcePoliciesClient.when(ResourcePoliciesClient::create).thenReturn(mockClient); + when(mockClient.get(any(GetResourcePolicyRequest.class))) + .thenReturn(mockResourcePolicy); + + ResourcePolicy resourcePolicy = GetSnapshotSchedule.getSnapshotSchedule( + PROJECT_ID, REGION, SCHEDULE_NAME); + + verify(mockClient, times(1)) + .get(any(GetResourcePolicyRequest.class)); + assertEquals(mockResourcePolicy, resourcePolicy); + } + } + + @Test + public void testDeleteSnapshotSchedule() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + try (MockedStatic mockedResourcePoliciesClient = + mockStatic(ResourcePoliciesClient.class)) { + Operation operation = mock(Operation.class); + ResourcePoliciesClient mockClient = mock(ResourcePoliciesClient.class); + OperationFuture mockFuture = mock(OperationFuture.class); + + mockedResourcePoliciesClient.when(ResourcePoliciesClient::create).thenReturn(mockClient); + when(mockClient.deleteAsync(any(DeleteResourcePolicyRequest.class))) + .thenReturn(mockFuture); + when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation); + when(operation.getStatus()).thenReturn(Operation.Status.DONE); + + Operation.Status status = DeleteSnapshotSchedule + .deleteSnapshotSchedule(PROJECT_ID, REGION, SCHEDULE_NAME); + + verify(mockClient, times(1)) + .deleteAsync(any(DeleteResourcePolicyRequest.class)); + verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class)); + assertEquals(Operation.Status.DONE, status); + } } -} +} \ No newline at end of file From d9657a02d59089a8da875aab70f155abefbceff5 Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Wed, 18 Dec 2024 22:07:01 +0100 Subject: [PATCH 15/15] Fixed code --- .../CreateSnapshotSchedule.java | 3 ++- .../DeleteSnapshotSchedule.java | 3 ++- .../snapshotschedule/EditSnapshotSchedule.java | 3 ++- .../snapshotschedule/SnapshotScheduleIT.java | 17 +++++++++-------- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java index d8ae82c319b..6f751ddb2ea 100644 --- a/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java @@ -19,6 +19,7 @@ // [START compute_snapshot_schedule_create] import com.google.cloud.compute.v1.InsertResourcePolicyRequest; import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.Operation.Status; import com.google.cloud.compute.v1.ResourcePoliciesClient; import com.google.cloud.compute.v1.ResourcePolicy; import com.google.cloud.compute.v1.ResourcePolicyHourlyCycle; @@ -57,7 +58,7 @@ public static void main(String[] args) } // Creates a snapshot schedule policy. - public static Operation.Status createSnapshotSchedule(String projectId, String region, + public static Status createSnapshotSchedule(String projectId, String region, String snapshotScheduleName, String scheduleDescription, int maxRetentionDays, String storageLocation, String onSourceDiskDelete) throws IOException, ExecutionException, InterruptedException, TimeoutException { diff --git a/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java index fe34de37e3c..9a0dea6815b 100644 --- a/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java @@ -19,6 +19,7 @@ // [START compute_snapshot_schedule_delete] import com.google.cloud.compute.v1.DeleteResourcePolicyRequest; import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.Operation.Status; import com.google.cloud.compute.v1.ResourcePoliciesClient; import java.io.IOException; import java.util.concurrent.ExecutionException; @@ -40,7 +41,7 @@ public static void main(String[] args) } // Deletes a snapshot schedule policy. - public static Operation.Status deleteSnapshotSchedule( + public static Status deleteSnapshotSchedule( String projectId, String region, String snapshotScheduleName) throws IOException, ExecutionException, InterruptedException, TimeoutException { // Initialize client that will be used to send requests. This client only needs to be created diff --git a/compute/cloud-client/src/main/java/compute/snapshotschedule/EditSnapshotSchedule.java b/compute/cloud-client/src/main/java/compute/snapshotschedule/EditSnapshotSchedule.java index 1755c4bcef7..fcfe3e8d153 100644 --- a/compute/cloud-client/src/main/java/compute/snapshotschedule/EditSnapshotSchedule.java +++ b/compute/cloud-client/src/main/java/compute/snapshotschedule/EditSnapshotSchedule.java @@ -18,6 +18,7 @@ // [START compute_snapshot_schedule_edit] import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.Operation.Status; import com.google.cloud.compute.v1.PatchResourcePolicyRequest; import com.google.cloud.compute.v1.ResourcePoliciesClient; import com.google.cloud.compute.v1.ResourcePolicy; @@ -47,7 +48,7 @@ public static void main(String[] args) throws Exception { editSnapshotSchedule(projectId, region, snapshotScheduleName); } - public static Operation.Status editSnapshotSchedule( + public static Status editSnapshotSchedule( String projectId, String region, String snapshotScheduleName) throws IOException, InterruptedException, ExecutionException, TimeoutException { String description = "Updated description11"; diff --git a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java index 5660d962e0e..85f4bb40b2c 100644 --- a/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java +++ b/compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java @@ -32,6 +32,7 @@ import com.google.cloud.compute.v1.GetResourcePolicyRequest; import com.google.cloud.compute.v1.InsertResourcePolicyRequest; import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.Operation.Status; import com.google.cloud.compute.v1.ResourcePoliciesClient; import com.google.cloud.compute.v1.ResourcePolicy; import java.io.IOException; @@ -84,10 +85,10 @@ public static void cleanup() @Test public void testEditSnapshotSchedule() throws IOException, ExecutionException, InterruptedException, TimeoutException { - Operation.Status status = EditSnapshotSchedule.editSnapshotSchedule( + Status status = EditSnapshotSchedule.editSnapshotSchedule( PROJECT_ID, REGION, SCHEDULE_NAME); - assertThat(status).isEqualTo(Operation.Status.DONE); + assertThat(status).isEqualTo(Status.DONE); } @Test @@ -112,16 +113,16 @@ public void testCreateSnapshotScheduleHourly() when(mockClient.insertAsync(any(InsertResourcePolicyRequest.class))) .thenReturn(mockFuture); when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation); - when(operation.getStatus()).thenReturn(Operation.Status.DONE); + when(operation.getStatus()).thenReturn(Status.DONE); - Operation.Status status = CreateSnapshotSchedule.createSnapshotSchedule( + Status status = CreateSnapshotSchedule.createSnapshotSchedule( PROJECT_ID, REGION, SCHEDULE_NAME, SCHEDULE_DESCRIPTION, MAX_RETENTION_DAYS, STORAGE_LOCATION, ON_SOURCE_DISK_DELETE); verify(mockClient, times(1)) .insertAsync(any(InsertResourcePolicyRequest.class)); verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class)); - assertEquals(Operation.Status.DONE, status); + assertEquals(Status.DONE, status); } } @@ -159,15 +160,15 @@ public void testDeleteSnapshotSchedule() when(mockClient.deleteAsync(any(DeleteResourcePolicyRequest.class))) .thenReturn(mockFuture); when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation); - when(operation.getStatus()).thenReturn(Operation.Status.DONE); + when(operation.getStatus()).thenReturn(Status.DONE); - Operation.Status status = DeleteSnapshotSchedule + Status status = DeleteSnapshotSchedule .deleteSnapshotSchedule(PROJECT_ID, REGION, SCHEDULE_NAME); verify(mockClient, times(1)) .deleteAsync(any(DeleteResourcePolicyRequest.class)); verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class)); - assertEquals(Operation.Status.DONE, status); + assertEquals(Status.DONE, status); } } } \ No newline at end of file