Search in sources :

Example 6 with TenantOrg

use of com.emc.storageos.db.client.model.TenantOrg in project coprhd-controller by CoprHD.

the class TenantsService method listProjects.

/**
 * List projects the user is authorized to see
 *
 * @param id the URN of a ViPR Tenant/Subtenant
 * @prereq none
 * @brief List projects
 * @return List of projects
 */
@GET
@Path("/{id}/projects")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public ProjectList listProjects(@PathParam("id") URI id) {
    TenantOrg tenant = getTenantById(id, false);
    StorageOSUser user = getUserFromContext();
    NamedElementQueryResultList projects = new NamedElementQueryResultList();
    if (_permissionsHelper.userHasGivenRole(user, tenant.getId(), Role.SYSTEM_MONITOR, Role.TENANT_ADMIN, Role.SECURITY_ADMIN)) {
        // list all
        _dbClient.queryByConstraint(ContainmentConstraint.Factory.getTenantOrgProjectConstraint(tenant.getId()), projects);
    } else {
        // list only projects that the user has access to
        if (!id.equals(URI.create(user.getTenantId()))) {
            throw APIException.forbidden.insufficientPermissionsForUser(user.getName());
        }
        Map<URI, Set<String>> allMyProjects = _permissionsHelper.getAllPermissionsForUser(user, tenant.getId(), null, false);
        if (!allMyProjects.keySet().isEmpty()) {
            List<Project> project_list = _dbClient.queryObjectField(Project.class, "label", new ArrayList<URI>(allMyProjects.keySet()));
            List<NamedElementQueryResultList.NamedElement> elements = new ArrayList<NamedElementQueryResultList.NamedElement>(project_list.size());
            for (Project p : project_list) {
                elements.add(NamedElementQueryResultList.NamedElement.createElement(p.getId(), p.getLabel()));
            }
            projects.setResult(elements.iterator());
        } else {
            // empty list
            projects.setResult(new ArrayList<NamedElementQueryResultList.NamedElement>().iterator());
        }
    }
    ProjectList list = new ProjectList();
    for (NamedElementQueryResultList.NamedElement el : projects) {
        list.getProjects().add(toNamedRelatedResource(ResourceTypeEnum.PROJECT, el.getId(), el.getName()));
    }
    return list;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) AbstractChangeTrackingSet(com.emc.storageos.db.client.model.AbstractChangeTrackingSet) StringSet(com.emc.storageos.db.client.model.StringSet) ArrayList(java.util.ArrayList) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) Project(com.emc.storageos.db.client.model.Project) StorageOSUser(com.emc.storageos.security.authentication.StorageOSUser) ProjectList(com.emc.storageos.model.project.ProjectList) TenantOrg(com.emc.storageos.db.client.model.TenantOrg) NamedElementQueryResultList(com.emc.storageos.db.client.constraint.NamedElementQueryResultList) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 7 with TenantOrg

use of com.emc.storageos.db.client.model.TenantOrg 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 8 with TenantOrg

use of com.emc.storageos.db.client.model.TenantOrg in project coprhd-controller by CoprHD.

the class TenantsService method createHost.

/**
 * Creates a new host for the tenant organization. Discovery is initiated
 * after the host is created.
 * <p>
 * This method is deprecated. Use /compute/hosts instead
 *
 * @param tid
 *            the tenant organization id
 * @param createParam
 *            the parameter that has the type and attribute of the host to be created.
 * @prereq none
 * @deprecated use {@link HostService#createHost(HostCreateParam,Boolean)}
 * @brief Create tenant host
 * @return the host discovery async task representation.
 */
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@CheckPermission(roles = { Role.TENANT_ADMIN })
@Path("/{id}/hosts")
@Deprecated
public TaskResourceRep createHost(@PathParam("id") URI tid, HostCreateParam createParam, @QueryParam("validate_connection") @DefaultValue("false") final Boolean validateConnection) {
    // This is mostly to validate the tenant
    TenantOrg tenant = getTenantById(tid, true);
    HostService hostService = _hostService;
    hostService.validateHostData(createParam, tid, null, validateConnection);
    // Create the host
    return hostService.createNewHost(tenant, createParam);
}
Also used : TenantOrg(com.emc.storageos.db.client.model.TenantOrg) 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)

Example 9 with TenantOrg

use of com.emc.storageos.db.client.model.TenantOrg in project coprhd-controller by CoprHD.

the class TenantsService method createVcenter.

/**
 * Creates a new vCenter for the tenant organization. Discovery is initiated
 * after the vCenter is created.
 *
 * @param tid
 *            the tenant organization id
 * @param createParam
 *            the parameter that has the attributes of the vCenter to be created.
 * @prereq none
 * @brief Create tenant vCenter
 * @return the vCenter discovery async task.
 */
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@CheckPermission(roles = { Role.TENANT_ADMIN })
@Path("/{id}/vcenters")
public TaskResourceRep createVcenter(@PathParam("id") URI tid, VcenterCreateParam createParam, @QueryParam("validate_connection") @DefaultValue("false") final Boolean validateConnection) {
    // This validates the tenant
    TenantOrg tenant = getTenantById(tid, true);
    VcenterService service = _vcenterService;
    // validates the create param and validation is successful then creates and persist the vcenter
    Vcenter vcenter = service.createNewTenantVcenter(tenant, createParam, validateConnection);
    vcenter.setRegistrationStatus(RegistrationStatus.REGISTERED.toString());
    _dbClient.createObject(vcenter);
    recordTenantResourceOperation(OperationTypeEnum.CREATE_VCENTER, tid, vcenter);
    return service.doDiscoverVcenter(vcenter);
}
Also used : Vcenter(com.emc.storageos.db.client.model.Vcenter) TenantOrg(com.emc.storageos.db.client.model.TenantOrg) 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)

