Search in sources :

Example 1 with NewPlanVersionBean

use of io.apiman.manager.api.beans.plans.NewPlanVersionBean in project apiman by apiman.

the class OrganizationResourceImpl method createPlan.

/**
 * @see IOrganizationResource#createPlan(java.lang.String,
 *      io.apiman.manager.api.beans.plans.NewPlanBean)
 */
@Override
public PlanBean createPlan(String organizationId, NewPlanBean bean) throws OrganizationNotFoundException, PlanAlreadyExistsException, NotAuthorizedException, InvalidNameException {
    securityContext.checkPermissions(PermissionType.planEdit, organizationId);
    FieldValidator.validateName(bean.getName());
    PlanBean newPlan = new PlanBean();
    newPlan.setName(bean.getName());
    newPlan.setDescription(bean.getDescription());
    newPlan.setId(BeanUtils.idFromName(bean.getName()));
    newPlan.setCreatedOn(new Date());
    newPlan.setCreatedBy(securityContext.getCurrentUser());
    try {
        // Store/persist the new plan
        storage.beginTx();
        OrganizationBean orgBean = getOrganizationFromStorage(organizationId);
        if (storage.getPlan(orgBean.getId(), newPlan.getId()) != null) {
            throw ExceptionFactory.planAlreadyExistsException(newPlan.getName());
        }
        newPlan.setOrganization(orgBean);
        storage.createPlan(newPlan);
        storage.createAuditEntry(AuditUtils.planCreated(newPlan, securityContext));
        if (bean.getInitialVersion() != null) {
            NewPlanVersionBean newPlanVersion = new NewPlanVersionBean();
            newPlanVersion.setVersion(bean.getInitialVersion());
            createPlanVersionInternal(newPlanVersion, newPlan);
        }
        storage.commitTx();
        // $NON-NLS-1$
        log.debug(String.format("Created plan: %s", newPlan));
        return newPlan;
    } catch (AbstractRestException e) {
        storage.rollbackTx();
        throw e;
    } catch (Exception e) {
        storage.rollbackTx();
        throw new SystemErrorException(e);
    }
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) NewPlanVersionBean(io.apiman.manager.api.beans.plans.NewPlanVersionBean) PlanBean(io.apiman.manager.api.beans.plans.PlanBean) UsagePerPlanBean(io.apiman.manager.api.beans.metrics.UsagePerPlanBean) NewPlanBean(io.apiman.manager.api.beans.plans.NewPlanBean) ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) UpdatePlanBean(io.apiman.manager.api.beans.plans.UpdatePlanBean) ResponseStatsPerPlanBean(io.apiman.manager.api.beans.metrics.ResponseStatsPerPlanBean) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) NewOrganizationBean(io.apiman.manager.api.beans.orgs.NewOrganizationBean) UpdateOrganizationBean(io.apiman.manager.api.beans.orgs.UpdateOrganizationBean) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) Date(java.util.Date) ClientAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientAlreadyExistsException) ApiVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) InvalidVersionException(io.apiman.manager.api.rest.exceptions.InvalidVersionException) OrganizationAlreadyExistsException(io.apiman.manager.api.rest.exceptions.OrganizationAlreadyExistsException) EntityStillActiveException(io.apiman.manager.api.rest.exceptions.EntityStillActiveException) PolicyNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyNotFoundException) PlanAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanAlreadyExistsException) ApiAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) UserNotFoundException(io.apiman.manager.api.rest.exceptions.UserNotFoundException) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) RoleNotFoundException(io.apiman.manager.api.rest.exceptions.RoleNotFoundException) InvalidNameException(io.apiman.manager.api.rest.exceptions.InvalidNameException) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) IOException(java.io.IOException) InvalidApiStatusException(io.apiman.manager.api.rest.exceptions.InvalidApiStatusException) ApiNotFoundException(io.apiman.manager.api.rest.exceptions.ApiNotFoundException) ContractAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ContractAlreadyExistsException) InvalidClientStatusException(io.apiman.manager.api.rest.exceptions.InvalidClientStatusException) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ClientVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientVersionAlreadyExistsException) InvalidPlanStatusException(io.apiman.manager.api.rest.exceptions.InvalidPlanStatusException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) ContractNotFoundException(io.apiman.manager.api.rest.exceptions.ContractNotFoundException) InvalidParameterException(io.apiman.manager.api.rest.exceptions.InvalidParameterException) ClientNotFoundException(io.apiman.manager.api.rest.exceptions.ClientNotFoundException) PlanNotFoundException(io.apiman.manager.api.rest.exceptions.PlanNotFoundException) InvalidMetricCriteriaException(io.apiman.manager.api.rest.exceptions.InvalidMetricCriteriaException) MalformedURLException(java.net.MalformedURLException) PlanVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanVersionAlreadyExistsException) PolicyDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException) OrganizationNotFoundException(io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException) ApiDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException)

