Last active
June 25, 2019 14:32
-
-
Save ranrubin/454012306fe8156dc69ab072f967c842 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.foo | |
/** A map that holds all constants and data members that can be override when constructing */ | |
consulEnv | |
/** | |
* Imitates a constructor | |
* Defines an instance of Consul object. All according to api | |
* @param ip - ip for consul | |
* @param port - port for consul | |
* @return | |
*/ | |
def construct(String ip, String port){ | |
consulEnv = [ | |
ip : ip, | |
port: port, | |
base_url: '', | |
debug: false // default value that can be overwritten | |
] | |
consulEnv.put('base_url', "http://${ip}:${port}/v1") | |
} | |
def getConsulEnv(){ | |
return consulEnv | |
} | |
def mergeMaps(Map baseMap, Map otherMap){ | |
if (!baseMap || !otherMap){ | |
return | |
} | |
otherMap.each{ key, value -> | |
baseMap[key] = value | |
} | |
} | |
def overrideConstants(Map values){ | |
mergeMaps(consulEnv, values) // overriding parameters | |
if (!values.base_url) { // if did not get any pther input from user, constructing it myself by default convention | |
consulEnv.put('base_url', "http://${consulEnv.ip}:${consulEnv.port}/v1") | |
} | |
} | |
def register(String data){ | |
return sh (script: "curl -X PUT -H 'Content-Type: application/json' ${consulEnv.base_url}/catalog/register -d \'$data\'", returnStdout: true) | |
} | |
def store(String key_path, String data){ | |
return sh (script: "curl -X PUT -H 'Content-Type: application/json' ${consulEnv.base_url}/${key_path} -d \'${data}\'", returnStdout: true) | |
} | |
def list_nodes(){ | |
return sh (script: "curl -X GET ${consulEnv.base_url}/catalog/nodes", returnStdout: true) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment