Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Reformat of bindings, and a few extra helper functions

  • Loading branch information...
commit c075cda496bfdfcf5a13d9d5dc96f792bb59da9f 1 parent 6c89252
Justin Santa Barbara justinsb authored
8 README.md
View
@@ -48,11 +48,13 @@ Installation notes
Debian / Ubuntu
```bash
-which mvn || apt-get install maven2
-apt-get install git openjdk-6-jdk postgresql
+which mvn || sudo apt-get install maven2
+sudo apt-get install git openjdk-6-jdk postgresql
+
+cd ~
git clone https://github.com/platformlayer/platformlayer.git
cd platformlayer
# (This will give you a sudo prompt, needed to install the DB)
-./install.sh
+./scripts/install-prereqs.sh
```
76 bindings/cli/src/main/java/org/platformlayer/client/cli/ConfigurationOptions.java
View
@@ -8,6 +8,7 @@
import java.util.Properties;
import org.kohsuke.args4j.Option;
+import org.openstack.utils.Io;
import org.openstack.utils.NoCloseInputStream;
import org.platformlayer.DirectPlatformLayerClient;
import org.platformlayer.IoUtils;
@@ -17,48 +18,49 @@
import com.fathomdb.cli.CliOptions;
public class ConfigurationOptions extends CliOptions {
- @Option(name = "-c", aliases = "--config", usage = "config file", required = true)
- String configFile;
+ @Option(name = "-c", aliases = "--config", usage = "config file", required = true)
+ String configFile;
- public PlatformLayerClient buildPlatformLayerClient() throws IOException, OpsException {
- PlatformLayerClient client;
- if (configFile == null) {
- throw new IllegalArgumentException("Config file is required");
- }
+ public PlatformLayerClient buildPlatformLayerClient() throws IOException, OpsException {
+ PlatformLayerClient client;
+ if (configFile == null) {
+ throw new IllegalArgumentException("Config file is required");
+ }
- InputStream is = null;
- try {
- if (configFile.equals("-")) {
- // Read from stdin
- // Don't auto-close it, and that terminates nailgun
- is = new NoCloseInputStream(System.in);
- } else {
- if (isServerMode()) {
- throw new IllegalArgumentException("Must pass config file over stdin in server mode");
- }
- File file = new File(configFile);
- if (!file.exists())
- throw new FileNotFoundException("Configuration file not found: " + file);
+ InputStream is = null;
+ try {
+ if (configFile.equals("-")) {
+ // Read from stdin
+ // Don't auto-close it, and that terminates nailgun
+ is = new NoCloseInputStream(System.in);
+ } else {
+ if (isServerMode()) {
+ throw new IllegalArgumentException("Must pass config file over stdin in server mode");
+ }
+ File file = Io.resolve(configFile);
+ if (!file.exists()) {
+ throw new FileNotFoundException("Configuration file not found: " + file);
+ }
- is = new FileInputStream(file);
- }
+ is = new FileInputStream(file);
+ }
- Properties properties = new Properties();
- try {
- properties.load(is);
- } catch (IOException e) {
- throw new IOException("Error reading configuration file", e);
- }
- client = DirectPlatformLayerClient.buildUsingProperties(properties);
+ Properties properties = new Properties();
+ try {
+ properties.load(is);
+ } catch (IOException e) {
+ throw new IOException("Error reading configuration file", e);
+ }
+ client = DirectPlatformLayerClient.buildUsingProperties(properties);
- // client = FederatedPlatformLayerClient.buildUsingConfig(is);
- } finally {
- if (is != System.in) {
- IoUtils.safeClose(is);
- }
- }
+ // client = FederatedPlatformLayerClient.buildUsingConfig(is);
+ } finally {
+ if (is != System.in) {
+ IoUtils.safeClose(is);
+ }
+ }
- return client;
- }
+ return client;
+ }
}
72 bindings/platformlayer-api/src/main/java/org/platformlayer/Filter.java
View
@@ -5,38 +5,44 @@
import org.platformlayer.core.model.Tags;
public class Filter {
- public static final Filter EMPTY = null;
-
- Tag requiredTag;
-
- public static Filter byTag(Tag requiredTag) {
- Filter filter = new Filter();
- filter.requiredTag = requiredTag;
- return filter;
- }
-
- public boolean matchesTags(Iterable<Tag> tags) {
- if (requiredTag == null)
- throw new IllegalStateException();
-
- for (Tag tag : tags) {
- if (tag.equals(requiredTag)) {
- return true;
- }
- }
- return false;
- }
-
- public boolean matches(Object item) {
- if (requiredTag == null)
- throw new IllegalStateException();
-
- if (item instanceof ItemBase) {
- Tags tags = ((ItemBase) item).getTags();
- return matchesTags(tags);
- }
-
- throw new IllegalArgumentException("Custom items not yet supported with filter");
- }
+ public static final Filter EMPTY = null;
+
+ Tag requiredTag;
+
+ public static Filter byTag(Tag requiredTag) {
+ Filter filter = new Filter();
+ filter.requiredTag = requiredTag;
+ return filter;
+ }
+
+ public boolean matchesTags(Iterable<Tag> tags) {
+ if (requiredTag == null) {
+ throw new IllegalStateException();
+ }
+
+ for (Tag tag : tags) {
+ if (tag.equals(requiredTag)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public boolean matches(Object item) {
+ if (requiredTag == null) {
+ throw new IllegalStateException();
+ }
+
+ if (item instanceof ItemBase) {
+ Tags tags = ((ItemBase) item).getTags();
+ return matchesTags(tags);
+ }
+
+ throw new IllegalArgumentException("Custom items not yet supported with filter");
+ }
+
+ public static Filter byParent(ItemBase item) {
+ return byTag(Tag.buildParentTag(item.getKey()));
+ }
}
280 bindings/platformlayer-api/src/main/java/org/platformlayer/PlatformLayerClientBase.java
View
@@ -25,137 +25,151 @@
import com.google.common.collect.Lists;
public abstract class PlatformLayerClientBase implements PlatformLayerClient {
- static final Logger log = LoggerFactory.getLogger(PlatformLayerClientBase.class);
-
- static Class<?>[] objectFactories = { org.platformlayer.service.dns.v1.ObjectFactory.class, org.platformlayer.service.imagefactory.v1.ObjectFactory.class,
- org.platformlayer.service.instancesupervisor.v1.ObjectFactory.class, };
-
- public static <T> JaxbHelper toJaxbHelper(Class<?> clazz, Class<?>... extraClasses) {
- List<Class<?>> extraClassesLists = Arrays.asList(extraClasses);
-
- // Package clazzPackage = clazz.getPackage();
- // for (Class<?> objectFactoryClass : objectFactories) {
- // if (clazzPackage.equals(objectFactoryClass.getPackage())) {
- // extraClassesLists.add(objectFactoryClass);
- // return JaxbHelper.get(objectFactoryClass, extraClassesLists);
- // }
- // }
-
- return JaxbHelper.get(clazz, extraClassesLists);
- }
-
- public static <T> JaxbHelper toJaxbHelper(T jaxbObject) {
- Class<?> clazz = jaxbObject.getClass();
- return toJaxbHelper(clazz, new Class[] {});
- }
-
- public static <T> String serialize(JaxbHelper jaxbHelper, T item) {
- String xml;
- try {
- xml = jaxbHelper.marshal(item, false);
- } catch (JAXBException e) {
- throw new IllegalArgumentException("Error marshalling object to XML", e);
- }
- return xml;
- }
-
- public static <T> T deserializeItem(Class<? extends T> itemClass, String retval) throws PlatformLayerClientException {
- T created;
-
- try {
- created = (T) JaxbHelper.deserializeXmlObject(retval, itemClass);
- } catch (UnmarshalException e) {
- throw new PlatformLayerClientException("Error parsing returned data", e);
- }
-
- return created;
- }
-
- protected <T> PlatformLayerKey toKey(JaxbHelper jaxbHelper, T item) throws PlatformLayerClientException {
- return toKey(jaxbHelper, item, listServices(true));
- }
-
- public static <T> PlatformLayerKey toKey(JaxbHelper jaxbHelper, T item, Collection<ServiceInfo> services) throws PlatformLayerClientException {
- String namespaceURI = jaxbHelper.getPrimaryNamespace();
- String nodeName = jaxbHelper.getXmlElementName();
-
- if (namespaceURI == null) {
- throw new IllegalArgumentException("Namespace could not be determined");
- }
-
- ServiceInfo service = getServiceInfo(services, namespaceURI);
- if (service == null) {
- throw new PlatformLayerClientException("Cannot find service for " + namespaceURI);
- }
-
- ServiceType serviceType = new ServiceType(service.getServiceType());
- ItemType itemType = new ItemType(nodeName);
-
- ManagedItemId id = item != null ? findId(item) : null;
-
- FederationKey host = null;
- ProjectId project = null;
- return new PlatformLayerKey(host, project, serviceType, itemType, id);
- }
-
- public static <T> ManagedItemId findId(T item) throws PlatformLayerClientException {
- String v;
-
- try {
- Method method = item.getClass().getMethod("getId");
- v = (String) method.invoke(item, null);
- } catch (SecurityException e) {
- throw new IllegalArgumentException("Cannot get id", e);
- } catch (NoSuchMethodException e) {
- throw new IllegalArgumentException("Cannot get id", e);
- } catch (IllegalArgumentException e) {
- throw new IllegalArgumentException("Cannot get id", e);
- } catch (IllegalAccessException e) {
- throw new IllegalArgumentException("Cannot get id", e);
- } catch (InvocationTargetException e) {
- throw new IllegalArgumentException("Cannot get id", e);
- }
-
- if (v == null || v.isEmpty()) {
- return null;
-
- // We could ask the server to figure it out for us (PUT on collection?)
- // throw new PlatformLayerClientException("Cannot determine item id");
- }
-
- return new ManagedItemId(v);
- }
-
- protected <T> PlatformLayerKey toKey(JaxbHelper jaxbHelper) throws PlatformLayerClientException {
- return toKey(jaxbHelper, null);
- }
-
- public ServiceInfo getServiceInfo(String namespace) throws PlatformLayerClientException {
- List<ServiceInfo> services = Lists.newArrayList(listServices(true));
- return getServiceInfo(services, namespace);
- }
-
- public static ServiceInfo getServiceInfo(Collection<ServiceInfo> services, String namespace) throws PlatformLayerClientException {
- return ServiceUtils.findByNamespace(services, namespace);
- }
-
- public static String buildRelativePath(PlatformLayerKey key) {
- return buildRelativePath(key.getServiceType(), key.getItemType(), key.getItemId());
- }
-
- public static String buildRelativePath(ServiceType serviceType, ItemType itemType, ManagedItemId id) {
- String s = urlEncode(serviceType.getKey()) + "/" + urlEncode(itemType.getKey());
- if (id != null) {
- s += "/" + urlEncode(id.getKey());
- }
- return s;
- }
-
- public static String urlEncode(String s) {
- try {
- return URLEncoder.encode(s, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- throw new IllegalStateException("UTF-8 Encoding not found", e);
- }
- }
+ static final Logger log = LoggerFactory.getLogger(PlatformLayerClientBase.class);
+
+ static Class<?>[] objectFactories = { org.platformlayer.service.dns.v1.ObjectFactory.class,
+ org.platformlayer.service.imagefactory.v1.ObjectFactory.class,
+ org.platformlayer.service.instancesupervisor.v1.ObjectFactory.class, };
+
+ public static <T> JaxbHelper toJaxbHelper(Class<?> clazz, Class<?>... extraClasses) {
+ List<Class<?>> extraClassesLists = Arrays.asList(extraClasses);
+
+ // Package clazzPackage = clazz.getPackage();
+ // for (Class<?> objectFactoryClass : objectFactories) {
+ // if (clazzPackage.equals(objectFactoryClass.getPackage())) {
+ // extraClassesLists.add(objectFactoryClass);
+ // return JaxbHelper.get(objectFactoryClass, extraClassesLists);
+ // }
+ // }
+
+ return JaxbHelper.get(clazz, extraClassesLists);
+ }
+
+ public static <T> JaxbHelper toJaxbHelper(T jaxbObject) {
+ Class<?> clazz = jaxbObject.getClass();
+ return toJaxbHelper(clazz, new Class[] {});
+ }
+
+ public static <T> String serialize(JaxbHelper jaxbHelper, T item) {
+ String xml;
+ try {
+ xml = jaxbHelper.marshal(item, false);
+ } catch (JAXBException e) {
+ throw new IllegalArgumentException("Error marshalling object to XML", e);
+ }
+ return xml;
+ }
+
+ public static <T> T deserializeItem(Class<? extends T> itemClass, String retval)
+ throws PlatformLayerClientException {
+ T created;
+
+ try {
+ created = JaxbHelper.deserializeXmlObject(retval, itemClass);
+ } catch (UnmarshalException e) {
+ throw new PlatformLayerClientException("Error parsing returned data", e);
+ }
+
+ return created;
+ }
+
+ protected <T> PlatformLayerKey toKey(JaxbHelper jaxbHelper, T item) throws PlatformLayerClientException {
+ return toKey(jaxbHelper, item, listServices(true));
+ }
+
+ public static <T> PlatformLayerKey toKey(JaxbHelper jaxbHelper, T item, Collection<ServiceInfo> services)
+ throws PlatformLayerClientException {
+ ManagedItemId id = item != null ? findId(item) : null;
+
+ return toKey(jaxbHelper, id, item != null ? item.getClass() : null, services);
+ }
+
+ protected <T> PlatformLayerKey toKey(JaxbHelper jaxbHelper, ManagedItemId id, Class<T> itemClass)
+ throws PlatformLayerClientException {
+ return toKey(jaxbHelper, id, itemClass, listServices(true));
+ }
+
+ public static <T> PlatformLayerKey toKey(JaxbHelper jaxbHelper, ManagedItemId id, Class<T> itemClass,
+ Collection<ServiceInfo> services) throws PlatformLayerClientException {
+ String namespaceURI = jaxbHelper.getPrimaryNamespace();
+ String nodeName = jaxbHelper.getXmlElementName();
+
+ if (namespaceURI == null) {
+ throw new IllegalArgumentException("Namespace could not be determined");
+ }
+
+ ServiceInfo service = getServiceInfo(services, namespaceURI);
+ if (service == null) {
+ throw new PlatformLayerClientException("Cannot find service for " + namespaceURI);
+ }
+
+ ServiceType serviceType = new ServiceType(service.getServiceType());
+ ItemType itemType = new ItemType(nodeName);
+
+ FederationKey host = null;
+ ProjectId project = null;
+ return new PlatformLayerKey(host, project, serviceType, itemType, id);
+ }
+
+ public static <T> ManagedItemId findId(T item) throws PlatformLayerClientException {
+ String v;
+
+ try {
+ Method method = item.getClass().getMethod("getId");
+ v = (String) method.invoke(item, null);
+ } catch (SecurityException e) {
+ throw new IllegalArgumentException("Cannot get id", e);
+ } catch (NoSuchMethodException e) {
+ throw new IllegalArgumentException("Cannot get id", e);
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException("Cannot get id", e);
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException("Cannot get id", e);
+ } catch (InvocationTargetException e) {
+ throw new IllegalArgumentException("Cannot get id", e);
+ }
+
+ if (v == null || v.isEmpty()) {
+ return null;
+
+ // We could ask the server to figure it out for us (PUT on collection?)
+ // throw new PlatformLayerClientException("Cannot determine item id");
+ }
+
+ return new ManagedItemId(v);
+ }
+
+ protected <T> PlatformLayerKey toKey(JaxbHelper jaxbHelper) throws PlatformLayerClientException {
+ return toKey(jaxbHelper, null);
+ }
+
+ public ServiceInfo getServiceInfo(String namespace) throws PlatformLayerClientException {
+ List<ServiceInfo> services = Lists.newArrayList(listServices(true));
+ return getServiceInfo(services, namespace);
+ }
+
+ public static ServiceInfo getServiceInfo(Collection<ServiceInfo> services, String namespace)
+ throws PlatformLayerClientException {
+ return ServiceUtils.findByNamespace(services, namespace);
+ }
+
+ public static String buildRelativePath(PlatformLayerKey key) {
+ return buildRelativePath(key.getServiceType(), key.getItemType(), key.getItemId());
+ }
+
+ public static String buildRelativePath(ServiceType serviceType, ItemType itemType, ManagedItemId id) {
+ String s = urlEncode(serviceType.getKey()) + "/" + urlEncode(itemType.getKey());
+ if (id != null) {
+ s += "/" + urlEncode(id.getKey());
+ }
+ return s;
+ }
+
+ public static String urlEncode(String s) {
+ try {
+ return URLEncoder.encode(s, "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ throw new IllegalStateException("UTF-8 Encoding not found", e);
+ }
+ }
}
523 bindings/platformlayer-api/src/main/java/org/platformlayer/TypedPlatformLayerClient.java
View
@@ -24,263 +24,270 @@
import com.google.common.collect.Lists;
public class TypedPlatformLayerClient implements PlatformLayerClient {
- static final Logger log = Logger.getLogger(TypedPlatformLayerClient.class);
-
- final PlatformLayerClient platformLayerClient;
- final TypedItemMapper mapper;
-
- @Inject
- public TypedPlatformLayerClient(PlatformLayerClient platformLayerClient, TypedItemMapper mapper) {
- this.platformLayerClient = platformLayerClient;
- this.mapper = mapper;
- }
-
- public <T> T promoteToTyped(UntypedItem untypedItem) throws OpsException {
- return mapper.promoteToTyped(untypedItem);
- }
-
- public <T> T promoteToTyped(UntypedItem untypedItem, Class<T> itemClass) throws OpsException {
- return mapper.promoteToTyped(untypedItem, itemClass);
- }
-
- // public ItemBase getItem(String path) throws OpsException {
- // ItemBase item = findItem(path);
- // if (item == null)
- // throw new OpsException("Item not found: " + item);
- // return item;
- // }
- //
- // public ItemBase findItem(String path) throws OpsException {
- // UntypedItem cloudItemUntyped = platformLayerClient.getUntypedItem(path);
- // if (cloudItemUntyped == null) {
- // return null;
- // }
- //
- // return promoteToTyped(cloudItemUntyped);
- // }
-
- public <T> T getItem(PlatformLayerKey path, Class<T> itemClass) throws OpsException {
- T item = findItem(path, itemClass);
- if (item == null)
- throw new OpsException("Item not found: " + path);
- return item;
- }
-
- public <T> T getItem(PlatformLayerKey path) throws OpsException {
- T item = findItem(path);
- if (item == null)
- throw new OpsException("Item not found: " + path);
- return item;
- }
-
- public <T> T findItem(PlatformLayerKey path, Class<T> itemClass) throws OpsException {
- UntypedItem cloudItemUntyped = platformLayerClient.getItemUntyped(path);
- if (cloudItemUntyped == null) {
- return null;
- }
-
- return promoteToTyped(cloudItemUntyped, itemClass);
- }
-
- public <T> T findItem(PlatformLayerKey path) throws OpsException {
- UntypedItem cloudItemUntyped = platformLayerClient.getItemUntyped(path);
- if (cloudItemUntyped == null) {
- return null;
- }
-
- return promoteToTyped(cloudItemUntyped);
- }
-
- /**
- * If using directly, consider using OwnedItem instead
- */
- @Deprecated
- 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, platformLayerClient.listServices(true));
-
- UntypedItem ret = platformLayerClient.putItemByTag(key, uniqueTag, xml, Format.XML);
- Class<T> itemClass = (Class<T>) item.getClass();
- return promoteToTyped(ret, itemClass);
-
- }
-
- /**
- * Consider using putItemByTag instead (or OwnedItem) for idempotency
- */
- @Deprecated
- public <T> T putItem(T item) throws OpsException {
- JaxbHelper jaxbHelper = PlatformLayerClientBase.toJaxbHelper(item);
-
- String xml = PlatformLayerClientBase.serialize(jaxbHelper, item);
-
- PlatformLayerKey key = PlatformLayerClientBase.toKey(jaxbHelper, item, platformLayerClient.listServices(true));
-
- UntypedItem created = platformLayerClient.putItem(key, xml, Format.XML);
-
- Class<T> itemClass = (Class<T>) item.getClass();
- return promoteToTyped(created, itemClass);
- }
-
- // public <T> Iterable<T> listItems(Class<T> itemClass) throws PlatformLayerClientException {
- // return platformLayerClient.listItems(itemClass);
- // }
-
- public void deleteItem(PlatformLayerKey key) throws PlatformLayerClientException {
- platformLayerClient.deleteItem(key);
- }
-
- public Tags addTag(PlatformLayerKey key, Tag tag) throws PlatformLayerClientException {
- TagChanges changeTags = new TagChanges();
-
- changeTags.addTags.add(tag);
-
- return changeTags(key, changeTags);
- }
-
- public Tags addUniqueTag(PlatformLayerKey key, Tag tag) throws PlatformLayerClientException {
- // Sometimes we require idempotency; we can do this using unique tags.
- // TODO: Implement this
- log.warn("addUniqueTag not properly implemented");
- return addTag(key, tag);
- }
-
- // public <T> Iterable<T> listItems(Class<T> itemClass, Filter filter) throws PlatformLayerClientException {
- // return platformLayerClient.listItems(itemClass, filter);
- // }
-
- public <T> List<T> listItems(Class<T> clazz) throws OpsException {
- return listItems(clazz, null);
- }
-
- public <T> List<T> listItems(Class<T> clazz, Filter filter) throws OpsException {
- JaxbHelper jaxbHelper = PlatformLayerClientBase.toJaxbHelper(clazz, ManagedItemCollection.class);
- PlatformLayerKey path = PlatformLayerClientBase.toKey(jaxbHelper, null, platformLayerClient.listServices(true));
-
- Iterable<UntypedItem> untypedItems = this.platformLayerClient.listItemsUntyped(path);
-
- List<T> items = Lists.newArrayList();
-
- for (UntypedItem untypedItem : untypedItems) {
- T item = promoteToTyped(untypedItem, clazz);
- items.add(item);
- }
-
- if (filter != null) {
- // TODO: Do filtering server-side
- List<T> filtered = Lists.newArrayList();
- for (T item : items) {
- if (filter.matches(item)) {
- filtered.add(item);
- }
- }
- return filtered;
- } else {
- return items;
- }
- }
-
- public Tags changeTags(PlatformLayerKey key, TagChanges tagChanges) throws PlatformLayerClientException {
- return platformLayerClient.changeTags(key, tagChanges);
- }
-
- public ProjectId getProject() {
- return platformLayerClient.getProject();
- }
-
- @Override
- public JobData doAction(PlatformLayerKey key, String action) throws PlatformLayerClientException {
- return platformLayerClient.doAction(key, action);
- }
-
- /**
- * Consider using putItemByTag instead (or OwnedItem) for idempotency
- */
- @Deprecated
- @Override
- public UntypedItem putItem(PlatformLayerKey key, String data, Format dataFormat) throws PlatformLayerClientException {
- return platformLayerClient.putItem(key, data, dataFormat);
- }
-
- /**
- * If using directly, consider using OwnedItem instead
- */
- @Deprecated
- @Override
- public UntypedItem putItemByTag(PlatformLayerKey key, Tag uniqueTag, String data, Format dataFormat) throws PlatformLayerClientException {
- return platformLayerClient.putItemByTag(key, uniqueTag, data, dataFormat);
- }
-
- @Override
- public UntypedItem getItemUntyped(PlatformLayerKey key) throws PlatformLayerClientException {
- return platformLayerClient.getItemUntyped(key);
- }
-
- @Override
- public Iterable<UntypedItem> listItemsUntyped(PlatformLayerKey path) throws PlatformLayerClientException {
- return platformLayerClient.listItemsUntyped(path);
- }
-
- @Override
- public Iterable<UntypedItem> listRoots() throws PlatformLayerClientException {
- return platformLayerClient.listRoots();
- }
-
- @Override
- public Iterable<UntypedItem> listChildren(PlatformLayerKey parent) throws PlatformLayerClientException {
- return platformLayerClient.listChildren(parent);
- }
-
- @Override
- public Iterable<JobData> listJobs() throws PlatformLayerClientException {
- return platformLayerClient.listJobs();
- }
-
- @Override
- public JobLog getJobLog(String jobId) throws PlatformLayerClientException {
- return platformLayerClient.getJobLog(jobId);
- }
-
- @Override
- public MetricValues getMetric(PlatformLayerKey key, String metricKey) throws PlatformLayerClientException {
- return platformLayerClient.getMetric(key, metricKey);
- }
-
- @Override
- public MetricInfoCollection listMetrics(PlatformLayerKey key) throws PlatformLayerClientException {
- return platformLayerClient.listMetrics(key);
- }
-
- @Override
- public Collection<ServiceInfo> listServices(boolean allowCache) throws PlatformLayerClientException {
- return platformLayerClient.listServices(allowCache);
- }
-
- @Override
- public String activateService(String serviceType, String data, Format format) throws PlatformLayerClientException {
- return platformLayerClient.activateService(serviceType, data, format);
- }
-
- @Override
- public String getActivation(String serviceType, Format format) throws PlatformLayerClientException {
- return platformLayerClient.getActivation(serviceType, format);
- }
-
- @Override
- public String getSshPublicKey(String serviceType) throws PlatformLayerClientException {
- return platformLayerClient.getSshPublicKey(serviceType);
- }
-
- @Override
- public String getSchema(String serviceType, Format format) throws PlatformLayerClientException {
- return platformLayerClient.getSchema(serviceType, format);
- }
-
- @Override
- public void ensureLoggedIn() throws PlatformLayerAuthenticationException {
- platformLayerClient.ensureLoggedIn();
- }
+ static final Logger log = Logger.getLogger(TypedPlatformLayerClient.class);
+
+ final PlatformLayerClient platformLayerClient;
+ final TypedItemMapper mapper;
+
+ @Inject
+ public TypedPlatformLayerClient(PlatformLayerClient platformLayerClient, TypedItemMapper mapper) {
+ this.platformLayerClient = platformLayerClient;
+ this.mapper = mapper;
+ }
+
+ public <T> T promoteToTyped(UntypedItem untypedItem) throws OpsException {
+ return mapper.promoteToTyped(untypedItem);
+ }
+
+ public <T> T promoteToTyped(UntypedItem untypedItem, Class<T> itemClass) throws OpsException {
+ return mapper.promoteToTyped(untypedItem, itemClass);
+ }
+
+ // public ItemBase getItem(String path) throws OpsException {
+ // ItemBase item = findItem(path);
+ // if (item == null)
+ // throw new OpsException("Item not found: " + item);
+ // return item;
+ // }
+ //
+ // public ItemBase findItem(String path) throws OpsException {
+ // UntypedItem cloudItemUntyped = platformLayerClient.getUntypedItem(path);
+ // if (cloudItemUntyped == null) {
+ // return null;
+ // }
+ //
+ // return promoteToTyped(cloudItemUntyped);
+ // }
+
+ public <T> T getItem(PlatformLayerKey path, Class<T> itemClass) throws OpsException {
+ T item = findItem(path, itemClass);
+ if (item == null) {
+ throw new OpsException("Item not found: " + path);
+ }
+ return item;
+ }
+
+ public <T> T getItem(PlatformLayerKey path) throws OpsException {
+ T item = findItem(path);
+ if (item == null) {
+ throw new OpsException("Item not found: " + path);
+ }
+ return item;
+ }
+
+ public <T> T findItem(PlatformLayerKey path, Class<T> itemClass) throws OpsException {
+ UntypedItem cloudItemUntyped = platformLayerClient.getItemUntyped(path);
+ if (cloudItemUntyped == null) {
+ return null;
+ }
+
+ return promoteToTyped(cloudItemUntyped, itemClass);
+ }
+
+ public <T> T findItem(PlatformLayerKey path) throws OpsException {
+ UntypedItem cloudItemUntyped = platformLayerClient.getItemUntyped(path);
+ if (cloudItemUntyped == null) {
+ return null;
+ }
+
+ return promoteToTyped(cloudItemUntyped);
+ }
+
+ /**
+ * If using directly, consider using OwnedItem instead
+ */
+ @Deprecated
+ 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, platformLayerClient.listServices(true));
+
+ UntypedItem ret = platformLayerClient.putItemByTag(key, uniqueTag, xml, Format.XML);
+ Class<T> itemClass = (Class<T>) item.getClass();
+ return promoteToTyped(ret, itemClass);
+
+ }
+
+ /**
+ * Consider using putItemByTag instead (or OwnedItem) for idempotency
+ */
+ @Deprecated
+ public <T> T putItem(T item) throws OpsException {
+ JaxbHelper jaxbHelper = PlatformLayerClientBase.toJaxbHelper(item);
+
+ String xml = PlatformLayerClientBase.serialize(jaxbHelper, item);
+
+ PlatformLayerKey key = PlatformLayerClientBase.toKey(jaxbHelper, item, platformLayerClient.listServices(true));
+
+ UntypedItem created = platformLayerClient.putItem(key, xml, Format.XML);
+
+ Class<T> itemClass = (Class<T>) item.getClass();
+ return promoteToTyped(created, itemClass);
+ }
+
+ // public <T> Iterable<T> listItems(Class<T> itemClass) throws PlatformLayerClientException {
+ // return platformLayerClient.listItems(itemClass);
+ // }
+
+ @Override
+ public void deleteItem(PlatformLayerKey key) throws PlatformLayerClientException {
+ platformLayerClient.deleteItem(key);
+ }
+
+ public Tags addTag(PlatformLayerKey key, Tag tag) throws PlatformLayerClientException {
+ TagChanges changeTags = new TagChanges();
+
+ changeTags.addTags.add(tag);
+
+ return changeTags(key, changeTags);
+ }
+
+ public Tags addUniqueTag(PlatformLayerKey key, Tag tag) throws PlatformLayerClientException {
+ // Sometimes we require idempotency; we can do this using unique tags.
+ // TODO: Implement this
+ log.warn("addUniqueTag not properly implemented");
+ return addTag(key, tag);
+ }
+
+ // public <T> Iterable<T> listItems(Class<T> itemClass, Filter filter) throws PlatformLayerClientException {
+ // return platformLayerClient.listItems(itemClass, filter);
+ // }
+
+ public <T> List<T> listItems(Class<T> clazz) throws OpsException {
+ return listItems(clazz, null);
+ }
+
+ public <T> List<T> listItems(Class<T> clazz, Filter filter) throws OpsException {
+ JaxbHelper jaxbHelper = PlatformLayerClientBase.toJaxbHelper(clazz, ManagedItemCollection.class);
+ PlatformLayerKey path = PlatformLayerClientBase.toKey(jaxbHelper, null, platformLayerClient.listServices(true));
+
+ Iterable<UntypedItem> untypedItems = this.platformLayerClient.listItemsUntyped(path);
+
+ List<T> items = Lists.newArrayList();
+
+ for (UntypedItem untypedItem : untypedItems) {
+ T item = promoteToTyped(untypedItem, clazz);
+ items.add(item);
+ }
+
+ if (filter != null) {
+ // TODO: Do filtering server-side
+ List<T> filtered = Lists.newArrayList();
+ for (T item : items) {
+ if (filter.matches(item)) {
+ filtered.add(item);
+ }
+ }
+ return filtered;
+ } else {
+ return items;
+ }
+ }
+
+ @Override
+ public Tags changeTags(PlatformLayerKey key, TagChanges tagChanges) throws PlatformLayerClientException {
+ return platformLayerClient.changeTags(key, tagChanges);
+ }
+
+ @Override
+ public ProjectId getProject() {
+ return platformLayerClient.getProject();
+ }
+
+ @Override
+ public JobData doAction(PlatformLayerKey key, String action) throws PlatformLayerClientException {
+ return platformLayerClient.doAction(key, action);
+ }
+
+ /**
+ * Consider using putItemByTag instead (or OwnedItem) for idempotency
+ */
+ @Deprecated
+ @Override
+ public UntypedItem putItem(PlatformLayerKey key, String data, Format dataFormat)
+ throws PlatformLayerClientException {
+ return platformLayerClient.putItem(key, data, dataFormat);
+ }
+
+ /**
+ * If using directly, consider using OwnedItem instead
+ */
+ @Deprecated
+ @Override
+ public UntypedItem putItemByTag(PlatformLayerKey key, Tag uniqueTag, String data, Format dataFormat)
+ throws PlatformLayerClientException {
+ return platformLayerClient.putItemByTag(key, uniqueTag, data, dataFormat);
+ }
+
+ @Override
+ public UntypedItem getItemUntyped(PlatformLayerKey key) throws PlatformLayerClientException {
+ return platformLayerClient.getItemUntyped(key);
+ }
+
+ @Override
+ public Iterable<UntypedItem> listItemsUntyped(PlatformLayerKey path) throws PlatformLayerClientException {
+ return platformLayerClient.listItemsUntyped(path);
+ }
+
+ @Override
+ public Iterable<UntypedItem> listRoots() throws PlatformLayerClientException {
+ return platformLayerClient.listRoots();
+ }
+
+ @Override
+ public Iterable<UntypedItem> listChildren(PlatformLayerKey parent) throws PlatformLayerClientException {
+ return platformLayerClient.listChildren(parent);
+ }
+
+ @Override
+ public Iterable<JobData> listJobs() throws PlatformLayerClientException {
+ return platformLayerClient.listJobs();
+ }
+
+ @Override
+ public JobLog getJobLog(String jobId) throws PlatformLayerClientException {
+ return platformLayerClient.getJobLog(jobId);
+ }
+
+ @Override
+ public MetricValues getMetric(PlatformLayerKey key, String metricKey) throws PlatformLayerClientException {
+ return platformLayerClient.getMetric(key, metricKey);
+ }
+
+ @Override
+ public MetricInfoCollection listMetrics(PlatformLayerKey key) throws PlatformLayerClientException {
+ return platformLayerClient.listMetrics(key);
+ }
+
+ @Override
+ public Collection<ServiceInfo> listServices(boolean allowCache) throws PlatformLayerClientException {
+ return platformLayerClient.listServices(allowCache);
+ }
+
+ @Override
+ public String activateService(String serviceType, String data, Format format) throws PlatformLayerClientException {
+ return platformLayerClient.activateService(serviceType, data, format);
+ }
+
+ @Override
+ public String getActivation(String serviceType, Format format) throws PlatformLayerClientException {
+ return platformLayerClient.getActivation(serviceType, format);
+ }
+
+ @Override
+ public String getSshPublicKey(String serviceType) throws PlatformLayerClientException {
+ return platformLayerClient.getSshPublicKey(serviceType);
+ }
+
+ @Override
+ public String getSchema(String serviceType, Format format) throws PlatformLayerClientException {
+ return platformLayerClient.getSchema(serviceType, format);
+ }
+
+ @Override
+ public void ensureLoggedIn() throws PlatformLayerAuthenticationException {
+ platformLayerClient.ensureLoggedIn();
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.