Refactor functions getting credentials by ID

 - add new improved function having support for much more credentials types
 - rewrite existing functions to use the new one

Change-Id: I75222240104291703da509cbe206d0a24f3c767a
diff --git a/src/com/mirantis/mk/Common.groovy b/src/com/mirantis/mk/Common.groovy
index 1aa6732..15676a9 100644
--- a/src/com/mirantis/mk/Common.groovy
+++ b/src/com/mirantis/mk/Common.groovy
@@ -55,30 +55,43 @@
 }
 
 /**
+ *
+ * Find credentials by ID
+ *
+ * @param credsId    Credentials ID
+ * @param credsType  Credentials type (optional)
+ *
+ */
+def getCredentialsById(String credsId, String credsType = 'any') {
+    def credClasses = [ // ordered by class name
+        sshKey:     com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.class,
+        cert:       com.cloudbees.plugins.credentials.common.CertificateCredentials.class,
+        password:   com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
+        any:        com.cloudbees.plugins.credentials.impl.BaseStandardCredentials.class,
+        dockerCert: org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials.class,
+        file:       org.jenkinsci.plugins.plaincredentials.FileCredentials.class,
+        string:     org.jenkinsci.plugins.plaincredentials.StringCredentials.class,
+    ]
+    return com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
+        credClasses[credsType],
+        jenkins.model.Jenkins.instance
+    ).findAll {cred -> cred.id == credsId}[0]
+}
+
+/**
  * Get credentials from store
  *
  * @param id    Credentials name
  */
 def getCredentials(id, cred_type = "username_password") {
-    def credClass;
-    if(cred_type == "username_password"){
-        credClass = com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class
-    }else if(cred_type == "key"){
-        credClass = com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.class
-    }
-    def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
-                    credClass,
-                    jenkins.model.Jenkins.instance
-                )
+    warningMsg('You are using obsolete function. Please switch to use `getCredentialsById()`')
 
-    for (Iterator<String> credsIter = creds.iterator(); credsIter.hasNext();) {
-        c = credsIter.next();
-        if ( c.id == id ) {
-            return c;
-        }
-    }
+    type_map = [
+        username_password: 'password',
+        key:               'sshKey',
+    ]
 
-    throw new Exception("Could not find credentials for ID ${id}")
+    return getCredentialsById(id, type_map[cred_type])
 }
 
 /**
@@ -258,19 +271,7 @@
  * @param id    Credentials name
  */
 def getPasswordCredentials(id) {
-    def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
-                    com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
-                    jenkins.model.Jenkins.instance
-                )
-
-    for (Iterator<String> credsIter = creds.iterator(); credsIter.hasNext();) {
-        c = credsIter.next();
-        if ( c.id == id ) {
-            return c;
-        }
-    }
-
-    throw new Exception("Could not find credentials for ID ${id}")
+    return getCredentialsById(id, 'password')
 }
 
 /**
@@ -279,19 +280,7 @@
  * @param id    Credentials name
  */
 def getSshCredentials(id) {
-    def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
-                    com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
-                    jenkins.model.Jenkins.instance
-                )
-
-    for (Iterator<String> credsIter = creds.iterator(); credsIter.hasNext();) {
-        c = credsIter.next();
-        if ( c.id == id ) {
-            return c;
-        }
-    }
-
-    throw new Exception("Could not find credentials for ID ${id}")
+    return getCredentialsById(id, 'sshKey')
 }
 
 /**