Add getEquinixMetroWithCapacity method

Bug: PRODX-37318
Change-Id: I38a370f6c932d1bbac2ff2d0bb1b786e316b32ce
diff --git a/src/com/mirantis/mk/KaasUtils.groovy b/src/com/mirantis/mk/KaasUtils.groovy
index f693c27..76e7d0b 100644
--- a/src/com/mirantis/mk/KaasUtils.groovy
+++ b/src/com/mirantis/mk/KaasUtils.groovy
@@ -1317,6 +1317,76 @@
 
 
 /**
+* getEquinixMetroWithCapacity returns list of Equinix metros using specified
+* instance type (nodeType), desired count of metros (metroCount) and
+* instances (nodeCount) in a metro using specified matal version.
+* Function downloads metal CLI from the
+* https://artifactory.mcp.mirantis.net:443/artifactory/binary-dev-kaas-local/core/bin/mirror/metal-${version}-linux
+* Empty list is returned in case of no metros with specified capacity was found or any other errors.
+* Non-empty list is shuffled.
+*
+* @param:        metroCount     (int) Desired count of metros
+* @param:        nodeCount      (int) Desired count of instances
+* @param:        nodeType    (string) Instance type
+* @param:        version     (string) Metal version to use
+* @return                  ([]string) List of selected metros
+*
+**/
+def getEquinixMetroWithCapacity(metroCount = 1, nodeCount = 50, nodeType = 'c3.small.x86', version = '0.9.0') {
+    def common = new com.mirantis.mk.Common()
+    def metalUrl = "https://artifactory.mcp.mirantis.net:443/artifactory/binary-dev-kaas-local/core/bin/mirror/metal-${version}-linux"
+    def metal = './metal --config metal.yaml'
+    def metro = []
+    def out = ''
+    def retries = 3 // number of retries
+    def i = 0
+    def delay = 60 // 1 minute sleep
+    def excludeMetro = [] // list of metros to exclude from selection
+    try {
+        if (excludeMetro.size() > 0) {
+            common.infoMsg("Excluded metros: ${excludeMetros}")
+        }
+        sh "curl -o metal -# ${metalUrl} && chmod +x metal"
+        withCredentials([string(credentialsId: env.KAAS_EQUINIX_API_TOKEN, variable: 'KAAS_EQUINIX_API_TOKEN')]) {
+            sh 'echo "project-id: ${KAAS_EQUINIX_PROJECT_ID}\ntoken: ${KAAS_EQUINIX_API_TOKEN}" >metal.yaml'
+        }
+        while (metro.size() < metroCount && i < retries) {
+            common.infoMsg("Selecting ${metroCount} available Equinix metros with free ${nodeCount} ${nodeType} hosts, try ${i+1}/${retries} ...")
+            if (i > 0) { // skip sleep on first step
+                sleep(delay)
+            }
+            out = sh(script: "${metal} capacity get -m -P ${nodeType}|awk '/${nodeType}/ {print \$2}'|paste -s -d,|xargs ${metal} capacity check -P ${nodeType} -q ${nodeCount} -m|grep true|awk '{print \$2}'|paste -s -d,", returnStdout: true).trim()
+            metro = out.tokenize(',')
+            metro -= excludeMetro
+            if (metro.size() < metroCount) {
+                nodeCount -= 10
+            // We need different metros for the [equinixmetalv2-child-diff-metro] case
+            } else if (metro.size() == 2 && metro[0][0, 1] == metro[1][0, 1]) {
+                nodeCount -= 10
+            }
+            i++
+        }
+        if (metro.size() > 0) {
+            f = metro.size() > 1 ? "${metro[0]},${metro[1]}" : "${metro[0]}"
+            sh "${metal} capacity check -P ${nodeType} -f ${f} -q ${nodeCount}"
+        }
+    } catch (Exception e) {
+        common.errorMsg "Exception: '${e}'"
+        return []
+    } finally {
+        sh 'rm metal.yaml'
+    }
+    if (metro.size() > 0) {
+        Collections.shuffle(metro)
+        common.infoMsg("Selected metros: ${metro}")
+    } else {
+        common.warningMsg('No any metros have been selected !!! :(')
+    }
+    return metro
+}
+
+
+/**
 * getEquinixFacilityWithCapacity returns list of Equinix facilities using specified
 * instance type (nodeType), desired count of facilities (facilityCount) and
 * instances (nodeCount) in a facility using specified matal version.
@@ -1332,7 +1402,9 @@
 * @return                  ([]string) List of selected facilities
 *
 **/
+@Deprecated
 def getEquinixFacilityWithCapacity(facilityCount = 1, nodeCount = 50, nodeType = 'c3.small.x86', version = '0.9.0') {
+    common.warningMsg('You are using deprecated method getEquinixFacilityWithCapacity. Use getEquinixMetroWithCapacity instead')
     def common = new com.mirantis.mk.Common()
     def metalUrl = "https://artifactory.mcp.mirantis.net:443/artifactory/binary-dev-kaas-local/core/bin/mirror/metal-${version}-linux"
     def metal = './metal --config metal.yaml'