Add GLOBAL configurable durability setting for pipelines

Prod related: PROD-21893 (PROD:21893)

Change-Id: Iffa3fa9b52a2494a9f08015f0ba3e9bb9f5c1b86
diff --git a/_states/jenkins_flowdurability.py b/_states/jenkins_flowdurability.py
new file mode 100644
index 0000000..2f261aa
--- /dev/null
+++ b/_states/jenkins_flowdurability.py
@@ -0,0 +1,35 @@
+import logging
+
+logger = logging.getLogger(__name__)
+
+def __virtual__():
+    '''
+    Only load if jenkins_common module exist.
+    '''
+    if 'jenkins_common.call_groovy_script' not in __salt__:
+        return (
+            False,
+            'The jenkins_flowdurability state module cannot be loaded: '
+            'jenkins_common not found')
+    return True
+
+
+def flowdurability(name, flowdurabilitylevel):
+    """
+    Jenkins workflow plugin flow durability level config state method
+
+    :flowdurabilitylevel: Flow Durability Level:
+    MAX_SURVIVABILITY
+    PERFORMANCE_OPTIMIZED
+    SURVIVABLE_NONATOMIC
+    :returns: salt-specified state dict
+    """
+
+    template = __salt__['jenkins_common.load_template'](
+        'salt://jenkins/files/groovy/flowdurability.template',
+        __env__)
+
+    return __salt__['jenkins_common.api_call'](name, template,
+                        ['CHANGED', 'EXISTS'],
+                        {'flowdurabilitylevel': flowdurabilitylevel},
+                        'FlowDurabilityLevel config')
diff --git a/jenkins/client/flowdurability.sls b/jenkins/client/flowdurability.sls
new file mode 100644
index 0000000..686f08d
--- /dev/null
+++ b/jenkins/client/flowdurability.sls
@@ -0,0 +1,8 @@
+{#- It's not recommended to call this state explicitly as it requires plugins #}
+{%- from "jenkins/map.jinja" import client with context %}
+
+{%- if client.flowdurabilitylevel is defined %}
+set_jenkins_flowdurability_level:
+  jenkins_flowdurability.flowdurability:
+    - flowdurabilitylevel: {{ client.flowdurabilitylevel }}
+{%- endif %}
diff --git a/jenkins/client/init.sls b/jenkins/client/init.sls
index 2ab41bc..9c8509c 100644
--- a/jenkins/client/init.sls
+++ b/jenkins/client/init.sls
@@ -68,6 +68,9 @@
 {%- if client.view is defined %}
   - jenkins.client.view
 {%- endif %}
+{%- if client.flowdurabilitylevel is defined %}
+  - jenkins.client.flowdurability
+{%- endif %}
 
 # execute job enforcements as last
 {%- if client.job is defined %}
diff --git a/jenkins/files/groovy/flowdurability.template b/jenkins/files/groovy/flowdurability.template
new file mode 100644
index 0000000..1014f48
--- /dev/null
+++ b/jenkins/files/groovy/flowdurability.template
@@ -0,0 +1,19 @@
+#!groovy
+
+import jenkins.model.Jenkins
+import org.jenkinsci.plugins.workflow.flow.GlobalDefaultFlowDurabilityLevel
+import org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint
+
+def jenkins = Jenkins.instance
+
+GlobalDefaultFlowDurabilityLevel.DescriptorImpl level = jenkins.getExtensionList(GlobalDefaultFlowDurabilityLevel.DescriptorImpl.class).get(0)
+
+String flowDurabilityLevel = "${flowdurabilitylevel}"
+String currentLevel = org.jenkinsci.plugins.workflow.flow.GlobalDefaultFlowDurabilityLevel.getDefaultDurabilityHint()
+
+if (currentLevel.equals(flowDurabilityLevel)) {
+    print("EXISTS")
+} else {
+    level.setDurabilityHint(FlowDurabilityHint[flowDurabilityLevel])
+    print("CHANGED")
+}
diff --git a/tests/pillar/client.sls b/tests/pillar/client.sls
index 8e5ea64..3d2cd31 100644
--- a/tests/pillar/client.sls
+++ b/tests/pillar/client.sls
@@ -1,6 +1,7 @@
 jenkins:
   client:
     enabled: true
+    flowdurabilitylevel: PERFORMANCE_OPTIMIZED
     master:
       host: jenkins.example.com
       port: 80