Search in sources :

Example 6 with AccountTagResponse

use of com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponse 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)

Example 7 with AccountTagResponse

use of com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponse in project cloudbreak by hortonworks.

the class AccountTagToAccountTagResponsesConverter method convert.

public AccountTagResponse convert(AccountTag source) {
    AccountTagResponse response = new AccountTagResponse();
    response.setKey(source.getTagKey());
    response.setValue(source.getTagValue());
    response.setAccountId(source.getAccountId());
    response.setResourceCrn(source.getResourceCrn());
    response.setStatus(AccountTagStatus.USER_MANAGED);
    return response;
}
Also used : AccountTagResponse(com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponse)

Example 8 with AccountTagResponse

use of com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponse in project cloudbreak by hortonworks.

the class AccountTagService method list.

public Map<String, String> list() {
    try {
        String accountId = ThreadBasedUserCrnProvider.getAccountId();
        AccountTagResponses list = ThreadBasedUserCrnProvider.doAsInternalActor(regionAwareInternalCrnGeneratorFactory.iam().getInternalCrnForServiceAsString(), () -> accountTagEndpoint.listInAccount(accountId));
        return list.getResponses().stream().collect(Collectors.toMap(AccountTagResponse::getKey, AccountTagResponse::getValue));
    } catch (ClientErrorException e) {
        try (Response response = e.getResponse()) {
            if (Response.Status.NOT_FOUND.getStatusCode() == response.getStatus()) {
                throw new BadRequestException(String.format("Account tag not found"), e);
            }
            String errorMessage = webApplicationExceptionMessageExtractor.getErrorMessage(e);
            throw new CloudbreakServiceException(String.format("Failed to get account tag: %s", errorMessage), e);
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) AccountTagResponse(com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponse) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) AccountTagResponses(com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponses) ClientErrorException(javax.ws.rs.ClientErrorException) BadRequestException(javax.ws.rs.BadRequestException)

Aggregations

AccountTagResponse (com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponse)6 AccountTagResponses (com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponses)5 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)2 CloudbreakServiceException (com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException)2 EnvironmentTags (com.sequenceiq.environment.environment.domain.EnvironmentTags)2 AccountTag (com.sequenceiq.environment.tags.domain.AccountTag)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 PostConstruct (javax.annotation.PostConstruct)2 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 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Optional (java.util.Optional)1