Search in sources :

Example 6 with ManagedItemId

use of org.platformlayer.ids.ManagedItemId in project platformlayer by platformlayer.

the class PlatformLayerClientBase method findId.

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);
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ManagedItemId(org.platformlayer.ids.ManagedItemId)

Example 7 with ManagedItemId

use of org.platformlayer.ids.ManagedItemId in project platformlayer by platformlayer.

the class PlatformLayerKey method build.

public static PlatformLayerKey build(String host, String project, String serviceType, String itemType, String itemId) {
    FederationKey federationKey = host != null ? FederationKey.build(host) : null;
    ProjectId projectKey = project != null ? new ProjectId(project) : null;
    ServiceType serviceKey = serviceType != null ? new ServiceType(serviceType) : null;
    ItemType itemKey = itemType != null ? new ItemType(itemType) : null;
    ManagedItemId idKey = itemId != null ? new ManagedItemId(itemId) : null;
    return new PlatformLayerKey(federationKey, projectKey, serviceKey, itemKey, idKey);
}
Also used : ServiceType(org.platformlayer.ids.ServiceType) ItemType(org.platformlayer.ids.ItemType) ProjectId(org.platformlayer.ids.ProjectId) ManagedItemId(org.platformlayer.ids.ManagedItemId) FederationKey(org.platformlayer.ids.FederationKey)

Example 8 with ManagedItemId

use of org.platformlayer.ids.ManagedItemId in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method getManagedItem.

@Override
@JdbcTransaction
public ItemBase getManagedItem(PlatformLayerKey key, boolean fetchTags, SecretProvider secretProvider) throws RepositoryException {
    DbHelper db = new DbHelper(key);
    try {
        ServiceProvider serviceProvider = serviceProviderDirectory.getServiceProvider(key.getServiceType());
        if (serviceProvider == null) {
            throw new IllegalStateException();
        }
        ModelClass<?> modelClass = serviceProvider.getModelClass(key.getItemType());
        ServiceType serviceType = key.getServiceType();
        ItemType itemType = key.getItemType();
        ProjectId project = key.getProject();
        ManagedItemId itemId = key.getItemId();
        return fetchItem(db, serviceType, itemType, project, itemId, modelClass.getJavaClass(), secretProvider, fetchTags);
    } catch (SQLException e) {
        throw new RepositoryException("Error running query", e);
    } finally {
        db.close();
    }
}
Also used : SQLException(java.sql.SQLException) ServiceType(org.platformlayer.ids.ServiceType) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) ItemType(org.platformlayer.ids.ItemType) ProjectId(org.platformlayer.ids.ProjectId) RepositoryException(org.platformlayer.RepositoryException) ManagedItemId(org.platformlayer.ids.ManagedItemId) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 9 with ManagedItemId

use of org.platformlayer.ids.ManagedItemId in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method mapToModel.

static <T extends ItemBase> T mapToModel(ProjectId project, ServiceType serviceType, ItemType itemType, ItemEntity entity, JaxbHelper jaxb, SecretProvider secretProvider) throws RepositoryException {
    try {
        int id = entity.id;
        String key = entity.key;
        int stateCode = entity.state;
        byte[] data = entity.data;
        SecretInfo secret = new SecretInfo(entity.secret);
        CryptoKey itemSecret = secretProvider.getItemSecret(secret);
        if (itemSecret == null) {
            throw new RepositoryException("Could not get secret to decrypt item");
        }
        if (itemSecret instanceof AesCryptoKey) {
            log.warn("Legacy AES crypto key on {} {} {} {}", new Object[] { project, serviceType, itemType, entity });
        }
        secret.unlock(itemSecret);
        byte[] plaintext = FathomdbCrypto.decrypt(itemSecret, data);
        String xml = new String(plaintext, Charsets.UTF_8);
        T model = (T) jaxb.unmarshal(xml);
        model.state = ManagedItemState.fromCode(stateCode);
        model.secret = secret;
        PlatformLayerKey plk = new PlatformLayerKey(null, project, serviceType, itemType, new ManagedItemId(key));
        model.setKey(plk);
        return model;
    } catch (JAXBException e) {
        throw new RepositoryException("Error deserializing data", e);
    }
}
Also used : AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) SecretInfo(org.platformlayer.core.model.SecretInfo) JAXBException(javax.xml.bind.JAXBException) CryptoKey(com.fathomdb.crypto.CryptoKey) AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) RepositoryException(org.platformlayer.RepositoryException) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) ManagedItemId(org.platformlayer.ids.ManagedItemId)

