Search in sources :

Example 1 with SchedulePolicyResp

use of com.emc.storageos.model.schedulepolicy.SchedulePolicyResp in project coprhd-controller by CoprHD.

the class TenantsService method createPolicy.

/**
 * Worker method for create schedule policy. Allows external requests (REST) as well as
 * internal requests that may not have a security context.
 *
 * @param id the URN of a CoprHD Tenant/Subtenant
 * @param param schedule policy parameters
 * @brief Create schedule policy
 * @return No data returned in response body
 * @throws BadRequestException
 */
public SchedulePolicyResp createPolicy(URI id, PolicyParam param) {
    TenantOrg tenant = getTenantById(id, true);
    // Make policy name as mandatory field
    ArgValidator.checkFieldNotNull(param.getPolicyName(), "policyName");
    // Check for duplicate policy name
    if (param.getPolicyName() != null && !param.getPolicyName().isEmpty()) {
        checkForDuplicateName(param.getPolicyName(), SchedulePolicy.class);
    }
    // check schedule policy type is valid or not
    if (!ArgValidator.isValidEnum(param.getPolicyType(), SchedulePolicyType.class)) {
        throw APIException.badRequests.invalidSchedulePolicyType(param.getPolicyType());
    }
    _log.info("Schedule policy creation started -- ");
    SchedulePolicy schedulePolicy = new SchedulePolicy();
    StringBuilder errorMsg = new StringBuilder();
    // Validate Schedule policy parameters
    boolean isValidSchedule = SchedulePolicyService.validateSchedulePolicyParam(param.getPolicySchedule(), schedulePolicy, errorMsg);
    if (!isValidSchedule && errorMsg != null && errorMsg.length() > 0) {
        _log.error("Failed to create schedule policy due to {} ", errorMsg.toString());
        throw APIException.badRequests.invalidSchedulePolicyParam(param.getPolicyName(), errorMsg.toString());
    }
    // Validate snapshot expire parameters
    boolean isValidSnapshotExpire = false;
    if (param.getSnapshotExpire() != null) {
        // check snapshot expire type is valid or not
        String expireType = param.getSnapshotExpire().getExpireType();
        if (!ArgValidator.isValidEnum(expireType, SnapshotExpireType.class)) {
            _log.error("Invalid schedule snapshot expire type {}. Valid Snapshot expire types are hours, days, weeks, months and never", expireType);
            throw APIException.badRequests.invalidScheduleSnapshotExpireType(expireType);
        }
        isValidSnapshotExpire = SchedulePolicyService.validateSnapshotExpireParam(param.getSnapshotExpire());
        if (!isValidSnapshotExpire) {
            int expireTime = param.getSnapshotExpire().getExpireValue();
            int minExpireTime = 2;
            int maxExpireTime = 10;
            _log.error("Invalid schedule snapshot expire time {}. Try an expire time between {} hours to {} years", expireTime, minExpireTime, maxExpireTime);
            throw APIException.badRequests.invalidScheduleSnapshotExpireValue(expireTime, minExpireTime, maxExpireTime);
        }
    } else {
        if (param.getPolicyType().equalsIgnoreCase(SchedulePolicyType.file_snapshot.toString())) {
            errorMsg.append("Required parameter snapshot_expire was missing or empty");
            _log.error("Failed to create schedule policy due to {} ", errorMsg.toString());
            throw APIException.badRequests.invalidSchedulePolicyParam(param.getPolicyName(), errorMsg.toString());
        }
    }
    if (isValidSchedule) {
        schedulePolicy.setId(URIUtil.createId(SchedulePolicy.class));
        schedulePolicy.setPolicyType(param.getPolicyType());
        schedulePolicy.setLabel(param.getPolicyName());
        schedulePolicy.setPolicyName(param.getPolicyName());
        schedulePolicy.setScheduleFrequency(param.getPolicySchedule().getScheduleFrequency().toLowerCase());
        if (isValidSnapshotExpire) {
            schedulePolicy.setSnapshotExpireType(param.getSnapshotExpire().getExpireType().toLowerCase());
            if (!param.getSnapshotExpire().getExpireType().equalsIgnoreCase(SnapshotExpireType.NEVER.toString())) {
                schedulePolicy.setSnapshotExpireTime((long) param.getSnapshotExpire().getExpireValue());
            }
        }
        schedulePolicy.setTenantOrg(new NamedURI(tenant.getId(), schedulePolicy.getLabel()));
        _dbClient.createObject(schedulePolicy);
        _log.info("Schedule policy {} created successfully", schedulePolicy);
    }
    recordTenantEvent(OperationTypeEnum.CREATE_SCHEDULE_POLICY, tenant.getId(), schedulePolicy.getId());
    return new SchedulePolicyResp(schedulePolicy.getId(), toLink(ResourceTypeEnum.SCHEDULE_POLICY, schedulePolicy.getId()), schedulePolicy.getLabel());
}
Also used : SchedulePolicyResp(com.emc.storageos.model.schedulepolicy.SchedulePolicyResp) NamedURI(com.emc.storageos.db.client.model.NamedURI) TenantOrg(com.emc.storageos.db.client.model.TenantOrg) SchedulePolicyType(com.emc.storageos.db.client.model.SchedulePolicy.SchedulePolicyType) SchedulePolicy(com.emc.storageos.db.client.model.SchedulePolicy) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint) ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) ContainmentPermissionsConstraint(com.emc.storageos.db.client.constraint.ContainmentPermissionsConstraint) Constraint(com.emc.storageos.db.client.constraint.Constraint) SnapshotExpireType(com.emc.storageos.db.client.model.SchedulePolicy.SnapshotExpireType)

