Content-Length: 1185936 | pFad | http://github.com/googleapis/python-compute/pull/225/files/18ee9d244f803f4583f44ab12de3afb2f8c0ee3f

EC chore(samples): Snippet Generating Script: part 2 - moving the snippets to new format by m-strzelczyk · Pull Request #225 · googleapis/python-compute · GitHub
Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

chore(samples): Snippet Generating Script: part 2 - moving the snippets to new format #225

Merged
merged 38 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
04ce9cd
chore(samples): Snippet Generating System
m-strzelczyk Jan 19, 2022
9de1e85
🦉 Updates from OwlBot
gcf-owl-bot[bot] Jan 19, 2022
8a45bfc
chore(samples): Making linter ignore the test fixtures.
m-strzelczyk Jan 19, 2022
4a5c4f6
chore(samples): Update region tags that shouldn't be region tags.
m-strzelczyk Jan 26, 2022
f7cfbdd
Merge branch 'main' into sgs-take-2
m-strzelczyk Jan 26, 2022
11ce9da
Merge branch 'main' into sgs-take-2
parthea Feb 8, 2022
680ed26
chore(samples): Syntax fix for Python 3.6
m-strzelczyk Feb 8, 2022
ab722b4
Removing the 3.8 walrus syntax
m-strzelczyk Feb 14, 2022
5d8739a
Fixing test requirements
m-strzelczyk Feb 15, 2022
774fd0b
Merge branch 'main' into sgs-take-2
m-strzelczyk Feb 15, 2022
4bb9668
chore(samples): Trying to make the tests work
m-strzelczyk Feb 17, 2022
39f0bc4
Cleaning up noxfile.
m-strzelczyk Feb 18, 2022
e3f933c
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 18, 2022
48a8696
Removing bonus requirements file as it confuses the system.
m-strzelczyk Feb 18, 2022
78f6bb0
Merge branch 'sgs-take-2' of github.com:googleapis/python-compute int…
m-strzelczyk Feb 18, 2022
2d59bbd
Merge branch 'main' into sgs-take-2
m-strzelczyk Feb 18, 2022
22d63da
One more try to fix tests.
m-strzelczyk Feb 18, 2022
5fb7b21
Changing README to make snippet bot happy
m-strzelczyk Feb 20, 2022
52a8068
Merge branch 'main' into sgs-take-2
m-strzelczyk Feb 22, 2022
3cd9e53
chore(samples): Changing the sampels structure to use SGS
m-strzelczyk Feb 22, 2022
2b35aba
Fixing some tests.
m-strzelczyk Feb 23, 2022
0f1124f
Adding custom hostname files.
m-strzelczyk Feb 24, 2022
a919dd3
Merge branch 'main' into sgs-take-2
m-strzelczyk Feb 25, 2022
4214b9f
Making tests work again.
m-strzelczyk Feb 28, 2022
aa02862
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 28, 2022
835c0da
Making lint happy.
m-strzelczyk Mar 1, 2022
593adc1
Merge branch 'main' into sgs-take-2
m-strzelczyk Mar 1, 2022
6178dc4
Merge branch 'main' into sgs-take-2
m-strzelczyk Mar 3, 2022
b3a0128
Making tests run faster with parallelism.
m-strzelczyk Mar 4, 2022
53b9f8d
Merge branch 'sgs-take-2' of github.com:googleapis/python-compute int…
m-strzelczyk Mar 4, 2022
932c8dd
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Mar 4, 2022
27b16bf
WIP.
m-strzelczyk Mar 4, 2022
c2569c6
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Mar 4, 2022
18ee9d2
Merge branch 'main' into sgs-take-2
m-strzelczyk Mar 4, 2022
e843a27
Merge branch 'main' into sgs-take-2
m-strzelczyk Mar 7, 2022
43a82a2
Apply suggestions from code review
m-strzelczyk Mar 7, 2022
e054440
Applying comments from review.
m-strzelczyk Mar 7, 2022
684463d
Merge branch 'sgs-take-2' of github.com:googleapis/python-compute int…
m-strzelczyk Mar 7, 2022
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
13 changes: 13 additions & 0 deletions samples/__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.
14 changes: 14 additions & 0 deletions samples/ingredients/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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.
# flake8: noqa
49 changes: 49 additions & 0 deletions samples/ingredients/disks/disk_from_snapshot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.
# flake8: noqa# flake8: noqa
from google.cloud import compute_v1