Example 10 with TenantOrg

use of com.emc.storageos.db.client.model.TenantOrg in project coprhd-controller by CoprHD.

the class TenantsService method getSchedulePolicies.

/**
 * Gets the policyIds, policyNames and self links for all schedule policies.
 *
 * @param id the URN of a CoprHD Tenant/Subtenant
 * @brief List schedule policies
 * @return policyList - A SchedulePolicyList reference specifying the policyIds, name and self links for
 *         the schedule policies.
 */
@GET
@Path("/{id}/schedule-policies")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@CheckPermission(roles = { Role.TENANT_ADMIN, Role.PROJECT_ADMIN })
public SchedulePolicyList getSchedulePolicies(@PathParam("id") URI id) {
    TenantOrg tenant = getTenantById(id, false);
    StorageOSUser user = getUserFromContext();
    NamedElementQueryResultList schedulePolicies = new NamedElementQueryResultList();
    if (_permissionsHelper.userHasGivenRole(user, tenant.getId(), Role.SYSTEM_MONITOR, Role.TENANT_ADMIN, Role.SECURITY_ADMIN)) {
        // list all schedule policies
        _dbClient.queryByConstraint(ContainmentConstraint.Factory.getTenantOrgSchedulePolicyConstraint(tenant.getId()), schedulePolicies);
    } else {
        // list only schedule policies that the user has access to
        if (!id.equals(URI.create(user.getTenantId()))) {
            throw APIException.forbidden.insufficientPermissionsForUser(user.getName());
        }
        Map<URI, Set<String>> allMySchedulePolicies = _permissionsHelper.getAllPermissionsForUser(user, tenant.getId(), null, false);
        if (!allMySchedulePolicies.keySet().isEmpty()) {
            List<SchedulePolicy> policyList = _dbClient.queryObjectField(SchedulePolicy.class, "label", new ArrayList<URI>(allMySchedulePolicies.keySet()));
            List<NamedElementQueryResultList.NamedElement> elements = new ArrayList<NamedElementQueryResultList.NamedElement>(policyList.size());
            for (SchedulePolicy policy : policyList) {
                elements.add(NamedElementQueryResultList.NamedElement.createElement(policy.getId(), policy.getLabel()));
            }
            schedulePolicies.setResult(elements.iterator());
        } else {
            // empty list
            schedulePolicies.setResult(new ArrayList<NamedElementQueryResultList.NamedElement>().iterator());
        }
    }
    SchedulePolicyList policyList = new SchedulePolicyList();
    for (NamedElementQueryResultList.NamedElement el : schedulePolicies) {
        policyList.getSchdulePolicies().add(toNamedRelatedResource(ResourceTypeEnum.SCHEDULE_POLICY, el.getId(), el.getName()));
    }
    return policyList;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) AbstractChangeTrackingSet(com.emc.storageos.db.client.model.AbstractChangeTrackingSet) StringSet(com.emc.storageos.db.client.model.StringSet) ArrayList(java.util.ArrayList) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) SchedulePolicyList(com.emc.storageos.model.schedulepolicy.SchedulePolicyList) StorageOSUser(com.emc.storageos.security.authentication.StorageOSUser) TenantOrg(com.emc.storageos.db.client.model.TenantOrg) NamedElementQueryResultList(com.emc.storageos.db.client.constraint.NamedElementQueryResultList) SchedulePolicy(com.emc.storageos.db.client.model.SchedulePolicy) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Aggregations

TenantOrg (com.emc.storageos.db.client.model.TenantOrg)138 URI (java.net.URI)64 NamedURI (com.emc.storageos.db.client.model.NamedURI)57 Project (com.emc.storageos.db.client.model.Project)54 Volume (com.emc.storageos.db.client.model.Volume)41 ArrayList (java.util.ArrayList)40 VirtualPool (com.emc.storageos.db.client.model.VirtualPool)37 DeviceControllerException (com.emc.storageos.exceptions.DeviceControllerException)34 ServiceError (com.emc.storageos.svcs.errorhandling.model.ServiceError)33 StorageSystem (com.emc.storageos.db.client.model.StorageSystem)29 StringSet (com.emc.storageos.db.client.model.StringSet)29 DatabaseException (com.emc.storageos.db.exceptions.DatabaseException)28 VirtualArray (com.emc.storageos.db.client.model.VirtualArray)26 Produces (javax.ws.rs.Produces)26 StoragePool (com.emc.storageos.db.client.model.StoragePool)25 List (java.util.List)23 Test (org.junit.Test)23 InternalException (com.emc.storageos.svcs.errorhandling.resources.InternalException)22 StringMap (com.emc.storageos.db.client.model.StringMap)21 Consumes (javax.ws.rs.Consumes)21