Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

chore(docs): Updating firewall samples. #134

Merged
merged 4 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 10 additions & 8 deletions samples/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))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we publish get_firewall_rule? It seems we don't, but we also didn't publish the print method then, so not a regression.

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 samples/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