diff --git a/lib/ansible/modules/network/netscaler/netscaler_service.py b/lib/ansible/modules/network/netscaler/netscaler_service.py index af7edd9d41f..eea52122b69 100644 --- a/lib/ansible/modules/network/netscaler/netscaler_service.py +++ b/lib/ansible/modules/network/netscaler/netscaler_service.py @@ -356,6 +356,16 @@ options: description: - Weight to assign to the binding between the monitor and service. + disabled: + description: + - When set to C(yes) the service state will be set to DISABLED. + - When set to C(no) the service state will be set to ENABLED. + - >- + Note that due to limitations of the underlying NITRO API a C(disabled) state change alone + does not cause the module result to report a changed status. + type: bool + default: false + extends_documentation_fragment: netscaler requirements: - nitro python sdk @@ -538,6 +548,16 @@ def all_identical(client, module, service_proxy, monitor_bindings_rw_attrs): return service_identical(client, module, service_proxy) and monitor_bindings_identical(client, module, monitor_bindings_rw_attrs) +def do_state_change(client, module, service_proxy): + if module.params['disabled']: + log('Disabling service') + result = service.disable(client, service_proxy.actual) + else: + log('Enabling service') + result = service.enable(client, service_proxy.actual) + return result + + def main(): module_specific_arguments = dict( @@ -675,6 +695,10 @@ def main(): hand_inserted_arguments = dict( monitor_bindings=dict(type='list'), + disabled=dict( + type='bool', + default=False, + ), ) argument_spec = dict() @@ -883,6 +907,12 @@ def main(): else: module_result['changed'] = False + if not module.check_mode: + res = do_state_change(client, module, service_proxy) + if res.errorcode != 0: + msg = 'Error when setting disabled state. errorcode: %s message: %s' % (res.errorcode, res.message) + module.fail_json(msg=msg, **module_result) + # Sanity check for state if not module.check_mode: log('Sanity checks for state present') diff --git a/test/integration/roles/netscaler_service/tests/nitro/flap_disabled.yaml b/test/integration/roles/netscaler_service/tests/nitro/flap_disabled.yaml new file mode 100644 index 00000000000..ea4367c3f39 --- /dev/null +++ b/test/integration/roles/netscaler_service/tests/nitro/flap_disabled.yaml @@ -0,0 +1,10 @@ +--- + +- include: "{{ role_path }}/tests/nitro/flap_disabled/setup.yaml" + vars: + check_mode: no + + +- include: "{{ role_path }}/tests/nitro/flap_disabled/remove.yaml" + vars: + check_mode: no diff --git a/test/integration/roles/netscaler_service/tests/nitro/flap_disabled/remove.yaml b/test/integration/roles/netscaler_service/tests/nitro/flap_disabled/remove.yaml new file mode 100644 index 00000000000..0be82a9041b --- /dev/null +++ b/test/integration/roles/netscaler_service/tests/nitro/flap_disabled/remove.yaml @@ -0,0 +1,16 @@ +--- + +- name: Remove htttp service + netscaler_service: + + nitro_user: "{{nitro_user}}" + nitro_pass: "{{nitro_pass}}" + nsip: "{{nsip}}" + + state: absent + + name: service-http + + delegate_to: localhost + register: result + check_mode: "{{ check_mode }}" diff --git a/test/integration/roles/netscaler_service/tests/nitro/flap_disabled/setup.yaml b/test/integration/roles/netscaler_service/tests/nitro/flap_disabled/setup.yaml new file mode 100644 index 00000000000..50a7446a37f --- /dev/null +++ b/test/integration/roles/netscaler_service/tests/nitro/flap_disabled/setup.yaml @@ -0,0 +1,47 @@ +--- + +- name: Flap service + netscaler_service: + + nitro_user: "{{nitro_user}}" + nitro_pass: "{{nitro_pass}}" + nsip: "{{nsip}}" + + state: present + + name: service-http + ip: 192.168.1.1 + ipaddress: 192.168.1.1 + port: 80 + servicetype: HTTP + + disabled: "{{ item|int % 2 }}" + with_sequence: count=20 + delay: 1 + + delegate_to: localhost + register: result + check_mode: "{{ check_mode }}" + +- name: Flap service + netscaler_service: + + nitro_user: "{{nitro_user}}" + nitro_pass: "{{nitro_pass}}" + nsip: "{{nsip}}" + + state: present + + name: service-http + ip: 192.168.1.1 + ipaddress: 192.168.1.1 + port: 80 + servicetype: HTTP + + disabled: "{{ item|int % 2 }}" + with_sequence: count=20 + delay: 5 + + delegate_to: localhost + register: result + check_mode: "{{ check_mode }}" diff --git a/test/integration/roles/netscaler_service/tests/nitro/http_service/setup.yaml b/test/integration/roles/netscaler_service/tests/nitro/http_service/setup.yaml index 9f58802ac6a..5ba5b82bf53 100644 --- a/test/integration/roles/netscaler_service/tests/nitro/http_service/setup.yaml +++ b/test/integration/roles/netscaler_service/tests/nitro/http_service/setup.yaml @@ -19,7 +19,7 @@ healthmonitor: no maxreq: 200 cacheable: no - cip: ENABLED + cip: enabled cipheader: client-ip usip: yes useproxyport: yes @@ -34,11 +34,11 @@ maxbandwidth: 10000 accessdown: "NO" monthreshold: 100 - downstateflush: ENABLED + downstateflush: enabled hashid: 10 comment: some comment - appflowlog: ENABLED - processlocal: ENABLED + appflowlog: enabled + processlocal: enabled graceful: no monitor_bindings: diff --git a/test/integration/roles/netscaler_service/tests/nitro/http_service/update.yaml b/test/integration/roles/netscaler_service/tests/nitro/http_service/update.yaml index b606423da54..3a5db7ee895 100644 --- a/test/integration/roles/netscaler_service/tests/nitro/http_service/update.yaml +++ b/test/integration/roles/netscaler_service/tests/nitro/http_service/update.yaml @@ -19,7 +19,7 @@ healthmonitor: no maxreq: 200 cacheable: no - cip: ENABLED + cip: enabled cipheader: client-ip usip: yes useproxyport: yes @@ -34,11 +34,11 @@ maxbandwidth: 20000 accessdown: "NO" monthreshold: 100 - downstateflush: ENABLED + downstateflush: enabled hashid: 10 comment: some comment - appflowlog: ENABLED - processlocal: ENABLED + appflowlog: enabled + processlocal: enabled monitor_bindings: - monitorname: http diff --git a/test/units/modules/network/netscaler/test_netscaler_service.py b/test/units/modules/network/netscaler/test_netscaler_service.py index 9b9fb98e052..2921d69ca40 100644 --- a/test/units/modules/network/netscaler/test_netscaler_service.py +++ b/test/units/modules/network/netscaler/test_netscaler_service.py @@ -162,6 +162,7 @@ class TestNetscalerServiceModule(TestModule): 'ansible.modules.network.netscaler.netscaler_service', ConfigProxy=m, service_exists=service_exists_mock, + do_state_change=Mock(return_value=Mock(errorcode=0)), ): self.module = netscaler_service result = self.exited() @@ -190,6 +191,7 @@ class TestNetscalerServiceModule(TestModule): service_identical=service_identical_mock, monitor_bindings_identical=monitor_bindings_identical_mock, all_identical=all_identical_mock, + do_state_change=Mock(return_value=Mock(errorcode=0)), ): self.module = netscaler_service result = self.exited() @@ -220,6 +222,7 @@ class TestNetscalerServiceModule(TestModule): monitor_bindings_identical=monitor_bindings_identical_mock, all_identical=all_identical_mock, sync_monitor_bindings=sync_monitor_bindings_mock, + do_state_change=Mock(return_value=Mock(errorcode=0)), ): self.module = netscaler_service result = self.exited() @@ -247,6 +250,7 @@ class TestNetscalerServiceModule(TestModule): service_exists=service_exists_mock, service_identical=service_identical_mock, monitor_bindings_identical=monitor_bindings_identical_mock, + do_state_change=Mock(return_value=Mock(errorcode=0)), ): self.module = netscaler_service result = self.exited()