use of org.platformlayer.PlatformLayerClient in project platformlayer by platformlayer.
the class SchedulerImpl method toRunnable.
private Runnable toRunnable(final ActionTask task) {
final PlatformLayerKey target = task.target;
final PlatformLayerEndpointInfo endpoint = rehydrateEndpoint(task.endpoint);
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
PlatformLayerClient platformLayer = HttpPlatformLayerClient.build(httpStrategy, endpoint.getPlatformlayerBaseUrl(), endpoint.getAuthenticator(), endpoint.getProjectId(), endpoint.getTrustKeys());
platformLayer.doAction(target, task.action);
// TODO: Wait for task completion??
// TODO: Link job id??
} catch (PlatformLayerClientException e) {
log.warn("Error running action", e);
}
}
@Override
public String toString() {
return task.action + " on " + task.target;
}
};
return runnable;
}
use of org.platformlayer.PlatformLayerClient in project platformlayer by platformlayer.
the class FederationMap method buildClient.
public TypedPlatformLayerClient buildClient(FederationMapping key) {
MappedTarget target = targetMap.get(key);
if (target == null) {
throw new IllegalArgumentException("Unknown key: " + key);
}
if (target.client == null) {
PlatformLayerClient client = HttpPlatformLayerClient.buildUsingConfiguration(httpStrategy, target.configuration);
TypedPlatformLayerClient typedClient = new TypedPlatformLayerClient(client, mapper);
// TODO: Save client??
return typedClient;
} else {
return target.client;
}
}
use of org.platformlayer.PlatformLayerClient in project platformlayer by platformlayer.
the class SshItem method runCommand.
@Override
public Object runCommand() throws PlatformLayerClientException {
PlatformLayerClient client = getPlatformLayerClient();
PlatformLayerKey key = path.resolve(getContext());
UntypedItem untypedItem = client.getItemUntyped(key, Format.XML);
InetAddress sshAddress = findSshAddress(client, untypedItem);
ClientAction action = null;
if (sshAddress != null) {
String user = "root";
ProjectId project = key.getProject();
if (project == null) {
project = client.getProject();
}
if (project == null) {
throw new CliException("Cannot determine project");
}
String projectKey = project.getKey();
String serviceKey = "service-" + key.getServiceType().getKey();
File sshKey = IoUtils.resolve("~/.credentials/ssh/" + projectKey + "/" + serviceKey);
// Hmmm... user? key?
action = new ClientAction(ClientAction.ClientActionType.SSH, user + "@" + sshAddress.getHostAddress(), sshKey.getAbsolutePath());
}
return action;
}
use of org.platformlayer.PlatformLayerClient in project platformlayer by platformlayer.
the class OpenItem method runCommand.
@Override
public Object runCommand() throws PlatformLayerClientException {
PlatformLayerClient client = getPlatformLayerClient();
PlatformLayerKey key = path.resolve(getContext());
UntypedItem untypedItem = client.getItemUntyped(key, getFormat());
List<EndpointInfo> endpointList = EndpointInfo.getEndpoints(untypedItem.getTags());
Set<EndpointInfo> endpoints = Sets.newHashSet(endpointList);
EndpointInfo bestEndpoint = null;
for (EndpointInfo candidate : endpoints) {
if (bestEndpoint == null) {
bestEndpoint = candidate;
} else {
throw new IllegalArgumentException("Cannot choose between: " + bestEndpoint + " and " + candidate);
}
}
ClientAction action = null;
if (bestEndpoint != null) {
// TODO: How do we want to do this? A new tag??
String id = key.getServiceType().getKey() + ":" + key.getItemType().getKey();
if (id.equals("jenkins:jenkinsService")) {
action = new ClientAction(ClientAction.ClientActionType.BROWSER, "http://" + bestEndpoint.publicIp + ":" + bestEndpoint.publicIp);
}
}
return action;
}
use of org.platformlayer.PlatformLayerClient in project platformlayer by platformlayer.
the class PutItem method runCommand.
@Override
public Object runCommand() throws PlatformLayerClientException, JSONException {
PlatformLayerClient client = getPlatformLayerClient();
if (json == null) {
InputStream stream = new NoCloseInputStream(System.in);
byte[] data;
try {
data = ByteStreams.toByteArray(stream);
json = new String(data, Charsets.UTF_8);
} catch (IOException e) {
throw new CliException("Error reading stdin", e);
}
}
JSONObject jsonObject = new JSONObject(json);
PlatformLayerKey parentKey = null;
if (parent != null || !tags.isEmpty()) {
JSONObject tagsObject = null;
if (jsonObject.has("tags")) {
tagsObject = jsonObject.getJSONObject("tags");
} else {
tagsObject = new JSONObject();
jsonObject.put("tags", tagsObject);
}
JSONArray tagsArray;
if (tagsObject.has("tags")) {
tagsArray = tagsObject.getJSONArray("tags");
} else {
tagsArray = new JSONArray();
tagsObject.put("tags", tagsArray);
}
if (parent != null) {
parentKey = parent.resolve(getContext());
Tag parentTag = Tag.buildParentTag(parentKey);
JSONObject jsonTag = new JSONObject();
jsonTag.put("key", parentTag.getKey());
jsonTag.put("value", parentTag.getValue());
tagsArray.put(jsonTag);
}
for (String tag : tags) {
int equalsIndex = tag.indexOf('=');
if (equalsIndex == -1) {
throw new CliException("Expected tagname=tagvalue");
}
String tagName = tag.substring(0, equalsIndex);
String tagValue = tag.substring(equalsIndex + 1);
JSONObject jsonTag = new JSONObject();
jsonTag.put("key", tagName);
jsonTag.put("value", tagValue);
tagsArray.put(jsonTag);
}
}
PlatformLayerKey key = path.resolve(getContext());
boolean wrap = false;
String data;
if (wrap) {
JSONObject wrapped = new JSONObject();
wrapped.put(key.getItemType().getKey(), jsonObject);
data = wrapped.toString();
} else {
data = jsonObject.toString();
}
UntypedItem retval = client.putItem(key, data, Format.JSON);
return retval;
}
Aggregations