Package org.platformlayer.client.cli.commands

Source Code of org.platformlayer.client.cli.commands.OpenItem

package org.platformlayer.client.cli.commands;

import java.util.List;
import java.util.Set;

import org.kohsuke.args4j.Argument;
import org.platformlayer.PlatformLayerClient;
import org.platformlayer.PlatformLayerClientException;
import org.platformlayer.client.cli.model.ItemPath;
import org.platformlayer.common.UntypedItem;
import org.platformlayer.core.model.EndpointInfo;
import org.platformlayer.core.model.PlatformLayerKey;

import com.fathomdb.cli.output.ClientAction;
import com.google.common.collect.Sets;

public class OpenItem extends PlatformLayerCommandRunnerBase {
  @Argument
  public ItemPath path;

  public OpenItem() {
    super("open", "item");
  }

  @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;
  }

}
TOP

Related Classes of org.platformlayer.client.cli.commands.OpenItem

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.