Search in sources :

Example 76 with BadRequestException

use of com.sequenceiq.cloudbreak.controller.BadRequestException in project cloudbreak by hortonworks.

the class CredentialSourceDecorator method decorate.

public Credential decorate(Credential credential, CredentialSourceRequest credentialSourceRequest, IdentityUser user) {
    if (credential == null) {
        credential = Strings.isNullOrEmpty(credentialSourceRequest.getSourceName()) ? credentialService.get(credentialSourceRequest.getSourceId()) : credentialService.get(credentialSourceRequest.getSourceName(), user.getAccount());
        if (credential == null) {
            throw new BadRequestException("Source credential does not exist!");
        } else {
            Map<String, Object> map = credential.getAttributes().getMap();
            for (Entry<String, Object> stringObjectEntry : credentialSourceRequest.getParameters().entrySet()) {
                map.put(stringObjectEntry.getKey(), stringObjectEntry.getValue().toString());
            }
            credential.setId(null);
            credential.setName(missingResourceNameGenerator.generateName(APIResourceType.CREDENTIAL));
            try {
                credential.setAttributes(new Json(map));
            } catch (JsonProcessingException ignored) {
                throw new BadRequestException("Could not create credential from source credential!");
            }
        }
    }
    return credential;
}
Also used : BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) Json(com.sequenceiq.cloudbreak.domain.json.Json) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 77 with BadRequestException

use of com.sequenceiq.cloudbreak.controller.BadRequestException in project cloudbreak by hortonworks.

the class HostGroupDecorator method getHostGroup.

private HostGroup getHostGroup(Long stackId, Constraint constraint, ConstraintJson constraintJson, HostGroup subject, IdentityUser user) {
    if (constraintJson == null) {
        throw new BadRequestException("The constraint field must be set in the reinstall request!");
    }
    HostGroup result = subject;
    String instanceGroupName = constraintJson.getInstanceGroupName();
    String constraintTemplateName = constraintJson.getConstraintTemplateName();
    Cluster cluster = clusterService.retrieveClusterByStackId(stackId);
    Constraint decoratedConstraint = decorateConstraint(stackId, user, constraint, instanceGroupName, constraintTemplateName);
    if (!isEmpty(instanceGroupName)) {
        result = getHostGroupByInstanceGroupName(decoratedConstraint, subject, cluster, instanceGroupName);
    } else if (!isEmpty(constraintTemplateName)) {
        subject.setConstraint(constraintRepository.save(constraint));
    } else {
        throw new BadRequestException("The constraint field must contain the 'constraintTemplateName' or 'instanceGroupName' parameter!");
    }
    return result;
}
Also used : Constraint(com.sequenceiq.cloudbreak.domain.Constraint) BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) HostGroup(com.sequenceiq.cloudbreak.domain.HostGroup) Cluster(com.sequenceiq.cloudbreak.domain.Cluster)

Example 78 with BadRequestException

use of com.sequenceiq.cloudbreak.controller.BadRequestException in project cloudbreak by hortonworks.

the class HostGroupDecorator method decorateConstraint.

private Constraint decorateConstraint(Long stackId, IdentityUser user, Constraint constraint, String instanceGroupName, String constraintTemplateName) {
    if (instanceGroupName != null) {
        InstanceGroup instanceGroup = instanceGroupRepository.findOneByGroupNameInStack(stackId, instanceGroupName);
        if (instanceGroup == null) {
            LOGGER.error("Instance group not found: {}", instanceGroupName);
            throw new BadRequestException(String.format("Instance group '%s' not found on stack.", instanceGroupName));
        }
        constraint.setInstanceGroup(instanceGroup);
    }
    if (constraintTemplateName != null) {
        ConstraintTemplate constraintTemplate = constraintTemplateRepository.findByNameInAccount(constraintTemplateName, user.getAccount(), user.getUserId());
        if (constraintTemplate == null) {
            throw new BadRequestException(String.format("Couldn't find constraint template with name: %s", constraintTemplateName));
        }
        constraint.setConstraintTemplate(constraintTemplate);
    }
    return constraint;
}
Also used : BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) ConstraintTemplate(com.sequenceiq.cloudbreak.domain.ConstraintTemplate) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup)

Example 79 with BadRequestException

use of com.sequenceiq.cloudbreak.controller.BadRequestException in project cloudbreak by hortonworks.

the class ClusterTemplateService method create.

@Transactional(TxType.NEVER)
public ClusterTemplate create(IdentityUser user, ClusterTemplate clusterTemplate) {
    LOGGER.debug("Creating clusterTemplate: [User: '{}', Account: '{}']", user.getUsername(), user.getAccount());
    ClusterTemplate savedClusterTemplate;
    clusterTemplate.setOwner(user.getUserId());
    clusterTemplate.setAccount(user.getAccount());
    try {
        savedClusterTemplate = clusterTemplateRepository.save(clusterTemplate);
    } catch (DataIntegrityViolationException ex) {
        String msg = String.format("Error with resource [%s], error: [%s]", APIResourceType.CLUSTER_TEMPLATE, getProperSqlErrorMessage(ex));
        throw new BadRequestException(msg);
    }
    return savedClusterTemplate;
}
Also used : ClusterTemplate(com.sequenceiq.cloudbreak.domain.ClusterTemplate) BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Transactional(javax.transaction.Transactional)

Example 80 with BadRequestException

use of com.sequenceiq.cloudbreak.controller.BadRequestException in project cloudbreak by hortonworks.

the class CredentialService method saveCredentialAndNotify.

private Credential saveCredentialAndNotify(Credential credential, ResourceEvent resourceEvent) {
    credential = credentialAdapter.init(credential);
    Credential savedCredential;
    try {
        savedCredential = credentialRepository.save(credential);
        userProfileCredentialHandler.createProfilePreparation(credential);
        sendCredentialNotification(credential, resourceEvent);
    } catch (DataIntegrityViolationException ex) {
        String msg = String.format("Error with resource [%s], error: [%s]", APIResourceType.CREDENTIAL, getProperSqlErrorMessage(ex));
        throw new BadRequestException(msg);
    }
    return savedCredential;
}
Also used : Credential(com.sequenceiq.cloudbreak.domain.Credential) BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Aggregations

BadRequestException (com.sequenceiq.cloudbreak.controller.BadRequestException)87 Stack (com.sequenceiq.cloudbreak.domain.Stack)16 Transactional (javax.transaction.Transactional)13 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)12 Cluster (com.sequenceiq.cloudbreak.domain.Cluster)12 Json (com.sequenceiq.cloudbreak.domain.json.Json)12 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)12 Blueprint (com.sequenceiq.cloudbreak.domain.Blueprint)11 HostGroup (com.sequenceiq.cloudbreak.domain.HostGroup)9 InstanceGroup (com.sequenceiq.cloudbreak.domain.InstanceGroup)9 IOException (java.io.IOException)7 Credential (com.sequenceiq.cloudbreak.domain.Credential)6 HashMap (java.util.HashMap)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Constraint (com.sequenceiq.cloudbreak.domain.Constraint)5 BlueprintParameterJson (com.sequenceiq.cloudbreak.api.model.BlueprintParameterJson)4 Platform (com.sequenceiq.cloudbreak.cloud.model.Platform)4 CloudbreakException (com.sequenceiq.cloudbreak.service.CloudbreakException)4 HashSet (java.util.HashSet)4 BlueprintInputJson (com.sequenceiq.cloudbreak.api.model.BlueprintInputJson)3