blob: 3f00a36a2c021aae65c69cfd6e3835ea94b0504b [file] [log] [blame]
Jakub Josefb0dd7732017-11-16 14:57:04 +01001import logging
2
3logger = logging.getLogger(__name__)
4
5add_prop_groovy = """\
6instance = Jenkins.getInstance()
7globalNodeProperties = instance.getGlobalNodeProperties()
8envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
9
10newEnvVarsNodeProperty = null
11envVars = null
12
13if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
14 newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
15 globalNodeProperties.add(newEnvVarsNodeProperty)
16 envVars = newEnvVarsNodeProperty.getEnvVars()
17} else {
18 envVars = envVarsNodePropertyList.get(0).getEnvVars()
19}
20if(!envVars.containsKey("${key}") || !envVars.get("${key}").equals("${value}")){
21 envVars.put("${key}", "${value}")
22 instance.save()
23 print("ADDED/CHANGED")
24}else{
25 print("EXISTS")
26}
27""" # noqa
28
29remove_prop_groovy = """\
30instance = Jenkins.getInstance()
31globalNodeProperties = instance.getGlobalNodeProperties()
32envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
33
34newEnvVarsNodeProperty = null
35envVars = null
36
37if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
38 print("NOT PRESENT")
39} else {
40 envVars = envVarsNodePropertyList.get(0).getEnvVars()
41 if(envVars.containsKey("${key}"){
42 envVars.remove("${key}")
43 print("REMOVED")
44 instance.save()
45 }else{
46 print("NOT PRESENT")
47 }
48}
49""" # noqa
50
51
52def __virtual__():
53 '''
54 Only load if jenkins_common module exist.
55 '''
56 if 'jenkins_common.call_groovy_script' not in __salt__:
57 return (
58 False,
59 'The jenkins_globalenvprop state module cannot be loaded: '
60 'jenkins_common not found')
61 return True
62
63
64def present(name, value, **kwargs):
65 """
66 Jenkins global env property present state method
67
68 :param name: global env property name
69 :param value: env property velue
70 :returns: salt-specified state dict
71 """
72 return _plugin_call(name, value, add_prop_groovy, [
73 "ADDED/CHANGED", "EXISTS"], **kwargs)
74
75
76def absent(name, **kwargs):
77 """
78 Jenkins global env property absent state method
79
80 :param name: global env property name
81 :returns: salt-specified state dict
82 """
83 return _plugin_call(name, None, remove_prop_groovy, [
84 "REMOVED", "NOT PRESENT"], **kwargs)
85
86
87def _plugin_call(name, value, template, success_msgs, **kwargs):
88 test = __opts__['test'] # noqa
89 ret = {
90 'name': name,
91 'changes': {},
92 'result': False,
93 'comment': '',
94 }
95 result = False
96 if test:
97 status = success_msgs[0]
98 ret['changes'][name] = status
99 ret['comment'] = 'Jenkins global enviroment property %s %s' % (name, status.lower())
100 else:
101 call_result = __salt__['jenkins_common.call_groovy_script'](
102 template, {"key": name, "value": value})
103 if call_result["code"] == 200 and call_result["msg"] in success_msgs:
104 status = call_result["msg"]
105 if status == success_msgs[0]:
106 ret['changes'][name] = status
107 ret['comment'] = 'Jenkins global enviroment property %s %s' % (name, status.lower())
108 result = True
109 else:
110 status = 'FAILED'
111 logger.error(
112 "Jenkins global enviroment property API call failure: %s", call_result["msg"])
113 ret['comment'] = 'Jenkins global enviroment property API call failure: %s' % (call_result[
114 "msg"])
115 ret['result'] = None if test else result
116 return ret