Add configuration for jenkins location

Location url is used to let Jenkins
know how to refer to itself

Change-Id: I44ac0d271d38e4c04721df605142f44345736cf2
Related-bug: PROD-26834
             PROD-30275
(cherry picked from commit I44ac0d271d38e4c04721df605142f44345736cf2)
diff --git a/README.rst b/README.rst
index d21f47f..42bcbeb 100644
--- a/README.rst
+++ b/README.rst
@@ -188,6 +188,8 @@
           host: jenkins.example.com
           port: 80
           protocol: http
+        location:
+          url: http://jenkins.example.com:80
         job:
           jobname:
             type: workflow
diff --git a/_states/jenkins_location.py b/_states/jenkins_location.py
new file mode 100644
index 0000000..7aac8bf
--- /dev/null
+++ b/_states/jenkins_location.py
@@ -0,0 +1,34 @@
+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_location state module cannot be loaded: '
+            'jenkins_common not found')
+    return True
+
+
+def config(name, url, email=''):
+    """
+    Jenkins location configuration. This value is used to
+    let Jenkins know how to refer to itself.
+
+    :url: Jenkins location url:
+    :email: Jenkins server email
+    :returns: salt-specified state dict
+    """
+
+    template = __salt__['jenkins_common.load_template'](
+        'salt://jenkins/files/groovy/location.template',
+        __env__)
+
+    return __salt__['jenkins_common.api_call'](name, template,
+                        ['CHANGED', 'EXISTS'],
+                        {'url': url, 'email': email},
+                        'location config')
diff --git a/jenkins/client/init.sls b/jenkins/client/init.sls
index 9c8509c..85cacb3 100644
--- a/jenkins/client/init.sls
+++ b/jenkins/client/init.sls
@@ -62,6 +62,9 @@
 {%- if client.theme is defined %}
   - jenkins.client.theme
 {%- endif %}
+{%- if client.location is defined %}
+  - jenkins.client.location
+{%- endif %}
 {%- if client.throttle_category is defined %}
   - jenkins.client.throttle_category
 {%- endif %}
diff --git a/jenkins/client/location.sls b/jenkins/client/location.sls
new file mode 100644
index 0000000..29bdab9
--- /dev/null
+++ b/jenkins/client/location.sls
@@ -0,0 +1,7 @@
+{%- from "jenkins/map.jinja" import client with context %}
+{%- if client.location is defined %}
+set_jenkins_location:
+  jenkins_location.config:
+    - url: {{ client.location.url }}
+    - email: {{ client.location.get('email', '') }}
+{%- endif %}
diff --git a/jenkins/files/groovy/location.template b/jenkins/files/groovy/location.template
new file mode 100644
index 0000000..2e26bbb
--- /dev/null
+++ b/jenkins/files/groovy/location.template
@@ -0,0 +1,22 @@
+#!groovy
+
+import jenkins.model.JenkinsLocationConfiguration
+
+String url = "${url}"
+String email = "${email}"
+def LC = JenkinsLocationConfiguration.getOrDie()
+def changed = false
+String jenkinsUrl = url.endsWith('/') ?: "${url}/"
+if (LC) {
+    if (jenkinsUrl != LC.getUrl()) {
+        LC.setUrl(jenkinsUrl)
+        changed = true
+    }
+    if (email != '' && email != LC.getAdminAddress()) {
+        LC.setAdminAddress(email)
+        changed = true
+    }
+}
+
+String message = changed ? "CHANGED" : "EXISTS"
+print(message)
diff --git a/tests/pillar/client.sls b/tests/pillar/client.sls
index 3d2cd31..cdaeec7 100644
--- a/tests/pillar/client.sls
+++ b/tests/pillar/client.sls
@@ -6,6 +6,8 @@
       host: jenkins.example.com
       port: 80
       protocol: http
+    location:
+      url: http://jenkins.example.com:80
     job:
       jobname:
         type: workflow