diff --git a/packages/google-cloud-compute/samples/snippets/sample_firewall.py b/packages/google-cloud-compute/samples/snippets/sample_firewall.py index 01a60c3d8e22..1454932a38c2 100644 --- a/packages/google-cloud-compute/samples/snippets/sample_firewall.py +++ b/packages/google-cloud-compute/samples/snippets/sample_firewall.py @@ -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] @@ -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 @@ -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) diff --git a/packages/google-cloud-compute/samples/snippets/test_sample_firewall.py b/packages/google-cloud-compute/samples/snippets/test_sample_firewall.py index 4e7384648546..9f99bfdbfbbe 100644 --- a/packages/google-cloud-compute/samples/snippets/test_sample_firewall.py +++ b/packages/google-cloud-compute/samples/snippets/test_sample_firewall.py @@ -22,6 +22,7 @@ from sample_firewall import ( create_firewall_rule, delete_firewall_rule, + get_firewall_rule, list_firewall_rules, patch_firewall_priority, ) @@ -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) @@ -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))