diff --git a/changelogs/fragments/486-delete-attribute-logging-server.yaml b/changelogs/fragments/486-delete-attribute-logging-server.yaml new file mode 100644 index 000000000..625146b2e --- /dev/null +++ b/changelogs/fragments/486-delete-attribute-logging-server.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - sonic_logging - Adding the ability to delete a specific attribute of a logging server into the logging module (https://github.com/ansible-collections/dellemc.enterprise_sonic/pull/486). \ No newline at end of file diff --git a/plugins/module_utils/network/sonic/argspec/logging/logging.py b/plugins/module_utils/network/sonic/argspec/logging/logging.py index c34ee11f6..305d8ebdc 100644 --- a/plugins/module_utils/network/sonic/argspec/logging/logging.py +++ b/plugins/module_utils/network/sonic/argspec/logging/logging.py @@ -45,15 +45,15 @@ def __init__(self, **kwargs): 'options': { 'host': {'required': True, 'type': 'str'}, + 'remote_port': {'type': 'int'}, 'message_type': {'choices': ['log', 'event', 'audit', 'auditd-system'], 'type': 'str'}, 'severity': {'choices': ['debug', 'info', 'notice', 'warning', 'error', 'critical', 'alert', 'emergency'], 'type': 'str'}, - 'remote_port': {'type': 'int'}, - 'source_interface': {'type': 'str'}, - 'vrf': {'type': 'str'}, 'protocol': {'choices': ['TCP', 'UDP', 'TLS'], 'type': 'str'}, + 'source_interface': {'type': 'str'}, + 'vrf': {'type': 'str'} }, 'type': 'list' } diff --git a/plugins/module_utils/network/sonic/config/logging/logging.py b/plugins/module_utils/network/sonic/config/logging/logging.py index 0538c7395..659a335c3 100644 --- a/plugins/module_utils/network/sonic/config/logging/logging.py +++ b/plugins/module_utils/network/sonic/config/logging/logging.py @@ -43,6 +43,7 @@ DEFAULT_REMOTE_PORT = 514 DEFAULT_LOG_TYPE = 'log' DEFAULT_PROTOCOL = 'UDP' +DEFAULT_SEVERITY = 'notice' TEST_KEYS = [ { @@ -156,7 +157,6 @@ def set_state(self, want, have): """ state = self._module.params['state'] - self.validate_want(want, state) self.preprocess_want(want, state) if state == 'deleted': @@ -328,23 +328,6 @@ def get_replaced_config(self, have, want): replaced_config['remote_servers'] = replaced_servers return replaced_config - def validate_want(self, want, state): - - if state == 'deleted': - - if 'remote_servers' in want and want['remote_servers'] is not None: - for server in want['remote_servers']: - source_interface_config = server.get('source_interface', None) - remote_port_config = server.get('remote_port', None) - protocol_config = server.get('protocol', None) - severity_config = server.get('severity', None) - message_type_config = server.get('message_type', None) - vrf_config = server.get('vrf', None) - if source_interface_config or remote_port_config or \ - message_type_config or vrf_config or protocol_config or severity_config: - err_msg = "Logging remote_server parameter(s) can not be deleted." - self._module.fail_json(msg=err_msg, code=405) - def preprocess_want(self, want, state): if state == 'merged': @@ -380,6 +363,8 @@ def preprocess_want(self, want, state): server['message_type'] = DEFAULT_LOG_TYPE if 'protocol' in server and not server['protocol']: server['protocol'] = DEFAULT_PROTOCOL + if 'severity' in server and not server['severity']: + server['severity'] = DEFAULT_SEVERITY def get_merge_requests(self, configs, have): @@ -453,9 +438,29 @@ def get_delete_servers_requests(self, configs): for config in configs: server_host = config['host'] url = 'data/openconfig-system:system/logging/remote-servers/remote-server={0}'.format(server_host) - request = {"path": url, "method": method} - requests.append(request) - + if not (config.get("vrf") or config.get("source_interface") or config.get("message_type") or + config.get("remote_port") or config.get("protocol") or config.get("severity")): + request = {"path": url, "method": method} + requests.append(request) + else: + if config.get('source_interface'): + request = {"path": "{}/config/openconfig-system-ext:source-interface".format(url), "method": method} + requests.append(request) + if config.get("message_type"): + request = {"path": "{}/config/openconfig-system-ext:message-type".format(url), "method": method} + requests.append(request) + if config.get("vrf"): + request = {"path": "{}/config/openconfig-system-ext:vrf-name".format(url), "method": method} + requests.append(request) + if config.get("remote_port"): + request = {"path": "{}/config/remote-port".format(url), "method": method} + requests.append(request) + if config.get("protocol"): + request = {"path": "{}/config/openconfig-system-ext:protocol".format(url), "method": method} + requests.append(request) + if config.get("severity"): + request = {"path": "{}/config/openconfig-system-ext:severity".format(url), "method": method} + requests.append(request) return requests def get_delete_all_servers_requests(self): diff --git a/plugins/modules/sonic_logging.py b/plugins/modules/sonic_logging.py index 325b5d9b9..335be185a 100644 --- a/plugins/modules/sonic_logging.py +++ b/plugins/modules/sonic_logging.py @@ -61,17 +61,14 @@ type: int description: - Destination port number for logging messages sent to the server. - - remote_port can not be deleted. source_interface: type: str description: - Source interface used as source ip for sending logging packets. - - source_interface can not be deleted. message_type: type: str description: - - Type of messages that remote server receives. - - message_type can not be deleted. + - Type of messages that remote server receives. Defaults to "log" value. choices: - log - event @@ -81,7 +78,7 @@ version_added: 3.1.0 type: str description: - - The log severity filter for remote syslog server. + - The log severity filter for remote syslog server. Defaults to "notice" value. choices: - debug - info @@ -94,7 +91,7 @@ protocol: type: str description: - - Type of the protocol for sending the messages. + - Type of the protocol for sending the messages. Defaults to "UDP" value. choices: - TCP - TLS @@ -121,13 +118,13 @@ # ------------- # #sonic# show logging servers -#--------------------------------------------------------------------------------------- -#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE PROTOCOL -#--------------------------------------------------------------------------------------- -#10.11.0.2 5 Ethernet24 - event udp -#10.11.1.1 616 Ethernet8 - log tcp -#log1.dell.com 6 Ethernet28 - audit udp -#10.11.1.2 116 Ethernet6 - log tls +#---------------------------------------------------------------------------------------------------------- +#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE SEVERITY PROTOCOL +#---------------------------------------------------------------------------------------------------------- +#10.11.0.2 5 Ethernet24 - event notice udp +#10.11.1.1 616 Ethernet8 - log alert tcp +#log1.dell.com 6 Ethernet28 - audit notice udp +#10.11.1.2 116 Ethernet6 - log notice tls # - name: Delete logging server configuration sonic_logging: @@ -135,17 +132,22 @@ remote_servers: - host: 10.11.0.2 - host: log1.dell.com + - host: 10.11.1.1 + message_type: log + protocol: tcp + source_interface: Ethernet8 + severity: alert state: deleted # After state: # ------------ # #sonic# show logging servers -#--------------------------------------------------------------------------------------- -#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE PROTOCOL -#--------------------------------------------------------------------------------------- -#10.11.1.1 616 Ethernet8 - log tcp -#10.11.1.2 116 Ethernet6 - log tls +#---------------------------------------------------------------------------------------------------------- +#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE SEVERITY PROTOCOL +#---------------------------------------------------------------------------------------------------------- +#10.11.1.1 616 - - log notice udp +#10.11.1.2 116 Ethernet6 - log notice tls # # # Using merged @@ -154,10 +156,10 @@ # ------------- # #sonic# show logging servers -#-------------------------------------------------------------------------------------- -#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE PROTOCOL -#-------------------------------------------------------------------------------------- -#10.11.1.1 616 Ethernet8 - log tcp +#---------------------------------------------------------------------------------------------------------- +#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE SEVERITY PROTOCOL +#---------------------------------------------------------------------------------------------------------- +#10.11.1.1 616 Ethernet8 - log notice tcp # - name: Merge logging server configuration sonic_logging: @@ -172,6 +174,8 @@ remote_port: 4 protocol: TLS source_interface: Ethernet2 + - host: 10.11.1.1 + severity: error - host: log1.dell.com remote_port: 6 protocol: udp @@ -181,15 +185,14 @@ # After state: # ------------ -# #sonic# show logging servers -#------------------------------------------------------------------------------------- -#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE PROTOCOL -#------------------------------------------------------------------------------------- -#10.11.0.2 5 Ethernet24 - event udp -#10.11.0.1 4 Ethernet2 - log tls -#10.11.1.1 616 Ethernet8 - log tcp -#log1.dell.com 6 Ethernet28 - audit udp +#---------------------------------------------------------------------------------------------------------- +#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE SEVERITY PROTOCOL +#---------------------------------------------------------------------------------------------------------- +#10.11.0.2 5 Ethernet24 - event notice udp +#10.11.0.1 4 Ethernet2 - log notice tls +#10.11.1.1 616 Ethernet8 - log error tcp +#log1.dell.com 6 Ethernet28 - audit notice udp # # # Using overridden @@ -198,12 +201,12 @@ # ------------- # #sonic# show logging servers -#-------------------------------------------------------------------------------------- -#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE PROTOCOL -#-------------------------------------------------------------------------------------- -#10.11.1.1 616 Ethernet8 - log tcp -#10.11.1.2 626 Ethernet16 - event udp -#10.11.1.3 626 Ethernet14 - log tls +#---------------------------------------------------------------------------------------------------------- +#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE SEVERITY PROTOCOL +#---------------------------------------------------------------------------------------------------------- +#10.11.1.1 616 Ethernet8 - log notice tcp +#10.11.1.2 626 Ethernet16 - event emergency udp +#10.11.1.3 626 Ethernet14 - log notice tls # - name: Override logging server configuration sonic_logging: @@ -213,17 +216,17 @@ remote_port: 622 protocol: TCP source_interface: Ethernet24 - message_type: auditd-system + message_type: audit + severity: alert state: overridden # # After state: # ------------ -# #sonic# show logging servers -#-------------------------------------------------------------------------------------- -#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE PROTOCOL -#-------------------------------------------------------------------------------------- -#10.11.1.2 622 Ethernet24 - auditd-system tcp +#---------------------------------------------------------------------------------------------------------- +#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE SEVERITY PROTOCOL +#---------------------------------------------------------------------------------------------------------- +#10.11.1.2 622 Ethernet24 - audit alert tcp # # Using replaced # @@ -231,11 +234,11 @@ # ------------- # #sonic# show logging servers -#-------------------------------------------------------------------------------------- -#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE PROTOCOL -#-------------------------------------------------------------------------------------- -#10.11.1.1 616 Ethernet8 - log tcp -#10.11.1.2 626 Ethernet16 - event udp +#---------------------------------------------------------------------------------------------------------- +#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE SEVERITY PROTOCOL +#---------------------------------------------------------------------------------------------------------- +#10.11.1.1 616 Ethernet8 - log notice tcp +#10.11.1.2 626 Ethernet16 - event notice udp # - name: Replace logging server configuration sonic_logging: @@ -245,6 +248,7 @@ remote_port: 622 protocol: UDP message_type: audit + severity: debug state: replaced # # After state: @@ -253,11 +257,11 @@ # "MESSAGE-TYPE" has default value of "log" # #sonic# show logging servers -#-------------------------------------------------------------------------------------- -#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE PROTOCOL -#-------------------------------------------------------------------------------------- -#10.11.1.1 616 Ethernet8 - log tcp -#10.11.1.2 622 - - audit udp +#---------------------------------------------------------------------------------------------------------- +#HOST PORT SOURCE-INTERFACE VRF MESSAGE-TYPE SEVERITY PROTOCOL +#---------------------------------------------------------------------------------------------------------- +#10.11.1.1 616 Ethernet8 - log notice tcp +#10.11.1.2 622 - - audit debug udp # """ RETURN = """ diff --git a/tests/regression/roles/sonic_logging/defaults/main.yml b/tests/regression/roles/sonic_logging/defaults/main.yml index 97dbb894f..8a6372421 100644 --- a/tests/regression/roles/sonic_logging/defaults/main.yml +++ b/tests/regression/roles/sonic_logging/defaults/main.yml @@ -19,20 +19,20 @@ logging_host_server: logging.dell.com tests: - name: test_case_01 - description: Create a logging remote server + description: Create a single logging remote server state: merged input: remote_servers: - host: "{{ logging_ip_server_1 }}" source_interface: "{{ interface1 }}" remote_port: 616 - protocol: UDP + protocol: TCP severity: debug message_type: event vrf: Vrf_logging_1 - name: test_case_02 - description: Create several logging remote servers + description: Create several logging remote servers state: merged input: remote_servers: @@ -44,7 +44,7 @@ tests: source_interface: "{{ vlan1 }}" remote_port: 818 protocol: TCP - message_type: auditd-system + message_type: event vrf: Vrf_logging_2 - host: "{{ logging_ip_server_7 }}" severity: debug @@ -65,7 +65,7 @@ tests: vrf: Vrf_logging_1 - name: test_case_03 - description: Replace logging remote server + description: Replace a single logging remote server state: replaced input: remote_servers: @@ -74,18 +74,13 @@ tests: source_interface: "{{ interface2 }}" remote_port: 838 protocol: UDP + message_type: event - name: test_case_04 - description: Replace logging remote servers + description: Replace several logging remote servers state: replaced input: remote_servers: - - host: "{{ logging_ip_server_3 }}" - severity: alert - source_interface: "{{ interface3 }}" - remote_port: 838 - protocol: UDP - message_type: event - host: "{{ logging_ip_server_4 }}" severity: critical source_interface: "{{ interface4 }}" @@ -100,10 +95,22 @@ tests: source_interface: "{{ interface5 }}" remote_port: 818 protocol: TLS - message_type: auditd-system + message_type: event - name: test_case_05 - description: Overridden logging remote servers + description: Override a single logging remote server + state: overridden + input: + remote_servers: + - host: "{{ logging_ip_server_1 }}" + severity: info + source_interface: "{{ interface1 }}" + remote_port: 777 + protocol: UDP + message_type: log + + - name: test_case_06 + description: Override several logging remote servers state: overridden input: remote_servers: @@ -117,8 +124,8 @@ tests: protocol: TCP message_type: event - - name: test_case_06 - description: Modify several created logging remote servers to change or add suboptions + - name: test_case_07 + description: Modify several created logging remote servers to change or add attributes state: merged input: remote_servers: @@ -127,29 +134,42 @@ tests: source_interface: "{{ interface1 }}" remote_port: 111 protocol: TCP - message_type: log + message_type: event vrf: Vrf_logging_1 - host: "{{ logging_ip_server_3 }}" severity: emergency source_interface: "{{ vlan1 }}" remote_port: 313 protocol: UDP - message_type: auditd-system + message_type: audit vrf: Vrf_logging_2 - host: "{{ logging_host_server }}" severity: warning source_interface: "{{ lo1 }}" + message_type: log + vrf: Vrf_logging_1 + + - name: test_case_08 + description: Delete a single logging remote server's attributes + state: deleted + input: + remote_servers: + - host: "{{ logging_ip_server_1 }}" + source_interface: "{{ interface1 }}" + remote_port: 616 + protocol: TCP message_type: event vrf: Vrf_logging_1 + severity: debug - - name: test_case_07 + - name: test_case_09 description: Delete a logging remote server state: deleted input: remote_servers: - host: "{{ logging_ip_server_1 }}" - - name: test_case_08 + - name: test_case_10 description: Delete several logging remote servers state: deleted input: @@ -159,7 +179,7 @@ tests: - host: "{{ logging_ip_server_7 }}" - host: "{{ logging_host_server }}" - - name: test_case_09 + - name: test_case_11 description: Delete all logging configurations state: deleted input: {} diff --git a/tests/unit/modules/network/sonic/fixtures/sonic_logging.yaml b/tests/unit/modules/network/sonic/fixtures/sonic_logging.yaml index 5d421e0c7..4d348c679 100644 --- a/tests/unit/modules/network/sonic/fixtures/sonic_logging.yaml +++ b/tests/unit/modules/network/sonic/fixtures/sonic_logging.yaml @@ -34,25 +34,25 @@ merged_01: - host: 10.11.0.2 config: host: 10.11.0.2 + remote-port: 5 source-interface: Eth1/24 message-type: event - remote-port: 5 protocol: TCP severity: INFORMATIONAL - host: 10.11.0.3 config: host: 10.11.0.3 + remote-port: 4 source-interface: Eth1/22 message-type: log - remote-port: 4 protocol: TLS severity: NOTICE - host: log1.dell.com config: host: log1.dell.com + remote-port: 6 source-interface: Eth1/28 message-type: audit - remote-port: 6 protocol: UDP severity: ERROR @@ -70,18 +70,18 @@ deleted_01: - host: 10.11.0.2 config: host: 10.11.0.2 - source-interface: Eth1/24 - message-type: event remote-port: 5 - protocol: TCP - severity: debug + openconfig-system-ext:source-interface: Eth1/24 + openconfig-system-ext:message-type: event + openconfig-system-ext:protocol: TCP + openconfig-system-ext:severity: debug - host: log1.dell.com config: host: log1.dell.com - source-interface: Eth1/28 remote-port: 6 - protocol: UDP - severity: error + openconfig-system-ext:source-interface: Eth1/28 + openconfig-system-ext:protocol: UDP + openconfig-system-ext:severity: error expected_config_requests: - path: "data/openconfig-system:system/logging/remote-servers" method: "delete" @@ -104,23 +104,67 @@ deleted_02: - host: 10.11.0.2 config: host: 10.11.0.2 - source-interface: Eth1/24 - message-type: event remote-port: 5 - protocol: TCP - severity: debug + openconfig-system-ext:source-interface: Eth1/24 + openconfig-system-ext:message-type: event + openconfig-system-ext:protocol: TCP + openconfig-system-ext:severity: debug - host: log1.dell.com config: host: log1.dell.com - source-interface: Eth1/28 remote-port: 6 - protocol: UDP - severity: error + openconfig-system-ext:source-interface: Eth1/28 + openconfig-system-ext:protocol: UDP + openconfig-system-ext:severity: error expected_config_requests: - path: "data/openconfig-system:system/logging/remote-servers/remote-server=10.11.0.2" method: "delete" data: +deleted_03: + module_args: + state: deleted + config: + remote_servers: + - host: 10.11.0.2 + remote_port: 5 + protocol: TCP + severity: error + source_interface: Eth1/24 + message_type: event + existing_logging_config: + - path: "data/openconfig-system:system/logging" + response: + code: 200 + value: + openconfig-system:logging: + remote-servers: + remote-server: + - host: 10.11.0.2 + config: + host: 10.11.0.2 + remote-port: 5 + openconfig-system-ext:protocol: TCP + openconfig-system-ext:severity: error + openconfig-system-ext:source-interface: Eth1/24 + openconfig-system-ext:message-type: event + expected_config_requests: + - path: "data/openconfig-system:system/logging/remote-servers/remote-server=10.11.0.2/config/openconfig-system-ext:source-interface" + method: "delete" + data: + - path: "data/openconfig-system:system/logging/remote-servers/remote-server=10.11.0.2/config/openconfig-system-ext:message-type" + method: "delete" + data: + - path: "data/openconfig-system:system/logging/remote-servers/remote-server=10.11.0.2/config/remote-port" + method: "delete" + data: + - path: "data/openconfig-system:system/logging/remote-servers/remote-server=10.11.0.2/config/openconfig-system-ext:protocol" + method: "delete" + data: + - path: "data/openconfig-system:system/logging/remote-servers/remote-server=10.11.0.2/config/openconfig-system-ext:severity" + method: "delete" + data: + replaced_01: module_args: state: replaced @@ -149,20 +193,32 @@ replaced_01: - host: 10.11.0.2 config: host: 10.11.0.2 - source-interface: Eth1/24 - message-type: event remote-port: 5 - protocol: TCP - severity: emergency + openconfig-system-ext:protocol: TCP + openconfig-system-ext:severity: emergency + openconfig-system-ext:source-interface: Eth1/24 + openconfig-system-ext:message-type: event - host: log1.dell.com config: host: log1.dell.com - source-interface: Eth1/28 remote-port: 6 - protocol: UDP - severity: debug + openconfig-system-ext:protocol: UDP + openconfig-system-ext:severity: debug + openconfig-system-ext:source-interface: Eth1/28 expected_config_requests: - - path: "data/openconfig-system:system/logging/remote-servers/remote-server=10.11.0.2" + - path: "data/openconfig-system:system/logging/remote-servers/remote-server=10.11.0.2/config/remote-port" + method: "delete" + data: + - path: "data/openconfig-system:system/logging/remote-servers/remote-server=10.11.0.2/config/openconfig-system-ext:source-interface" + method: "delete" + data: + - path: "data/openconfig-system:system/logging/remote-servers/remote-server=10.11.0.2/config/openconfig-system-ext:protocol" + method: "delete" + data: + - path: "data/openconfig-system:system/logging/remote-servers/remote-server=10.11.0.2/config/openconfig-system-ext:severity" + method: "delete" + data: + - path: "data/openconfig-system:system/logging/remote-servers/remote-server=10.11.0.2/config/openconfig-system-ext:message-type" method: "delete" data: - path: "data/openconfig-system:system/logging/remote-servers" @@ -211,18 +267,18 @@ overridden_01: - host: 10.11.0.2 config: host: 10.11.0.2 - source-interface: Eth1/24 - message-type: event remote-port: 5 - protocol: TCP - severity: error + openconfig-system-ext:source-interface: Eth1/24 + openconfig-system-ext:message-type: event + openconfig-system-ext:protocol: TCP + openconfig-system-ext:severity: error - host: log1.dell.com config: host: log1.dell.com - source-interface: Eth1/28 remote-port: 6 - protocol: UDP - severity: debug + openconfig-system-ext:source-interface: Eth1/28 + openconfig-system-ext:protocol: UDP + openconfig-system-ext:severity: debug expected_config_requests: - path: "data/openconfig-system:system/logging/remote-servers" method: "delete" diff --git a/tests/unit/modules/network/sonic/test_sonic_logging.py b/tests/unit/modules/network/sonic/test_sonic_logging.py index eb744d0f1..22d9bddd1 100644 --- a/tests/unit/modules/network/sonic/test_sonic_logging.py +++ b/tests/unit/modules/network/sonic/test_sonic_logging.py @@ -72,6 +72,13 @@ def test_sonic_logging_deleted_02(self): result = self.execute_module(changed=True) self.validate_config_requests() + def test_sonic_logging_deleted_03(self): + set_module_args(self.fixture_data['deleted_03']['module_args']) + self.initialize_facts_get_requests(self.fixture_data['deleted_03']['existing_logging_config']) + self.initialize_config_requests(self.fixture_data['deleted_03']['expected_config_requests']) + result = self.execute_module(changed=True) + self.validate_config_requests() + def test_sonic_logging_replaced_01(self): set_module_args(self.fixture_data['replaced_01']['module_args']) self.initialize_facts_get_requests(self.fixture_data['replaced_01']['existing_logging_config'])