blob: dc941e905ee1803894bd5aedac9ace0119e20ce5 [file] [log] [blame]
import logging
from json import dumps
logger = logging.getLogger(__name__)
add_view_groovy = """\
import java.util.stream.Collectors
import org.jenkinsci.plugins.categorizedview.CategorizedJobsView
import org.jenkinsci.plugins.categorizedview.GroupingRule
view = Jenkins.instance.getView("{view_name}")
if(view){{
if(view.getClass().getName().equals("hudson.model.ListView")){{
include_regex="{include_regex}"
if(include_regex != "" && !view.getIncludeRegex().equals(include_regex)){{
view.setIncludeRegex(include_regex)
print("ADDED/CHANGED")
}}else{{
print("EXISTS")
}}
}}else if(view.getClass().getName().equals("org.jenkinsci.plugins.categorizedview.CategorizedJobsView")){{
def jsonSlurper = new groovy.json.JsonSlurper()
def inputCategories = jsonSlurper.parseText('{categories_string}')
def groupRegexes = inputCategories.stream().map{{e -> e["group_regex"]}}.collect(Collectors.toList())
def namingRules = inputCategories.stream().map{{e -> e["naming_rule"]}}.collect(Collectors.toList())
def actualCategories = view.categorizationCriteria
def equals = !actualCategories.isEmpty()
def include_regex="{include_regex}"
if(include_regex != "" && !view.getIncludeRegex().equals(include_regex)){{
view.setIncludeRegex(include_regex)
equals = false
}}
for(int i=0;i<actualCategories.size();i++){{
if(!groupRegexes.contains(actualCategories[i].groupRegex) || !namingRules.contains(actualCategories[i].namingRule)){{
equals = false
}}
}}
if(!equals){{
view.categorizationCriteria.clear()
for(int i=0;i<inputCategories.size();i++){{
view.categorizationCriteria.add(new GroupingRule(inputCategories[i].group_regex,inputCategories[i].naming_rule))
}}
print("ADDED/CHANGED")
}}else{{
print("EXISTS")
}}
}}else{{
print("EXISTS")
}}
}}else{{
try{{
{view_def}
Jenkins.instance.addView(view)
print("ADDED/CHANGED")
}}catch(Exception e){{
print("FAILED")
}}
}}
""" # noqa
remove_view_groovy = """\
view = Jenkins.instance.getView("{view_name}")
if(view){{
try{{
Jenkins.instance.deleteView(view)
print("REMOVED")
}}catch(Exception e){{
print("FAILED")
}}
}}else{{
print("NOT PRESENT")
}}
""" # noqa
def __virtual__():
'''
Only load if jenkins_common module exist.
'''
if 'jenkins_common.call_groovy_script' not in __salt__:
return (
False,
'The jenkins_view state module cannot be loaded: '
'jenkins_common not found')
return True
def present(name, type="ListView", **kwargs):
"""
Jenkins view present state method
:param name: view name
:param type: view type (default ListView)
:returns: salt-specified state dict
"""
return _plugin_call(name, type, add_view_groovy, ["ADDED/CHANGED", "EXISTS"], **kwargs)
def absent(name, **kwargs):
"""
Jenkins view absent state method
:param name: view name
:returns: salt-specified state dict
"""
return _plugin_call(name, None, remove_view_groovy, ["REMOVED", "NOT PRESENT"], **kwargs)
def _plugin_call(name, type, template, success_msgs, **kwargs):
test = __opts__['test'] # noqa
ret = {
'name': name,
'changes': {},
'result': False,
'comment': '',
}
result = False
if test:
status = success_msgs[0]
ret['changes'][name] = status
ret['comment'] = 'Jenkins view %s %s' % (name, status.lower())
else:
view_def = "view = new {}(\"{}\")".format(type, name)
# handle view specific params
include_regex = kwargs.get('include_regex')
categories_string = ""
if type == "ListView":
if include_regex:
view_def += "\nview.setIncludeRegex(\"{}\")".format(
include_regex)
if type == "CategorizedJobsView":
# add imports for categorized views
if include_regex:
view_def += "\nview.setIncludeRegex(\"{}\")".format(
include_regex)
categories = kwargs.get('categories', [])
for category in categories:
view_def += "\nview.categorizationCriteria.add(new GroupingRule(\"{}\", \"{}\"))".format(
category["group_regex"], category["naming_rule"])
# create catogories string readable in groovy
categories_string = dumps(categories)
call_result = __salt__['jenkins_common.call_groovy_script'](
template, {"view_def": view_def, "view_name": name, "type": type if type else "", "include_regex": include_regex if include_regex else "", "categories_string": categories_string if categories_string else ""})
if call_result["code"] == 200 and call_result["msg"] in success_msgs:
status = call_result["msg"]
if status == success_msgs[0]:
ret['changes'][name] = status
ret['comment'] = 'Jenkins view %s %s' % (name, status.lower())
result = True
else:
status = 'FAILED'
logger.error(
"Jenkins view API call failure: %s", call_result["msg"])
ret['comment'] = 'Jenkins view API call failure: %s' % (call_result[
"msg"])
ret['result'] = None if test else result
return ret