Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Sep 13, 2023
1 parent 6824cd0 commit 4981cec
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
13 changes: 10 additions & 3 deletions plugins/modules/hcloud_firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,17 @@ def _prepare_result_rule(self, rule):
"description": to_native(rule.description) if rule.description is not None else None,
}

def _prepare_result_applied_to(self, resource: FirewallResource) -> Dict[str, Any]:
def _prepare_result_applied_to(
self,
resource: FirewallResource,
applied_to_resources: bool = True,
) -> Dict[str, Any]:
result = {
"type": resource.type,
"server": to_native(resource.server.id) if resource.server is not None else None,
"label_selector": resource.label_selector.selector if resource.label_selector is not None else None,
}
if resource.applied_to_resources is not None:
if applied_to_resources and resource.applied_to_resources is not None:
result["applied_to_resources"] = [
{
"type": item.type,
Expand Down Expand Up @@ -403,7 +407,10 @@ def _update_firewall(self):
self.hcloud_firewall.set_rules(new_rules)
self._mark_as_changed()

applied_to = [self._prepare_result_applied_to(resource) for resource in self.hcloud_firewall.applied_to]
applied_to = [
self._prepare_result_applied_to(resource, applied_to_resources=False)
for resource in self.hcloud_firewall.applied_to
]

apply_to: Optional[List[Dict[str, Any]]] = self.module.params.get("apply_to")
if apply_to is not None:
Expand Down
33 changes: 29 additions & 4 deletions tests/integration/targets/hcloud_firewall/tasks/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,27 @@
that:
- result is not changed

- name: test update firewall remove_from
- name: test update firewall remove_from with server
hetzner.hcloud.hcloud_firewall:
name: "{{ hcloud_firewall_name }}"
remove_from:
- type: server
server: "{{ test_server.hcloud_server.id }}"
register: result
- name: verify update firewall remove_from
- name: verify update firewall remove_from with server
assert:
that:
- result is changed
- result.hcloud_firewall.applied_to | list | count == 0

- name: test update firewall remove_from idempotence
- name: test update firewall remove_from with server idempotence
hetzner.hcloud.hcloud_firewall:
name: "{{ hcloud_firewall_name }}"
remove_from:
- type: server
server: "{{ test_server.hcloud_server.id }}"
register: result
- name: verify update firewall remove_from idempotence
- name: verify update firewall remove_from with server idempotence
assert:
that:
- result is not changed
Expand Down Expand Up @@ -192,6 +192,31 @@
that:
- result is not changed

- name: test update firewall remove_from with label_selector
hetzner.hcloud.hcloud_firewall:
name: "{{ hcloud_firewall_name }}"
remove_from:
- type: label_selector
label_selector: key=value
register: result
- name: verify update firewall remove_from with label_selector
assert:
that:
- result is changed
- result.hcloud_firewall.applied_to | list | count == 0

- name: test update firewall remove_from with label_selector idempotence
hetzner.hcloud.hcloud_firewall:
name: "{{ hcloud_firewall_name }}"
remove_from:
- type: label_selector
label_selector: key=value
register: result
- name: verify update firewall remove_from with label_selector idempotence
assert:
that:
- result is not changed

- name: test update firewall with check mode
hetzner.hcloud.hcloud_firewall:
id: "{{ firewall.hcloud_firewall.id }}"
Expand Down

0 comments on commit 4981cec

Please sign in to comment.