Search in sources :

Example 16 with ProjectId

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

the class FederatedPlatformLayerClient method mapToChild.

private MappedPlatformLayerKey mapToChild(PlatformLayerKey plk) {
    // if (plk.getHost() != null) {
    //
    // }
    ManagedItemId itemId = plk.getItemId();
    if (itemId == null || itemId.isEmpty()) {
        throw new IllegalArgumentException();
    }
    FederationKey host = plk.getHost();
    if (host == null) {
        host = FederationKey.LOCAL;
    }
    ProjectId project = plk.getProject();
    if (project == null) {
        project = defaultProject;
    // project = federationMap.getLocalClient().getProject();
    }
    ChildClient childClient = getClient(new FederationMapping(host, project));
    MappedPlatformLayerKey mapped = new MappedPlatformLayerKey();
    mapped.child = childClient;
    mapped.key = new PlatformLayerKey(host, project, plk.getServiceType(), plk.getItemType(), plk.getItemId());
    return mapped;
// Iterable<ChildClient> childClients = getChildClients(plk);
// ChildClient client = null;
// for (ChildClient childClient : childClients) {
// if (client == null) {
// client = childClient;
// } else {
// throw new IllegalStateException("Multiple clients found");
// }
// }
// return client;
}
Also used : ProjectId(org.platformlayer.ids.ProjectId) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) ManagedItemId(org.platformlayer.ids.ManagedItemId) FederationKey(org.platformlayer.ids.FederationKey)

Example 17 with ProjectId

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

the class HttpPlatformLayerClient method buildUsingConfiguration.

public static HttpPlatformLayerClient buildUsingConfiguration(HttpStrategy httpStrategy, PlatformLayerConnectionConfiguration config) {
    String project = config.tenant;
    String server = config.authenticationEndpoint;
    String username = config.username;
    String secret = config.secret;
    List<String> authTrustKeys = config.authTrustKeys;
    Authenticator authenticator = new PlatformlayerAuthenticator(httpStrategy, username, secret, server, authTrustKeys);
    ProjectId projectId = new ProjectId(project);
    return build(httpStrategy, config.platformlayerEndpoint, authenticator, projectId, config.platformlayerTrustKeys);
}
Also used : ProjectId(org.platformlayer.ids.ProjectId) PlatformlayerAuthenticator(org.platformlayer.auth.PlatformlayerAuthenticator) Authenticator(org.platformlayer.auth.Authenticator) PlatformlayerAuthenticator(org.platformlayer.auth.PlatformlayerAuthenticator)

Example 18 with ProjectId

use of org.platformlayer.ids.ProjectId 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)

Example 19 with ProjectId

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

the class ItemServiceImpl method findItem.

@Override
public <T extends ItemBase> T findItem(ProjectAuthorization auth, Class<T> itemClass, String id) throws OpsException {
    ModelClass<T> modelClass = serviceProviderDirectory.getModelClass(itemClass);
    // Class<T> javaClass = modelClass.getJavaClass();
    ProjectId project = getProjectId(auth);
    PlatformLayerKey modelKey = new PlatformLayerKey(null, project, modelClass.getServiceType(), modelClass.getItemType(), new ManagedItemId(id));
    boolean fetchTags = true;
    T managedItem;
    try {
        managedItem = Casts.checkedCast(repository.getManagedItem(modelKey, fetchTags, SecretProvider.from(auth)), itemClass);
    } catch (RepositoryException e) {
        throw new OpsException("Error fetching item from database", e);
    }
    return managedItem;
}
Also used : OpsException(org.platformlayer.ops.OpsException) ProjectId(org.platformlayer.ids.ProjectId) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) RepositoryException(org.platformlayer.RepositoryException) ManagedItemId(org.platformlayer.ids.ManagedItemId)

Example 20 with ProjectId

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

the class SchedulerImpl method rehydrateEndpoint.

private static PlatformLayerEndpointInfo rehydrateEndpoint(final EndpointRecord in) {
    final Authenticator authenticator;
    final String platformlayerBaseUrl = in.url;
    final ProjectId projectId = new ProjectId(in.project);
    final List<String> trustKeys;
    if (Strings.isNullOrEmpty(in.trustKeys)) {
        trustKeys = Collections.emptyList();
    } else {
        trustKeys = Lists.newArrayList(Splitter.on(",").split(in.trustKeys));
    }
    {
        String token = in.token;
        CryptoKey secret = FathomdbCrypto.deserializeKey(Hex.fromHex(in.secret.plaintext()));
        DirectAuthenticationToken authenticationToken = new DirectAuthenticationToken(token, secret);
        authenticator = new DirectAuthenticator(authenticationToken);
    }
    PlatformLayerEndpointInfo out = new PlatformLayerEndpointInfo(authenticator, platformlayerBaseUrl, projectId, trustKeys);
    return out;
}
Also used : PlatformLayerEndpointInfo(org.platformlayer.PlatformLayerEndpointInfo) DirectAuthenticationToken(org.platformlayer.auth.DirectAuthenticationToken) ProjectId(org.platformlayer.ids.ProjectId) CryptoKey(com.fathomdb.crypto.CryptoKey) DirectAuthenticator(org.platformlayer.auth.DirectAuthenticator) Authenticator(org.platformlayer.auth.Authenticator) DirectAuthenticator(org.platformlayer.auth.DirectAuthenticator)

Aggregations

ProjectId (org.platformlayer.ids.ProjectId)33 RepositoryException (org.platformlayer.RepositoryException)14 FederationKey (org.platformlayer.ids.FederationKey)11 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)10 ManagedItemId (org.platformlayer.ids.ManagedItemId)9 OpsException (org.platformlayer.ops.OpsException)8 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)7 SQLException (java.sql.SQLException)7 ServiceType (org.platformlayer.ids.ServiceType)7 ItemType (org.platformlayer.ids.ItemType)6 ProjectAuthorization (org.platformlayer.model.ProjectAuthorization)4 PlatformLayerClient (org.platformlayer.PlatformLayerClient)3 ItemBase (org.platformlayer.core.model.ItemBase)3 JobData (org.platformlayer.jobs.model.JobData)3 TypedPlatformLayerClient (org.platformlayer.TypedPlatformLayerClient)2 Authenticator (org.platformlayer.auth.Authenticator)2 DirectAuthenticator (org.platformlayer.auth.DirectAuthenticator)2 FederatedPlatformLayerClient (org.platformlayer.federation.FederatedPlatformLayerClient)2 PlatformLayerConnectionConfiguration (org.platformlayer.federation.model.PlatformLayerConnectionConfiguration)2 DirectPlatformLayerClient (org.platformlayer.ops.DirectPlatformLayerClient)2