diff --git a/examples/smtp.yml b/examples/smtp.yml index fae4beb7..a8607f18 100644 --- a/examples/smtp.yml +++ b/examples/smtp.yml @@ -1,22 +1,38 @@ --- -- name: Modify SMTP configuration without authentication - scale_computing.hypercore.smtp: - server: smtp-relay.gmail.com - port: 25 - use_ssl: false - requires_auth: false - from_address: example@example.com +- name: Create and modify SMTP configuration + hosts: localhost + connection: local + gather_facts: false + vars: + smtp_server: smtp-relay.gmail.com + smtp_port: 25 + smtp_from_address: example@example.com -- name: Modify SMTP configuration with authentication - scale_computing.hypercore.smtp: - server: smtp-relay.gmail.com - port: 25 - use_ssl: false - auth_user: example - auth_password: example123 - from_address: example@example.com + tasks: + - name: Modify SMTP configuration with authentication + scale_computing.hypercore.smtp: + server: "{{ smtp_server }}" + port: "{{ smtp_port }}" + use_ssl: false + auth_user: example + auth_password: example123 + from_address: "{{ smtp_from_address }}" -- name: Modify SMTP configuration with required params only - scale_computing.hypercore.smtp: - server: smtp-relay.gmail.com - port: 25 + - name: Modify SMTP configuration with required params only, authentication is preserved + scale_computing.hypercore.smtp: + server: "{{ smtp_server }}" + port: "{{ smtp_port }}" + + - name: Modify SMTP configuration, remove authentication + scale_computing.hypercore.smtp: + server: "{{ smtp_server }}" + port: "{{ smtp_port }}" + auth_user: "" + auth_password: "" + + - name: List SMTP Configuration + scale_computing.hypercore.smtp_info: + register: smtp_info + + - ansible.builtin.debug: + msg: Final SMTP configuration is "{{ smtp_info }}" diff --git a/examples/smtp_info.yml b/examples/smtp_info.yml index 0748ded4..1ab98789 100644 --- a/examples/smtp_info.yml +++ b/examples/smtp_info.yml @@ -1,6 +1,13 @@ --- -- name: List SMTP Configuration - scale_computing.hypercore.smtp_info: - register: smtp_info -- ansible.builtin.debug: - msg: "{{ smtp_info }}" +- name: List SMTP configuration + hosts: localhost + connection: local + gather_facts: false + + tasks: + - name: List SMTP Configuration + scale_computing.hypercore.smtp_info: + register: smtp_info + + - ansible.builtin.debug: + msg: "{{ smtp_info }}" diff --git a/plugins/module_utils/smtp.py b/plugins/module_utils/smtp.py index aa521e12..56800cf2 100644 --- a/plugins/module_utils/smtp.py +++ b/plugins/module_utils/smtp.py @@ -23,7 +23,7 @@ class SMTP(PayloadMapper): def __init__( self, uuid: Union[str, None] = None, - smtp_server: Union[str, None] = None, + server: Union[str, None] = None, port: Union[int, None] = None, use_ssl: Union[bool, None] = False, use_auth: Union[bool, None] = False, @@ -33,7 +33,7 @@ def __init__( latest_task_tag: Union[TypedTaskTag, dict[Any, Any], None] = None, ): self.uuid = uuid - self.smtp_server = smtp_server + self.server = server self.port = port self.use_ssl = use_ssl self.use_auth = use_auth @@ -46,7 +46,7 @@ def __init__( def from_ansible(cls, ansible_data: TypedSmtpFromAnsible) -> SMTP: return SMTP( uuid=ansible_data["uuid"], - smtp_server=ansible_data["smtp_server"], + server=ansible_data["server"], port=ansible_data["port"], use_ssl=ansible_data["use_ssl"], use_auth=ansible_data["use_auth"], @@ -63,7 +63,7 @@ def from_hypercore(cls, hypercore_data: dict[Any, Any]) -> SMTP: # raise xzy return cls( uuid=hypercore_data["uuid"], - smtp_server=hypercore_data["smtpServer"], + server=hypercore_data["smtpServer"], port=hypercore_data["port"], use_ssl=hypercore_data["useSSL"], use_auth=hypercore_data["useAuth"], @@ -75,7 +75,7 @@ def from_hypercore(cls, hypercore_data: dict[Any, Any]) -> SMTP: def to_hypercore(self) -> dict[Any, Any]: return dict( - smtpServer=self.smtp_server, + smtpServer=self.server, port=self.port, useSSL=self.use_ssl, useAuth=self.use_auth, @@ -87,7 +87,7 @@ def to_hypercore(self) -> dict[Any, Any]: def to_ansible(self) -> TypedSmtpToAnsible: return dict( uuid=self.uuid, - smtp_server=self.smtp_server, + server=self.server, port=self.port, use_ssl=self.use_ssl, use_auth=self.use_auth, @@ -104,7 +104,7 @@ def __eq__(self, other: object) -> bool: return all( ( self.uuid == other.uuid, - self.smtp_server == other.smtp_server, + self.server == other.server, self.port == other.port, self.use_ssl == other.use_ssl, self.use_auth == other.use_auth, diff --git a/plugins/module_utils/typed_classes.py b/plugins/module_utils/typed_classes.py index 1d423ba4..53e21f34 100644 --- a/plugins/module_utils/typed_classes.py +++ b/plugins/module_utils/typed_classes.py @@ -113,7 +113,7 @@ class TypedDiff(TypedDict): # smtp module class TypedSmtpToAnsible(TypedDict): uuid: Union[str, None] - smtp_server: Union[str, None] + server: Union[str, None] port: Union[int, None] use_ssl: Union[bool, None] use_auth: Union[bool, None] @@ -125,7 +125,7 @@ class TypedSmtpToAnsible(TypedDict): class TypedSmtpFromAnsible(TypedDict): uuid: Union[str, None] - smtp_server: Union[str, None] + server: Union[str, None] port: Union[int, None] use_ssl: Union[bool, None] use_auth: Union[bool, None] diff --git a/plugins/modules/smtp.py b/plugins/modules/smtp.py index 6a41edce..d1d7e628 100644 --- a/plugins/modules/smtp.py +++ b/plugins/modules/smtp.py @@ -83,7 +83,7 @@ """ RETURN = r""" -results: +record: description: - Output from modifying entries of the SMTP configuration on HyperCore API. returned: success @@ -107,7 +107,7 @@ state: COMPLETE taskTag: 761 port: 25 - smtp_server: smtp-relay.gmail.com + server: smtp-relay.gmail.com use_auth: false use_ssl: false uuid: smtpconfig_guid @@ -187,7 +187,7 @@ def modify_smtp_config( ) # get the state of SMTP config before modification new_smtp_server, new_smtp_server_change_needed = build_entry( - before.get("smtp_server"), module.params["server"] + before.get("server"), module.params["server"] ) new_port, new_port_change_needed = build_entry( before.get("port"), module.params["port"] diff --git a/plugins/modules/smtp_info.py b/plugins/modules/smtp_info.py index 12af5953..701cc605 100644 --- a/plugins/modules/smtp_info.py +++ b/plugins/modules/smtp_info.py @@ -36,7 +36,7 @@ """ RETURN = r""" -results: +record: description: - SMTP configuration record. returned: success @@ -60,7 +60,7 @@ state: COMPLETE taskTag: 761 port: 25 - smtp_server: smtp-relay.gmail.com + server: smtp-relay.gmail.com use_auth: false use_ssl: false uuid: smtpconfig_guid diff --git a/tests/integration/targets/smtp/tasks/01_smtp_info.yml b/tests/integration/targets/smtp/tasks/01_smtp_info.yml index b55fbe61..1ddb69e9 100644 --- a/tests/integration/targets/smtp/tasks/01_smtp_info.yml +++ b/tests/integration/targets/smtp/tasks/01_smtp_info.yml @@ -36,9 +36,9 @@ - ansible.builtin.assert: that: - result.changed == False - - result.record.keys() | sort == ['auth_password', 'auth_user', 'from_address', 'latest_task_tag', 'port', 'smtp_server', 'use_auth', 'use_ssl', 'uuid'] + - result.record.keys() | sort == ['auth_password', 'auth_user', 'from_address', 'latest_task_tag', 'port', 'server', 'use_auth', 'use_ssl', 'uuid'] - result.record.uuid == "smtpconfig_guid" - - result.record.smtp_server == "mail.example.com" + - result.record.server == "mail.example.com" - result.record.port == 25 - result.record.use_ssl == False - result.record.use_auth == False diff --git a/tests/integration/targets/smtp/tasks/02_smtp.yml b/tests/integration/targets/smtp/tasks/02_smtp.yml index c4869b69..369a16fe 100644 --- a/tests/integration/targets/smtp/tasks/02_smtp.yml +++ b/tests/integration/targets/smtp/tasks/02_smtp.yml @@ -38,8 +38,8 @@ that: - result.changed == True - result.diff.before != result.diff.after - - result.record.keys() | sort == ['auth_password', 'auth_user', 'from_address', 'latest_task_tag', 'port', 'smtp_server', 'use_auth', 'use_ssl', 'uuid'] - - info.record.smtp_server == "smtp.office365.com" + - result.record.keys() | sort == ['auth_password', 'auth_user', 'from_address', 'latest_task_tag', 'port', 'server', 'use_auth', 'use_ssl', 'uuid'] + - info.record.server == "smtp.office365.com" - info.record.port == 21 - info.record.use_ssl == False - info.record.use_auth == False @@ -61,8 +61,8 @@ that: - result.changed == False - result.diff.before == result.diff.after - - result.record.keys() | sort == ['auth_password', 'auth_user', 'from_address', 'latest_task_tag', 'port', 'smtp_server', 'use_auth', 'use_ssl', 'uuid'] - - info.record.smtp_server == "smtp.office365.com" + - result.record.keys() | sort == ['auth_password', 'auth_user', 'from_address', 'latest_task_tag', 'port', 'server', 'use_auth', 'use_ssl', 'uuid'] + - info.record.server == "smtp.office365.com" - info.record.port == 21 - info.record.use_ssl == False - info.record.use_auth == False @@ -90,7 +90,7 @@ that: - result.changed == True - result.diff.before != result.diff.after - - info.record.smtp_server == "smtp.office365.com" + - info.record.server == "smtp.office365.com" - info.record.port == 26 - info.record.use_ssl == False - info.record.use_auth == False @@ -112,7 +112,7 @@ that: - result.changed == False - result.diff.before == result.diff.after - - info.record.smtp_server == "smtp.office365.com" + - info.record.server == "smtp.office365.com" - info.record.port == 26 - info.record.use_ssl == False - info.record.use_auth == False @@ -141,7 +141,7 @@ that: - result.changed == True - result.diff.before != result.diff.after - - info.record.smtp_server == "smtp-relay.gmail.com" + - info.record.server == "smtp-relay.gmail.com" - info.record.port == 25 - info.record.use_ssl == False - info.record.use_auth == True @@ -168,7 +168,7 @@ # Complication "before == after" fails too # latest_task_tag has different sessionID, modifiedTime etc. # - result.diff.before == result.diff.after - - info.record.smtp_server == "smtp-relay.gmail.com" + - info.record.server == "smtp-relay.gmail.com" - info.record.port == 25 - info.record.use_ssl == False - info.record.use_auth == True @@ -192,7 +192,7 @@ # Complication "before == after" fails too # latest_task_tag has different sessionID, modifiedTime etc. # - result.diff.before == result.diff.after - - info.record.smtp_server == "smtp-relay.gmail.com" + - info.record.server == "smtp-relay.gmail.com" - info.record.port == 26 - info.record.use_ssl == False - info.record.use_auth == True diff --git a/tests/unit/plugins/module_utils/test_smtp.py b/tests/unit/plugins/module_utils/test_smtp.py index 4917d3bb..101d6752 100644 --- a/tests/unit/plugins/module_utils/test_smtp.py +++ b/tests/unit/plugins/module_utils/test_smtp.py @@ -30,7 +30,7 @@ class TestSMTP: def setup_method(self): self.smtp = SMTP( uuid="test", - smtp_server="smtp-relay.gmail.com", + server="smtp-relay.gmail.com", port=25, use_ssl=True, use_auth=True, @@ -61,7 +61,7 @@ def setup_method(self): ) self.ansible_dict = dict( uuid="test", - smtp_server="smtp-relay.gmail.com", + server="smtp-relay.gmail.com", port=25, use_ssl=True, use_auth=True, @@ -124,7 +124,7 @@ def test_get_state(self, rest_client): print(f"result={result}") assert result == { "uuid": "test", - "smtp_server": "smtp-relay.gmail.com", + "server": "smtp-relay.gmail.com", "port": 25, "use_ssl": True, "use_auth": True, diff --git a/tests/unit/plugins/modules/test_smtp.py b/tests/unit/plugins/modules/test_smtp.py index f0ca465a..136fdd35 100644 --- a/tests/unit/plugins/modules/test_smtp.py +++ b/tests/unit/plugins/modules/test_smtp.py @@ -41,20 +41,20 @@ def test_build_entry(self, api_entry, module_entry, expected): @pytest.mark.parametrize( ( "description", - "rc_smtp_server", + "rc_server", "rc_port", "rc_use_ssl", "rc_use_auth", "rc_auth_user", "rc_auth_password", "rc_from_address", - "smtp_server_param", + "server_param", "port_param", "use_ssl_param", "auth_user_param", "auth_password_param", "from_address_param", - "expected_smtp_server", + "expected_server", "expected_port", "expected_use_ssl", "expected_use_auth", @@ -157,20 +157,20 @@ def test_modify_smtp_config( rest_client, task_wait, mocker, - rc_smtp_server, + rc_server, rc_port, rc_use_ssl, rc_use_auth, rc_auth_user, rc_auth_password, rc_from_address, - smtp_server_param, + server_param, port_param, use_ssl_param, auth_user_param, auth_password_param, from_address_param, - expected_smtp_server, + expected_server, expected_port, expected_use_ssl, expected_use_auth, @@ -184,7 +184,7 @@ def test_modify_smtp_config( cluster_instance=dict( host="https://0.0.0.0", username="admin", password="admin" ), - server=smtp_server_param, + server=server_param, port=port_param, use_ssl=use_ssl_param, auth_user=auth_user_param, @@ -196,7 +196,7 @@ def test_modify_smtp_config( "ansible_collections.scale_computing.hypercore.plugins.module_utils.smtp.SMTP.get_by_uuid" ).return_value = SMTP( uuid="test", - smtp_server=rc_smtp_server, + server=rc_server, port=rc_port, use_ssl=rc_use_ssl, use_auth=rc_use_auth, @@ -215,7 +215,7 @@ def test_modify_smtp_config( called_with_dict = dict( endpoint="/rest/v1/AlertSMTPConfig/test", payload=dict( - smtpServer=expected_smtp_server, + smtpServer=expected_server, port=expected_port, useSSL=expected_use_ssl, authUser=expected_auth_user, diff --git a/tests/unit/plugins/modules/test_smtp_info.py b/tests/unit/plugins/modules/test_smtp_info.py index d28f7fe5..e0cea541 100644 --- a/tests/unit/plugins/modules/test_smtp_info.py +++ b/tests/unit/plugins/modules/test_smtp_info.py @@ -44,7 +44,7 @@ def test_run_record_present(self, rest_client): result = smtp_info.run(rest_client) assert result == { "uuid": "test", - "smtp_server": "smtp-relay.gmail.com", + "server": "smtp-relay.gmail.com", "port": 25, "use_ssl": True, "use_auth": True,