Skip to content

Commit 361f09f

Browse files
Added compute_snapshot_schedule_get sample, created test
1 parent dbf5596 commit 361f09f

File tree

2 files changed

+80
-10
lines changed

2 files changed

+80
-10
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package compute.snapshot;
18+
19+
// [START compute_snapshot_schedule_get]
20+
import com.google.cloud.compute.v1.GetResourcePolicyRequest;
21+
import com.google.cloud.compute.v1.ResourcePoliciesClient;
22+
import com.google.cloud.compute.v1.ResourcePolicy;
23+
import java.io.IOException;
24+
25+
public class GetSnapshotSchedule {
26+
27+
public static void main(String[] args) throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
// Project ID or project number of the Cloud project you want to use.
30+
String projectId = "YOUR_PROJECT_ID";
31+
// Name of the region in which your snapshot schedule is located.
32+
String region = "us-central1";
33+
// Name of your snapshot schedule.
34+
String scheduleName = "YOUR_SCHEDULE_NAME";
35+
36+
getSnapshotSchedule(projectId, region, scheduleName);
37+
}
38+
39+
// Retrieves the details of a snapshot schedule.
40+
public static ResourcePolicy getSnapshotSchedule(
41+
String projectId, String region, String scheduleName) throws IOException {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests.
44+
try (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) {
45+
GetResourcePolicyRequest request = GetResourcePolicyRequest.newBuilder()
46+
.setProject(projectId)
47+
.setRegion(region)
48+
.setResourcePolicy(scheduleName)
49+
.build();
50+
return resourcePoliciesClient.get(request);
51+
}
52+
}
53+
}
54+
// [END compute_snapshot_schedule_get]

compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,27 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020
import static com.google.common.truth.Truth.assertWithMessage;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2122

2223
import com.google.cloud.compute.v1.Operation;
24+
import com.google.cloud.compute.v1.ResourcePolicy;
2325
import java.io.IOException;
2426
import java.util.UUID;
2527
import java.util.concurrent.ExecutionException;
2628
import java.util.concurrent.TimeUnit;
2729
import java.util.concurrent.TimeoutException;
28-
import org.junit.jupiter.api.AfterAll;
2930
import org.junit.jupiter.api.BeforeAll;
31+
import org.junit.jupiter.api.MethodOrderer;
32+
import org.junit.jupiter.api.Order;
3033
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.api.TestMethodOrder;
3135
import org.junit.jupiter.api.Timeout;
3236
import org.junit.runner.RunWith;
3337
import org.junit.runners.JUnit4;
3438

3539
@RunWith(JUnit4.class)
3640
@Timeout(value = 6, unit = TimeUnit.MINUTES)
41+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
3742
public class SnapshotIT {
3843
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
3944
private static final String ZONE = "asia-south1-a";
@@ -58,22 +63,33 @@ public static void setUp()
5863
requireEnvVar("GOOGLE_CLOUD_PROJECT");
5964
}
6065

61-
@AfterAll
62-
public static void cleanup()
66+
@Test
67+
@Order(1)
68+
public void testCreateSnapshotScheduleHourly()
6369
throws IOException, ExecutionException, InterruptedException, TimeoutException {
64-
// Delete snapshot schedule created for testing.
65-
Operation.Status status = DeleteSnapshotSchedule
66-
.deleteSnapshotSchedule(PROJECT_ID, REGION, SCHEDULE_NAME);
70+
Operation.Status status = CreateSnapshotSchedule.createSnapshotSchedule(
71+
PROJECT_ID, REGION, SCHEDULE_NAME, SCHEDULE_DESCRIPTION,
72+
MAX_RETENTION_DAYS, STORAGE_LOCATION, ON_SOURCE_DISK_DELETE);
6773

6874
assertThat(status).isEqualTo(Operation.Status.DONE);
6975
}
7076

7177
@Test
72-
public void testCreateSnapshotScheduleHourly()
78+
@Order(2)
79+
public void testGetSnapshotSchedule() throws IOException {
80+
81+
ResourcePolicy resourcePolicy = GetSnapshotSchedule.getSnapshotSchedule(
82+
PROJECT_ID, REGION, SCHEDULE_NAME);
83+
assertNotNull(resourcePolicy);
84+
assertThat(resourcePolicy.getName()).isEqualTo(SCHEDULE_NAME);
85+
}
86+
87+
@Test
88+
@Order(3)
89+
public void testDeleteSnapshotSchedule()
7390
throws IOException, ExecutionException, InterruptedException, TimeoutException {
74-
Operation.Status status = CreateSnapshotSchedule.createSnapshotSchedule(
75-
PROJECT_ID, REGION, SCHEDULE_NAME, SCHEDULE_DESCRIPTION,
76-
MAX_RETENTION_DAYS, STORAGE_LOCATION, ON_SOURCE_DISK_DELETE);
91+
Operation.Status status = DeleteSnapshotSchedule
92+
.deleteSnapshotSchedule(PROJECT_ID, REGION, SCHEDULE_NAME);
7793

7894
assertThat(status).isEqualTo(Operation.Status.DONE);
7995
}

0 commit comments

Comments
 (0)