Skip to content

migrate code from googleapis/python-compute #8479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 116 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
ca29697
feat: Adding code samples and tests for them (#55)
m-strzelczyk Jun 6, 2021
dc35c09
chore(deps): update dependency google-cloud-compute to v0.4.0 (#62)
renovate-bot Jun 9, 2021
dbb71b9
chore(deps): update dependency google-cloud-compute to v0.4.1 (#68)
renovate-bot Jun 18, 2021
a9f9eb6
feat: adding samples for default values (#64)
m-strzelczyk Jun 29, 2021
f66ff11
chore(deps): update dependency google-cloud-storage to v1.40.0 (#72)
renovate-bot Jul 1, 2021
88b75bc
docs: samples docstring and comments update (#69)
m-strzelczyk Jul 2, 2021
eb57021
chore(deps): update dependency google-cloud-storage to v1.41.0 (#76)
renovate-bot Jul 15, 2021
6ca837d
chore: update templated files
yoshi-automation Jul 21, 2021
df25add
fix: Kokoro uses separate project for concurent tests. (#83)
m-strzelczyk Jul 22, 2021
07cb9e5
chore(deps): update dependency google-cloud-storage to v1.41.1 (#82)
renovate-bot Jul 26, 2021
70529d2
chore(deps): update dependency google-cloud-compute to v0.4.2 (#87)
renovate-bot Jul 27, 2021
7455de4
feat: adding samples to start/stop/reset operations (#75)
m-strzelczyk Aug 4, 2021
41ab432
fix!: uint64/int64 fields accept ints instead of strings (#92)
yoshi-automation Aug 9, 2021
1a633f2
chore(deps): update dependency google-cloud-compute to v0.5.0 (#97)
renovate-bot Aug 9, 2021
3397e21
chore: fix INSTALL_LIBRARY_FROM_SOURCE in noxfile.py (#98)
yoshi-automation Aug 10, 2021
246e562
docs(samples): Adding pagination sample. (#78)
m-strzelczyk Aug 10, 2021
720e9c3
chore(samples): fix LRO wrapper (#99)
FrodoTheTrue Aug 12, 2021
c561363
chore(deps): update dependency google-cloud-storage to v1.42.0 (#103)
renovate-bot Aug 12, 2021
911c203
chore: remove 2.7 from samples noxfile (#106)
yoshi-automation Aug 16, 2021
84664d4
fix: correct region tag formatting (#102)
Xmasreturns Aug 18, 2021
a308ef5
fix(compute): Updated max_results to 100 to avoid too many API calls …
rsamborski Aug 20, 2021
26efae4
chore(deps): update dependency pytest to v6.2.5 (#112)
renovate-bot Aug 31, 2021
20ac508
chore(deps): update dependency google-cloud-storage to v1.42.1 (#119)
renovate-bot Sep 9, 2021
ee88292
chore: blacken samples noxfile template (#121)
yoshi-automation Sep 17, 2021
dc44b6a
chore(deps): update dependency google-cloud-storage to v1.42.2 (#120)
renovate-bot Sep 17, 2021
f9b4818
chore(docs): Adding firewall samples. (#117)
m-strzelczyk Sep 24, 2021
cbf749b
chore(deps): update dependency google-cloud-storage to v1.42.3 (#122)
renovate-bot Oct 1, 2021
6135efc
changes without context (#123)
yoshi-automation Oct 14, 2021
c4fb30a
feat: add support for python 3.10 (#131)
parthea Oct 14, 2021
6fa82a9
chore(docs): Adding samples for GCE instance creation (#130)
m-strzelczyk Oct 19, 2021
282ff66
chore(deps): update dependency google-cloud-compute to v0.6.0 (#135)
renovate-bot Oct 19, 2021
9a26bfd
chore(docs): Securing tests against leaks. (#139)
m-strzelczyk Nov 3, 2021
1a41815
chore(docs): Updating firewall samples. (#134)
m-strzelczyk Nov 8, 2021
683155d
chore(samples): fix region tag import (#142)
FrodoTheTrue Nov 9, 2021
27d95f1
chore(deps): update dependency google-cloud-compute to v0.7.0 (#148)
renovate-bot Nov 9, 2021
900b485
feat: update to the latest version of the code generator (#147)
parthea Nov 16, 2021
e95ba15
chore(deps): update dependency google-cloud-compute to v0.8.0 (#154)
renovate-bot Nov 17, 2021
f2bbb5b
docs(samples): Adding template samples (#136)
m-strzelczyk Nov 17, 2021
aa43047
chore(deps): update dependency google-cloud-storage to v1.43.0 (#157)
renovate-bot Nov 18, 2021
1253c81
docs(samples): Adding samples for instance from template creation (#159)
m-strzelczyk Nov 24, 2021
d0ad6ae
feat: Switch to string enums for compute (#685) (#158)
gcf-owl-bot[bot] Nov 29, 2021
2ce2287
test: Making firewall test tolerant of GCE Enforcer. (#162)
m-strzelczyk Dec 1, 2021
aa6bc6d
docs(samples): Renaming region to avoid name collision with different…
m-strzelczyk Dec 6, 2021
b1e71c7
chore: update to the latest version of the generator (#170)
parthea Dec 13, 2021
8e443aa
chore: release 0.9.0 (#161)
release-please[bot] Dec 14, 2021
3a81c03
chore(deps): update dependency google-cloud-compute to v0.9.0 (#171)
renovate-bot Dec 14, 2021
bf69687
docs(samples): Samples for custom vm types (#155)
m-strzelczyk Dec 14, 2021
5fc56fb
chore(samples): Fixing a small issue with sample region tags (#180)
m-strzelczyk Jan 5, 2022
468d911
chore(samples): Removing some leaked instances. (#179)
m-strzelczyk Jan 10, 2022
46669e4
chore(deps): update dependency google-cloud-storage to v1.44.0 (#182)
renovate-bot Jan 11, 2022
a4d52f4
chore(samples): Add check for tests in directory (#186)
gcf-owl-bot[bot] Jan 11, 2022
072edbe
chore(deps): update all dependencies (#187)
renovate-bot Jan 14, 2022
444c484
chore(python): Noxfile recognizes that tests can live in a folder (#197)
gcf-owl-bot[bot] Jan 19, 2022
a9a7051
chore(samples): Docstring fix (#198)
m-strzelczyk Jan 19, 2022
88e8562
chore(deps): update dependency google-cloud-storage to v2.1.0 (#200)
renovate-bot Jan 22, 2022
4f0a8fa
docs(samples): Adding samples for delete protection (#208)
m-strzelczyk Feb 8, 2022
554b445
chore(samples): Fixing test with deprecated OS image. (#217)
m-strzelczyk Feb 10, 2022
719452e
chore(samples): Samples for preemptible instances (#213)
m-strzelczyk Feb 11, 2022
5f1d9fd
chore(deps): update all dependencies (#215)
renovate-bot Feb 14, 2022
337cb2f
chore(samples): Fixing docstring (#222)
m-strzelczyk Feb 17, 2022
e8a36cc
chore(samples): Snippet Generating System (part 1) (#199)
m-strzelczyk Feb 21, 2022
098ed98
chore(samples): Adding samples for custom hostname (#221)
m-strzelczyk Feb 23, 2022
2625f39
chore(samples): Typo fix (#230)
m-strzelczyk Mar 2, 2022
5b88f85
chore: Adding support for pytest-xdist and pytest-parallel (#235)
gcf-owl-bot[bot] Mar 4, 2022
cdbd9c4
chore(samples): Snippet Generating Script: part 2 - moving the snippe…
m-strzelczyk Mar 8, 2022
48b01d9
chore(deps): update dependency google-cloud-compute to v1.1.0 (#237)
renovate-bot Mar 8, 2022
4476aed
chore(deps): update dependency pytest to v7.1.0 (#238)
renovate-bot Mar 13, 2022
b351db5
chore(deps): update dependency google-cloud-storage to v2.2.0 (#239)
renovate-bot Mar 14, 2022
c02f703
chore(deps): update dependency google-cloud-storage to v2.2.1 (#240)
renovate-bot Mar 16, 2022
82f7b7e
chore(deps): update dependency pytest to v7.1.1 (#241)
renovate-bot Mar 19, 2022
5638466
chore(samples): Making samples required for moving VM instance docs (…
m-strzelczyk Mar 23, 2022
0adb8b6
docs(samples): Add samples for moving VM to different regions/zones (…
m-strzelczyk Mar 26, 2022
ccb1fec
chore(samples): Adding a test to make sure snippets are up-to-date (#…
m-strzelczyk Mar 28, 2022
bfcda19
chore(python): use black==22.3.0 (#249)
gcf-owl-bot[bot] Mar 29, 2022
9730d6c
chore(deps): update dependency google-cloud-compute to v1.2.0 (#257)
renovate-bot Apr 8, 2022
efac50c
docs(samples): Add samples for suspending/resuming an instance (#259)
m-strzelczyk Apr 13, 2022
bff198a
chore(deps): update dependency google-cloud-storage to v2.3.0 (#262)
renovate-bot Apr 13, 2022
de2fa19
chore(python): add nox session to sort python imports (#266)
gcf-owl-bot[bot] Apr 21, 2022
e9c5ce9
docs(samples): Migrate samples to use new type of operations (#264)
m-strzelczyk Apr 21, 2022
ce2e992
chore(deps): update dependency google-cloud-compute to v1.3.0 (#269)
renovate-bot Apr 21, 2022
ce1816e
chore(deps): update dependency pytest to v7.1.2 (#271)
renovate-bot Apr 25, 2022
781e792
docs(samples): Adding samples for image creation and deletion (#270)
m-strzelczyk Apr 26, 2022
d3b238b
docs(samples): Adding samples for image related operations (#277)
m-strzelczyk May 2, 2022
e4077ae
docs(samples): Improving snapshot samples (#276)
m-strzelczyk May 4, 2022
322055e
docs(samples): Adding Windows server samples (#274)
m-strzelczyk May 5, 2022
2a7f7b0
chore(deps): update dependency google-cloud-compute to v1.3.1 (#284)
renovate-bot May 19, 2022
52e192f
chore(docs): Fix a minor typo (#297)
savija-tv Jun 27, 2022
64f3dc5
docs(samples): Adding the missing compute_create_windows_instance_ext…
m-strzelczyk Jul 1, 2022
accc349
docs(samples): Bulk insert sample (#299)
m-strzelczyk Jul 6, 2022
4ec8432
fix: require python 3.7+ (#304)
gcf-owl-bot[bot] Jul 10, 2022
54c352d
docs(samples): Adding samples for Spot VMs (#285)
m-strzelczyk Jul 14, 2022
abc2477
chore(deps): update all dependencies (#293)
renovate-bot Jul 16, 2022
ad92de6
docs(samples): adding sample for local SSD disk (#294)
m-strzelczyk Jul 18, 2022
435fa35
chore(tests): Request id test (#298)
m-strzelczyk Jul 19, 2022
ab5e257
chore(deps): update all dependencies (#310)
renovate-bot Aug 4, 2022
792cc4a
chore(deps): update all dependencies (#313)
renovate-bot Aug 4, 2022
affdebc
docs(samples): Various ways of creating a new disk (#308)
m-strzelczyk Aug 10, 2022
f99074e
chore(deps): update all dependencies to v1.5.0 (#317)
renovate-bot Aug 12, 2022
7b0bcb8
chore(deps): update all dependencies (#324)
renovate-bot Aug 19, 2022
e000859
chore(deps): update dependency black to v22.8.0 (#331)
renovate-bot Sep 1, 2022
fc767a9
chore(deps): update dependency pytest to v7.1.3 (#334)
renovate-bot Sep 6, 2022
239ab99
chore(deps): update dependency google-cloud-compute to v1.5.2 (#339)
renovate-bot Sep 13, 2022
a01f135
chore: detect samples tests in nested directories (#343)
gcf-owl-bot[bot] Sep 13, 2022
8a35f22
chore(deps): update all dependencies (#348)
renovate-bot Oct 4, 2022
7e0f35b
docs(samples): Fixing an issue with listing all instances (#349)
m-strzelczyk Oct 5, 2022
fc3390b
chore(deps): update dependency black to v22.10.0 (#352)
renovate-bot Oct 8, 2022
7d4074a
chore(deps): update all dependencies (#353)
renovate-bot Oct 17, 2022
4290d6b
chore(deps): update dependency pytest to v7.2.0 (#357)
renovate-bot Oct 26, 2022
4862cfd
Merge remote-tracking branch 'migration/main' into python-compute-mig…
m-strzelczyk Nov 9, 2022
b115165
Removing noxfile.py
m-strzelczyk Nov 9, 2022
bb7571b
Fixing license header
m-strzelczyk Nov 9, 2022
93eb1a7
Fixing a test
m-strzelczyk Nov 9, 2022
74a8198
Fixing lint
m-strzelczyk Nov 10, 2022
10fb8e0
Merge remote-tracking branch 'origin/main' into python-compute-migration
m-strzelczyk Nov 10, 2022
a95ec4e
Renaming compute to client_library
m-strzelczyk Nov 15, 2022
1dbbd07
Merge remote-tracking branch 'origin/main' into python-compute-migration
m-strzelczyk Nov 15, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
53 changes: 53 additions & 0 deletions compute/client_library/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Code samples for the Compute Engine library

In this folder you can find the source code for the code samples used throughout the
[public documentation](https://cloud.google.com/compute/docs/) of Google Compute Engine.

The samples can be found in the `snippets` folder, where they are organized to mimic the
structure of the public documentation. Files that are saved there are generated by the `sgs.py`
script from pieces found in `ingredients` and `recipes`. This way, one piece of code can be easily
included in multiple snippets and updating the code requires less work.

## Working with the SGS

SGS (Snippet Generating System) works by scanning the `recipes` folder, finding all files
and filling them with pieces of code found in `ingredients`. The folder structure of `recipes` is
reconstructed in the `snippets` folder.

### Adding new sample

To create a new sample, just prepare a new file in one of the `recipes` subfolders. The SGS will pick it up
automatically when you run it, by executing `python3 sgs.py generate` in this (`samples/`) directory.

### Removing/moving a sample

To remove or move a sample, you need to simply modify the `recipes` folder to match your desired structure, then delete
the generated snippet from the `snippets` directory. The SGS script will create the snippet in the new location next
time you run `python3 sgs.py generate`.

### Interacting with GIT

SGS will not interact with Git repository in any way. All changes made by the script need to be committed manually -
preferably in the same commit as the update to the source files.

## Preparing an ingredient
To add a new ingredient, create a new `.py` file with the code you want to later use in the snippets. Mark the beginning
of the code you want to include with `# <INGREDIENT name>` and the end with `# </INGREDIENT>`.

Please leave the imports required by this ingredient **OUTSIDE** the area marked with ingredient comments. The SGS
script will automatically collect all the required imports and put them in the final snippet in the right place and in
right order.

## Preparing a recipe
Each recipe is a file located in the `recipes` folder. It should have the `.py` extension and should be a valid Python
script. Each recipe has to have an `# <IMPORTS/>` line and at least one `# <INGREDIENT name/>` line.
Apart from those restrictions, the contents of the file can be whatever you want.

The SGS will copy the recipe file to the destination folder in `snippets` and replace the `# <IMPORTS/>` and
`# <INGREDIENT name/>` lines with the `import` statements required by the used ingredients and with the ingredient
body.

### Regions
You should use `# <REGION region_name>` and `# </REGION region_name>` lines to indicate where start and end
of a region should be placed in the generated snippet. Those lines will be simply replaced with the proper
`START region_name` and `END region_name` lines.
13 changes: 13 additions & 0 deletions compute/client_library/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2022 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.
18 changes: 18 additions & 0 deletions compute/client_library/ingredients/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2022 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
53 changes: 53 additions & 0 deletions compute/client_library/ingredients/disks/autodelete_change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2022 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
import sys
from typing import NoReturn


from google.cloud import compute_v1


# <INGREDIENT set_disk_autodelete>
def set_disk_autodelete(project_id: str, zone: str, instance_name: str, disk_name: str, autodelete: bool) -> NoReturn:
"""
Set the autodelete flag of a disk to given value.

Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone in which is the disk you want to modify.
instance_name: name of the instance the disk is attached to.
disk_name: the name of the disk which flag you want to modify.
autodelete: the new value of the autodelete flag.
"""
instance_client = compute_v1.InstancesClient()
instance = instance_client.get(project=project_id, zone=zone, instance=instance_name)

for disk in instance.disks:
if disk.device_name == disk_name:
break
else:
raise RuntimeError(f"Instance {instance_name} doesn't have a disk named {disk_name} attached.")

disk.auto_delete = autodelete

operation = instance_client.update(project=project_id, zone=zone, instance=instance_name, instance_resource=instance)

wait_for_extended_operation(operation, "disk update")
return
# </INGREDIENT>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2022 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_disk_from_customer_encrypted_disk>
def create_disk_from_customer_encrypted_disk(
project_id: str, zone: str, disk_name: str, disk_type: str,
disk_size_gb: int, disk_link: str,
encryption_key: bytes) -> compute_v1.Disk:
"""
Creates a zonal non-boot persistent disk in a project with the copy of data from an existing disk.

The encryption key must be the same for the source disk and the new disk.

Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone in which you want to create the disk.
disk_name: name of the disk you want to create.
disk_type: the type of disk you want to create. This value uses the following format:
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
For example: "zones/us-west3-b/diskTypes/pd-ssd"
disk_size_gb: size of the new disk in gigabytes
disk_link: a link to the disk you want to use as a source for the new disk.
This value uses the following format: "projects/{project_name}/zones/{zone}/disks/{disk_name}"
encryption_key: customer-supplied encryption key used for encrypting
data in the source disk. The data will be encrypted with the same key
in the new disk.

Returns:
An attachable copy of an existing disk.
"""
disk_client = compute_v1.DisksClient()
disk = compute_v1.Disk()
disk.zone = zone
disk.size_gb = disk_size_gb
disk.source_disk = disk_link
disk.type_ = disk_type
disk.name = disk_name
disk.disk_encryption_key = compute_v1.CustomerEncryptionKey()
disk.disk_encryption_key.raw_key = encryption_key
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)

wait_for_extended_operation(operation, "disk creation")

return disk_client.get(project=project_id, zone=zone, disk=disk_name)
# </INGREDIENT>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2022 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_disk_from_kms_encrypted_disk>
def create_disk_from_kms_encrypted_disk(
project_id: str, zone: str, disk_name: str, disk_type: str,
disk_size_gb: int, disk_link: str,
kms_key_name: str) -> compute_v1.Disk:
"""
Creates a zonal non-boot disk in a project with the copy of data from an existing disk.

The encryption key must be the same for the source disk and the new disk.

Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone in which you want to create the disk.
disk_name: name of the disk you want to create.
disk_type: the type of disk you want to create. This value uses the following format:
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
For example: "zones/us-west3-b/diskTypes/pd-ssd"
disk_size_gb: size of the new disk in gigabytes
disk_link: a link to the disk you want to use as a source for the new disk.
This value uses the following format: "projects/{project_name}/zones/{zone}/disks/{disk_name}"
kms_key_name: URL of the key from KMS. The key might be from another project, as
long as you have access to it. The data will be encrypted with the same key
in the new disk. This value uses following format:
"projects/{kms_project_id}/locations/{region}/keyRings/{key_ring}/cryptoKeys/{key}"

Returns:
An attachable copy of an existing disk.
"""
disk_client = compute_v1.DisksClient()
disk = compute_v1.Disk()
disk.zone = zone
disk.size_gb = disk_size_gb
disk.source_disk = disk_link
disk.type_ = disk_type
disk.name = disk_name
disk.disk_encryption_key = compute_v1.CustomerEncryptionKey()
disk.disk_encryption_key.kms_key_name = kms_key_name
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)

wait_for_extended_operation(operation, "disk creation")

return disk_client.get(project=project_id, zone=zone, disk=disk_name)
# </INGREDIENT>
55 changes: 55 additions & 0 deletions compute/client_library/ingredients/disks/create_empty_disk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2022 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
import sys

from google.cloud import compute_v1


# <INGREDIENT create_empty_disk>
def create_empty_disk(
project_id: str, zone: str, disk_name: str, disk_type: str, disk_size_gb: int
) -> compute_v1.Disk:
"""
Creates a new empty disk in a project in given zone.

Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone in which you want to create the disk.
disk_name: name of the disk you want to create.
disk_type: the type of disk you want to create. This value uses the following format:
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
For example: "zones/us-west3-b/diskTypes/pd-ssd"
disk_size_gb: size of the new disk in gigabytes

Returns:
An unattached Disk instance.
"""
disk = compute_v1.Disk()
disk.size_gb = disk_size_gb
disk.name = disk_name
disk.zone = zone
disk.type_ = disk_type

disk_client = compute_v1.DisksClient()
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)

wait_for_extended_operation(operation, "disk creation")

return disk_client.get(project=project_id, zone=zone, disk=disk.name)
# </INGREDIENT>
59 changes: 59 additions & 0 deletions compute/client_library/ingredients/disks/create_from_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2022 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
import sys

from google.cloud import compute_v1


# <INGREDIENT create_disk_from_image>
def create_disk_from_image(
project_id: str, zone: str, disk_name: str, disk_type: str, disk_size_gb: int, source_image: str
) -> compute_v1.Disk:
"""
Creates a new disk in a project in given zone using an image as base.

Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone in which you want to create the disk.
disk_name: name of the disk you want to create.
disk_type: the type of disk you want to create. This value uses the following format:
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
For example: "zones/us-west3-b/diskTypes/pd-ssd"
disk_size_gb: size of the new disk in gigabytes
source_image: source image to use when creating this disk. You must have read access to this disk. This
can be one of the publicly available images or an image from one of your projects.
This value uses the following format: "projects/{project_name}/global/images/{image_name}"

Returns:
An unattached Disk instance.
"""
disk = compute_v1.Disk()
disk.size_gb = disk_size_gb
disk.name = disk_name
disk.zone = zone
disk.type_ = disk_type
disk.source_image = source_image

disk_client = compute_v1.DisksClient()
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)

wait_for_extended_operation(operation, "disk creation")

return disk_client.get(project=project_id, zone=zone, disk=disk.name)
# </INGREDIENT>
Loading