Search in sources :

Example 6 with PoliciesBean

use of io.apiman.manager.api.es.beans.PoliciesBean in project apiman by apiman.

the class EsStorage method deletePolicy.

/**
 * @see io.apiman.manager.api.core.IStorage#deletePolicy(io.apiman.manager.api.beans.policies.PolicyBean)
 */
@Override
public void deletePolicy(PolicyBean policy) throws StorageException {
    String docType = getPoliciesDocType(policy.getType());
    String pid = id(policy.getOrganizationId(), policy.getEntityId(), policy.getEntityVersion());
    Map<String, Object> source = getEntity(docType, pid);
    if (source == null) {
        // $NON-NLS-1$
        throw new StorageException("Policy not found.");
    }
    PoliciesBean policies = EsMarshalling.unmarshallPolicies(source);
    // $NON-NLS-1$
    if (policies == null)
        throw new StorageException("Policy not found.");
    List<PolicyBean> policyBeans = policies.getPolicies();
    boolean found = false;
    if (policyBeans != null) {
        for (PolicyBean policyBean : policyBeans) {
            if (policyBean.getId().equals(policy.getId())) {
                policies.getPolicies().remove(policyBean);
                found = true;
                break;
            }
        }
    }
    if (found) {
        updateEntity(docType, pid, EsMarshalling.marshall(policies));
    } else {
        // $NON-NLS-1$
        throw new StorageException("Policy not found.");
    }
}
Also used : PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) PoliciesBean(io.apiman.manager.api.es.beans.PoliciesBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 7 with PoliciesBean

use of io.apiman.manager.api.es.beans.PoliciesBean in project apiman by apiman.

the class EsStorage method createPolicy.

/**
 * @see io.apiman.manager.api.core.IStorage#createPolicy(io.apiman.manager.api.beans.policies.PolicyBean)
 */
@Override
public void createPolicy(PolicyBean policy) throws StorageException {
    String docType = getPoliciesDocType(policy.getType());
    String id = id(policy.getOrganizationId(), policy.getEntityId(), policy.getEntityVersion());
    Map<String, Object> source = getEntity(docType, id);
    if (source == null) {
        // $NON-NLS-1$
        throw new StorageException("Failed to create policy (missing PoliciesBean).");
    }
    PoliciesBean policies = EsMarshalling.unmarshallPolicies(source);
    policy.setId(generateGuid());
    policies.getPolicies().add(policy);
    orderPolicies(policies);
    updateEntity(docType, id, EsMarshalling.marshall(policies));
}
Also used : PoliciesBean(io.apiman.manager.api.es.beans.PoliciesBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 8 with PoliciesBean

use of io.apiman.manager.api.es.beans.PoliciesBean in project apiman by apiman.

the class EsStorage method reorderPolicies.

/**
 * @see io.apiman.manager.api.core.IStorage#reorderPolicies(io.apiman.manager.api.beans.policies.PolicyType, java.lang.String, java.lang.String, java.lang.String, java.util.List)
 */
@Override
public void reorderPolicies(PolicyType type, String organizationId, String entityId, String entityVersion, List<Long> newOrder) throws StorageException {
    String docType = getPoliciesDocType(type);
    String pid = id(organizationId, entityId, entityVersion);
    Map<String, Object> source = getEntity(docType, pid);
    if (source == null) {
        return;
    }
    PoliciesBean policiesBean = EsMarshalling.unmarshallPolicies(source);
    List<PolicyBean> policies = policiesBean.getPolicies();
    List<PolicyBean> reordered = new ArrayList<>(policies.size());
    for (Long policyId : newOrder) {
        ListIterator<PolicyBean> iterator = policies.listIterator();
        while (iterator.hasNext()) {
            PolicyBean policyBean = iterator.next();
            if (policyBean.getId().equals(policyId)) {
                iterator.remove();
                reordered.add(policyBean);
                break;
            }
        }
    }
    // remaining at the end of the list.
    for (PolicyBean policyBean : policies) {
        reordered.add(policyBean);
    }
    policiesBean.setPolicies(reordered);
    updateEntity(docType, pid, EsMarshalling.marshall(policiesBean));
}
Also used : PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) ArrayList(java.util.ArrayList) PoliciesBean(io.apiman.manager.api.es.beans.PoliciesBean)

Example 9 with PoliciesBean

use of io.apiman.manager.api.es.beans.PoliciesBean in project apiman by apiman.

the class EsStorage method getPolicies.

/**
 * @see io.apiman.manager.api.core.IStorageQuery#getPolicies(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.policies.PolicyType)
 */
@Override
public List<PolicySummaryBean> getPolicies(String organizationId, String entityId, String version, PolicyType type) throws StorageException {
    try {
        String docType = getPoliciesDocType(type);
        String pid = id(organizationId, entityId, version);
        List<PolicySummaryBean> rval = new ArrayList<>();
        Map<String, Object> source = getEntity(docType, pid);
        if (source == null) {
            return rval;
        }
        PoliciesBean policies = EsMarshalling.unmarshallPolicies(source);
        if (policies == null)
            return rval;
        List<PolicyBean> policyBeans = policies.getPolicies();
        if (policyBeans != null) {
            for (PolicyBean policyBean : policyBeans) {
                PolicyDefinitionBean def = getPolicyDefinition(policyBean.getDefinition().getId());
                policyBean.setDefinition(def);
                PolicyTemplateUtil.generatePolicyDescription(policyBean);
                PolicySummaryBean psb = new PolicySummaryBean();
                psb.setCreatedBy(policyBean.getCreatedBy());
                psb.setCreatedOn(policyBean.getCreatedOn());
                psb.setDescription(policyBean.getDescription());
                psb.setIcon(def.getIcon());
                psb.setId(policyBean.getId());
                psb.setName(policyBean.getName());
                psb.setPolicyDefinitionId(def.getId());
                rval.add(psb);
            }
        }
        return rval;
    } catch (Exception e) {
        throw new StorageException(e);
    }
}
Also used : PolicyDefinitionBean(io.apiman.manager.api.beans.policies.PolicyDefinitionBean) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) ArrayList(java.util.ArrayList) PoliciesBean(io.apiman.manager.api.es.beans.PoliciesBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException) IOException(java.io.IOException) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 10 with PoliciesBean

