blob: cbe52ec567febb9927bd9e9d03ddcfb87e758857 [file] [log] [blame]
Jakub Josefa6d4c832017-01-19 14:06:12 +01001import logging
2logger = logging.getLogger(__name__)
3
4config_slack_groovy = """\
5jenkins = jenkins.model.Jenkins.getInstance()
6try{{
7slack = jenkins.getDescriptorByType(jenkins.plugins.slack.SlackNotifier.DescriptorImpl)
8if(slack.teamDomain.equals("{team_domain}") &&
9 slack.token.equals("{token}") &&
10 slack.tokenCredentialId.equals("{token_credential_id}") &&
11 slack.room.equals("{room}") &&
12 slack.sendAs.equals("{send_as}")){{
13 print("EXISTS")
14}}else{{
15 slack.teamDomain = "{team_domain}"
16 slack.token = "{token}"
17 slack.tokenCredentialId = "{token_credential_id}"
18 slack.room = "{room}"
19 slack.sendAs = "{send_as}"
20 slack.save()
21 print("SUCCESS")
22}}
23}}catch(all){{
24 print("Cannot instantiate Jenkins Slack plugin, maybe plugin is not installed")
25}}
26""" # noqa
27
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040028
29def __virtual__():
30 '''
31 Only load if jenkins_common module exist.
32 '''
33 if 'jenkins_common.call_groovy_script' not in __salt__:
34 return (
35 False,
36 'The jenkins_slack state module cannot be loaded: '
37 'jenkins_common not found')
38 return True
39
40
Jakub Josefa6d4c832017-01-19 14:06:12 +010041def config(name, team_domain, token, token_credential_id="", room="", send_as=None):
42 """
43 Jenkins Slack config state method
44
45 :param name: configuration name
46 :param team_domain: slack team domain
47 :param token: slack token
48 :param token_credential_id: slack token credential id
49 :param room: slack room
50 :param send_as: slack send as param
51 :returns: salt-specified state dict
52 """
53 test = __opts__['test'] # noqa
54 ret = {
55 'name': name,
56 'changes': {},
57 'result': False,
58 'comment': '',
59 }
60 result = False
61 if test:
62 status = "SUCCESS"
63 ret['changes'][name] = status
64 ret['comment'] = 'Jenkins Slack config %s %s' % (name, status.lower())
65 else:
66 call_result = __salt__['jenkins_common.call_groovy_script'](
67 config_slack_groovy, {"team_domain":team_domain,
68 "token":token,
69 "token_credential_id": token_credential_id if token_credential_id else "",
70 "room": room if room else "",
71 "send_as": send_as if send_as else ""})
72 if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
73 status = call_result["msg"]
74 if status == "SUCCESS":
75 ret['changes'][name] = status
76 ret['comment'] = 'Jenkins Slack config %s %s' % (name, status.lower())
77 result = True
78 else:
79 status = 'FAILED'
80 logger.error(
81 "Jenkins slack API call failure: %s", call_result["msg"])
82 ret['comment'] = 'Jenkins slack API call failure: %s' % (call_result[
83 "msg"])
84 ret['result'] = None if test else result
85 return ret