# <INGREDIENT disk_from_snapshot>
def disk_from_snapshot(
disk_type: str, disk_size_gb: int, boot: bool, disk_snapshot: str
) -> compute_v1.AttachedDisk():
"""
Create an AttachedDisk object to be used in VM instance creation. Uses a disk snapshot as the
source for the new disk.

Args:
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
boot: boolean flag indicating whether this disk should be used as a boot disk of an instance
disk_snapshot: disk snapshot to use when creating this disk. You must have read access to this disk.
This value uses the following format: "projects/{project_name}/global/snapshots/{snapshot_name}"

Returns:
AttachedDisk object configured to be created using the specified snapshot.
"""
disk = compute_v1.AttachedDisk()
initialize_params = compute_v1.AttachedDiskInitializeParams()
initialize_params.source_snapshot = disk_snapshot
initialize_params.disk_type = disk_type
initialize_params.disk_size_gb = disk_size_gb
disk.initialize_params = initialize_params
# Remember to set auto_delete to True if you want the disk to be deleted when you delete
# your VM instance.
disk.auto_delete = True
disk.boot = boot
return disk
# </INGREDIENT>
43 changes: 43 additions & 0 deletions samples/ingredients/disks/empty_disk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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.
# flake8: noqa
from google.cloud import compute_v1


# <INGREDIENT empty_disk>
def empty_disk(disk_type: str, disk_size_gb: int) -> compute_v1.AttachedDisk():
"""
Create an AttachedDisk object to be used in VM instance creation. The created disk contains
no data and requires formatting before it can be used.

Args:
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:
AttachedDisk object configured to be created as an empty disk.
"""
disk = compute_v1.AttachedDisk()
initialize_params = compute_v1.AttachedDiskInitializeParams()
initialize_params.disk_type = disk_type
initialize_params.disk_size_gb = disk_size_gb
disk.initialize_params = initialize_params
# Remember to set auto_delete to True if you want the disk to be deleted when you delete
# your VM instance.
disk.auto_delete = True
disk.boot = False
return disk
# </INGREDIENT>
50 changes: 50 additions & 0 deletions samples/ingredients/disks/from_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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.
# flake8: noqa
from google.cloud import compute_v1


# <INGREDIENT disk_from_image>
def disk_from_image(
disk_type: str, disk_size_gb: int, boot: bool, source_image: str
) -> compute_v1.AttachedDisk:
"""
Create an AttachedDisk object to be used in VM instance creation. Uses an image as the
source for the new disk.

Args:
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
boot: boolean flag indicating whether this disk should be used as a boot disk of an instance
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:
AttachedDisk object configured to be created using the specified image.
"""
boot_disk = compute_v1.AttachedDisk()
initialize_params = compute_v1.AttachedDiskInitializeParams()
initialize_params.source_image = source_image
initialize_params.disk_size_gb = disk_size_gb
initialize_params.disk_type = disk_type
boot_disk.initialize_params = initialize_params
# Remember to set auto_delete to True if you want the disk to be deleted when you delete
# your VM instance.
boot_disk.auto_delete = True
boot_disk.boot = boot
return boot_disk
# </INGREDIENT>
65 changes: 65 additions & 0 deletions samples/ingredients/firewall/create.py
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.
# flake8: noqa
from google.cloud import compute_v1


# <INGREDIENT create_firewall_rule>
def create_firewall_rule(
project_id: str, firewall_rule_name: str, network: str = "global/networks/default"
) -> compute_v1.Firewall:
"""
Creates a simple firewall rule allowing for incoming HTTP and HTTPS access from the entire Internet.

Args:
project_id: project ID or project number of the Cloud project you want to use.
firewall_rule_name: name of the rule that is created.
network: name of the network the rule will be applied to. Available name formats:
* https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network}
* projects/{project_id}/global/networks/{network}
* global/networks/{network}
"""
firewall_rule = compute_v1.Firewall()
firewall_rule.name = firewall_rule_name
firewall_rule.direction = "INGRESS"

