Search in sources :

Example 1 with AccountTagResponses

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

the class AccountTagClientService 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 (WebApplicationException | ProcessingException | IllegalStateException e) {
        String message = String.format("Failed to GET AccountTags with account id, due to: '%s' ", e.getMessage());
        LOGGER.error(message, e);
        throw new CloudbreakServiceException(message, e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) AccountTagResponses(com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponses) ProcessingException(javax.ws.rs.ProcessingException)

Example 2 with AccountTagResponses

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

the class AccountTagService method list.

public Map<String, String> list() {
    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));
}
Also used : AccountTagResponses(com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponses)

Example 3 with AccountTagResponses

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

the class EnvironmentDtoConverter method getTags.

private Json getTags(EnvironmentCreationDto creationDto) {
    boolean internalTenant = entitlementService.internalTenant(creationDto.getAccountId());
    Map<String, String> userDefinedTags = creationDto.getTags();
    Set<AccountTag> accountTags = accountTagService.get(creationDto.getAccountId());
    List<AccountTagResponse> accountTagResponses = accountTags.stream().map(accountTagToAccountTagResponsesConverter::convert).collect(Collectors.toList());
    defaultInternalAccountTagService.merge(accountTagResponses);
    Map<String, String> accountTagsMap = accountTagResponses.stream().collect(Collectors.toMap(AccountTagResponse::getKey, AccountTagResponse::getValue));
    CDPTagGenerationRequest request = CDPTagGenerationRequest.Builder.builder().withCreatorCrn(creationDto.getCreator()).withEnvironmentCrn(creationDto.getCrn()).withAccountId(creationDto.getAccountId()).withPlatform(creationDto.getCloudPlatform()).withResourceCrn(creationDto.getCrn()).withIsInternalTenant(internalTenant).withUserName(getUserNameFromCrn(creationDto.getCreator())).withAccountTags(accountTagsMap).withUserDefinedTags(userDefinedTags).build();
    try {
        Map<String, String> defaultTags = costTagging.prepareDefaultTags(request);
        return new Json(new EnvironmentTags(Objects.requireNonNullElseGet(userDefinedTags, HashMap::new), defaultTags));
    } catch (AccountTagValidationFailed aTVF) {
        throw new BadRequestException(aTVF.getMessage());
    } catch (Exception e) {
        throw new BadRequestException("Failed to convert dynamic tags. " + e.getMessage(), e);
    }
}
Also used : AccountTag(com.sequenceiq.environment.tags.domain.AccountTag) HashMap(java.util.HashMap) AccountTagResponse(com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponse) Json(com.sequenceiq.cloudbreak.common.json.Json) AccountTagValidationFailed(com.sequenceiq.cloudbreak.tag.AccountTagValidationFailed) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) EnvironmentTags(com.sequenceiq.environment.environment.domain.EnvironmentTags) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) CDPTagGenerationRequest(com.sequenceiq.cloudbreak.tag.request.CDPTagGenerationRequest)

Example 4 with AccountTagResponses

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

the class DefaultInternalAccountTagService method init.

@PostConstruct
public void init() throws IOException {
    String internalTags = cloudbreakResourceReaderService.resourceDefinition("default-internal-tags");
    Map<String, String> definition = JsonUtil.readValue(internalTags, Map.class);
    Set<AccountTagResponse> responses = new HashSet<>();
    for (Map.Entry<String, String> entry : definition.entrySet()) {
        AccountTagResponse accountTagResponse = new AccountTagResponse();
        accountTagResponse.setKey(entry.getKey());
        accountTagResponse.setValue(entry.getValue());
        accountTagResponse.setStatus(AccountTagStatus.DEFAULT);
        responses.add(accountTagResponse);
    }
    internalAccountTagResponses = new AccountTagResponses(responses);
}
Also used : AccountTagResponse(com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponse) AccountTagResponses(com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponses) Map(java.util.Map) HashSet(java.util.HashSet) PostConstruct(javax.annotation.PostConstruct)

Example 5 with AccountTagResponses

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

AccountTagResponses (com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponses)4 AccountTagResponse (com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponse)3 CloudbreakServiceException (com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException)2 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)1 Json (com.sequenceiq.cloudbreak.common.json.Json)1 AccountTagValidationFailed (com.sequenceiq.cloudbreak.tag.AccountTagValidationFailed)1 CDPTagGenerationRequest (com.sequenceiq.cloudbreak.tag.request.CDPTagGenerationRequest)1 EnvironmentTags (com.sequenceiq.environment.environment.domain.EnvironmentTags)1 AccountTag (com.sequenceiq.environment.tags.domain.AccountTag)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 PostConstruct (javax.annotation.PostConstruct)1 BadRequestException (javax.ws.rs.BadRequestException)1 ClientErrorException (javax.ws.rs.ClientErrorException)1 ProcessingException (javax.ws.rs.ProcessingException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Response (javax.ws.rs.core.Response)1