Example 10 with ManagedItemId

use of org.platformlayer.ids.ManagedItemId in project platformlayer by platformlayer.

the class ItemServiceImpl method ensureItem.

<T extends ItemBase> T ensureItem(final ProjectAuthorization auth, final T item, final boolean canExist, final boolean generateUniqueName, final String uniqueTagKey) throws OpsException {
    final ModelClass<T> modelClass = (ModelClass<T>) serviceProviderDirectory.getModelClass(item.getClass());
    if (modelClass == null) {
        throw new IllegalStateException("Unknown item type");
    }
    final Class<T> javaClass = modelClass.getJavaClass();
    // JaxbHelper jaxbHelper = JaxbHelper.get(javaClass);
    final ServiceProvider serviceProvider = modelClass.getProvider();
    String id = item.getId();
    if (Strings.isNullOrEmpty(id)) {
        if (generateUniqueName) {
            id = serviceProvider.buildItemId(modelClass, item);
        } else {
            // otherwise we end up with lots of randomly named items
            throw new OpsException("Must specify item id");
        // id = UUID.randomUUID().toString();
        // item.setId(id);
        }
    }
    ProjectId project = getProjectId(auth);
    PlatformLayerKey itemKey = new PlatformLayerKey(null, project, modelClass.getServiceType(), modelClass.getItemType(), new ManagedItemId(id));
    item.setKey(itemKey);
    item.state = ManagedItemState.CREATION_REQUESTED;
    final OpsContext opsContext = buildTemporaryOpsContext(modelClass.getServiceType(), auth);
    T created = OpsContext.runInContext(opsContext, new CheckedCallable<T, Exception>() {

        @Override
        public T call() throws Exception {
            PlatformLayerKey itemKey = item.getKey();
            T existing;
            SecretProvider secretProvider = SecretProvider.from(auth);
            if (uniqueTagKey != null) {
                boolean fetchTags = true;
                Tag uniqueTag = null;
                for (Tag tag : item.getTags()) {
                    if (Objects.equal(tag.getKey(), uniqueTagKey)) {
                        uniqueTag = tag;
                    }
                }
                if (uniqueTag == null) {
                    throw new IllegalArgumentException("Could not find unique tag");
                }
                Filter filter = TagFilter.byTag(uniqueTag);
                filter = StateFilter.excludeDeleted(filter);
                existing = null;
                List<T> existingList = repository.findAll(modelClass, itemKey.getProject(), fetchTags, secretProvider, filter);
                if (!existingList.isEmpty()) {
                    if (existingList.size() != 1) {
                        throw new IllegalArgumentException("Found multiple items with unique tag");
                    }
                    existing = existingList.get(0);
                }
                if (existing == null) {
                    itemKey = findUniqueId(item, itemKey, secretProvider);
                }
            } else {
                if (generateUniqueName) {
                    itemKey = findUniqueId(item, itemKey, secretProvider);
                }
                try {
                    boolean fetchTags = true;
                    existing = Casts.checkedCast(repository.getManagedItem(itemKey, fetchTags, secretProvider), javaClass);
                } catch (RepositoryException e) {
                    throw new OpsException("Error fetching item from database", e);
                }
            }
            if (!canExist && existing != null) {
                throw new OpsException("Item already exists");
            }
            serviceProvider.beforeCreateItem(item);
            ProjectId project = getProjectId(auth);
            T newItem;
            try {
                if (existing == null) {
                    newItem = repository.createManagedItem(project, item);
                } else {
                    item.secret = existing.secret;
                    item.setKey(existing.getKey());
                    newItem = repository.updateManagedItem(project, item);
                    TagChanges tagChanges = new TagChanges();
                    for (Tag tag : item.getTags()) {
                        if (newItem.getTags().hasTag(tag)) {
                            continue;
                        }
                        boolean uniqueTagKey = false;
                        if (tag.getKey().equals(Tag.PARENT.getKey())) {
                            uniqueTagKey = true;
                        }
                        tagChanges.addTags.add(tag);
                        if (uniqueTagKey) {
                            for (Tag oldTag : newItem.getTags().findTags(tag.getKey())) {
                                tagChanges.removeTags.add(oldTag);
                            }
                        }
                    }
                    if (!tagChanges.isEmpty()) {
                        repository.changeTags(modelClass, project, newItem.getKey().getItemId(), tagChanges, null);
                    }
                }
            } catch (RepositoryException e) {
                throw new OpsException("Error writing object to database", e);
            }
            itemKey = newItem.getKey();
            JobData jobKey = changeQueue.notifyChange(auth, itemKey, ManagedItemState.CREATION_REQUESTED);
            return newItem;
        }

        private <T extends ItemBase> PlatformLayerKey findUniqueId(final T item, final PlatformLayerKey itemKey, SecretProvider secretProvider) throws RepositoryException {
            int sequence = 0;
            while (true) {
                String tryId = item.getId();
                if (sequence != 0) {
                    tryId += sequence;
                }
                final PlatformLayerKey tryKey = itemKey.withId(new ManagedItemId(tryId));
                boolean fetchTags = false;
                ItemBase found = repository.getManagedItem(tryKey, fetchTags, secretProvider);
                if (found == null) {
                    item.setKey(tryKey);
                    return tryKey;
                }
                sequence++;
            }
        }
    });
    return created;
}
Also used : OpsException(org.platformlayer.ops.OpsException) ItemBase(org.platformlayer.core.model.ItemBase) ProjectId(org.platformlayer.ids.ProjectId) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) RepositoryException(org.platformlayer.RepositoryException) OpsContext(org.platformlayer.ops.OpsContext) TagChanges(org.platformlayer.core.model.TagChanges) ManagedItemId(org.platformlayer.ids.ManagedItemId) RepositoryException(org.platformlayer.RepositoryException) OpsException(org.platformlayer.ops.OpsException) ModelClass(org.platformlayer.xaas.services.ModelClass) StateFilter(org.platformlayer.StateFilter) TagFilter(org.platformlayer.TagFilter) Filter(org.platformlayer.Filter) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) List(java.util.List) Tag(org.platformlayer.core.model.Tag) JobData(org.platformlayer.jobs.model.JobData) SecretProvider(org.platformlayer.auth.crypto.SecretProvider)

Aggregations

ManagedItemId (org.platformlayer.ids.ManagedItemId)20 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)13 ProjectId (org.platformlayer.ids.ProjectId)9 RepositoryException (org.platformlayer.RepositoryException)7 ItemType (org.platformlayer.ids.ItemType)5 ServiceType (org.platformlayer.ids.ServiceType)5 FederationKey (org.platformlayer.ids.FederationKey)4 JobData (org.platformlayer.jobs.model.JobData)4 OpsException (org.platformlayer.ops.OpsException)4 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)3 SQLException (java.sql.SQLException)3 AesCryptoKey (com.fathomdb.crypto.AesCryptoKey)2 CryptoKey (com.fathomdb.crypto.CryptoKey)2 Path (javax.ws.rs.Path)2 SecretProvider (org.platformlayer.auth.crypto.SecretProvider)2 ModelClass (org.platformlayer.xaas.services.ModelClass)2 ServiceProvider (org.platformlayer.xaas.services.ServiceProvider)2 JaxbHelper (org.platformlayer.xml.JaxbHelper)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1