-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/main' into consistency-group-stop-replication
# Conflicts: # compute/client_library/snippets/tests/test_disks.py
- Loading branch information
Showing
44 changed files
with
533 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# pin pytest to 4.6.11 for Python2. | ||
pytest==4.6.11; python_version < '3.0' | ||
pytest==8.3.4; python_version >= '3.0' | ||
six==1.17.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Flask==1.1.4; python_version < '3.0' | ||
Flask==3.0.0; python_version > '3.0' | ||
Werkzeug==1.0.1; python_version < '3.0' | ||
Werkzeug==3.0.3; python_version > '3.0' | ||
Werkzeug==3.0.6; python_version > '3.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
google-cloud-bigquery==3.25.0 | ||
google-cloud-bigquery==3.27.0 | ||
google-cloud-pubsub==2.21.5 | ||
pytest==8.2.0 | ||
mock==5.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
google-cloud-bigquery-migration==0.11.9 | ||
google-cloud-bigquery-migration==0.11.11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
google-cloud-build==3.16.0 | ||
google-cloud-build==3.27.1 | ||
google-auth==2.19.1 |
60 changes: 60 additions & 0 deletions
60
compute/client_library/ingredients/disks/create_replicated_disk.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# 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 create_replicated_disk> | ||
def create_regional_replicated_disk( | ||
project_id, | ||
region, | ||
disk_name, | ||
size_gb, | ||
disk_type: str = "pd-ssd", | ||
) -> compute_v1.Disk: | ||
"""Creates a synchronously replicated disk in a region across two zones. | ||
Args: | ||
project_id (str): The ID of the Google Cloud project. | ||
region (str): The region where the disk will be created. | ||
disk_name (str): The name of the disk. | ||
size_gb (int): The size of the disk in gigabytes. | ||
disk_type (str): The type of the disk. Default is 'pd-ssd'. | ||
Returns: | ||
compute_v1.Disk: The created disk object. | ||
""" | ||
disk = compute_v1.Disk() | ||
disk.name = disk_name | ||
|
||
# You can specify the zones where the disk will be replicated. | ||
disk.replica_zones = [ | ||
f"zones/{region}-a", | ||
f"zones/{region}-b", | ||
] | ||
disk.size_gb = size_gb | ||
disk.type = f"regions/{region}/diskTypes/{disk_type}" | ||
|
||
client = compute_v1.RegionDisksClient() | ||
operation = client.insert(project=project_id, region=region, disk_resource=disk) | ||
|
||
wait_for_extended_operation(operation, "Replicated disk creation") | ||
|
||
return client.get(project=project_id, region=region, disk=disk_name) | ||
|
||
|
||
# </INGREDIENT> |
52 changes: 52 additions & 0 deletions
52
compute/client_library/ingredients/disks/сonsistency_groups/clone_disks_consistency_group.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# 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 consistency_group_clone_disks> | ||
def clone_disks_to_consistency_group(project_id, group_region, group_name): | ||
""" | ||
Clones disks to a consistency group in the specified region. | ||
Args: | ||
project_id (str): The ID of the Google Cloud project. | ||
group_region (str): The region where the consistency group is located. | ||
group_name (str): The name of the consistency group. | ||
Returns: | ||
bool: True if the disks were successfully cloned to the consistency group. | ||
""" | ||
consistency_group_policy = ( | ||
f"projects/{project_id}/regions/{group_region}/resourcePolicies/{group_name}" | ||
) | ||
|
||
resource = compute_v1.BulkInsertDiskResource( | ||
source_consistency_group_policy=consistency_group_policy | ||
) | ||
client = compute_v1.RegionDisksClient() | ||
request = compute_v1.BulkInsertRegionDiskRequest( | ||
project=project_id, | ||
region=group_region, | ||
bulk_insert_disk_resource_resource=resource, | ||
) | ||
operation = client.bulk_insert(request=request) | ||
wait_for_extended_operation(operation, verbose_name="bulk insert disk") | ||
return True | ||
|
||
|
||
# </INGREDIENT> |
23 changes: 23 additions & 0 deletions
23
compute/client_library/recipes/disks/create_replicated_disk.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_regional_replicated> | ||
# <IMPORTS/> | ||
|
||
# <INGREDIENT wait_for_extended_operation /> | ||
|
||
# <INGREDIENT create_replicated_disk /> | ||
|
||
# </REGION compute_disk_regional_replicated> |
23 changes: 23 additions & 0 deletions
23
compute/client_library/recipes/disks/сonsistency_groups/clone_disks_consistency_group.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_consistency_group_clone> | ||
# <IMPORTS/> | ||
|
||
# <INGREDIENT wait_for_extended_operation /> | ||
|
||
# <INGREDIENT consistency_group_clone_disks /> | ||
|
||
# </REGION compute_consistency_group_clone> |
116 changes: 116 additions & 0 deletions
116
compute/client_library/snippets/disks/create_replicated_disk.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# 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_regional_replicated] | ||
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 create_regional_replicated_disk( | ||
project_id, | ||
region, | ||
disk_name, | ||
size_gb, | ||
disk_type: str = "pd-ssd", | ||
) -> compute_v1.Disk: | ||
"""Creates a synchronously replicated disk in a region across two zones. | ||
Args: | ||
project_id (str): The ID of the Google Cloud project. | ||
region (str): The region where the disk will be created. | ||
disk_name (str): The name of the disk. | ||
size_gb (int): The size of the disk in gigabytes. | ||
disk_type (str): The type of the disk. Default is 'pd-ssd'. | ||
Returns: | ||
compute_v1.Disk: The created disk object. | ||
""" | ||
disk = compute_v1.Disk() | ||
disk.name = disk_name | ||
|
||
# You can specify the zones where the disk will be replicated. | ||
disk.replica_zones = [ | ||
f"zones/{region}-a", | ||
f"zones/{region}-b", | ||
] | ||
disk.size_gb = size_gb | ||
disk.type = f"regions/{region}/diskTypes/{disk_type}" | ||
|
||
client = compute_v1.RegionDisksClient() | ||
operation = client.insert(project=project_id, region=region, disk_resource=disk) | ||
|
||
wait_for_extended_operation(operation, "Replicated disk creation") | ||
|
||
return client.get(project=project_id, region=region, disk=disk_name) | ||
|
||
|
||
# [END compute_disk_regional_replicated] |
Oops, something went wrong.