Example 2 with SchedulePolicyResp

use of com.emc.storageos.model.schedulepolicy.SchedulePolicyResp in project coprhd-controller by CoprHD.

the class TenantsService method createSchedulePolicy.

/**
 * Create schedule policy and persist into CoprHD DB.
 *
 * @param id the URN of a CoprHD Tenant/Subtenant
 * @param param schedule policy parameters
 * @brief Create schedule policy
 * @return No data returned in response body
 * @throws BadRequestException
 */
@POST
@Path("/{id}/schedule-policies")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@CheckPermission(roles = { Role.TENANT_ADMIN })
public SchedulePolicyResp createSchedulePolicy(@PathParam("id") URI id, PolicyParam param) {
    SchedulePolicyResp schedulePolicyResp = createPolicy(id, param);
    auditOp(OperationTypeEnum.CREATE_SCHEDULE_POLICY, true, null, param.getPolicyName(), id.toString(), schedulePolicyResp.getId().toString());
    return schedulePolicyResp;
}
Also used : SchedulePolicyResp(com.emc.storageos.model.schedulepolicy.SchedulePolicyResp) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Aggregations

SchedulePolicyResp (com.emc.storageos.model.schedulepolicy.SchedulePolicyResp)2 AlternateIdConstraint (com.emc.storageos.db.client.constraint.AlternateIdConstraint)1 Constraint (com.emc.storageos.db.client.constraint.Constraint)1 ContainmentConstraint (com.emc.storageos.db.client.constraint.ContainmentConstraint)1 ContainmentPermissionsConstraint (com.emc.storageos.db.client.constraint.ContainmentPermissionsConstraint)1 NamedURI (com.emc.storageos.db.client.model.NamedURI)1 SchedulePolicy (com.emc.storageos.db.client.model.SchedulePolicy)1 SchedulePolicyType (com.emc.storageos.db.client.model.SchedulePolicy.SchedulePolicyType)1 SnapshotExpireType (com.emc.storageos.db.client.model.SchedulePolicy.SnapshotExpireType)1 TenantOrg (com.emc.storageos.db.client.model.TenantOrg)1 CheckPermission (com.emc.storageos.security.authorization.CheckPermission)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1