Debian Codegraph @

ansible_collections/purestorage/flashblade/plugins/modules/purefb_smtp.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2018, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefb_smtp
20 version_added: '1.0.0'
21 short_description: Configure SMTP for Pure Storage FlashBlade
22 description:
23 - Configure SMTP for a Pure Storage FlashBlade.
24 - Whilst there can be no relay host, a sender domain must be configured.
25 author:
26 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
27 options:
28 host:
29 description:
30 - Relay server name
31 type: str
32 domain:
33 description:
34 - Domain name for alert messages
35 required: true
36 type: str
37 encryption:
38 description:
39 - Enforces an encryption mode when sending alert email messages.
40 - Use "" to clear.
41 type: str
42 choices: [ "starttls", "" ]
43 version_added: 1.19.0
44 extends_documentation_fragment:
45 - purestorage.flashblade.purestorage.fb
46 """
47
48 EXAMPLES = r"""
49 - name: Configure SMTP settings
50 purestorage.flashblade.purefb_smtp:
51 host: hostname
52 encryption: starttls
53 domain: xyz.com
54 fb_url: 10.10.10.2
55 api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6
56 """
57
58 RETURN = r"""
59 """
60
61 HAS_PYPURECLIENT = True
62 try:
63 from pypureclient.flashblade import SmtpServer
64 except ImportError:
65 HAS_PYPURECLIENT = False
66
67 from ansible.module_utils.basic import AnsibleModule
68 from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import (
69 get_system,
70 purefb_argument_spec,
71 )
72
73
74 SMTP_ENCRYPT_API_VERSION = "2.15"
75
76
77 def set_smtp(module, blade):
78 """Configure SMTP settings"""
79 changed = False
80 current_smtp = list(blade.get_smtp_servers(names=["management"]).items)[0]
81 relay_host = current_smtp.relay_host
82 domain = current_smtp.sender_domain
83 encrypt = getattr(current_smtp, "encryption_mode", "")
84 if module.params["host"] and module.params["host"] != relay_host:
85 relay_host = module.params["host"]
86 changed = True
87 if module.params["domain"] and module.params["domain"] != domain:
88 domain = module.params["domain"]
89 changed = True
90 if (
91 module.params["encryption"] is not None
92 and module.params["encryption"] != encrypt
93 ):
94 encrypt = module.params["encryption"]
95 changed = True
96 if changed and not module.check_mode:
97 if SMTP_ENCRYPT_API_VERSION in list(blade.get_versions().items):
98 res = blade.patch_smtp_servers(
99 smtp=SmtpServer(
100 relay_host=relay_host, sender_domain=domain, encryption_mode=encrypt
101 )
102 )
103 else:
104 res = blade.patch_smtp_servers(
105 smtp=SmtpServer(relay_host=relay_host, sender_domain=domain)
106 )
107 if res.status_code != 200:
108 module.fail_json(
109 msg="Failed to set SMTP configuration. Error: {0}".format(
110 res.errors[0].message
111 )
112 )
113 module.exit_json(changed=changed)
114
115
116 def main():
117 argument_spec = purefb_argument_spec()
118 argument_spec.update(
119 dict(
120 host=dict(type="str"),
121 domain=dict(type="str", required=True),
122 encryption=dict(type="str", choices=["starttls", ""]),
123 )
124 )
125
126 module = AnsibleModule(argument_spec, supports_check_mode=True)
127
128 blade = get_system(module)
129 api_version = list(blade.get_versions().items)
130 if not HAS_PYPURECLIENT:
131 module.fail_json(msg="py-pure-client SDK is required for this module")
132 if SMTP_ENCRYPT_API_VERSION not in api_version and module.params["encryption"]:
133 module.fail_json(msg="Purity//FB must be upgraded to support encryption.")
134
135 set_smtp(module, blade)
136 module.exit_json(changed=False)
137
138
139 if __name__ == "__main__":
140 main()