Merge "Add uploadPpa function"
diff --git a/src/com/mirantis/mk/Common.groovy b/src/com/mirantis/mk/Common.groovy
index 6a01a12..dbff286 100644
--- a/src/com/mirantis/mk/Common.groovy
+++ b/src/com/mirantis/mk/Common.groovy
@@ -32,9 +32,15 @@
  *
  * @param id    Credentials name
  */
-def getCredentials(id) {
+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(
-                    com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
+                    credClass,
                     jenkins.model.Jenkins.instance
                 )
 
diff --git a/src/com/mirantis/mk/Salt.groovy b/src/com/mirantis/mk/Salt.groovy
index 91da71f..5ec5c50 100644
--- a/src/com/mirantis/mk/Salt.groovy
+++ b/src/com/mirantis/mk/Salt.groovy
@@ -261,25 +261,36 @@
  */
 def checkResult(result, failOnError = true) {
     def common = new com.mirantis.mk.Common()
-    for (entry in result['return']) {
-        if (!entry) {
-            if (failOnError) {
-                throw new Exception("Salt API returned empty response: ${result}")
-            } else {
-                common.errorMsg("Salt API returned empty response: ${result}")
+    if(result['return']){
+        for (int i=0;i<result['return'].size();i++) {
+            def entry = result['return'][i]
+            if (!entry) {
+                if (failOnError) {
+                    throw new Exception("Salt API returned empty response: ${result}")
+                } else {
+                    common.errorMsg("Salt API returned empty response: ${result}")
+                }
             }
-        }
-        for (node in entry) {
-            for (resource in node.value) {
-                if(!resource[result] || resource[result] != "true") {
-                    if (failOnError) {
-                        throw new Exception("Salt state on node ${node.key} failed: ${node.value}")
-                    } else {
-                        common.errorMsg("Salt state on node ${node.key} failed: ${node.value}")
+            for (int j=0;j<entry.size();j++) {
+                def node=entry[j]
+                for (int k=0;k<node.value.size();k++) {
+                    def res = node.value[k]
+                    if(node.value[k] instanceof Map.Entry){
+                        res = node.value[k].value
+                    }
+                    common.errorMsg("Checking resource: ${res}")
+                    if(!res["result"] || (res["result"] instanceof String && res["result"] != "true")){
+                        if (failOnError) {
+                            throw new Exception("Salt state on node ${node.key} failed: ${res}. State output: ${node.value}")
+                        } else {
+                            common.errorMsg("Salt state on node ${node.key} failed: ${res}. State output: ${node.value}")
+                        }
                     }
                 }
             }
         }
+    }else{
+        common.errorMsg("Salt result hasn't return attribute! Result: ${result}")
     }
 }
 
@@ -293,30 +304,39 @@
 def printSaltStateResult(result, onlyChanges = true) {
     def common = new com.mirantis.mk.Common()
     def out = [:]
-    for (entry in result['return']) {
-        for (node in entry) {
-            out[node.key] = [:]
-            for (resource in node.value) {
-                if (resource instanceof String) {
-                    out[node.key] = node.value
-                } else if (resource.value.result.toString().toBoolean() == false || resource.value.changes || onlyChanges == false) {
-                    out[node.key][resource.key] = resource.value
+    if(result['return']){
+        for (int i=0; i<result['return'].size(); i++) {
+            def entry = result['return'][i]
+            for (int j=0; j<entry.size(); j++) {
+                common.errorMsg("Entry is: ${entry}")
+                def node=entry[j]
+                out[node.key] = [:]
+                for (int k=0; k<node.value.size(); k++) {
+                    def resource = node.value[k]
+                    if (resource instanceof String) {
+                        out[node.key] = node.value
+                    } else if (resource.value.result.toString().toBoolean() == false || resource.value.changes || onlyChanges == false) {
+                        out[node.key][resource.key] = resource.value
 
-                    //if (resource.value.result.toString().toBoolean() == false && resource.key instanceof String && node.key instanceof String) {
-                    //    common.warningMsg("Resource ${resource.key} failed on node ${node.key}!")
-                    //}
+                        //if (resource.value.result.toString().toBoolean() == false && resource.key instanceof String && node.key instanceof String) {
+                        //    common.warningMsg("Resource ${resource.key} failed on node ${node.key}!")
+                        //}
+                    }
                 }
             }
         }
-    }
 
-    for (node in out) {
-        if (node.value) {
-            println "Node ${node.key} changes:"
-            print new groovy.json.JsonBuilder(node.value).toPrettyString().replace('\\n', System.getProperty('line.separator'))
-        } else {
-            println "No changes for node ${node.key}"
+        for (int i=0; i<out.size(); i++) {
+            def node=out[i]
+            if (node.value) {
+                println "Node ${node.key} changes:"
+                print new groovy.json.JsonBuilder(node.value).toPrettyString().replace('\\n', System.getProperty('line.separator'))
+            } else {
+                println "No changes for node ${node.key}"
+            }
         }
+    }else{
+        common.errorMsg("Salt result hasn't return attribute! Result: ${result}")
     }
 }
 
@@ -327,16 +347,22 @@
  */
 def printSaltCommandResult(result) {
     def out = [:]
-    for (entry in result['return']) {
-        for (node in entry) {
-            out[node.key] = [:]
-            for (resource in node.value) {
-                out[node.key] = node.value
+    if(result['return']){
+        for (int i=0; i<result['return'].size(); i++) {
+            def entry = result['return'][i]
+            for (int j=0; j<entry.size(); j++) {
+                def node=entry[j]
+                out[node.key] = [:]
+                for (int k=0; k<node.value.size(); k++) {
+                    def resource = node.value[k]
+                    //ORIGINAL??out[node.key] = node.value
+                    out[node.key] = resource
+                }
             }
         }
-    }
 
-    for (node in out) {
+    for (int i=0; i<out.size(); i++) {
+        def node=out[i]
         if (node.value) {
             println "Node ${node.key} changes:"
             print new groovy.json.JsonBuilder(node.value).toPrettyString()
@@ -344,4 +370,7 @@
             println "No changes for node ${node.key}"
         }
     }
+        }else{
+        common.errorMsg("Salt result hasn't return attribute! Result: ${result}")
+    }
 }