blob: d0d5636000c5eb00cc73fd5063e1c0e4bc10e2c6 [file] [log] [blame]
Jakub Josefa6d4c832017-01-19 14:06:12 +01001import logging
Adam Tengler70763e02017-08-21 16:50:32 +00002
Jakub Josefa6d4c832017-01-19 14:06:12 +01003logger = logging.getLogger(__name__)
4
5config_slack_groovy = """\
6jenkins = jenkins.model.Jenkins.getInstance()
Adam Tengler70763e02017-08-21 16:50:32 +00007try{
Jakub Josefa6d4c832017-01-19 14:06:12 +01008slack = jenkins.getDescriptorByType(jenkins.plugins.slack.SlackNotifier.DescriptorImpl)
Adam Tengler70763e02017-08-21 16:50:32 +00009if(slack.teamDomain.equals("${team_domain}") &&
10 slack.token.equals("${token}") &&
11 slack.tokenCredentialId.equals("${token_credential_id}") &&
12 slack.room.equals("${room}") &&
13 slack.sendAs.equals("${send_as}")){
Jakub Josefa6d4c832017-01-19 14:06:12 +010014 print("EXISTS")
Adam Tengler70763e02017-08-21 16:50:32 +000015}else{
16 slack.teamDomain = "${team_domain}"
17 slack.token = "${token}"
18 slack.tokenCredentialId = "${token_credential_id}"
19 slack.room = "${room}"
20 slack.sendAs = "${send_as}"
Jakub Josefa6d4c832017-01-19 14:06:12 +010021 slack.save()
22 print("SUCCESS")
Adam Tengler70763e02017-08-21 16:50:32 +000023}
24}catch(all){
Jakub Josefa6d4c832017-01-19 14:06:12 +010025 print("Cannot instantiate Jenkins Slack plugin, maybe plugin is not installed")
Adam Tengler70763e02017-08-21 16:50:32 +000026}
27""" # noqa
Jakub Josefa6d4c832017-01-19 14:06:12 +010028
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040029
30def __virtual__():
31 '''
32 Only load if jenkins_common module exist.
33 '''
34 if 'jenkins_common.call_groovy_script' not in __salt__:
35 return (
36 False,
37 'The jenkins_slack state module cannot be loaded: '
38 'jenkins_common not found')
39 return True
40
41
Adam Tengler70763e02017-08-21 16:50:32 +000042def config(name, team_domain, token,
43 token_credential_id="", room="", send_as=None):
Jakub Josefa6d4c832017-01-19 14:06:12 +010044 """
45 Jenkins Slack config state method
46
47 :param name: configuration name
48 :param team_domain: slack team domain
49 :param token: slack token
50 :param token_credential_id: slack token credential id
51 :param room: slack room
52 :param send_as: slack send as param
53 :returns: salt-specified state dict
54 """
55 test = __opts__['test'] # noqa
56 ret = {
57 'name': name,
58 'changes': {},
59 'result': False,
60 'comment': '',
61 }
62 result = False
63 if test:
64 status = "SUCCESS"
65 ret['changes'][name] = status
66 ret['comment'] = 'Jenkins Slack config %s %s' % (name, status.lower())
67 else:
68 call_result = __salt__['jenkins_common.call_groovy_script'](
Adam Tengler70763e02017-08-21 16:50:32 +000069 config_slack_groovy, {"team_domain": team_domain,
70 "token": token,
Jakub Josefa6d4c832017-01-19 14:06:12 +010071 "token_credential_id": token_credential_id if token_credential_id else "",
72 "room": room if room else "",
73 "send_as": send_as if send_as else ""})
Adam Tengler70763e02017-08-21 16:50:32 +000074 if call_result["code"] == 200 and call_result["msg"] in [
75 "SUCCESS", "EXISTS"]:
Jakub Josefa6d4c832017-01-19 14:06:12 +010076 status = call_result["msg"]
77 if status == "SUCCESS":
78 ret['changes'][name] = status
Adam Tengler70763e02017-08-21 16:50:32 +000079 ret['comment'] = 'Jenkins Slack config %s %s' % (
80 name, status.lower())
Jakub Josefa6d4c832017-01-19 14:06:12 +010081 result = True
82 else:
83 status = 'FAILED'
84 logger.error(
85 "Jenkins slack API call failure: %s", call_result["msg"])
86 ret['comment'] = 'Jenkins slack API call failure: %s' % (call_result[
Adam Tengler70763e02017-08-21 16:50:32 +000087 "msg"])
Jakub Josefa6d4c832017-01-19 14:06:12 +010088 ret['result'] = None if test else result
89 return ret