Example 2 with NewPlanVersionBean

use of io.apiman.manager.api.beans.plans.NewPlanVersionBean in project apiman by apiman.

the class OrganizationResourceImpl method createPlanVersionInternal.

/**
 * Creates a plan version.
 * @param bean
 * @param plan
 * @throws StorageException
 */
private PlanVersionBean createPlanVersionInternal(NewPlanVersionBean bean, PlanBean plan) throws StorageException {
    if (!BeanUtils.isValidVersion(bean.getVersion())) {
        // $NON-NLS-1$
        throw new StorageException("Invalid/illegal plan version: " + bean.getVersion());
    }
    PlanVersionBean newVersion = new PlanVersionBean();
    newVersion.setCreatedBy(securityContext.getCurrentUser());
    newVersion.setCreatedOn(new Date());
    newVersion.setModifiedBy(securityContext.getCurrentUser());
    newVersion.setModifiedOn(new Date());
    newVersion.setStatus(PlanStatus.Created);
    newVersion.setPlan(plan);
    newVersion.setVersion(bean.getVersion());
    storage.createPlanVersion(newVersion);
    storage.createAuditEntry(AuditUtils.planVersionCreated(newVersion, securityContext));
    return newVersion;
}
Also used : StorageException(io.apiman.manager.api.core.exceptions.StorageException) Date(java.util.Date) NewPlanVersionBean(io.apiman.manager.api.beans.plans.NewPlanVersionBean) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean)

Example 3 with NewPlanVersionBean

use of io.apiman.manager.api.beans.plans.NewPlanVersionBean in project apiman by apiman.

the class PlanService method createPlan.

public PlanBean createPlan(String organizationId, NewPlanBean bean) throws OrganizationNotFoundException, PlanAlreadyExistsException, NotAuthorizedException, InvalidNameException {
    securityContext.checkPermissions(PermissionType.planEdit, organizationId);
    FieldValidator.validateName(bean.getName());
    PlanBean newPlan = new PlanBean();
    newPlan.setName(bean.getName());
    newPlan.setDescription(bean.getDescription());
    newPlan.setId(BeanUtils.idFromName(bean.getName()));
    newPlan.setCreatedOn(new Date());
    newPlan.setCreatedBy(securityContext.getCurrentUser());
    return tryAction(() -> {
        // Store/persist the new plan
        OrganizationBean orgBean = organizationService.getOrg(organizationId);
        if (storage.getPlan(orgBean.getId(), newPlan.getId()) != null) {
            throw ExceptionFactory.planAlreadyExistsException(newPlan.getName());
        }
        newPlan.setOrganization(orgBean);
        storage.createPlan(newPlan);
        storage.createAuditEntry(AuditUtils.planCreated(newPlan, securityContext));
        if (bean.getInitialVersion() != null) {
            NewPlanVersionBean newPlanVersion = new NewPlanVersionBean();
            newPlanVersion.setVersion(bean.getInitialVersion());
            createPlanVersionInternal(newPlanVersion, newPlan);
        }
        // $NON-NLS-1$
        LOGGER.debug(String.format("Created plan: %s", newPlan));
        return newPlan;
    });
}
Also used : NewPlanVersionBean(io.apiman.manager.api.beans.plans.NewPlanVersionBean) NewPlanBean(io.apiman.manager.api.beans.plans.NewPlanBean) PlanBean(io.apiman.manager.api.beans.plans.PlanBean) UpdatePlanBean(io.apiman.manager.api.beans.plans.UpdatePlanBean) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) Date(java.util.Date)

Example 4 with NewPlanVersionBean

use of io.apiman.manager.api.beans.plans.NewPlanVersionBean in project apiman by apiman.

the class PlanService method createPlanVersion.

