use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class ClusterResource method post.
/**
* Activate/deactivate a cluster in distributed controller mode
* <p>
* Usage: <code> curl -d 'jsonParameters=
* {"command":"activateCluster","grandCluster":"{controllerCluster}","enabled":"{true/false}"}' -H
* "Content-Type: application/json" http://{host:port}/clusters/{clusterName}}
*/
@Override
public Representation post(Representation entity) {
try {
String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
JsonParameters jsonParameters = new JsonParameters(entity);
String command = jsonParameters.getCommand();
if (command == null) {
throw new HelixException("Could NOT find 'command' in parameterMap: " + jsonParameters._parameterMap);
} else if (command.equalsIgnoreCase(ClusterSetup.activateCluster) || JsonParameters.CLUSTERSETUP_COMMAND_ALIASES.get(ClusterSetup.activateCluster).contains(command)) {
jsonParameters.verifyCommand(ClusterSetup.activateCluster);
boolean enabled = true;
if (jsonParameters.getParameter(JsonParameters.ENABLED) != null) {
enabled = Boolean.parseBoolean(jsonParameters.getParameter(JsonParameters.ENABLED));
}
String grandCluster = jsonParameters.getParameter(JsonParameters.GRAND_CLUSTER);
setupTool.activateCluster(clusterName, grandCluster, enabled);
} else if (command.equalsIgnoreCase(ClusterSetup.expandCluster)) {
setupTool.expandCluster(clusterName);
} else {
throw new HelixException("Unsupported command: " + command + ". Should be one of [" + ClusterSetup.activateCluster + ", " + ClusterSetup.expandCluster + "]");
}
getResponse().setEntity(getClusterRepresentation(clusterName));
getResponse().setStatus(Status.SUCCESS_OK);
} catch (Exception e) {
getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
getResponse().setStatus(Status.SUCCESS_OK);
}
return getResponseEntity();
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class ClustersResource method post.
/**
* Add a new Helix cluster
* <p>
* Usage: <code> curl -d 'jsonParameters={"command":"addCluster","clusterName":"{clusterName}"}' -H
* "Content-Type: application/json" http://{host:port}/clusters
*/
@Override
public Representation post(Representation entity) {
try {
JsonParameters jsonParameters = new JsonParameters(entity);
String command = jsonParameters.getCommand();
if (command == null) {
throw new HelixException("Could NOT find 'command' in parameterMap: " + jsonParameters._parameterMap);
} else if (command.equalsIgnoreCase(ClusterSetup.addCluster)) {
jsonParameters.verifyCommand(ClusterSetup.addCluster);
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
setupTool.addCluster(jsonParameters.getParameter(JsonParameters.CLUSTER_NAME), false);
} else {
throw new HelixException("Unsupported command: " + command + ". Should be one of [" + ClusterSetup.addCluster + "]");
}
getResponse().setEntity(getClustersRepresentation());
getResponse().setStatus(Status.SUCCESS_OK);
} catch (Exception e) {
getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
getResponse().setStatus(Status.SUCCESS_OK);
LOG.error("Error in posting " + entity, e);
}
return null;
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class ClustersResource method getClustersRepresentation.
StringRepresentation getClustersRepresentation() throws JsonGenerationException, JsonMappingException, IOException {
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
List<String> clusters = setupTool.getClusterManagementTool().getClusters();
ZNRecord clustersRecord = new ZNRecord("Clusters Summary");
clustersRecord.setListField("clusters", clusters);
StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(clustersRecord), MediaType.APPLICATION_JSON);
return representation;
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class ConfigResource method setConfigs.
/**
* set or remove configs depends on "command" field of jsonParameters in POST body
* @param entity
* @param scopeStr
* @throws Exception
*/
void setConfigs(Representation entity, ConfigScopeProperty type, String scopeArgs) throws Exception {
JsonParameters jsonParameters = new JsonParameters(entity);
String command = jsonParameters.getCommand();
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
if (command.equalsIgnoreCase(ClusterSetup.setConfig)) {
jsonParameters.verifyCommand(ClusterSetup.setConfig);
String propertiesStr = jsonParameters.getParameter(JsonParameters.CONFIGS);
setupTool.setConfig(type, scopeArgs, propertiesStr);
} else if (command.equalsIgnoreCase(ClusterSetup.removeConfig)) {
jsonParameters.verifyCommand(ClusterSetup.removeConfig);
String propertiesStr = jsonParameters.getParameter(JsonParameters.CONFIGS);
setupTool.removeConfig(type, scopeArgs, propertiesStr);
} else {
throw new HelixException("Unsupported command: " + command + ". Should be one of [" + ClusterSetup.setConfig + ", " + ClusterSetup.removeConfig + "]");
}
getResponse().setEntity(get());
getResponse().setStatus(Status.SUCCESS_OK);
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class ConfigResource method getConfigs.
StringRepresentation getConfigs(ConfigScopeProperty scopeProperty, String... keys) throws Exception {
StringRepresentation representation = null;
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
HelixAdmin admin = setupTool.getClusterManagementTool();
ZNRecord record = new ZNRecord(scopeProperty + " Config");
HelixConfigScope scope = new HelixConfigScopeBuilder(scopeProperty, keys).build();
List<String> configKeys = admin.getConfigKeys(scope);
Map<String, String> configs = admin.getConfig(scope, configKeys);
record.setSimpleFields(configs);
representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(record), MediaType.APPLICATION_JSON);
return representation;
}
Aggregations