Skip to content

Commit 8cd5313

Browse files
feat: Created Snapshot Schedule Samples -> Get, Create, Delete, List operations (#13012)
* Created Get, Create, Delete, List samples * Created Schedule attach disk sample * Created Schedule detach disk sample * Created an Update a snapshot schedule Sample * Added some delays
1 parent fa9235a commit 8cd5313

23 files changed

+1356
-3
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
20+
from google.cloud import compute_v1
21+
22+
23+
# <INGREDIENT attach_disk_schedule_snapshots>
24+
def snapshot_schedule_attach(
25+
project_id: str, zone: str, region: str, disk_name: str, schedule_name: str
26+
) -> None:
27+
"""
28+
Attaches a snapshot schedule to a specified disk.
29+
Args:
30+
project_id (str): The ID of the Google Cloud project.
31+
zone (str): The zone where the disk is located.
32+
region (str): The region where the snapshot schedule was created
33+
disk_name (str): The name of the disk to which the snapshot schedule will be attached.
34+
schedule_name (str): The name of the snapshot schedule that you are applying to this disk
35+
Returns:
36+
None
37+
"""
38+
disks_add_request = compute_v1.DisksAddResourcePoliciesRequest(
39+
resource_policies=[f"regions/{region}/resourcePolicies/{schedule_name}"]
40+
)
41+
42+
client = compute_v1.DisksClient()
43+
operation = client.add_resource_policies(
44+
project=project_id,
45+
zone=zone,
46+
disk=disk_name,
47+
disks_add_resource_policies_request_resource=disks_add_request,
48+
)
49+
wait_for_extended_operation(operation, "Attaching snapshot schedule to disk")
50+
51+
52+
# </INGREDIENT>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
20+
from google.cloud import compute_v1
21+
22+
23+
# <INGREDIENT create_schedule_snapshots>
24+
def snapshot_schedule_create(
25+
project_id: str,
26+
region: str,
27+
schedule_name: str,
28+
schedule_description: str,
29+
labels: dict,
30+
) -> compute_v1.ResourcePolicy:
31+
"""
32+
Creates a snapshot schedule for disks for a specified project and region.
33+
Args:
34+
project_id (str): The ID of the Google Cloud project.
35+
region (str): The region where the snapshot schedule will be created.
36+
schedule_name (str): The name of the snapshot schedule group.
37+
schedule_description (str): The description of the snapshot schedule group.
38+
labels (dict): The labels to apply to the snapshots. Example: {"env": "dev", "media": "images"}
39+
Returns:
40+
compute_v1.ResourcePolicy: The created resource policy.
41+
"""
42+
43+
# # Every hour, starts at 12:00 AM
44+
# hourly_schedule = compute_v1.ResourcePolicyHourlyCycle(
45+
# hours_in_cycle=1, start_time="00:00"
46+
# )
47+
#
48+
# # Every Monday, starts between 12:00 AM and 1:00 AM
49+
# day = compute_v1.ResourcePolicyWeeklyCycleDayOfWeek(
50+
# day="MONDAY", start_time="00:00"
51+
# )
52+
# weekly_schedule = compute_v1.ResourcePolicyWeeklyCycle(day_of_weeks=[day])
53+
54+
# In this example we use daily_schedule - every day, starts between 12:00 AM and 1:00 AM
55+
daily_schedule = compute_v1.ResourcePolicyDailyCycle(
56+
days_in_cycle=1, start_time="00:00"
57+
)
58+
59+
schedule = compute_v1.ResourcePolicySnapshotSchedulePolicySchedule()
60+
# You can change the schedule type to daily_schedule, weekly_schedule, or hourly_schedule
61+
schedule.daily_schedule = daily_schedule
62+
63+
# Autodelete snapshots after 5 days
64+
retention_policy = compute_v1.ResourcePolicySnapshotSchedulePolicyRetentionPolicy(
65+
max_retention_days=5
66+
)
67+
snapshot_properties = (
68+
compute_v1.ResourcePolicySnapshotSchedulePolicySnapshotProperties(
69+
guest_flush=False, labels=labels
70+
)
71+
)
72+
73+
snapshot_policy = compute_v1.ResourcePolicySnapshotSchedulePolicy()
74+
snapshot_policy.schedule = schedule
75+
snapshot_policy.retention_policy = retention_policy
76+
snapshot_policy.snapshot_properties = snapshot_properties
77+
78+
resource_policy_resource = compute_v1.ResourcePolicy(
79+
name=schedule_name,
80+
description=schedule_description,
81+
snapshot_schedule_policy=snapshot_policy,
82+
)
83+
84+
client = compute_v1.ResourcePoliciesClient()
85+
operation = client.insert(
86+
project=project_id,
87+
region=region,
88+
resource_policy_resource=resource_policy_resource,
89+
)
90+
wait_for_extended_operation(operation, "Resource Policy creation")
91+
92+
return client.get(project=project_id, region=region, resource_policy=schedule_name)
93+
94+
95+
# </INGREDIENT>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
20+
from google.cloud import compute_v1
21+
22+
23+
# <INGREDIENT delete_schedule_snapshots>
24+
def snapshot_schedule_delete(
25+
project_id: str, region: str, snapshot_schedule_name: str
26+
) -> None:
27+
"""
28+
Deletes a snapshot schedule for a specified project and region.
29+
Args:
30+
project_id (str): The ID of the Google Cloud project.
31+
region (str): The region where the snapshot schedule is located.
32+
snapshot_schedule_name (str): The name of the snapshot schedule to delete.
33+
Returns:
34+
None
35+
"""
36+
client = compute_v1.ResourcePoliciesClient()
37+
operation = client.delete(
38+
project=project_id, region=region, resource_policy=snapshot_schedule_name
39+
)
40+
wait_for_extended_operation(operation, "Resource Policy deletion")
41+
42+
43+
# </INGREDIENT>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
20+
from google.cloud import compute_v1
21+
22+
23+
# <INGREDIENT get_schedule_snapshots>
24+
def snapshot_schedule_get(
25+
project_id: str, region: str, snapshot_schedule_name: str
26+
) -> compute_v1.ResourcePolicy:
27+
"""
28+
Retrieves a snapshot schedule for a specified project and region.
29+
Args:
30+
project_id (str): The ID of the Google Cloud project.
31+
region (str): The region where the snapshot schedule is located.
32+
snapshot_schedule_name (str): The name of the snapshot schedule.
33+
Returns:
34+
compute_v1.ResourcePolicy: The retrieved snapshot schedule.
35+
"""
36+
client = compute_v1.ResourcePoliciesClient()
37+
schedule = client.get(
38+
project=project_id, region=region, resource_policy=snapshot_schedule_name
39+
)
40+
return schedule
41+
42+
43+
# </INGREDIENT>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
20+
from google.cloud import compute_v1
21+
from google.cloud.compute_v1.services.resource_policies import pagers
22+
23+
24+
# <INGREDIENT list_schedule_snapshots>
25+
def snapshot_schedule_list(project_id: str, region: str) -> pagers.ListPager:
26+
"""
27+
Lists snapshot schedules for a specified project and region.
28+
Args:
29+
project_id (str): The ID of the Google Cloud project.
30+
region (str): The region where the snapshot schedules are located.
31+
Returns:
32+
ListPager: A pager for iterating through the list of snapshot schedules.
33+
"""
34+
client = compute_v1.ResourcePoliciesClient()
35+
36+
request = compute_v1.ListResourcePoliciesRequest(
37+
project=project_id,
38+
region=region,
39+
filter='status = "READY"', # Optional filter
40+
)
41+
42+
schedules = client.list(request=request)
43+
return schedules
44+
45+
46+
# </INGREDIENT>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
20+
from google.cloud import compute_v1
21+
22+
23+
# <INGREDIENT remove_disk_schedule_snapshots>
24+
def snapshot_schedule_detach_disk(
25+
project_id: str, zone: str, region: str, disk_name: str, schedule_name: str
26+
) -> None:
27+
"""
28+
Detaches a snapshot schedule from a specified disk in a given project and zone.
29+
Args:
30+
project_id (str): The ID of the Google Cloud project.
31+
zone (str): The zone where the disk is located.
32+
region (str): The location of the snapshot schedule
33+
disk_name (str): The name of the disk with the associated snapshot schedule
34+
schedule_name (str): The name of the snapshot schedule that you are removing from this disk
35+
Returns:
36+
None
37+
"""
38+
disks_remove_request = compute_v1.DisksRemoveResourcePoliciesRequest(
39+
resource_policies=[f"regions/{region}/resourcePolicies/{schedule_name}"]
40+
)
41+
42+
client = compute_v1.DisksClient()
43+
operation = client.remove_resource_policies(
44+
project=project_id,
45+
zone=zone,
46+
disk=disk_name,
47+
disks_remove_resource_policies_request_resource=disks_remove_request,
48+
)
49+
wait_for_extended_operation(operation, "Detaching snapshot schedule from disk")
50+
51+
52+
# </INGREDIENT>

0 commit comments

Comments
 (0)