Skip to content

Commit 3f0b332

Browse files
Created Create/Delete Samples for consistency group (#12787)
1 parent db6cba2 commit 3f0b332

File tree

10 files changed

+417
-0
lines changed

10 files changed

+417
-0
lines changed

compute/client_library/ingredients/disks/сonsistency_groups/__init__.py

Whitespace-only changes.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 consistency_group_create>
24+
def create_consistency_group(
25+
project_id: str, region: str, group_name: str, group_description: str
26+
) -> compute_v1.ResourcePolicy:
27+
"""
28+
Creates a consistency group in Google Cloud Compute Engine.
29+
Args:
30+
project_id (str): The ID of the Google Cloud project.
31+
region (str): The region where the consistency group will be created.
32+
group_name (str): The name of the consistency group.
33+
group_description (str): The description of the consistency group.
34+
Returns:
35+
compute_v1.ResourcePolicy: The consistency group object
36+
"""
37+
38+
# Initialize the ResourcePoliciesClient
39+
client = compute_v1.ResourcePoliciesClient()
40+
41+
# Create the ResourcePolicy object with the provided name, description, and policy
42+
resource_policy_resource = compute_v1.ResourcePolicy(
43+
name=group_name,
44+
description=group_description,
45+
disk_consistency_group_policy=compute_v1.ResourcePolicyDiskConsistencyGroupPolicy(),
46+
)
47+
48+
operation = client.insert(
49+
project=project_id,
50+
region=region,
51+
resource_policy_resource=resource_policy_resource,
52+
)
53+
wait_for_extended_operation(operation, "Consistency group creation")
54+
55+
return client.get(project=project_id, region=region, resource_policy=group_name)
56+
57+
58+
# </INGREDIENT>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 consistency_group_delete>
24+
def delete_consistency_group(project_id: str, region: str, group_name: str) -> None:
25+
"""
26+
Deletes a consistency group in Google Cloud Compute Engine.
27+
Args:
28+
project_id (str): The ID of the Google Cloud project.
29+
region (str): The region where the consistency group is located.
30+
group_name (str): The name of the consistency group to delete.
31+
Returns:
32+
None
33+
"""
34+
35+
# Initialize the ResourcePoliciesClient
36+
client = compute_v1.ResourcePoliciesClient()
37+
38+
# Delete the (consistency group) from the specified project and region
39+
operation = client.delete(
40+
project=project_id,
41+
region=region,
42+
resource_policy=group_name,
43+
)
44+
wait_for_extended_operation(operation, "Consistency group deletion")
45+
46+
47+
# </INGREDIENT>

compute/client_library/recipes/disks/сonsistency_groups/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
# <REGION compute_consistency_group_create>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT consistency_group_create />
22+
23+
# </REGION compute_consistency_group_create>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
# <REGION compute_consistency_group_delete>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT consistency_group_delete />
22+
23+
# </REGION compute_consistency_group_delete>

compute/client_library/snippets/disks/сonsistency_groups/__init__.py

Whitespace-only changes.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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_create]
23+
from __future__ import annotations
24+
25+
import sys
26+
from typing import Any
27+
28+
from google.api_core.extended_operation import ExtendedOperation
29+
from google.cloud import compute_v1
30+
31+
32+
def wait_for_extended_operation(
33+
operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
34+
) -> Any:
35+
"""
36+
Waits for the extended (long-running) operation to complete.
37+
38+
If the operation is successful, it will return its result.
39+
If the operation ends with an error, an exception will be raised.
40+
If there were any warnings during the execution of the operation
41+
they will be printed to sys.stderr.
42+
43+
Args:
44+
operation: a long-running operation you want to wait on.
45+
verbose_name: (optional) a more verbose name of the operation,
46+
used only during error and warning reporting.
47+
timeout: how long (in seconds) to wait for operation to finish.
48+
If None, wait indefinitely.
49+
50+
Returns:
51+
Whatever the operation.result() returns.
52+
53+
Raises:
54+
This method will raise the exception received from `operation.exception()`
55+
or RuntimeError if there is no exception set, but there is an `error_code`
56+
set for the `operation`.
57+
58+
In case of an operation taking longer than `timeout` seconds to complete,
59+
a `concurrent.futures.TimeoutError` will be raised.
60+
"""
61+
result = operation.result(timeout=timeout)
62+
63+
if operation.error_code:
64+
print(
65+
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
66+
file=sys.stderr,
67+
flush=True,
68+
)
69+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
70+
raise operation.exception() or RuntimeError(operation.error_message)
71+
72+
if operation.warnings:
73+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
74+
for warning in operation.warnings:
75+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
76+
77+
return result
78+
79+
80+
def create_consistency_group(
81+
project_id: str, region: str, group_name: str, group_description: str
82+
) -> compute_v1.ResourcePolicy:
83+
"""
84+
Creates a consistency group in Google Cloud Compute Engine.
85+
Args:
86+
project_id (str): The ID of the Google Cloud project.
87+
region (str): The region where the consistency group will be created.
88+
group_name (str): The name of the consistency group.
89+
group_description (str): The description of the consistency group.
90+
Returns:
91+
compute_v1.ResourcePolicy: The consistency group object
92+
"""
93+
94+
# Initialize the ResourcePoliciesClient
95+
client = compute_v1.ResourcePoliciesClient()
96+
97+
# Create the ResourcePolicy object with the provided name, description, and policy
98+
resource_policy_resource = compute_v1.ResourcePolicy(
99+
name=group_name,
100+
description=group_description,
101+
disk_consistency_group_policy=compute_v1.ResourcePolicyDiskConsistencyGroupPolicy(),
102+
)
103+
104+
operation = client.insert(
105+
project=project_id,
106+
region=region,
107+
resource_policy_resource=resource_policy_resource,
108+
)
109+
wait_for_extended_operation(operation, "Consistency group creation")
110+
111+
return client.get(project=project_id, region=region, resource_policy=group_name)
112+
113+
114+
# [END compute_consistency_group_create]
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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_delete]
23+
from __future__ import annotations
24+
25+
import sys
26+
from typing import Any
27+
28+
from google.api_core.extended_operation import ExtendedOperation
29+
from google.cloud import compute_v1
30+
31+
32+
def wait_for_extended_operation(
33+
operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
34+
) -> Any:
35+
"""
36+
Waits for the extended (long-running) operation to complete.
37+
38+
If the operation is successful, it will return its result.
39+
If the operation ends with an error, an exception will be raised.
40+
If there were any warnings during the execution of the operation
41+
they will be printed to sys.stderr.
42+
43+
Args:
44+
operation: a long-running operation you want to wait on.
45+
verbose_name: (optional) a more verbose name of the operation,
46+
used only during error and warning reporting.
47+
timeout: how long (in seconds) to wait for operation to finish.
48+
If None, wait indefinitely.
49+
50+
Returns:
51+
Whatever the operation.result() returns.
52+
53+
Raises:
54+
This method will raise the exception received from `operation.exception()`
55+
or RuntimeError if there is no exception set, but there is an `error_code`
56+
set for the `operation`.
57+
58+
In case of an operation taking longer than `timeout` seconds to complete,
59+
a `concurrent.futures.TimeoutError` will be raised.
60+
"""
61+
result = operation.result(timeout=timeout)
62+
63+
if operation.error_code:
64+
print(
65+
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
66+
file=sys.stderr,
67+
flush=True,
68+
)
69+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
70+
raise operation.exception() or RuntimeError(operation.error_message)
71+
72+
if operation.warnings:
73+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
74+
for warning in operation.warnings:
75+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
76+
77+
return result
78+
79+
80+
def delete_consistency_group(project_id: str, region: str, group_name: str) -> None:
81+
"""
82+
Deletes a consistency group in Google Cloud Compute Engine.
83+
Args:
84+
project_id (str): The ID of the Google Cloud project.
85+
region (str): The region where the consistency group is located.
86+
group_name (str): The name of the consistency group to delete.
87+
Returns:
88+
None
89+
"""
90+
91+
# Initialize the ResourcePoliciesClient
92+
client = compute_v1.ResourcePoliciesClient()
93+
94+
# Delete the (consistency group) from the specified project and region
95+
operation = client.delete(
96+
project=project_id,
97+
region=region,
98+
resource_policy=group_name,
99+
)
100+
wait_for_extended_operation(operation, "Consistency group deletion")
101+
102+
103+
# [END compute_consistency_group_delete]

0 commit comments

Comments
 (0)