Search in sources :

Example 21 with Tag

use of org.platformlayer.core.model.Tag in project platformlayer by platformlayer.

the class ManagedItemResource method listChildren.

@GET
@Produces({ XML, JSON })
@Path("children")
public ManagedItemCollection<ItemBase> listChildren(@QueryParam("deleted") boolean includeDeleted) throws OpsException, RepositoryException {
    boolean fetchTags = true;
    ItemBase item = getManagedItem(fetchTags);
    Tag parentTag = Tag.buildParentTag(item.getKey());
    Filter filter = TagFilter.byTag(parentTag);
    if (!includeDeleted) {
        filter = StateFilter.excludeDeleted(filter);
    }
    List<ItemBase> roots = itemService.listAll(getProjectAuthorization(), filter);
    ManagedItemCollection<ItemBase> collection = new ManagedItemCollection<ItemBase>();
    collection.items = roots;
    return cleanup(collection);
}
Also used : ItemBase(org.platformlayer.core.model.ItemBase) StateFilter(org.platformlayer.StateFilter) TagFilter(org.platformlayer.TagFilter) Filter(org.platformlayer.Filter) ManagedItemCollection(org.platformlayer.core.model.ManagedItemCollection) Tag(org.platformlayer.core.model.Tag) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 22 with Tag

use of org.platformlayer.core.model.Tag in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method listAll.

@Override
@JdbcTransaction
public List<ItemBase> listAll(ProjectId project, Filter filter, SecretProvider secretProvider) throws RepositoryException {
    DbHelper db = new DbHelper(project);
    try {
        log.debug("listAll with filter: {}", filter);
        // TODO: Use this logic for item selection as well
        List<Tag> requiredTags = filter.getRequiredTags();
        JoinedQueryResult result;
        if (!requiredTags.isEmpty()) {
            Tag requiredTag = requiredTags.get(0);
            int projectId = db.mapToValue(project);
            result = db.queries.listAllItemsWithTag(projectId, projectId, requiredTag.getKey(), requiredTag.getValue());
        } else {
            log.warn("Unable to optimize filter; selecting all items.  Filter={}", filter);
            result = db.listAllItems();
        }
        List<ItemBase> items = mapItemsAndTags(project, secretProvider, db, result);
        return applyFilter(items, filter);
    } catch (SQLException e) {
        throw new RepositoryException("Error fetching items", e);
    } finally {
        db.close();
    }
}
Also used : ItemBase(org.platformlayer.core.model.ItemBase) SQLException(java.sql.SQLException) RepositoryException(org.platformlayer.RepositoryException) Tag(org.platformlayer.core.model.Tag) JoinedQueryResult(com.fathomdb.jpa.impl.JoinedQueryResult) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 23 with Tag

use of org.platformlayer.core.model.Tag in project platformlayer by platformlayer.

the class AddTag method runCommand.

@Override
public Object runCommand() throws PlatformLayerClientException {
    PlatformLayerClient client = getPlatformLayerClient();
    PlatformLayerKey resolved = path.resolve(getContext());
    TagChanges tagChanges = new TagChanges();
    Tag tag = Tag.build(tagKey, tagValue);
    tagChanges.addTags.add(tag);
    return client.changeTags(resolved, tagChanges, null);
}
Also used : PlatformLayerClient(org.platformlayer.PlatformLayerClient) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Tag(org.platformlayer.core.model.Tag) TagChanges(org.platformlayer.core.model.TagChanges)

Example 24 with Tag

use of org.platformlayer.core.model.Tag in project platformlayer by platformlayer.

the class DeleteTag method runCommand.

@Override
public Object runCommand() throws PlatformLayerClientException {
    PlatformLayerClient client = getPlatformLayerClient();
    PlatformLayerKey key = path.resolve(getContext());
    UntypedItem ret = client.getItemUntyped(key, Format.XML);
    TagChanges tagChanges = new TagChanges();
    for (Tag tag : ret.getTags()) {
        if (!tagKey.equals(tag.getKey())) {
            continue;
        }
        if (tagValue != null && !tagValue.equals(tag.getValue())) {
            continue;
        }
        tagChanges.removeTags.add(tag);
    }
    Tags newTags = client.changeTags(key, tagChanges, null);
    return newTags;
}
Also used : PlatformLayerClient(org.platformlayer.PlatformLayerClient) UntypedItem(org.platformlayer.common.UntypedItem) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Tag(org.platformlayer.core.model.Tag) TagChanges(org.platformlayer.core.model.TagChanges) Tags(org.platformlayer.core.model.Tags)

