Search in sources :

Example 6 with AccountTag

use of com.sequenceiq.environment.tags.domain.AccountTag in project cloudbreak by hortonworks.

the class DefaultInternalAccountTagService method validate.

public void validate(List<AccountTag> accountTags) {
    for (AccountTagResponse defaultTag : getDefaults().getResponses()) {
        Optional<AccountTag> requestContainsUnmodifiableTag = accountTags.stream().filter(e -> e.getTagKey().equals(defaultTag.getKey())).findFirst();
        if (requestContainsUnmodifiableTag.isPresent()) {
            throw new BadRequestException(String.format("Tag with %s key exist as an unmodifiable tag.", defaultTag.getKey()));
        }
    }
    for (AccountTag accountTag : accountTags) {
        Pattern pattern = Pattern.compile(accountTagPattern);
        Matcher keyMatcher = pattern.matcher(accountTag.getTagKey());
        Matcher valueMatcher = pattern.matcher(accountTag.getTagValue());
        if (!keyMatcher.matches()) {
            throw new BadRequestException(String.format("The key '%s' can not start with microsoft or azure or aws or windows " + "or space and can contains only '-' and '_' and '{' and '}' characters.", accountTag.getTagKey()));
        }
        if (!valueMatcher.matches()) {
            throw new BadRequestException(String.format("The value '%s' can not start with microsoft or azure or aws or windows " + "or space and can contains only '-' and '_' and '{' and '}' characters.", accountTag.getTagValue()));
        }
        if (isAccountTagContainsTemplate(accountTag.getTagKey()) && isAccountTagInvalid(accountTag.getTagKey())) {
            throw new BadRequestException(String.format("The key '%s' of the tag contains invalid templates", accountTag.getTagKey()));
        }
        if (isAccountTagContainsTemplate(accountTag.getTagValue()) && isAccountTagInvalid(accountTag.getTagValue())) {
            throw new BadRequestException(String.format("The value '%s' of the tag contains invalid templates", accountTag.getTagValue()));
        }
    }
}
Also used : BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) CloudbreakResourceReaderService(com.sequenceiq.cloudbreak.service.CloudbreakResourceReaderService) Set(java.util.Set) IOException(java.io.IOException) JsonUtil(com.sequenceiq.cloudbreak.common.json.JsonUtil) Collectors(java.util.stream.Collectors) HandleBarModelKey(com.sequenceiq.cloudbreak.tag.HandleBarModelKey) HashSet(java.util.HashSet) Value(org.springframework.beans.factory.annotation.Value) List(java.util.List) Matcher(java.util.regex.Matcher) Service(org.springframework.stereotype.Service) AccountTagStatus(com.sequenceiq.environment.api.v1.tags.model.AccountTagStatus) Map(java.util.Map) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) AccountTag(com.sequenceiq.environment.tags.domain.AccountTag) AccountTagResponse(com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponse) AccountTagResponses(com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponses) Pattern(java.util.regex.Pattern) AccountTag(com.sequenceiq.environment.tags.domain.AccountTag) Matcher(java.util.regex.Matcher) AccountTagResponse(com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponse) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException)

Aggregations

AccountTag (com.sequenceiq.environment.tags.domain.AccountTag)6 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)2 AccountTagResponse (com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponse)2 ArrayList (java.util.ArrayList)2 CrnUser (com.sequenceiq.cloudbreak.auth.CrnUser)1 Json (com.sequenceiq.cloudbreak.common.json.Json)1 JsonUtil (com.sequenceiq.cloudbreak.common.json.JsonUtil)1 CloudbreakResourceReaderService (com.sequenceiq.cloudbreak.service.CloudbreakResourceReaderService)1 AccountTagValidationFailed (com.sequenceiq.cloudbreak.tag.AccountTagValidationFailed)1 HandleBarModelKey (com.sequenceiq.cloudbreak.tag.HandleBarModelKey)1 CDPTagGenerationRequest (com.sequenceiq.cloudbreak.tag.request.CDPTagGenerationRequest)1 AccountTagStatus (com.sequenceiq.environment.api.v1.tags.model.AccountTagStatus)1 AccountTagResponses (com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponses)1 EnvironmentTags (com.sequenceiq.environment.environment.domain.EnvironmentTags)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1