Search in sources :

Example 21 with PlatformLayerKey

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;
}
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 22 with PlatformLayerKey

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);
}
Also used : ServiceType(org.platformlayer.ids.ServiceType) ItemType(org.platformlayer.ids.ItemType) ProjectId(org.platformlayer.ids.ProjectId) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) FederationKey(org.platformlayer.ids.FederationKey)

Example 23 with PlatformLayerKey

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;
}
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 24 with PlatformLayerKey

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();
}
Also used : PlatformLayerClient(org.platformlayer.PlatformLayerClient) UntypedItemXml(org.platformlayer.UntypedItemXml) Links(org.platformlayer.core.model.Links) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Link(org.platformlayer.core.model.Link)

Example 25 with PlatformLayerKey

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;
}
Also used : PlatformLayerClient(org.platformlayer.PlatformLayerClient) UntypedItemXml(org.platformlayer.UntypedItemXml) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Aggregations

PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)86 PlatformLayerClient (org.platformlayer.PlatformLayerClient)21 OpsException (org.platformlayer.ops.OpsException)16 ItemBase (org.platformlayer.core.model.ItemBase)14 ManagedItemId (org.platformlayer.ids.ManagedItemId)13 UntypedItem (org.platformlayer.common.UntypedItem)10 ProjectId (org.platformlayer.ids.ProjectId)10 Tag (org.platformlayer.core.model.Tag)8 RepositoryException (org.platformlayer.RepositoryException)7 ServiceType (org.platformlayer.ids.ServiceType)7 JobData (org.platformlayer.jobs.model.JobData)7 InstanceBase (org.platformlayer.core.model.InstanceBase)6 ItemType (org.platformlayer.ids.ItemType)6 OpsTarget (org.platformlayer.ops.OpsTarget)6 JaxbHelper (org.platformlayer.xml.JaxbHelper)6 Handler (org.platformlayer.ops.Handler)5 TypedPlatformLayerClient (org.platformlayer.TypedPlatformLayerClient)4 UntypedItemXml (org.platformlayer.UntypedItemXml)4 FederationKey (org.platformlayer.ids.FederationKey)4 Machine (org.platformlayer.ops.Machine)4