Skip to content

Commit a4bbb88

Browse files
feat: New Consistency group Samples : Add Disk, List Disks, Remove Disk (#12796)
* Added new consistency group samples. * Updated tests. Renamed one test fixture in test_disks.py * Refactored add_disk. Added new remove_disk_consistency_group.py Sample. Updated tests * Removed disk_region_flag parameter from add/remove/list samples
1 parent d9b2f9c commit a4bbb88

File tree

11 files changed

+532
-2
lines changed

11 files changed

+532
-2
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
# <INGREDIENT add_disk_to_consistency_group>
23+
24+
25+
def add_disk_consistency_group(
26+
project_id: str,
27+
disk_name: str,
28+
disk_location: str,
29+
consistency_group_name: str,
30+
consistency_group_region: str,
31+
) -> None:
32+
"""Adds a disk to a specified consistency group.
33+
Args:
34+
project_id (str): The ID of the Google Cloud project.
35+
disk_name (str): The name of the disk to be added.
36+
disk_location (str): The region or zone of the disk
37+
consistency_group_name (str): The name of the consistency group.
38+
consistency_group_region (str): The region of the consistency group.
39+
Returns:
40+
None
41+
"""
42+
consistency_group_link = (
43+
f"regions/{consistency_group_region}/resourcePolicies/{consistency_group_name}"
44+
)
45+
46+
# Checking if the disk is zonal or regional
47+
# If the final character of the disk_location is a digit, it is a regional disk
48+
if disk_location[-1].isdigit():
49+
policy = compute_v1.RegionDisksAddResourcePoliciesRequest(
50+
resource_policies=[consistency_group_link]
51+
)
52+
disk_client = compute_v1.RegionDisksClient()
53+
disk_client.add_resource_policies(
54+
project=project_id,
55+
region=disk_location,
56+
disk=disk_name,
57+
region_disks_add_resource_policies_request_resource=policy,
58+
)
59+
# For zonal disks we use DisksClient
60+
else:
61+
print("Using DisksClient")
62+
policy = compute_v1.DisksAddResourcePoliciesRequest(
63+
resource_policies=[consistency_group_link]
64+
)
65+
disk_client = compute_v1.DisksClient()
66+
disk_client.add_resource_policies(
67+
project=project_id,
68+
zone=disk_location,
69+
disk=disk_name,
70+
disks_add_resource_policies_request_resource=policy,
71+
)
72+
73+
print(f"Disk {disk_name} added to consistency group {consistency_group_name}")
74+
75+
76+
# </INGREDIENT>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 list_disks_in_consistency_group>
24+
def list_disks_consistency_group(
25+
project_id: str,
26+
disk_location: str,
27+
consistency_group_name: str,
28+
consistency_group_region: str,
29+
) -> list:
30+
"""
31+
Lists disks that are part of a specified consistency group.
32+
Args:
33+
project_id (str): The ID of the Google Cloud project.
34+
disk_location (str): The region or zone of the disk
35+
disk_region_flag (bool): Flag indicating if the disk is regional.
36+
consistency_group_name (str): The name of the consistency group.
37+
consistency_group_region (str): The region of the consistency group.
38+
Returns:
39+
list: A list of disks that are part of the specified consistency group.
40+
"""
41+
consistency_group_link = (
42+
f"https://www.googleapis.com/compute/v1/projects/{project_id}/regions/"
43+
f"{consistency_group_region}/resourcePolicies/{consistency_group_name}"
44+
)
45+
# If the final character of the disk_location is a digit, it is a regional disk
46+
if disk_location[-1].isdigit():
47+
region_client = compute_v1.RegionDisksClient()
48+
disks = region_client.list(project=project_id, region=disk_location)
49+
# For zonal disks we use DisksClient
50+
else:
51+
client = compute_v1.DisksClient()
52+
disks = client.list(project=project_id, zone=disk_location)
53+
return [disk for disk in disks if consistency_group_link in disk.resource_policies]
54+
55+
56+
# </INGREDIENT>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
# flake8: noqa
15+
16+
17+
# This file is automatically generated. Please do not modify it directly.
18+
# Find the relevant recipe file in the samples/recipes or samples/ingredients
19+
# directory and apply your changes there.
20+
21+
22+
from google.cloud import compute_v1
23+
24+
25+
# <INGREDIENT remove_disk_from_consistency_group>
26+
def remove_disk_consistency_group(
27+
project_id: str,
28+
disk_name: str,
29+
disk_location: str,
30+
consistency_group_name: str,
31+
consistency_group_region: str,
32+
) -> None:
33+
"""Removes a disk from a specified consistency group.
34+
Args:
35+
project_id (str): The ID of the Google Cloud project.
36+
disk_name (str): The name of the disk to be deleted.
37+
disk_location (str): The region or zone of the disk
38+
consistency_group_name (str): The name of the consistency group.
39+
consistency_group_region (str): The region of the consistency group.
40+
Returns:
41+
None
42+
"""
43+
consistency_group_link = (
44+
f"regions/{consistency_group_region}/resourcePolicies/{consistency_group_name}"
45+
)
46+
# Checking if the disk is zonal or regional
47+
# If the final character of the disk_location is a digit, it is a regional disk
48+
if disk_location[-1].isdigit():
49+
policy = compute_v1.RegionDisksRemoveResourcePoliciesRequest(
50+
resource_policies=[consistency_group_link]
51+
)
52+
disk_client = compute_v1.RegionDisksClient()
53+
disk_client.remove_resource_policies(
54+
project=project_id,
55+
region=disk_location,
56+
disk=disk_name,
57+
region_disks_remove_resource_policies_request_resource=policy,
58+
)
59+
# For zonal disks we use DisksClient
60+
else:
61+
policy = compute_v1.DisksRemoveResourcePoliciesRequest(
62+
resource_policies=[consistency_group_link]
63+
)
64+
disk_client = compute_v1.DisksClient()
65+
disk_client.remove_resource_policies(
66+
project=project_id,
67+
zone=disk_location,
68+
disk=disk_name,
69+
disks_remove_resource_policies_request_resource=policy,
70+
)
71+
72+
print(f"Disk {disk_name} removed from consistency group {consistency_group_name}")
73+
74+
75+
# </INGREDIENT>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
# flake8: noqa
15+
16+
17+
# <REGION compute_consistency_group_add_disk>
18+
# <IMPORTS/>
19+
20+
# <INGREDIENT add_disk_to_consistency_group />
21+
22+
# </REGION compute_consistency_group_add_disk>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
# flake8: noqa
15+
16+
17+
# <REGION compute_consistency_group_disks_list>
18+
# <IMPORTS/>
19+
20+
# <INGREDIENT list_disks_in_consistency_group />
21+
22+
# </REGION compute_consistency_group_disks_list>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
# flake8: noqa
15+
16+
17+
# <REGION compute_consistency_group_remove_disk>
18+
# <IMPORTS/>
19+
20+
# <INGREDIENT remove_disk_from_consistency_group />
21+
22+
# </REGION compute_consistency_group_remove_disk>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
# flake8: noqa
15+
16+
17+
# This file is automatically generated. Please do not modify it directly.
18+
# Find the relevant recipe file in the samples/recipes or samples/ingredients
19+
# directory and apply your changes there.
20+
21+
22+
# [START compute_consistency_group_add_disk]
23+
from google.cloud import compute_v1
24+
25+
26+
def add_disk_consistency_group(
27+
project_id: str,
28+
disk_name: str,
29+
disk_location: str,
30+
consistency_group_name: str,
31+
consistency_group_region: str,
32+
) -> None:
33+
"""Adds a disk to a specified consistency group.
34+
Args:
35+
project_id (str): The ID of the Google Cloud project.
36+
disk_name (str): The name of the disk to be added.
37+
disk_location (str): The region or zone of the disk
38+
consistency_group_name (str): The name of the consistency group.
39+
consistency_group_region (str): The region of the consistency group.
40+
Returns:
41+
None
42+
"""
43+
consistency_group_link = (
44+
f"regions/{consistency_group_region}/resourcePolicies/{consistency_group_name}"
45+
)
46+
47+
# Checking if the disk is zonal or regional
48+
# If the final character of the disk_location is a digit, it is a regional disk
49+
if disk_location[-1].isdigit():
50+
policy = compute_v1.RegionDisksAddResourcePoliciesRequest(
51+
resource_policies=[consistency_group_link]
52+
)
53+
disk_client = compute_v1.RegionDisksClient()
54+
disk_client.add_resource_policies(
55+
project=project_id,
56+
region=disk_location,
57+
disk=disk_name,
58+
region_disks_add_resource_policies_request_resource=policy,
59+
)
60+
# For zonal disks we use DisksClient
61+
else:
62+
print("Using DisksClient")
63+
policy = compute_v1.DisksAddResourcePoliciesRequest(
64+
resource_policies=[consistency_group_link]
65+
)
66+
disk_client = compute_v1.DisksClient()
67+
disk_client.add_resource_policies(
68+
project=project_id,
69+
zone=disk_location,
70+
disk=disk_name,
71+
disks_add_resource_policies_request_resource=policy,
72+
)
73+
74+
print(f"Disk {disk_name} added to consistency group {consistency_group_name}")
75+
76+
77+
# [END compute_consistency_group_add_disk]

0 commit comments

Comments
 (0)