Implemented artifactory server enforcing

Change-Id: Ia1062208f3e4d95a62220e684c8fb96605742001
diff --git a/_states/jenkins_artifactory.py b/_states/jenkins_artifactory.py
new file mode 100644
index 0000000..03a015c
--- /dev/null
+++ b/_states/jenkins_artifactory.py
@@ -0,0 +1,97 @@
+import logging
+logger = logging.getLogger(__name__)
+
+add_artifactory_groovy = u"""\
+import jenkins.model.*
+import org.jfrog.*
+import org.jfrog.hudson.*
+def inst = Jenkins.getInstance()
+def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder")
+def server =  desc.getArtifactoryServers().find{{it -> it.name.equals("{name}")}}
+if(server &&
+   server.getName().equals("{name}") &&
+   server.getUrl().equals("{serverUrl}") &&
+   server.getDeployerCredentialsConfig().getCredentialsId().equals("{credentialsId}") &&
+   server.getResolverCredentialsConfig().getCredentialsId().equals("{credentialsId}")){{
+        print("EXISTS")
+}}else{{
+    desc.getArtifactoryServers().removeIf{{it -> it.name.equals("{name}")}}
+    def newServer = new ArtifactoryServer(
+      "{name}",
+      "{serverUrl}",
+      new CredentialsConfig("", "", "{credentialsId}"),
+      new CredentialsConfig("", "", "{credentialsId}"),
+      300,
+      false,
+      null)
+    desc.getArtifactoryServers().add(newServer)
+    desc.save()
+    print("ADDED/CHANGED")
+}}
+"""  # noqa
+
+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}")}}){{
+    print("REMOVED")
+}}else{{
+    print("NOT PRESENT")
+}}
+"""  # noqa
+
+
+def present(name, url, credential_id, **kwargs):
+    """
+    Jenkins artifactory present state method
+
+    :param name: artifactory server name
+    :param url: artifactory server url
+    :param credential_id: artifactory server credential id
+    :returns: salt-specified state dict
+    """
+    return _plugin_call(name, url, credential_id, add_artifactory_groovy, ["ADDED/CHANGED", "EXISTS"], **kwargs)
+
+
+def absent(name, **kwargs):
+    """
+    Jenkins artifactory present state method
+
+    :param name: artifactory server name
+    :returns: salt-specified state dict
+    """
+    return _plugin_call(name, None, None, delete_artifactory_groovy, ["REMOVED", "NOT PRESENT"], **kwargs)
+
+
+def _plugin_call(name, url, credentialsId, 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 artifactory server %s %s' % (
+            name, status.lower())
+    else:
+        call_result = __salt__['jenkins_common.call_groovy_script'](
+            template, {"name": name, "serverUrl": url, "credentialsId": credentialsId})
+        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 artifactory server %s %s' % (
+                name, status.lower())
+            result = True
+        else:
+            status = 'FAILED'
+            logger.error(
+                "Jenkins artifactory API call failure: %s", call_result["msg"])
+            ret['comment'] = 'Jenkins artifactory API call failure: %s' % (call_result[
+                "msg"])
+    ret['result'] = None if test else result
+    return ret