use of io.apiman.manager.api.es.beans.PoliciesBean in project apiman by apiman.

the class EsStorage method getAllPolicies.

/**
 * @see io.apiman.manager.api.core.IStorage#getAllPolicies(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.policies.PolicyType)
 */
@Override
public Iterator<PolicyBean> getAllPolicies(String organizationId, String entityId, String version, PolicyType type) throws StorageException {
    try {
        String docType = getPoliciesDocType(type);
        String pid = id(organizationId, entityId, version);
        Map<String, Object> source = getEntity(docType, pid);
        PoliciesBean policies = EsMarshalling.unmarshallPolicies(source);
        if (policies == null || policies.getPolicies() == null) {
            return new ArrayList<PolicyBean>().iterator();
        }
        List<PolicyBean> policyBeans = policies.getPolicies();
        // TODO resolve the policy def, since we know we'll only have the definition ID here
        for (PolicyBean policyBean : policyBeans) {
            PolicyDefinitionBean def = getPolicyDefinition(policyBean.getDefinition().getId());
            if (def != null) {
                policyBean.setDefinition(def);
            }
        }
        return policyBeans.iterator();
    } catch (Exception e) {
        throw new StorageException(e);
    }
}
Also used : PolicyDefinitionBean(io.apiman.manager.api.beans.policies.PolicyDefinitionBean) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) PoliciesBean(io.apiman.manager.api.es.beans.PoliciesBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException) IOException(java.io.IOException) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Aggregations

PoliciesBean (io.apiman.manager.api.es.beans.PoliciesBean)12 PolicyBean (io.apiman.manager.api.beans.policies.PolicyBean)8 PolicyDefinitionBean (io.apiman.manager.api.beans.policies.PolicyDefinitionBean)5 StorageException (io.apiman.manager.api.core.exceptions.StorageException)5 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ApiBean (io.apiman.manager.api.beans.apis.ApiBean)1 ApiPlanBean (io.apiman.manager.api.beans.apis.ApiPlanBean)1 ClientBean (io.apiman.manager.api.beans.clients.ClientBean)1 PlanBean (io.apiman.manager.api.beans.plans.PlanBean)1 PolicyDefinitionTemplateBean (io.apiman.manager.api.beans.policies.PolicyDefinitionTemplateBean)1 PolicyType (io.apiman.manager.api.beans.policies.PolicyType)1 PolicySummaryBean (io.apiman.manager.api.beans.summary.PolicySummaryBean)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 Test (org.junit.Test)1