allowed_ports = compute_v1.Allowed()
allowed_ports.I_p_protocol = "tcp"
allowed_ports.ports = ["80", "443"]

firewall_rule.allowed = [allowed_ports]
firewall_rule.source_ranges = ["0.0.0.0/0"]
firewall_rule.network = network
firewall_rule.description = "Allowing TCP traffic on port 80 and 443 from Internet."

firewall_rule.target_tags = ["web"]

# Note that the default value of priority for the firewall API is 1000.
# If you check the value of `firewall_rule.priority` at this point it
# will be equal to 0, however it is not treated as "set" by the library and thus
# the default will be applied to the new rule. If you want to create a rule that
# has priority == 0, you need to explicitly set it so:

# firewall_rule.priority = 0

firewall_client = compute_v1.FirewallsClient()
op = firewall_client.insert_unary(
project=project_id, firewall_resource=firewall_rule
)

op_client = compute_v1.GlobalOperationsClient()
op_client.wait(project=project_id, operation=op.name)

return firewall_client.get(project=project_id, firewall=firewall_rule_name)
# </INGREDIENT>
35 changes: 35 additions & 0 deletions samples/ingredients/firewall/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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.
# flake8: noqa
from google.cloud import compute_v1


# <INGREDIENT delete_firewall_rule>
def delete_firewall_rule(project_id: str, firewall_rule_name: str):
"""
Deleted a firewall rule from the project.

Args:
project_id: project ID or project number of the Cloud project you want to use.
firewall_rule_name: name of the firewall rule you want to delete.
"""
firewall_client = compute_v1.FirewallsClient()
operation = firewall_client.delete_unary(
project=project_id, firewall=firewall_rule_name
)

operation_client = compute_v1.GlobalOperationsClient()
operation_client.wait(project=project_id, operation=operation.name)
return
# </INGREDIENT>
22 changes: 22 additions & 0 deletions samples/ingredients/firewall/get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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.
# flake8: noqa
from google.cloud import compute_v1


# <INGREDIENT get_firewall_rule>
def get_firewall_rule(project_id: str, firewall_rule_name: str) -> compute_v1.Firewall:
firewall_client = compute_v1.FirewallsClient()
return firewall_client.get(project=project_id, firewall=firewall_rule_name)
# </INGREDIENT>
40 changes: 40 additions & 0 deletions samples/ingredients/firewall/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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.
# flake8: noqa
from typing import Iterable

from google.cloud import compute_v1


# <INGREDIENT list_firewall_rules>
def list_firewall_rules(project_id: str) -> Iterable[compute_v1.Firewall]:
"""
Return a list of all the firewall rules in specified project. Also prints the
list of firewall names and their descriptions.

Args:
project_id: project ID or project number of the Cloud project you want to use.

Returns:
A flat list of all firewall rules defined for given project.
"""
firewall_client = compute_v1.FirewallsClient()
firewalls_list = firewall_client.list(project=project_id)

for firewall in firewalls_list:
print(f" - {firewall.name}: {firewall.description}")

return firewalls_list
# </INGREDIENT>

42 changes: 42 additions & 0 deletions samples/ingredients/firewall/patch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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.
# flake8: noqa
from google.cloud import compute_v1


# <INGREDIENT patch_firewall_priority>
def patch_firewall_priority(project_id: str, firewall_rule_name: str, priority: int):
"""
Modifies the priority of a given firewall rule.

Args:
project_id: project ID or project number of the Cloud project you want to use.
firewall_rule_name: name of the rule you want to modify.
priority: the new priority to be set for the rule.
"""
firewall_rule = compute_v1.Firewall()
firewall_rule.priority = priority

# The patch operation doesn't require the full definition of a Firewall object. It will only update
# the values that were set in it, in this case it will only change the priority.
firewall_client = compute_v1.FirewallsClient()
operation = firewall_client.patch_unary(
project=project_id, firewall=firewall_rule_name, firewall_resource=firewall_rule
)

operation_client = compute_v1.GlobalOperationsClient()
operation_client.wait(project=project_id, operation=operation.name)
return
# </INGREDIENT>

Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/googleapis/python-compute/pull/225/files/18ee9d244f803f4583f44ab12de3afb2f8c0ee3f

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy