This repository was archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
chore(samples): Snippet Generating Script: part 2 - moving the snippets to new format #225
Merged
Merged
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 9de1e85
🦉 Updates from OwlBot
gcf-owl-bot[bot] 8a45bfc
chore(samples): Making linter ignore the test fixtures.
m-strzelczyk 4a5c4f6
chore(samples): Update region tags that shouldn't be region tags.
m-strzelczyk f7cfbdd
Merge branch 'main' into sgs-take-2
m-strzelczyk 11ce9da
Merge branch 'main' into sgs-take-2
parthea 680ed26
chore(samples): Syntax fix for Python 3.6
m-strzelczyk ab722b4
Removing the 3.8 walrus syntax
m-strzelczyk 5d8739a
Fixing test requirements
m-strzelczyk 774fd0b
Merge branch 'main' into sgs-take-2
m-strzelczyk 4bb9668
chore(samples): Trying to make the tests work
m-strzelczyk 39f0bc4
Cleaning up noxfile.
m-strzelczyk e3f933c
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 48a8696
Removing bonus requirements file as it confuses the system.
m-strzelczyk 78f6bb0
Merge branch 'sgs-take-2' of github.com:googleapis/python-compute int…
m-strzelczyk 2d59bbd
Merge branch 'main' into sgs-take-2
m-strzelczyk 22d63da
One more try to fix tests.
m-strzelczyk 5fb7b21
Changing README to make snippet bot happy
m-strzelczyk 52a8068
Merge branch 'main' into sgs-take-2
m-strzelczyk 3cd9e53
chore(samples): Changing the sampels structure to use SGS
m-strzelczyk 2b35aba
Fixing some tests.
m-strzelczyk 0f1124f
Adding custom hostname files.
m-strzelczyk a919dd3
Merge branch 'main' into sgs-take-2
m-strzelczyk 4214b9f
Making tests work again.
m-strzelczyk aa02862
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 835c0da
Making lint happy.
m-strzelczyk 593adc1
Merge branch 'main' into sgs-take-2
m-strzelczyk 6178dc4
Merge branch 'main' into sgs-take-2
m-strzelczyk b3a0128
Making tests run faster with parallelism.
m-strzelczyk 53b9f8d
Merge branch 'sgs-take-2' of github.com:googleapis/python-compute int…
m-strzelczyk 932c8dd
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 27b16bf
WIP.
m-strzelczyk c2569c6
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 18ee9d2
Merge branch 'main' into sgs-take-2
m-strzelczyk e843a27
Merge branch 'main' into sgs-take-2
m-strzelczyk 43a82a2
Apply suggestions from code review
m-strzelczyk e054440
Applying comments from review.
m-strzelczyk 684463d
Merge branch 'sgs-take-2' of github.com:googleapis/python-compute int…
m-strzelczyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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. |
This file contains hidden or 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,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 |
This file contains hidden or 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,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 | ||
m-strzelczyk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> 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> |
This file contains hidden or 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,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 | ||
m-strzelczyk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return disk | ||
# </INGREDIENT> |
This file contains hidden or 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,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> |
This file contains hidden or 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,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> |
This file contains hidden or 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,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. | ||
m-strzelczyk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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> |
This file contains hidden or 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,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: | ||
m-strzelczyk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
firewall_client = compute_v1.FirewallsClient() | ||
return firewall_client.get(project=project_id, firewall=firewall_rule_name) | ||
# </INGREDIENT> |
This file contains hidden or 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,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> | ||
|
This file contains hidden or 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,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. | ||
m-strzelczyk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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> | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.