Skip to content

Commit 1a41815

Browse files
chore(docs): Updating firewall samples. (#134)
Applying suggested changes. Closes #133 Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent 9a26bfd commit 1a41815

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

compute/compute/snippets/sample_firewall.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ def list_firewall_rules(project_id: str) -> Iterable:
4848
# [END compute_firewall_list]
4949

5050

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

5555

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

75-
tcp_80_443_allowed = compute_v1.Allowed()
76-
tcp_80_443_allowed.I_p_protocol = "tcp"
77-
tcp_80_443_allowed.ports = ["80", "443"]
75+
allowed_ports = compute_v1.Allowed()
76+
allowed_ports.I_p_protocol = "tcp"
77+
allowed_ports.ports = ["80", "443"]
7878

79-
firewall_rule.allowed = [tcp_80_443_allowed]
79+
firewall_rule.allowed = [allowed_ports]
8080
firewall_rule.source_ranges = ["0.0.0.0/0"]
8181
firewall_rule.network = network
8282
firewall_rule.description = "Allowing TCP traffic on port 80 and 443 from Internet."
8383

84+
firewall_rule.target_tags = ['web']
85+
8486
# Note that the default value of priority for the firewall API is 1000.
8587
# If you check the value of `firewall_rule.priority` at this point it
8688
# will be equal to 0, however it is not treated as "set" by the library and thus
@@ -164,11 +166,11 @@ def delete_firewall_rule(project_id: str, firewall_rule_name: str):
164166
create_firewall_rule(default_project_id, rule_name)
165167
try:
166168
print("Rule created:")
167-
print_firewall_rule(default_project_id, rule_name)
169+
print(get_firewall_rule(default_project_id, rule_name))
168170
print("Updating rule priority to 10...")
169171
patch_firewall_priority(default_project_id, rule_name, 10)
170172
print("Rule updated: ")
171-
print_firewall_rule(default_project_id, rule_name)
173+
print(get_firewall_rule(default_project_id, rule_name))
172174
print(f"Deleting rule {rule_name}...")
173175
finally:
174176
delete_firewall_rule(default_project_id, rule_name)

compute/compute/snippets/test_sample_firewall.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from sample_firewall import (
2323
create_firewall_rule,
2424
delete_firewall_rule,
25+
get_firewall_rule,
2526
list_firewall_rules,
2627
patch_firewall_priority,
2728
)
@@ -34,13 +35,14 @@ def firewall_rule():
3435
firewall_rule = compute_v1.Firewall()
3536
firewall_rule.name = "firewall-sample-test" + uuid.uuid4().hex[:10]
3637
firewall_rule.direction = compute_v1.Firewall.Direction.INGRESS
37-
tcp_80_443_allowed = compute_v1.Allowed()
38-
tcp_80_443_allowed.I_p_protocol = "tcp"
39-
tcp_80_443_allowed.ports = ["80"]
40-
firewall_rule.allowed = [tcp_80_443_allowed]
38+
allowed_ports = compute_v1.Allowed()
39+
allowed_ports.I_p_protocol = "tcp"
40+
allowed_ports.ports = ["80"]
41+
firewall_rule.allowed = [allowed_ports]
4142
firewall_rule.source_ranges = ["0.0.0.0/0"]
4243
firewall_rule.network = "global/networks/default"
4344
firewall_rule.description = "Rule generated by Python sample test fixture."
45+
firewall_rule.target_tags = ['web']
4446

4547
firewall_client = compute_v1.FirewallsClient()
4648
op = firewall_client.insert(project=PROJECT, firewall_resource=firewall_rule)
@@ -57,7 +59,9 @@ def firewall_rule():
5759
def test_create_delete():
5860
rule_name = "firewall-sample-test-" + uuid.uuid4().hex[:10]
5961
create_firewall_rule(PROJECT, rule_name)
60-
assert any(rule.name == rule_name for rule in list_firewall_rules(PROJECT))
62+
rule = get_firewall_rule(PROJECT, rule_name)
63+
assert rule.name == rule_name
64+
assert 'web' in rule.target_tags
6165
delete_firewall_rule(PROJECT, rule_name)
6266
assert all(rule.name != rule_name for rule in list_firewall_rules(PROJECT))
6367

0 commit comments

Comments
 (0)