Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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.

# 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


# <INGREDIENT start_disk_replication>
def start_disk_replication(
project_id: str,
primary_disk_location: str,
primary_disk_name: str,
secondary_disk_location: str,
secondary_disk_name: str,
) -> bool:
"""Starts the asynchronous replication of a primary disk to a secondary disk.
Args:
project_id (str): The ID of the Google Cloud project.
primary_disk_location (str): The location of the primary disk, either a zone or a region.
primary_disk_name (str): The name of the primary disk.
secondary_disk_location (str): The location of the secondary disk, either a zone or a region.
secondary_disk_name (str): The name of the secondary disk.
Returns:
bool: True if the replication was successfully started.
"""
# Check if the primary disk location is a region or a zone.
if primary_disk_location[-1].isdigit():
region_client = compute_v1.RegionDisksClient()
request_resource = compute_v1.RegionDisksStartAsyncReplicationRequest(
async_secondary_disk=f"projects/{project_id}/regions/{secondary_disk_location}/disks/{secondary_disk_name}"
)
operation = region_client.start_async_replication(
project=project_id,
region=primary_disk_location,
disk=primary_disk_name,
region_disks_start_async_replication_request_resource=request_resource,
)
else:
client = compute_v1.DisksClient()
request_resource = compute_v1.DisksStartAsyncReplicationRequest(
async_secondary_disk=f"zones/{secondary_disk_location}/disks/{secondary_disk_name}"
)
operation = client.start_async_replication(
project=project_id,
zone=primary_disk_location,
disk=primary_disk_name,
disks_start_async_replication_request_resource=request_resource,
)
wait_for_extended_operation(operation, verbose_name="replication operation")
print(f"Replication for disk {primary_disk_name} started.")
return True


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

# 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


# <INGREDIENT stop_disk_replication>
def stop_disk_replication(
project_id: str, primary_disk_location: str, primary_disk_name: str
) -> bool:
"""
Stops the asynchronous replication of a disk.
Args:
project_id (str): The ID of the Google Cloud project.
primary_disk_location (str): The location of the primary disk, either a zone or a region.
primary_disk_name (str): The name of the primary disk.
Returns:
bool: True if the replication was successfully stopped.
"""
# Check if the primary disk is in a region or a zone
if primary_disk_location[-1].isdigit():
region_client = compute_v1.RegionDisksClient()
operation = region_client.stop_async_replication(
project=project_id, region=primary_disk_location, disk=primary_disk_name
)
else:
zone_client = compute_v1.DisksClient()
operation = zone_client.stop_async_replication(
project=project_id, zone=primary_disk_location, disk=primary_disk_name
)

wait_for_extended_operation(operation, verbose_name="replication operation")
print(f"Replication for disk {primary_disk_name} stopped.")
return True


# </INGREDIENT>
23 changes: 23 additions & 0 deletions compute/client_library/recipes/disks/replication_disk_start.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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_disk_start_replication>
# <IMPORTS/>

# <INGREDIENT wait_for_extended_operation />

# <INGREDIENT start_disk_replication />

# </REGION compute_disk_start_replication>
23 changes: 23 additions & 0 deletions compute/client_library/recipes/disks/replication_disk_stop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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_disk_stop_replication>
# <IMPORTS/>

# <INGREDIENT wait_for_extended_operation />

# <INGREDIENT stop_disk_replication />

# </REGION compute_disk_stop_replication>
125 changes: 125 additions & 0 deletions compute/client_library/snippets/disks/replication_disk_start.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# 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_disk_start_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 start_disk_replication(
project_id: str,
primary_disk_location: str,
primary_disk_name: str,
secondary_disk_location: str,
secondary_disk_name: str,
) -> bool:
"""Starts the asynchronous replication of a primary disk to a secondary disk.
Args:
project_id (str): The ID of the Google Cloud project.
primary_disk_location (str): The location of the primary disk, either a zone or a region.
primary_disk_name (str): The name of the primary disk.
secondary_disk_location (str): The location of the secondary disk, either a zone or a region.
secondary_disk_name (str): The name of the secondary disk.
Returns:
bool: True if the replication was successfully started.
"""
# Check if the primary disk location is a region or a zone.
if primary_disk_location[-1].isdigit():
region_client = compute_v1.RegionDisksClient()
request_resource = compute_v1.RegionDisksStartAsyncReplicationRequest(
async_secondary_disk=f"projects/{project_id}/regions/{secondary_disk_location}/disks/{secondary_disk_name}"
)
operation = region_client.start_async_replication(
project=project_id,
region=primary_disk_location,
disk=primary_disk_name,
region_disks_start_async_replication_request_resource=request_resource,
)
else:
client = compute_v1.DisksClient()
request_resource = compute_v1.DisksStartAsyncReplicationRequest(
async_secondary_disk=f"zones/{secondary_disk_location}/disks/{secondary_disk_name}"
)
operation = client.start_async_replication(
project=project_id,
zone=primary_disk_location,
disk=primary_disk_name,
disks_start_async_replication_request_resource=request_resource,
)
wait_for_extended_operation(operation, verbose_name="replication operation")
print(f"Replication for disk {primary_disk_name} started.")
return True


# [END compute_disk_start_replication]
Loading