use of org.platformlayer.core.model.PlatformLayerKey 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.core.model.PlatformLayerKey in project platformlayer by platformlayer.
the class PlatformLayerCommandRunnerBase method pathToKey.
public static PlatformLayerKey pathToKey(PlatformLayerClient client, String path) throws PlatformLayerClientException {
String serviceType;
String itemType;
if (path.contains("/")) {
String[] components = path.split("/");
if (components.length != 2) {
throw new IllegalArgumentException("Cannot parse path: " + path);
}
serviceType = components[0];
itemType = components[1];
} else {
itemType = path;
serviceType = getServiceTypeFromItemType(client, itemType);
}
FederationKey host = null;
ProjectId project = client.getProject();
return new PlatformLayerKey(host, project, new ServiceType(serviceType), new ItemType(itemType), null);
}
use of org.platformlayer.core.model.PlatformLayerKey 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;
}
use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.
the class PutLink method runCommand.
@Override
public Object runCommand() throws PlatformLayerClientException {
PlatformLayerClient client = getPlatformLayerClient();
PlatformLayerKey resolved = path.resolve(getContext());
UntypedItemXml item = (UntypedItemXml) client.getItemUntyped(resolved, Format.XML);
Links links = item.getLinks();
Link link = new Link();
link.name = name;
link.target = target.resolve(getContext());
Link existing = links.findLink(name);
List<Link> linkList = links.getLinks();
if (existing != null) {
linkList.remove(existing);
}
linkList.add(link);
item.setLinks(links);
String xml = item.serialize();
UntypedItemXml updated = (UntypedItemXml) client.putItem(resolved, xml, Format.XML);
return updated.getLinks().getLinks();
}
use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.
the class ItemMutatorCommand method runCommand.
protected UntypedItemXml runCommand(ItemPath path) throws PlatformLayerClientException {
PlatformLayerClient client = getPlatformLayerClient();
PlatformLayerKey resolved = path.resolve(getContext());
UntypedItemXml item = (UntypedItemXml) client.getItemUntyped(resolved, Format.XML);
changeItem(item);
String xml = item.serialize();
UntypedItemXml updated = (UntypedItemXml) client.putItem(resolved, xml, Format.XML);
return updated;
}
Aggregations