package com.sequenceiq.freeipa.service;

import java.util.Map;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Service;

import com.sequenceiq.cloudbreak.auth.ThreadBasedUserCrnProvider;
import com.sequenceiq.cloudbreak.auth.altus.GrpcUmsClient;
import com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException;
import com.sequenceiq.cloudbreak.common.exception.WebApplicationExceptionMessageExtractor;
import com.sequenceiq.environment.api.v1.tags.endpoint.AccountTagEndpoint;
import com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponse;
import com.sequenceiq.environment.api.v1.tags.model.response.AccountTagResponses;

@Service
public class AccountTagService {

    @Inject
    private AccountTagEndpoint accountTagEndpoint;

    @Inject
    private WebApplicationExceptionMessageExtractor webApplicationExceptionMessageExtractor;

    public Map<String, String> list() {
        try {
            AccountTagResponses list = ThreadBasedUserCrnProvider
                    .doAs(GrpcUmsClient.INTERNAL_ACTOR_CRN, () -> accountTagEndpoint.list());
            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);
            }
        }
    }
}

@@ -289,133 +289,8 @@
 e;%0A%0A
-import com.sequenceiq.cloudbreak.auth.ThreadBasedUserCrnProvider;%0Aimport com.sequenceiq.cloudbreak.auth.altus.GrpcUmsClient;%0A
 impo
@@ -1002,101 +1002,8 @@
 st =
- ThreadBasedUserCrnProvider%0A                    .doAs(GrpcUmsClient.INTERNAL_ACTOR_CRN, () -%3E
  acc
@@ -1024,17 +1024,16 @@
 t.list()
-)
 ;%0A      

Revert "Call accounttag API with internal actor from freeipa"