-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(compute): add compute disk create with snapshot schedule #9788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+139
−11
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8f97467
Implemented compute_disk_create_with_snapshot_schedule sample, create…
TetyanaYahodska 226b70f
Merge branch 'main' into compute_disk_create_with_snapshot_schedule
TetyanaYahodska 64d9494
Merge branch 'main' into compute_disk_create_with_snapshot_schedule
TetyanaYahodska 26dddb7
Fixed code
TetyanaYahodska 2e917df
Resolved merge conflict
TetyanaYahodska a267cdb
Fixed code
TetyanaYahodska 3468355
Fixed test
TetyanaYahodska b9593d4
Fixed code
TetyanaYahodska bc8e69f
Resolved merge conflict
TetyanaYahodska c90d835
Fixed code as requested in the comments
TetyanaYahodska 04143cc
Resolved merge conflict
TetyanaYahodska e4ad368
Fixed lint issue
TetyanaYahodska fadf8e8
Fixed lint issue
TetyanaYahodska d8fe623
Fixed code as requested in the comments
TetyanaYahodska e43b692
Merge branch 'main' into compute_disk_create_with_snapshot_schedule
TetyanaYahodska 61f0db1
Deleted redundant code
TetyanaYahodska File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
compute/cloud-client/src/main/java/compute/disks/CreateDiskWithSnapshotSchedule.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| // [START compute_disk_create_with_snapshot_schedule] | ||
| import com.google.cloud.compute.v1.Disk; | ||
| import com.google.cloud.compute.v1.DisksClient; | ||
| import com.google.cloud.compute.v1.Operation; | ||
| import com.google.cloud.compute.v1.Operation.Status; | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
|
|
||
| public class CreateDiskWithSnapshotSchedule { | ||
| 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 disk. | ||
| String zone = "us-central1-a"; | ||
| // Name of the disk you want to create. | ||
| String diskName = "YOUR_DISK_NAME"; | ||
| // Name of the schedule you want to link to the disk. | ||
| String snapshotScheduleName = "YOUR_SCHEDULE_NAME"; | ||
|
|
||
| createDiskWithSnapshotSchedule(projectId, zone, diskName, snapshotScheduleName); | ||
| } | ||
|
|
||
| // Creates disk with linked snapshot schedule. | ||
| public static Status createDiskWithSnapshotSchedule( | ||
| String projectId, String zone, String diskName, 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 (DisksClient disksClient = DisksClient.create()) { | ||
| String region = zone.substring(0, zone.lastIndexOf('-')); | ||
| // Get the resource policy to link to the disk | ||
| String resourcePolicyLink = String.format("projects/%s/regions/%s/resourcePolicies/%s", | ||
| projectId, region, snapshotScheduleName); | ||
|
|
||
| Disk disk = Disk.newBuilder() | ||
| .setName(diskName) | ||
| .setZone(zone) | ||
| .addAllResourcePolicies(List.of(resourcePolicyLink)) | ||
| .build(); | ||
|
|
||
| Operation response = disksClient.insertAsync(projectId, zone, disk).get(3, TimeUnit.MINUTES); | ||
|
|
||
| if (response.hasError()) { | ||
| throw new Error("Disk creation failed! " + response.getError()); | ||
| } | ||
| return response.getStatus(); | ||
| } | ||
| } | ||
| } | ||
| // [END compute_disk_create_with_snapshot_schedule] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the client initializer comment here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Added comment