Skip to content

Commit

Permalink
feat: Created Stop replication consistency group sample (#12985)
Browse files Browse the repository at this point in the history
* Created Stop replication consistency group sample

* Fix lint

* Updated dependencies

* Updated tests

* Test fix

* Fixed conflicts and updated comments
  • Loading branch information
Thoughtseize1 authored Dec 18, 2024
1 parent f9fddc3 commit 1a866be
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
# 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.
# flake8: noqa


# This file is automatically generated. Please do not modify it directly.
# Find the relevant recipe file in the samples/recipes or samples/ingredients
# directory and apply your changes there.
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
# folder for complete code samples that are ready to be used.
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
# flake8: noqa


from google.cloud import compute_v1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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.
# flake8: noqa

from google.cloud import compute_v1


# <INGREDIENT stop_replication_in_consistency_group>
def stop_replication_consistency_group(project_id, location, consistency_group_name):
"""
Stops the asynchronous replication for a consistency group.
Args:
project_id (str): The ID of the Google Cloud project.
location (str): The region where the consistency group is located.
consistency_group_id (str): The ID of the consistency group.
Returns:
bool: True if the replication was successfully stopped.
"""
consistency_group = compute_v1.DisksStopGroupAsyncReplicationResource(
resource_policy=f"regions/{location}/resourcePolicies/{consistency_group_name}"
)
region_client = compute_v1.RegionDisksClient()
operation = region_client.stop_group_async_replication(
project=project_id,
region=location,
disks_stop_group_async_replication_resource_resource=consistency_group,
)
wait_for_extended_operation(operation, "Stopping replication for consistency group")

return True


# </INGREDIENT>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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.
# flake8: noqa


# <REGION compute_consistency_group_stop_replication>
# <IMPORTS/>

# <INGREDIENT wait_for_extended_operation />

# <INGREDIENT stop_replication_in_consistency_group />

# </REGION compute_consistency_group_stop_replication>
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# 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.
# flake8: noqa


# This file is automatically generated. Please do not modify it directly.
# Find the relevant recipe file in the samples/recipes or samples/ingredients
# directory and apply your changes there.


# [START compute_consistency_group_stop_replication]
from __future__ import annotations

import sys
from typing import Any

from google.api_core.extended_operation import ExtendedOperation
from google.cloud import compute_v1


def wait_for_extended_operation(
operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
) -> Any:
"""
Waits for the extended (long-running) operation to complete.
If the operation is successful, it will return its result.
If the operation ends with an error, an exception will be raised.
If there were any warnings during the execution of the operation
they will be printed to sys.stderr.
Args:
operation: a long-running operation you want to wait on.
verbose_name: (optional) a more verbose name of the operation,
used only during error and warning reporting.
timeout: how long (in seconds) to wait for operation to finish.
If None, wait indefinitely.
Returns:
Whatever the operation.result() returns.
Raises:
This method will raise the exception received from `operation.exception()`
or RuntimeError if there is no exception set, but there is an `error_code`
set for the `operation`.
In case of an operation taking longer than `timeout` seconds to complete,
a `concurrent.futures.TimeoutError` will be raised.
"""
result = operation.result(timeout=timeout)

if operation.error_code:
print(
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
file=sys.stderr,
flush=True,
)
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
raise operation.exception() or RuntimeError(operation.error_message)

if operation.warnings:
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
for warning in operation.warnings:
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)

return result


def stop_replication_consistency_group(project_id, location, consistency_group_name):
"""
Stops the asynchronous replication for a consistency group.
Args:
project_id (str): The ID of the Google Cloud project.
location (str): The region where the consistency group is located.
consistency_group_id (str): The ID of the consistency group.
Returns:
bool: True if the replication was successfully stopped.
"""
consistency_group = compute_v1.DisksStopGroupAsyncReplicationResource(
resource_policy=f"regions/{location}/resourcePolicies/{consistency_group_name}"
)
region_client = compute_v1.RegionDisksClient()
operation = region_client.stop_group_async_replication(
project=project_id,
region=location,
disks_stop_group_async_replication_resource_resource=consistency_group,
)
wait_for_extended_operation(operation, "Stopping replication for consistency group")

return True


# [END compute_consistency_group_stop_replication]
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_add_remove_and_list_disks_consistency_group(
consistency_group_name=autodelete_consistency_group.name,
consistency_group_region=REGION,
)
assert disks[0].name == autodelete_regional_blank_disk.name
assert any(disk.name == autodelete_regional_blank_disk.name for disk in disks)
# Remove disk from consistency group
remove_disk_consistency_group(
project_id=PROJECT_ID,
Expand Down
48 changes: 47 additions & 1 deletion compute/client_library/snippets/tests/test_disks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from google.api_core.exceptions import NotFound
import google.auth
from google.cloud import compute_v1, kms_v1

import pytest

from ..disks.attach_disk import attach_disk
Expand Down Expand Up @@ -50,14 +51,16 @@
from ..disks.сonsistency_groups.remove_disk_consistency_group import (
remove_disk_consistency_group,
)
from ..disks.сonsistency_groups.stop_replication_consistency_group import (
stop_replication_consistency_group,
)
from ..images.get import get_image_from_family
from ..instances.create import create_instance, disk_from_image
from ..instances.delete import delete_instance
from ..instances.get import get_instance
from ..snapshots.create import create_snapshot
from ..snapshots.delete import delete_snapshot


PROJECT = google.auth.default()[1]
ZONE = "europe-west2-c"
ZONE_SECONDARY = "europe-west1-c"
Expand Down Expand Up @@ -589,3 +592,46 @@ def test_clone_disks_in_consistency_group(
)
delete_consistency_group(PROJECT, REGION, group_name1)
delete_consistency_group(PROJECT, REGION_SECONDARY, group_name2)


def test_stop_replications_in_consistency_group(
autodelete_regional_blank_disk, autodelete_regional_disk_name
):
group_name = "test-consistency-group" + uuid.uuid4().hex[:5]
create_consistency_group(PROJECT, REGION, group_name, "description")
add_disk_consistency_group(
project_id=PROJECT,
disk_name=autodelete_regional_blank_disk.name,
disk_location=REGION,
consistency_group_name=group_name,
consistency_group_region=REGION,
)
second_disk = create_secondary_region_disk(
autodelete_regional_blank_disk.name,
PROJECT,
REGION,
autodelete_regional_disk_name,
PROJECT,
REGION_SECONDARY,
DISK_SIZE,
)
start_disk_replication(
project_id=PROJECT,
primary_disk_location=REGION,
primary_disk_name=autodelete_regional_blank_disk.name,
secondary_disk_location=REGION_SECONDARY,
secondary_disk_name=second_disk.name,
)
time.sleep(15)
try:
assert stop_replication_consistency_group(PROJECT, REGION, group_name)
finally:
remove_disk_consistency_group(
project_id=PROJECT,
disk_name=autodelete_regional_blank_disk.name,
disk_location=REGION,
consistency_group_name=group_name,
consistency_group_region=REGION,
)
time.sleep(10)
delete_consistency_group(PROJECT, REGION, group_name)

0 comments on commit 1a866be

Please sign in to comment.