Search in sources :

Example 6 with UntypedItem

use of org.platformlayer.common.UntypedItem in project platformlayer by platformlayer.

the class PlatformLayerClientBase method putItemByTag.

@Override
public <T extends ItemBase> T putItemByTag(T item, Tag uniqueTag) throws OpsException {
    JaxbHelper jaxbHelper = PlatformLayerClientBase.toJaxbHelper(item);
    String xml = PlatformLayerClientBase.serialize(jaxbHelper, item);
    PlatformLayerKey key = PlatformLayerClientBase.toKey(jaxbHelper, item, listServices(true));
    UntypedItem ret = putItemByTag(key, uniqueTag, xml, Format.XML);
    Class<T> itemClass = (Class<T>) item.getClass();
    return promoteToTyped(ret, itemClass);
}
Also used : UntypedItem(org.platformlayer.common.UntypedItem) JaxbHelper(org.platformlayer.xml.JaxbHelper) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Example 7 with UntypedItem

use of org.platformlayer.common.UntypedItem 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;
}
Also used : PlatformLayerClient(org.platformlayer.PlatformLayerClient) ClientAction(com.fathomdb.cli.output.ClientAction) UntypedItem(org.platformlayer.common.UntypedItem) EndpointInfo(org.platformlayer.core.model.EndpointInfo) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Example 8 with UntypedItem

use of org.platformlayer.common.UntypedItem 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;
}
Also used : UntypedItem(org.platformlayer.common.UntypedItem) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) IOException(java.io.IOException) PlatformLayerClient(org.platformlayer.PlatformLayerClient) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) CliException(com.fathomdb.cli.CliException) JSONObject(org.json.JSONObject) Tag(org.platformlayer.core.model.Tag)

Example 9 with UntypedItem

use of org.platformlayer.common.UntypedItem in project platformlayer by platformlayer.

the class HttpPlatformLayerClient method putItemByTag.

@Override
public UntypedItem putItemByTag(PlatformLayerKey key, Tag uniqueTag, String data, Format dataFormat) throws PlatformLayerClientException {
    if (key.getItemId() == null) {
        throw new IllegalArgumentException("id is required on a put");
    }
    String relativePath = buildRelativePath(key);
    if (uniqueTag != null) {
        relativePath += "?unique=" + urlEncode(uniqueTag.getKey());
    }
    String xml = doRequest(HttpMethod.PUT, relativePath, String.class, Format.XML, data, dataFormat);
    UntypedItem item = UntypedItemXml.build(xml);
    return item;
}
Also used : UntypedItem(org.platformlayer.common.UntypedItem)

Example 10 with UntypedItem

use of org.platformlayer.common.UntypedItem in project platformlayer by platformlayer.

the class UntypedItemXmlCollection method findItems.

private void findItems() {
    Node itemsElement = XmlHelper.findUniqueChild(root, "items", false);
    if (itemsElement != null) {
        NodeList childNodes = itemsElement.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if (child instanceof Element) {
                Element childElement = (Element) child;
                // String namespaceURI = childElement.getNamespaceURI();
                String nodeName = childElement.getLocalName();
                if (nodeName.equals("item")) {
                    UntypedItem untypedItem = new UntypedItemXml(childElement);
                    items.add(untypedItem);
                }
            }
        }
    }
}
Also used : UntypedItem(org.platformlayer.common.UntypedItem) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element)

Aggregations

UntypedItem (org.platformlayer.common.UntypedItem)22 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)10 PlatformLayerClient (org.platformlayer.PlatformLayerClient)7 Ansi (com.fathomdb.cli.commands.Ansi)3 UntypedItemXml (org.platformlayer.UntypedItemXml)3 JaxbHelper (org.platformlayer.xml.JaxbHelper)3 CliException (com.fathomdb.cli.CliException)2 ClientAction (com.fathomdb.cli.output.ClientAction)2 InetAddress (java.net.InetAddress)2 EndpointInfo (org.platformlayer.core.model.EndpointInfo)2 Tag (org.platformlayer.core.model.Tag)2 Tags (org.platformlayer.core.model.Tags)2 NoCloseInputStream (com.fathomdb.io.NoCloseInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 UntypedItemCollection (org.platformlayer.common.UntypedItemCollection)1 ItemBase (org.platformlayer.core.model.ItemBase)1