Search in sources :

Example 1 with DirectCloudHost

use of org.platformlayer.service.cloud.direct.ops.cloud.DirectCloudHost in project platformlayer by platformlayer.

the class CloudInstanceMapper method doOperation.

@Handler
public void doOperation() throws OpsException, IOException {
    Tag tag = Tag.build(Tag.ASSIGNED, instance.getKey().getUrl());
    List<DirectHost> hosts = Lists.newArrayList(platformLayer.listItems(DirectHost.class, TagFilter.byTag(tag)));
    if (hosts.size() > 1) {
        // Huh?
        throw new OpsException("Multiple hosts already assigned");
    }
    DirectHost host;
    if (hosts.isEmpty()) {
        if (OpsContext.isDelete()) {
            host = null;
        } else {
            if (createInstance) {
                DirectCloudHost cloudHost = cloudMap.pickHost(instance);
                host = cloudHost.getModel();
                platformLayer.addTag(host.getKey(), tag);
            } else {
                throw new OpsException("Instance not yet assigned");
            }
        }
    } else {
        host = hosts.get(0);
    }
    RecursionState recursion = getRecursionState();
    if (host != null) {
        this.cloud = platformLayer.getItem(host.cloud, DirectCloud.class);
        this.hostTarget = directHelpers.toTarget(host);
        recursion.pushChildScope(cloud);
        recursion.pushChildScope(host);
        recursion.pushChildScope(hostTarget);
    } else {
        if (!OpsContext.isDelete()) {
            throw new IllegalStateException();
        }
        log.info("No host set; won't recurse in");
        recursion.setPreventRecursion(true);
    }
}
Also used : DirectCloud(org.platformlayer.service.cloud.direct.model.DirectCloud) DirectHost(org.platformlayer.service.cloud.direct.model.DirectHost) OpsException(org.platformlayer.ops.OpsException) DirectCloudHost(org.platformlayer.service.cloud.direct.ops.cloud.DirectCloudHost) Tag(org.platformlayer.core.model.Tag) Handler(org.platformlayer.ops.Handler)

Aggregations

Tag (org.platformlayer.core.model.Tag)1 Handler (org.platformlayer.ops.Handler)1 OpsException (org.platformlayer.ops.OpsException)1 DirectCloud (org.platformlayer.service.cloud.direct.model.DirectCloud)1 DirectHost (org.platformlayer.service.cloud.direct.model.DirectHost)1 DirectCloudHost (org.platformlayer.service.cloud.direct.ops.cloud.DirectCloudHost)1