forked from jasonfill/ColdFusion-ElasticSearch-Client
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClusterManager.cfc
More file actions
85 lines (69 loc) · 2.41 KB
/
ClusterManager.cfc
File metadata and controls
85 lines (69 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
component accessors="true"{
property name="active" type="struct";
property name="serverList" type="struct";
property name="inactive" type="struct";
property name="ClusterName" type="string";
public ClusterManager function init(any nodeConfig=""){
variables.active = {};
variables.inactive = {};
loadConfigFromString(arguments.nodeConfig);
return this;
}
private ClusterManager function loadConfigFromString(required any nodeConfig){
/*
config = [{
host = "",
port = "",
path = "",
secure = "",
username = "",
password = ""
}]
*/
var config = "";
if(isSimpleValue(arguments.nodeConfig) && len(trim(arguments.nodeConfig))){
if(isJson(arguments.nodeConfig)){
config = deserializeJSON(arguments.NodeConfig);
}else{
throw(message="The node config passed to the ElasticSearch ClusterManager is not a valid JSON string.");
}
}else if(isArray(arguments.nodeConfig)){
config = arguments.nodeConfig;
} else {
return this;
}
for(var c=1; c<=arrayLen(config); c++){
addNode(new NodeConfig(argumentCollection=config[c]));
}
return this;
}
public ClusterManager function addNode(required NodeConfig NodeConfig){
variables.active[arguments.NodeConfig.getServerId()] = arguments.NodeConfig;
updateServerList();
return this;
}
public ClusterManager function updateServerList(){
variables.serverList = {active = structKeyList(getActive()), inactive = structKeyList(getInactive())};
return this;
}
public string function getEndpoint(){
return variables.active[listGetAt(variables.serverList.active, RandRange(1,listLen(variables.serverList.active)))].url();
}
public struct function doRequest(string Endpoint=getEndPoint(), required string Resource, string Method="GET", string Body="", string ResponseType="Response"){
var httpSvc = new http();
var response = createObject("component", "responses.#arguments.ResponseType#").init();
if(find("@", arguments.endpoint)){
var basicAuth = listLast(listFirst(arguments.endpoint, "@"), "/");
httpSvc.setUsername(listFirst(basicAuth, ":"));
httpSvc.setPassword(listLast(basicAuth, ":"));
}
httpSvc.setUrl(arguments.endpoint & Arguments.Resource);
httpSvc.setMethod(Arguments.Method);
if(len(trim(Arguments.Body))){
httpSvc.addParam(type="body",value=Arguments.Body);
}
var sendResult = httpSvc.send().getPrefix();
response.handleResponse(sendResult);
return response;
}
}