Skip to content

Commit 9f398f4

Browse files
m-strzelczykgcf-owl-bot[bot]parthea
authored andcommitted
chore(samples): Snippet Generating Script: part 2 - moving the snippets to new format (#225)
* chore(samples): Snippet Generating System Preparing the SGS (Snippet Generating System) script to handle the burden of duplicated code in the samples. * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore(samples): Making linter ignore the test fixtures. * chore(samples): Update region tags that shouldn't be region tags. * chore(samples): Syntax fix for Python 3.6 * Removing the 3.8 walrus syntax * Fixing test requirements * chore(samples): Trying to make the tests work * Cleaning up noxfile. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Removing bonus requirements file as it confuses the system. * One more try to fix tests. * Changing README to make snippet bot happy * chore(samples): Changing the sampels structure to use SGS * Fixing some tests. * Adding custom hostname files. * Making tests work again. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Making lint happy. * Making tests run faster with parallelism. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * WIP. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Apply suggestions from code review Co-authored-by: Anthonios Partheniou <[email protected]> * Applying comments from review. Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent a42dd69 commit 9f398f4

File tree

198 files changed

+9278
-2124
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

198 files changed

+9278
-2124
lines changed

compute/compute/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2022 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.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2022 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
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2022 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+
from google.cloud import compute_v1
20+
21+
22+
# <INGREDIENT disk_from_snapshot>
23+
def disk_from_snapshot(
24+
disk_type: str, disk_size_gb: int, boot: bool, source_snapshot: str, auto_delete: bool = False
25+
) -> compute_v1.AttachedDisk():
26+
"""
27+
Create an AttachedDisk object to be used in VM instance creation. Uses a disk snapshot as the
28+
source for the new disk.
29+
30+
Args:
31+
disk_type: the type of disk you want to create. This value uses the following format:
32+
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
33+
For example: "zones/us-west3-b/diskTypes/pd-ssd"
34+
disk_size_gb: size of the new disk in gigabytes
35+
boot: boolean flag indicating whether this disk should be used as a boot disk of an instance
36+
source_snapshot: disk snapshot to use when creating this disk. You must have read access to this disk.
37+
This value uses the following format: "projects/{project_name}/global/snapshots/{snapshot_name}"
38+
auto_delete: boolean flag indicating whether this disk should be deleted with the VM that uses it
39+
40+
Returns:
41+
AttachedDisk object configured to be created using the specified snapshot.
42+
"""
43+
disk = compute_v1.AttachedDisk()
44+
initialize_params = compute_v1.AttachedDiskInitializeParams()
45+
initialize_params.source_snapshot = source_snapshot
46+
initialize_params.disk_type = disk_type
47+
initialize_params.disk_size_gb = disk_size_gb
48+
disk.initialize_params = initialize_params
49+
# Remember to set auto_delete to True if you want the disk to be deleted when you delete
50+
# your VM instance.
51+
disk.auto_delete = auto_delete
52+
disk.boot = boot
53+
return disk
54+
# </INGREDIENT>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2022 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+
from google.cloud import compute_v1
20+
21+
22+
# <INGREDIENT empty_disk>
23+
def empty_disk(disk_type: str, disk_size_gb: int, boot: bool = False, auto_delete: bool = False) -> compute_v1.AttachedDisk():
24+
"""
25+
Create an AttachedDisk object to be used in VM instance creation. The created disk contains
26+
no data and requires formatting before it can be used.
27+
28+
Args:
29+
disk_type: the type of disk you want to create. This value uses the following format:
30+
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
31+
For example: "zones/us-west3-b/diskTypes/pd-ssd"
32+
disk_size_gb: size of the new disk in gigabytes
33+
boot: boolean flag indicating whether this disk should be used as a boot disk of an instance
34+
auto_delete: boolean flag indicating whether this disk should be deleted with the VM that uses it
35+
36+
Returns:
37+
AttachedDisk object configured to be created as an empty disk.
38+
"""
39+
disk = compute_v1.AttachedDisk()
40+
initialize_params = compute_v1.AttachedDiskInitializeParams()
41+
initialize_params.disk_type = disk_type
42+
initialize_params.disk_size_gb = disk_size_gb
43+
disk.initialize_params = initialize_params
44+
# Remember to set auto_delete to True if you want the disk to be deleted when you delete
45+
# your VM instance.
46+
disk.auto_delete = True
47+
disk.boot = False
48+
return disk
49+
# </INGREDIENT>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2022 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+
from google.cloud import compute_v1
20+
21+
22+
# <INGREDIENT disk_from_image>
23+
def disk_from_image(
24+
disk_type: str, disk_size_gb: int, boot: bool, source_image: str, auto_delete: bool = False
25+
) -> compute_v1.AttachedDisk:
26+
"""
27+
Create an AttachedDisk object to be used in VM instance creation. Uses an image as the
28+
source for the new disk.
29+
30+
Args:
31+
disk_type: the type of disk you want to create. This value uses the following format:
32+
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
33+
For example: "zones/us-west3-b/diskTypes/pd-ssd"
34+
disk_size_gb: size of the new disk in gigabytes
35+
boot: boolean flag indicating whether this disk should be used as a boot disk of an instance
36+
source_image: source image to use when creating this disk. You must have read access to this disk. This can be one
37+
of the publicly available images or an image from one of your projects.
38+
This value uses the following format: "projects/{project_name}/global/images/{image_name}"
39+
auto_delete: boolean flag indicating whether this disk should be deleted with the VM that uses it
40+
41+
Returns:
42+
AttachedDisk object configured to be created using the specified image.
43+
"""
44+
boot_disk = compute_v1.AttachedDisk()
45+
initialize_params = compute_v1.AttachedDiskInitializeParams()
46+
initialize_params.source_image = source_image
47+
initialize_params.disk_size_gb = disk_size_gb
48+
initialize_params.disk_type = disk_type
49+
boot_disk.initialize_params = initialize_params
50+
# Remember to set auto_delete to True if you want the disk to be deleted when you delete
51+
# your VM instance.
52+
boot_disk.auto_delete = auto_delete
53+
boot_disk.boot = boot
54+
return boot_disk
55+
# </INGREDIENT>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2022 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+
from google.cloud import compute_v1
20+
21+
22+
# <INGREDIENT create_firewall_rule>
23+
def create_firewall_rule(
24+
project_id: str, firewall_rule_name: str, network: str = "global/networks/default"
25+
) -> compute_v1.Firewall:
26+
"""
27+
Creates a simple firewall rule allowing for incoming HTTP and HTTPS access from the entire Internet.
28+
29+
Args:
30+
project_id: project ID or project number of the Cloud project you want to use.
31+
firewall_rule_name: name of the rule that is created.
32+
network: name of the network the rule will be applied to. Available name formats:
33+
* https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network}
34+
* projects/{project_id}/global/networks/{network}
35+
* global/networks/{network}
36+
37+
Returns:
38+
A Firewall object.
39+
"""
40+
firewall_rule = compute_v1.Firewall()
41+
firewall_rule.name = firewall_rule_name
42+
firewall_rule.direction = "INGRESS"
43+
44+
allowed_ports = compute_v1.Allowed()
45+
allowed_ports.I_p_protocol = "tcp"
46+
allowed_ports.ports = ["80", "443"]
47+
48+
firewall_rule.allowed = [allowed_ports]
49+
firewall_rule.source_ranges = ["0.0.0.0/0"]
50+
firewall_rule.network = network
51+
firewall_rule.description = "Allowing TCP traffic on port 80 and 443 from Internet."
52+
53+
firewall_rule.target_tags = ["web"]
54+
55+
# Note that the default value of priority for the firewall API is 1000.
56+
# If you check the value of `firewall_rule.priority` at this point it
57+
# will be equal to 0, however it is not treated as "set" by the library and thus
58+
# the default will be applied to the new rule. If you want to create a rule that
59+
# has priority == 0, you need to explicitly set it so:
60+
# TODO: Uncomment to set the priority to 0
61+
# firewall_rule.priority = 0
62+
63+
firewall_client = compute_v1.FirewallsClient()
64+
op = firewall_client.insert_unary(
65+
project=project_id, firewall_resource=firewall_rule
66+
)
67+
68+
op_client = compute_v1.GlobalOperationsClient()
69+
op_client.wait(project=project_id, operation=op.name)
70+
71+
return firewall_client.get(project=project_id, firewall=firewall_rule_name)
72+
# </INGREDIENT>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2022 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+
from google.cloud import compute_v1
20+
21+
22+
# <INGREDIENT delete_firewall_rule>
23+
def delete_firewall_rule(project_id: str, firewall_rule_name: str) -> None:
24+
"""
25+
Deletes a firewall rule from the project.
26+
27+
Args:
28+
project_id: project ID or project number of the Cloud project you want to use.
29+
firewall_rule_name: name of the firewall rule you want to delete.
30+
"""
31+
firewall_client = compute_v1.FirewallsClient()
32+
operation = firewall_client.delete_unary(
33+
project=project_id, firewall=firewall_rule_name
34+
)
35+
36+
operation_client = compute_v1.GlobalOperationsClient()
37+
operation_client.wait(project=project_id, operation=operation.name)
38+
return
39+
# </INGREDIENT>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2022 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+
from google.cloud import compute_v1
20+
21+
22+
# <INGREDIENT get_firewall_rule>
23+
def get_firewall_rule(project_id: str, firewall_rule_name: str) -> compute_v1.Firewall:
24+
"""
25+
Retrieve a Firewall from a project.
26+
27+
Args:
28+
project_id: project ID or project number of the Cloud project you want to use.
29+
firewall_rule_name: name of the firewall rule you want to retrieve.
30+
31+
Returns:
32+
A Firewall object.
33+
"""
34+
firewall_client = compute_v1.FirewallsClient()
35+
return firewall_client.get(project=project_id, firewall=firewall_rule_name)
36+
# </INGREDIENT>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2022 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+
from typing import Iterable
20+
21+
from google.cloud import compute_v1
22+
23+
24+
# <INGREDIENT list_firewall_rules>
25+
def list_firewall_rules(project_id: str) -> Iterable[compute_v1.Firewall]:
26+
"""
27+
Return a list of all the firewall rules in specified project. Also prints the
28+
list of firewall names and their descriptions.
29+
30+
Args:
31+
project_id: project ID or project number of the Cloud project you want to use.
32+
33+
Returns:
34+
A flat list of all firewall rules defined for given project.
35+
"""
36+
firewall_client = compute_v1.FirewallsClient()
37+
firewalls_list = firewall_client.list(project=project_id)
38+
39+
for firewall in firewalls_list:
40+
print(f" - {firewall.name}: {firewall.description}")
41+
42+
return firewalls_list
43+
# </INGREDIENT>
44+

0 commit comments

Comments
 (0)