Skip to content

Commit

Permalink
chore(docs): Updating firewall samples. (#134)
Browse files Browse the repository at this point in the history
Applying suggested changes.

Closes #133

Co-authored-by: Anthonios Partheniou <[email protected]>
  • Loading branch information
2 people authored and dandhlee committed Nov 16, 2022
1 parent 5e1784e commit 799186f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
18 changes: 10 additions & 8 deletions compute/compute/snippets/sample_firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def list_firewall_rules(project_id: str) -> Iterable:
# [END compute_firewall_list]


def print_firewall_rule(project_id: str, firewall_rule_name: str):
def get_firewall_rule(project_id: str, firewall_rule_name: str) -> compute_v1.Firewall:
firewall_client = compute_v1.FirewallsClient()
print(firewall_client.get(project=project_id, firewall=firewall_rule_name))
return firewall_client.get(project=project_id, firewall=firewall_rule_name)


# [START compute_firewall_create]
Expand All @@ -72,15 +72,17 @@ def create_firewall_rule(
firewall_rule.name = firewall_rule_name
firewall_rule.direction = compute_v1.Firewall.Direction.INGRESS

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

firewall_rule.allowed = [tcp_80_443_allowed]
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
Expand Down Expand Up @@ -164,11 +166,11 @@ def delete_firewall_rule(project_id: str, firewall_rule_name: str):
create_firewall_rule(default_project_id, rule_name)
try:
print("Rule created:")
print_firewall_rule(default_project_id, rule_name)
print(get_firewall_rule(default_project_id, rule_name))
print("Updating rule priority to 10...")
patch_firewall_priority(default_project_id, rule_name, 10)
print("Rule updated: ")
print_firewall_rule(default_project_id, rule_name)
print(get_firewall_rule(default_project_id, rule_name))
print(f"Deleting rule {rule_name}...")
finally:
delete_firewall_rule(default_project_id, rule_name)
Expand Down
14 changes: 9 additions & 5 deletions compute/compute/snippets/test_sample_firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from sample_firewall import (
create_firewall_rule,
delete_firewall_rule,
get_firewall_rule,
list_firewall_rules,
patch_firewall_priority,
)
Expand All @@ -34,13 +35,14 @@ def firewall_rule():
firewall_rule = compute_v1.Firewall()
firewall_rule.name = "firewall-sample-test" + uuid.uuid4().hex[:10]
firewall_rule.direction = compute_v1.Firewall.Direction.INGRESS
tcp_80_443_allowed = compute_v1.Allowed()
tcp_80_443_allowed.I_p_protocol = "tcp"
tcp_80_443_allowed.ports = ["80"]
firewall_rule.allowed = [tcp_80_443_allowed]
allowed_ports = compute_v1.Allowed()
allowed_ports.I_p_protocol = "tcp"
allowed_ports.ports = ["80"]
firewall_rule.allowed = [allowed_ports]
firewall_rule.source_ranges = ["0.0.0.0/0"]
firewall_rule.network = "global/networks/default"
firewall_rule.description = "Rule generated by Python sample test fixture."
firewall_rule.target_tags = ['web']

firewall_client = compute_v1.FirewallsClient()
op = firewall_client.insert(project=PROJECT, firewall_resource=firewall_rule)
Expand All @@ -57,7 +59,9 @@ def firewall_rule():
def test_create_delete():
rule_name = "firewall-sample-test-" + uuid.uuid4().hex[:10]
create_firewall_rule(PROJECT, rule_name)
assert any(rule.name == rule_name for rule in list_firewall_rules(PROJECT))
rule = get_firewall_rule(PROJECT, rule_name)
assert rule.name == rule_name
assert 'web' in rule.target_tags
delete_firewall_rule(PROJECT, rule_name)
assert all(rule.name != rule_name for rule in list_firewall_rules(PROJECT))

Expand Down

0 comments on commit 799186f

Please sign in to comment.