|
| 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> |
0 commit comments