blob: 48e19c49dd19aa34f56df13b3a8f8ac55e8dbc2e [file] [log] [blame]
Jakub Josef60cc9d22017-01-18 12:02:14 +01001import logging
2logger = logging.getLogger(__name__)
3
4set_smtp_groovy = """\
5def desc = Jenkins.getInstance().getDescriptor("hudson.tasks.Mailer")
6if(desc.getSmtpServer().equals("{host}") &&
7 desc.getSmtpAuthUserName().equals("{username}") &&
8 desc.getSmtpAuthPassword().toString().equals("{password}") &&
9 desc.getSmtpPort().equals("{port}") &&
10 desc.getUseSsl() == {ssl} &&
11 desc.getCharset().equals("{charset}") &&
12 (!{reply_to_exists} || desc.getReplyAddress().equals("{reply_to}"))){{
13 print("EXISTS")
14}}else{{
15 desc.setSmtpAuth("{username}", "{password}")
16 desc.setSmtpHost("{host}")
17 desc.setUseSsl({ssl})
18 desc.setSmtpPort("{port}")
19 desc.setCharset("{charset}")
20 if({reply_to_exists}){{
21 desc.setReplyToAddress("{reply_to}")
22 }}
23 desc.save()
24 print("SUCCESS")
25}}
26""" # noqa
27
28def config(name, host, username, password, reply_to=None, port=25, ssl=False, charset="UTF-8"):
29 """
30 Jenkins SMTP server config state method
31
32 :param name: configuration name
33 :param host: SMTP host
34 :param username: SMTP username
35 :param password: SMTP password
36 :param reply_to: sent emails ReplyTo header (optional)
37 :param port: SMTP port (optional, default 25)
38 :param ssl: use SSL for SMTP (optional, default False)
39 :param charset: SMTP charset (optional, default UTF-8)
40 :returns: salt-specified state dict
41 """
42 test = __opts__['test'] # noqa
43 ret = {
44 'name': name,
45 'changes': {},
46 'result': False,
47 'comment': '',
48 }
49 result = False
50 if test:
51 status = "SUCCESS"
52 ret['changes'][name] = status
53 ret['comment'] = 'Jenkins SMTP config %s %s' % (name, status.lower())
54 else:
55 call_result = __salt__['jenkins_common.call_groovy_script'](
56 set_smtp_groovy, {"username": username, "password": password, "host": host,
57 "reply_to_exists": "true" if reply_to else "false",
58 "reply_to": reply_to,
59 "port": port if port else 25,
60 "ssl": "true" if ssl else "false",
61 "charset": charset if charset else "UTF-8"})
62 if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
63 status = call_result["msg"]
64 if status == "SUCCESS":
65 ret['changes'][name] = status
66 ret['comment'] = 'Jenkins smtp config %s %s' % (name, status.lower())
67 result = True
68 else:
69 status = 'FAILED'
70 logger.error(
71 "Jenkins smtp API call failure: %s", call_result["msg"])
72 ret['comment'] = 'Jenkins smtp API call failure: %s' % (call_result[
73 "msg"])
74 ret['result'] = None if test else result
75 return ret