public PlanVersionBean createPlanVersion(String organizationId, String planId, NewPlanVersionBean bean) throws PlanNotFoundException, NotAuthorizedException, InvalidVersionException, PlanVersionAlreadyExistsException {
    FieldValidator.validateVersion(bean.getVersion());
    PlanVersionBean newVersion = tryAction(() -> {
        PlanBean plan = storage.getPlan(organizationId, planId);
        if (plan == null) {
            throw ExceptionFactory.planNotFoundException(planId);
        }
        if (storage.getPlanVersion(organizationId, planId, bean.getVersion()) != null) {
            throw ExceptionFactory.planVersionAlreadyExistsException(planId, bean.getVersion());
        }
        return createPlanVersionInternal(bean, plan);
    });
    if (bean.isClone() && bean.getCloneVersion() != null) {
        try {
            List<PolicySummaryBean> policies = listPlanPolicies(organizationId, planId, bean.getCloneVersion());
            for (PolicySummaryBean policySummary : policies) {
                PolicyBean policy = getPlanPolicy(organizationId, planId, bean.getCloneVersion(), policySummary.getId());
                NewPolicyBean npb = new NewPolicyBean();
                npb.setDefinitionId(policy.getDefinition().getId());
                npb.setConfiguration(policy.getConfiguration());
                createPlanPolicy(organizationId, planId, newVersion.getVersion(), npb);
            }
        } catch (Exception e) {
        // TODO it's ok if the clone fails - we did our best
        }
    }
    // $NON-NLS-1$
    LOGGER.debug(String.format("Created plan %s version: %s", planId, newVersion));
    return newVersion;
}
Also used : PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) NewPolicyBean(io.apiman.manager.api.beans.policies.NewPolicyBean) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) NewPolicyBean(io.apiman.manager.api.beans.policies.NewPolicyBean) UpdatePolicyBean(io.apiman.manager.api.beans.policies.UpdatePolicyBean) NewPlanBean(io.apiman.manager.api.beans.plans.NewPlanBean) PlanBean(io.apiman.manager.api.beans.plans.PlanBean) UpdatePlanBean(io.apiman.manager.api.beans.plans.UpdatePlanBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException) InvalidVersionException(io.apiman.manager.api.rest.exceptions.InvalidVersionException) InvalidPlanStatusException(io.apiman.manager.api.rest.exceptions.InvalidPlanStatusException) PolicyNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyNotFoundException) PlanAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanAlreadyExistsException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) PlanNotFoundException(io.apiman.manager.api.rest.exceptions.PlanNotFoundException) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) InvalidNameException(io.apiman.manager.api.rest.exceptions.InvalidNameException) PlanVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanVersionAlreadyExistsException) OrganizationNotFoundException(io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException) ApiNotFoundException(io.apiman.manager.api.rest.exceptions.ApiNotFoundException) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean) NewPlanVersionBean(io.apiman.manager.api.beans.plans.NewPlanVersionBean)

Example 5 with NewPlanVersionBean

use of io.apiman.manager.api.beans.plans.NewPlanVersionBean in project apiman by apiman.

the class PlanService method createPlanVersionInternal.

/**
 * Creates a plan version.
 */
private PlanVersionBean createPlanVersionInternal(NewPlanVersionBean bean, PlanBean plan) throws StorageException {
    if (!BeanUtils.isValidVersion(bean.getVersion())) {
        // $NON-NLS-1$
        throw new StorageException("Invalid/illegal plan version: " + bean.getVersion());
    }
    PlanVersionBean newVersion = new PlanVersionBean();
    newVersion.setCreatedBy(securityContext.getCurrentUser());
    newVersion.setCreatedOn(new Date());
    newVersion.setModifiedBy(securityContext.getCurrentUser());
    newVersion.setModifiedOn(new Date());
    newVersion.setStatus(PlanStatus.Created);
    newVersion.setPlan(plan);
    newVersion.setVersion(bean.getVersion());
    storage.createPlanVersion(newVersion);
    storage.createAuditEntry(AuditUtils.planVersionCreated(newVersion, securityContext));
    return newVersion;
}
Also used : StorageException(io.apiman.manager.api.core.exceptions.StorageException) Date(java.util.Date) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean) NewPlanVersionBean(io.apiman.manager.api.beans.plans.NewPlanVersionBean)

Aggregations

NewPlanVersionBean (io.apiman.manager.api.beans.plans.NewPlanVersionBean)6 StorageException (io.apiman.manager.api.core.exceptions.StorageException)5 NewPlanBean (io.apiman.manager.api.beans.plans.NewPlanBean)4 PlanBean (io.apiman.manager.api.beans.plans.PlanBean)4 PlanVersionBean (io.apiman.manager.api.beans.plans.PlanVersionBean)4 UpdatePlanBean (io.apiman.manager.api.beans.plans.UpdatePlanBean)4 Date (java.util.Date)4 ApiNotFoundException (io.apiman.manager.api.rest.exceptions.ApiNotFoundException)3 InvalidNameException (io.apiman.manager.api.rest.exceptions.InvalidNameException)3 InvalidPlanStatusException (io.apiman.manager.api.rest.exceptions.InvalidPlanStatusException)3 InvalidVersionException (io.apiman.manager.api.rest.exceptions.InvalidVersionException)3 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)3 OrganizationNotFoundException (io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException)3 PlanAlreadyExistsException (io.apiman.manager.api.rest.exceptions.PlanAlreadyExistsException)3 PlanNotFoundException (io.apiman.manager.api.rest.exceptions.PlanNotFoundException)3 ApiPlanBean (io.apiman.manager.api.beans.apis.ApiPlanBean)2 ResponseStatsPerPlanBean (io.apiman.manager.api.beans.metrics.ResponseStatsPerPlanBean)2 UsagePerPlanBean (io.apiman.manager.api.beans.metrics.UsagePerPlanBean)2 OrganizationBean (io.apiman.manager.api.beans.orgs.OrganizationBean)2 NewPolicyBean (io.apiman.manager.api.beans.policies.NewPolicyBean)2