Example 25 with Tag

use of org.platformlayer.core.model.Tag in project platformlayer by platformlayer.

the class InstanceBuilder method doOperation.

@Handler
public void doOperation() throws OpsException, IOException {
    ItemBase item = ops.getInstance(ItemBase.class);
    Tag parentTag = Tag.buildParentTag(item.getKey());
    PersistentInstance persistentInstanceTemplate = buildPersistentInstanceTemplate();
    persistentInstanceTemplate.getTags().add(parentTag);
    // Set during doOperation
    Machine machine = null;
    PersistentInstance persistentInstance = null;
    InstanceBase instance = null;
    OpsTarget target = null;
    persistentInstance = getOrCreate(parentTag, persistentInstanceTemplate);
    if (persistentInstance != null) {
        // We have to connect to the underlying machine not-via-DNS for Dns service => use instance id
        // TODO: Should we always use the instance id??
        instance = instances.findInstance(persistentInstance);
        if (instance == null && !OpsContext.isDelete()) {
            // A machine has not (yet) been assigned
            throw new OpsException("Machine is not yet built").setRetry(TimeSpan.ONE_MINUTE);
        }
    }
    if (instance != null) {
        machine = cloudHelpers.toMachine(instance);
    }
    if (addTagToManaged && !OpsContext.isDelete()) {
        // Add tag with instance id to persistent instance (very helpful for
        // DNS service!)
        PlatformLayerKey machineKey = machine.getKey();
        platformLayer.addTag(item.getKey(), Tag.INSTANCE_KEY.build(machineKey));
    }
    SshKey sshKey = service.getSshKey();
    if (machine != null) {
        if (OpsContext.isDelete()) {
            target = null;
            machine = null;
        } else {
            target = machine.getTarget(sshKey);
        }
    }
    RecursionState recursion = getRecursionState();
    if (OpsContext.isDelete() && machine == null) {
        // Don't recurse into no machine :-)
        recursion.setPreventRecursion(true);
    }
    recursion.pushChildScope(Machine.class, machine);
    recursion.pushChildScope(PersistentInstance.class, persistentInstance);
    recursion.pushChildScope(InstanceBase.class, instance);
    recursion.pushChildScope(OpsTarget.class, target);
}
Also used : SshKey(org.platformlayer.ops.helpers.SshKey) OpsTarget(org.platformlayer.ops.OpsTarget) OpsException(org.platformlayer.ops.OpsException) PersistentInstance(org.platformlayer.instances.model.PersistentInstance) ItemBase(org.platformlayer.core.model.ItemBase) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Tag(org.platformlayer.core.model.Tag) InstanceBase(org.platformlayer.core.model.InstanceBase) Machine(org.platformlayer.ops.Machine) Handler(org.platformlayer.ops.Handler)

Aggregations

Tag (org.platformlayer.core.model.Tag)41 OpsException (org.platformlayer.ops.OpsException)16 ItemBase (org.platformlayer.core.model.ItemBase)8 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)8 Tags (org.platformlayer.core.model.Tags)8 Handler (org.platformlayer.ops.Handler)8 TagChanges (org.platformlayer.core.model.TagChanges)6 JSONObject (org.json.JSONObject)4 OpenstackException (org.openstack.client.OpenstackException)4 RepositoryException (org.platformlayer.RepositoryException)4 Machine (org.platformlayer.ops.Machine)4 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)3 SQLException (java.sql.SQLException)3 OpenstackImageClient (org.openstack.client.common.OpenstackImageClient)3 Filter (org.platformlayer.Filter)3 PlatformLayerClient (org.platformlayer.PlatformLayerClient)3 StateFilter (org.platformlayer.StateFilter)3 OpsTarget (org.platformlayer.ops.OpsTarget)3 UniqueTag (org.platformlayer.ops.UniqueTag)3 SshKey (org.platformlayer.ops.helpers.SshKey)3