Add resource ordering for jenkins.client state

Change-Id: Iaba55d38b53644fa66081b26dc4938e8313ed91a
Related-task: #PROD-21892 (PROD:21892)
diff --git a/_states/jenkins_artifactory.py b/_states/jenkins_artifactory.py
index d88b84a..91dc408 100644
--- a/_states/jenkins_artifactory.py
+++ b/_states/jenkins_artifactory.py
@@ -43,7 +43,7 @@
 delete_artifactory_groovy = u"""\
 def inst = Jenkins.getInstance()
 def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder")
-if(desc.getArtifactoryServers().removeIf{it -> it.name.equals("${name}")}){
+if(desc && desc.getArtifactoryServers().removeIf{it -> it.name.equals("${name}")}){
     print("REMOVED")
 }else{
     print("NOT PRESENT")
diff --git a/jenkins/client/approval.sls b/jenkins/client/approval.sls
index 6969807..640b33d 100644
--- a/jenkins/client/approval.sls
+++ b/jenkins/client/approval.sls
@@ -1,3 +1,4 @@
+{#- It's not recommended to call this state explicitly as it requires plugins #}
 {% from "jenkins/map.jinja" import client with context %}
 {% for approval in client.get("approved_scripts",[]) %}
   approve_jenkins_signature_{{ approval }}:
diff --git a/jenkins/client/artifactory.sls b/jenkins/client/artifactory.sls
index 7f06e20..d6f7bd0 100644
--- a/jenkins/client/artifactory.sls
+++ b/jenkins/client/artifactory.sls
@@ -1,3 +1,4 @@
+{#- It's not recommended to call this state explicitly as it requires plugins and credentials #}
 {% from "jenkins/map.jinja" import client with context %}
 {% for name, artifactory in client.get('artifactory',{}).iteritems() %}
 {% if artifactory.get('enabled', True) %}
diff --git a/jenkins/client/credential.sls b/jenkins/client/credential.sls
index c0962fa..6155eec 100644
--- a/jenkins/client/credential.sls
+++ b/jenkins/client/credential.sls
@@ -1,3 +1,4 @@
+{#- It's not recommended to call this state explicitly as it requires plugins #}
 {% from "jenkins/map.jinja" import client with context %}
 {% for name, cred in client.get('credential',{}).iteritems() %}
 credential_{{ name }}:
diff --git a/jenkins/client/gerrit.sls b/jenkins/client/gerrit.sls
index 24c4abf..3bfe2e4 100644
--- a/jenkins/client/gerrit.sls
+++ b/jenkins/client/gerrit.sls
@@ -1,3 +1,4 @@
+{#- It's not recommended to call this state explicitly as it requires plugins #}
 {% from "jenkins/map.jinja" import client with context %}
 {%- if client.gerrit is defined %}
 {% for name, gerrit in client.get('gerrit',{}).iteritems() %}
diff --git a/jenkins/client/init.sls b/jenkins/client/init.sls
index f68d83d..954c3e2 100644
--- a/jenkins/client/init.sls
+++ b/jenkins/client/init.sls
@@ -1,54 +1,72 @@
 {% from "jenkins/map.jinja" import client with context %}
 {%- if client.enabled %}
 
+jenkins_client_install:
+  pkg.installed:
+  - names: {{ client.pkgs }}
+  - order: 1
+
+jenkins_client_dirs:
+  file.directory:
+  - names:
+    - {{ client.dir.jenkins_source_root }}
+    - {{ client.dir.jenkins_jobs_root }}
+  - makedirs: true
+
 include:
-{%- if client.plugin is defined %}
-  - jenkins.client.plugin
-{%- endif %}
-{%- if client.security is defined %}
-  - jenkins.client.security
-{%- endif %}
+# independent statements
 {%- if client.source is defined %}
   - jenkins.client.source
 {%- endif %}
-{%- if client.credential is defined %}
-  - jenkins.client.credential
+{%- if client.globalenvprop is defined %}
+  - jenkins.client.globalenvprop
+{%- endif %}
+{%- if client.plugin is defined %}
+  - jenkins.client.plugin
 {%- endif %}
 {%- if client.user is defined %}
   - jenkins.client.user
 {%- endif %}
-{%- if client.node is defined %}
-  - jenkins.client.node
+# credential statement depends only on plugin
+{%- if client.credential is defined %}
+  - jenkins.client.credential
 {%- endif %}
-{%- if client.view is defined %}
-  - jenkins.client.view
-{%- endif %}
-{%- if client.smtp is defined %}
-  - jenkins.client.smtp
-{%- endif %}
-{%- if client.slack is defined %}
-  - jenkins.client.slack
-{%- endif %}
-{%- if client.lib is defined %}
-  - jenkins.client.lib
-{%- endif %}
-{%- if client.theme is defined %}
-  - jenkins.client.theme
-{%- endif %}
+# below are statements which depends on plugin and/or on credential
 {%- if client.approved_scripts is defined %}
   - jenkins.client.approval
 {%- endif %}
 {%- if client.artifactory is defined %}
   - jenkins.client.artifactory
 {%- endif %}
-{%- if client.globalenvprop is defined %}
-  - jenkins.client.globalenvprop
+{%- if client.gerrit is defined %}
+  - jenkins.client.gerrit
+{%- endif %}
+{%- if client.jira is defined %}
+  - jenkins.client.jira
+{%- endif %}
+{%- if client.lib is defined %}
+  - jenkins.client.lib
+{%- endif %}
+{%- if client.node is defined %}
+  - jenkins.client.node
+{%- endif %}
+{%- if client.security is defined %}
+  - jenkins.client.security
+{%- endif %}
+{%- if client.slack is defined %}
+  - jenkins.client.slack
+{%- endif %}
+{%- if client.smtp is defined %}
+  - jenkins.client.smtp
+{%- endif %}
+{%- if client.theme is defined %}
+  - jenkins.client.theme
 {%- endif %}
 {%- if client.throttle_category is defined %}
   - jenkins.client.throttle_category
 {%- endif %}
-{%- if client.jira is defined %}
-  - jenkins.client.jira
+{%- if client.view is defined %}
+  - jenkins.client.view
 {%- endif %}
 
 # execute job enforcements as last
@@ -59,20 +77,4 @@
   - jenkins.client.job_template
 {%- endif %}
 
-{%- if client.gerrit is defined %}
-  - jenkins.client.gerrit
-{%- endif %}
-
-
-jenkins_client_install:
-  pkg.installed:
-  - names: {{ client.pkgs }}
-
-jenkins_client_dirs:
-  file.directory:
-  - names:
-    - {{ client.dir.jenkins_source_root }}
-    - {{ client.dir.jenkins_jobs_root }}
-  - makedirs: true
-
 {%- endif %}
diff --git a/jenkins/client/jira.sls b/jenkins/client/jira.sls
index 39d7105..80bdade 100644
--- a/jenkins/client/jira.sls
+++ b/jenkins/client/jira.sls
@@ -1,3 +1,4 @@
+{#- It's not recommended to call this state explicitly as it requires plugins #}
 {% from "jenkins/map.jinja" import client with context %}
 {% if client.jira.get('enabled', True) %}
 jenkins_jira_enable:
diff --git a/jenkins/client/job.sls b/jenkins/client/job.sls
index f9b1541..4dbe75d 100644
--- a/jenkins/client/job.sls
+++ b/jenkins/client/job.sls
@@ -1,8 +1,7 @@
+{#- It's not recommended to call this state explicitly as it should be called
+ in the end of Jenkins instance configuration #}
 {% from "jenkins/map.jinja" import client with context %}
 
-include:
-  - jenkins.client
-
 {%- for job_name, job in client.get('job', {}).iteritems() %}
   {%- include "jenkins/client/_job.sls" %}
 {%- endfor %}
diff --git a/jenkins/client/job_template.sls b/jenkins/client/job_template.sls
index 3f8acea..74c8cfe 100644
--- a/jenkins/client/job_template.sls
+++ b/jenkins/client/job_template.sls
@@ -1,3 +1,5 @@
+{#- It's not recommended to call this state explicitly as it should be called
+ in the end of Jenkins instance configuration #}
 {% from "jenkins/map.jinja" import client with context %}
 
 {%- if salt['pillar.get']('job_template_name', False) %}
diff --git a/jenkins/client/lib.sls b/jenkins/client/lib.sls
index fd27d28..b17d844 100644
--- a/jenkins/client/lib.sls
+++ b/jenkins/client/lib.sls
@@ -1,3 +1,4 @@
+{#- It's not recommended to call this state explicitly as it requires plugins and credentials #}
 {% from "jenkins/map.jinja" import client with context %}
 {% for name, lib in client.get("lib",{}).iteritems() %}
 {%- if lib.enabled|default(True) %}
diff --git a/jenkins/client/node.sls b/jenkins/client/node.sls
index 1fa2ce1..f801a70 100644
--- a/jenkins/client/node.sls
+++ b/jenkins/client/node.sls
@@ -1,3 +1,4 @@
+{#- It's not recommended to call this state explicitly as it may require plugins #}
 {% from "jenkins/map.jinja" import client with context %}
 {% for name, node in client.get("node",{}).iteritems() %}
 {% if node.get('name', name) == "master" %}
diff --git a/jenkins/client/security.sls b/jenkins/client/security.sls
index 4b9cc41..d86adcd 100644
--- a/jenkins/client/security.sls
+++ b/jenkins/client/security.sls
@@ -1,3 +1,4 @@
+{#- It's not recommended to call this state explicitly as it requires plugins #}
 {%- from "jenkins/map.jinja" import client with context %}
 {%- if client.security.ldap is defined %}
 set_jenkins_ldap:
diff --git a/jenkins/client/slack.sls b/jenkins/client/slack.sls
index ed71773..ec37b0f 100644
--- a/jenkins/client/slack.sls
+++ b/jenkins/client/slack.sls
@@ -1,3 +1,4 @@
+{#- 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.slack is defined %}
 config_jenkins_slack:
diff --git a/jenkins/client/smtp.sls b/jenkins/client/smtp.sls
index ae378eb..d289ff6 100644
--- a/jenkins/client/smtp.sls
+++ b/jenkins/client/smtp.sls
@@ -1,3 +1,4 @@
+{#- It's not recommended to call this state explicitly as it requires plugins #}
 {%- from "jenkins/map.jinja" import client with context %}
 {%- if client.smtp is defined %}
 set_jenkins_smtp:
diff --git a/jenkins/client/source.sls b/jenkins/client/source.sls
index 71e6500..1005e74 100644
--- a/jenkins/client/source.sls
+++ b/jenkins/client/source.sls
@@ -1,8 +1,6 @@
+{#- This state isn't designed to be called explicitly #}
 {% from "jenkins/map.jinja" import client with context %}
 
-include:
-  - jenkins.client
-
 {%- for source_name, source in client.get('source', {}).iteritems() %}
 
 {%- if source.engine == "git" %}
@@ -13,6 +11,8 @@
   - target: {{ client.dir.jenkins_source_root }}/{{ source_name }}
   - rev: {{ source.branch }}
   - reload_pillar: True
+  - require:
+    - jenkins_client_dirs
 
 {%- elif client.source.engine == "local" %}
 
@@ -20,6 +20,8 @@
   file.managed:
   - name: {{ client.dir.jenkins_source_root }}/{{ source_name }}
   - mode: 700
+  - require:
+    - jenkins_client_dirs
 
 {%- endif %}
 
diff --git a/jenkins/client/theme.sls b/jenkins/client/theme.sls
index 7707add..61a1dfa 100644
--- a/jenkins/client/theme.sls
+++ b/jenkins/client/theme.sls
@@ -1,3 +1,4 @@
+{#- It's not recommended to call this state explicitly as it requires plugins #}
 {%- from "jenkins/map.jinja" import client with context %}
 {%- if client.theme is defined %}
 set_jenkins_theme:
diff --git a/jenkins/client/throttle_category.sls b/jenkins/client/throttle_category.sls
index de508a0..246a350 100644
--- a/jenkins/client/throttle_category.sls
+++ b/jenkins/client/throttle_category.sls
@@ -1,3 +1,4 @@
+{#- It's not recommended to call this state explicitly as it requires plugins #}
 {% from "jenkins/map.jinja" import client with context %}
 {% for name, throttle_category in client.get("throttle_category",{}).iteritems() %}
 {% if throttle_category.get('enabled', True) %}
@@ -7,8 +8,6 @@
     - max_total: {{ throttle_category.get('max_total', 0) }}
     - max_per_node: {{ throttle_category.get('max_per_node', 0) }}
     - labels: {{ throttle_category.get('max_per_label',[]) }}
-    - require:
-      - jenkins_client_install
 {% else %}
 'throttle_category_{{ name }}_disable':
    jenkins_throttle_category.absent:
diff --git a/jenkins/client/view.sls b/jenkins/client/view.sls
index 7ac0b07..3fcb549 100644
--- a/jenkins/client/view.sls
+++ b/jenkins/client/view.sls
@@ -1,3 +1,4 @@
+{#- It's not recommended to call this state explicitly as it requires plugins #}
 {% from "jenkins/map.jinja" import client with context %}
 {% for name, view in client.get('view',{}).iteritems() %}
 {% if view.get('enabled', True) %}
diff --git a/jenkins/master/user.sls b/jenkins/master/user.sls
index 96639c3..6524558 100644
--- a/jenkins/master/user.sls
+++ b/jenkins/master/user.sls
@@ -5,11 +5,15 @@
 {{ master.home }}/users/{{ user_name }}:
   file.directory:
   - makedirs: true
+  - user: jenkins
+  - group: jenkins
 
 {{ master.home }}/users/{{ user_name }}/config.xml:
   file.managed:
   - source: salt://jenkins/files/config.xml.user
   - template: jinja
+  - user: jenkins
+  - group: jenkins
   - require:
     - file: {{ master.home }}/users/{{ user_name }}
   - defaults:
@@ -19,7 +23,9 @@
   - unless: test -e {{ master.home }}/users/{{ user_name }}/.config_created
 
 {{ master.home }}/users/{{ user_name }}/.config_created:
-  file.touch:
+  file.managed:
+  - user: jenkins
+  - group: jenkins
   - require:
     - file: {{ master.home }}/users/{{ user_name }}/config.xml
   - unless: test -e {{ master.home }}/users/{{ user_name }}/.config_created