Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azure_rm_securitygroup - idempotent when args are lists #507

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 8 additions & 6 deletions plugins/modules/azure_rm_securitygroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ def compare_rules_change(old_list, new_list, purge_list):


def compare_rules(old_rule, rule):
def compare_list_rule(old_rule, rule, key):
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be even better if the compare_list_rule function were defined outside compare_rules. Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was following the similar example here:

def check_plural(src, dest):
. I can change, but guess it should be a static method as it doesn't need anything from the class (unless you're ignoring linter warnings about static methods?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes, I will confirm with developer! Thank you very much!

return set(map(str, rule.get(key) or [])) != set(map(str, old_rule.get(key) or []))
changed = False
if old_rule['name'] != rule['name']:
changed = True
Expand All @@ -496,17 +498,17 @@ def compare_rules(old_rule, rule):
changed = True
if str(rule['destination_address_prefix']) != str(old_rule['destination_address_prefix']):
changed = True
if set(rule.get('source_address_prefixes') or []) != set(old_rule.get('source_address_prefixes') or []):
if compare_list_rule(old_rule, rule, 'source_address_prefixes'):
changed = True
if set(rule.get('destination_address_prefixes') or []) != set(old_rule.get('destination_address_prefixes') or []):
if compare_list_rule(old_rule, rule, 'destination_address_prefixes'):
changed = True
if set(rule.get('source_port_ranges') or []) != set(old_rule.get('source_port_ranges') or []):
if compare_list_rule(old_rule, rule, 'source_port_ranges'):
changed = True
if set(rule.get('destination_port_ranges') or []) != set(old_rule.get('destination_port_ranges') or []):
if compare_list_rule(old_rule, rule, 'destination_port_ranges'):
changed = True
if set(rule.get('source_application_security_groups') or []) != set(old_rule.get('source_application_security_groups') or []):
if compare_list_rule(old_rule, rule, 'source_application_security_groups'):
changed = True
if set(rule.get('destination_application_security_groups') or []) != set(old_rule.get('destination_application_security_groups') or []):
if compare_list_rule(old_rule, rule, 'destination_application_security_groups'):
changed = True
return changed

Expand Down
20 changes: 17 additions & 3 deletions tests/integration/targets/azure_rm_securitygroup/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,18 @@
source_address_prefix: 174.109.158.0/24
destination_port_range: 22-23
priority: 102
- name: AllowHTTPandHTTPS
protocol: Tcp
source_address_prefix: 174.109.158.0/24
destination_port_range:
- 80
- 443
priority: 103
register: output

- assert:
- assert:
that:
- "{{ output.state.rules | length }} == 3"
- "{{ output.state.rules | length }} == 4"
- output.state.rules[0].source_address_prefix == '174.108.158.0/24'

- name: Test idempotence
Expand All @@ -84,6 +91,13 @@
source_address_prefix: 174.109.158.0/24
destination_port_range: 22-23
priority: 102
- name: AllowHTTPandHTTPS
protocol: Tcp
source_address_prefix: 174.109.158.0/24
destination_port_range:
- 80
- 443
priority: 103
register: output

- assert:
Expand Down Expand Up @@ -214,7 +228,7 @@
register: output

- assert:
that:
that:
- output.changed
- "{{ output.state.rules | length }} == 2"

Expand Down