Add timestamper plugin management to jenkins

Related-Prod: PROD-34521
Change-Id: I74f5c4bb323df1f31953a376045e7c876b3c8f76
diff --git a/_states/jenkins_timestamper.py b/_states/jenkins_timestamper.py
new file mode 100644
index 0000000..5e41e79
--- /dev/null
+++ b/_states/jenkins_timestamper.py
@@ -0,0 +1,122 @@
+import logging
+
+logger = logging.getLogger(__name__)
+
+test_timestamper_groovy = """\
+import hudson.plugins.timestamper.TimestamperConfig
+
+def timestamper = TimestamperConfig.get()
+if (timestamper.isAllPipelines()) {
+  print("ENABLED")
+} else {
+  print("DISABLED")
+}
+"""
+
+enable_timestamper_groovy = """\
+import hudson.plugins.timestamper.TimestamperConfig
+
+def timestamper = TimestamperConfig.get()
+if (!timestamper.isAllPipelines()) {
+  timestamper.setAllPipelines(true)
+  print("ENABLED")
+} else {
+  print("ALREADY ENABLED")
+}
+"""
+
+disable_timestamper_groovy = """\
+import hudson.plugins.timestamper.TimestamperConfig
+
+def timestamper = TimestamperConfig.get()
+if (timestamper.isAllPipelines()) {
+  timestamper.setAllPipelines(false)
+  print("DISABLED")
+} else {
+  print("ALREADY DISABLED")
+}
+"""
+
+def __virtual__():
+    '''
+    Only load if jenkins_common module exist.
+    '''
+    if 'jenkins_common.call_groovy_script' not in __salt__:
+        return (
+            False,
+            'The jenkins_timestamper state module cannot be loaded: '
+            'jenkins_common not found')
+    return True
+
+
+def present(name='timestamper'):
+    test = __opts__['test']  # noqa
+    ret = {
+        'name': name,
+        'changes': {},
+        'result': False,
+        'comment': '',
+    }
+    result = False
+    if test:
+        call_result = __salt__['jenkins_common.call_groovy_script'](
+            test_timestamper_groovy, {})
+        if call_result["code"] == 200 and call_result["msg"] in [
+                "DISABLED"]:
+            ret['comment'] = 'Jenkins timestamper will be enabled'
+    else:
+        call_result = __salt__['jenkins_common.call_groovy_script'](
+            enable_timestamper_groovy, {})
+        if call_result["code"] == 200 and call_result["msg"] in [
+                "ENABLED", "ALREADY ENABLED"]:
+            status = call_result["msg"]
+            if status == "ENABLED":
+                ret['changes'][name] = status
+            ret['comment'] = 'Jenkins timestamper config %s %s' % (
+                name, status.lower())
+            result = True
+        else:
+            status = 'FAILED'
+            logger.error(
+                "Jenkins timestamper config API call failure: %s", call_result["msg"])
+            ret['comment'] = 'Jenkins timestamper config API call failure: %s' % (call_result[
+                "msg"])
+    ret['result'] = None if test else result
+    return ret
+
+
+def absent(name='timestamper'):
+    test = __opts__['test']  # noqa
+    ret = {
+        'name': name,
+        'changes': {},
+        'result': False,
+        'comment': '',
+    }
+    result = False
+    if test:
+        call_result = __salt__['jenkins_common.call_groovy_script'](
+            test_timestamper_groovy, {})
+        if call_result["code"] == 200 and call_result["msg"] in [
+                "ENABLED"]:
+            ret['comment'] = 'Jenkins timestamper will be disabled'
+    else:
+        call_result = __salt__['jenkins_common.call_groovy_script'](
+            disable_timestamper_groovy, {})
+        if call_result["code"] == 200 and call_result["msg"] in [
+                "DISABLED", "ALREADY DISABLED"]:
+            status = call_result["msg"]
+            if status == "DISABLED":
+                ret['changes'][name] = status
+            ret['comment'] = 'Jenkins timestamper config %s %s' % (
+                name, status.lower())
+            result = True
+        else:
+            status = 'FAILED'
+            logger.error(
+                "Jenkins timestamper config API call failure: %s", call_result["msg"])
+            ret['comment'] = 'Jenkins timestamper config API call failure: %s' % (call_result[
+                "msg"])
+    ret['result'] = None if test else result
+    return ret
+
diff --git a/jenkins/client/init.sls b/jenkins/client/init.sls
index 99b2897..06ab4b5 100644
--- a/jenkins/client/init.sls
+++ b/jenkins/client/init.sls
@@ -86,4 +86,8 @@
   - jenkins.client.job_template
 {%- endif %}
 
+{%- if client.timestamper is defined %}
+  - jenkins.client.timestamper
+{%- endif %}
+
 {%- endif %}
diff --git a/jenkins/client/timestamper.sls b/jenkins/client/timestamper.sls
new file mode 100644
index 0000000..8739b29
--- /dev/null
+++ b/jenkins/client/timestamper.sls
@@ -0,0 +1,9 @@
+{#- It's not recommended to call this state explicitly as it requires plugins and credentials #}
+{% from "jenkins/map.jinja" import client with context %}
+{%- if client.timestamper.get('enabled', True) %}
+  timestamper_enable:
+    jenkins_timestamper.present
+{%- else %}
+  timestamper_disable:
+    jenkins_timestamper.absent
+{%- endif %}