Search in sources :

Example 1 with ResourceBasicView

use of com.sequenceiq.cloudbreak.common.dal.ResourceBasicView in project cloudbreak by hortonworks.

the class DatalakeService method addSharedServiceResponse.

public void addSharedServiceResponse(Stack stack, StackV4Response stackResponse) {
    SharedServiceV4Response sharedServiceResponse = new SharedServiceV4Response();
    if (!Strings.isNullOrEmpty(stack.getDatalakeCrn())) {
        LOGGER.debug("Checking datalake through the datalakeCrn.");
        Optional<ResourceBasicView> resourceBasicView = stackService.getResourceBasicViewByResourceCrn(stack.getDatalakeCrn());
        if (resourceBasicView.isPresent()) {
            ResourceBasicView s = resourceBasicView.get();
            sharedServiceResponse.setSharedClusterId(s.getId());
            sharedServiceResponse.setSharedClusterName(s.getName());
        } else {
            LOGGER.debug("Unable to find datalake with CRN {}", stack.getDatalakeCrn());
        }
    }
    stackResponse.setSharedService(sharedServiceResponse);
}
Also used : ResourceBasicView(com.sequenceiq.cloudbreak.common.dal.ResourceBasicView) SharedServiceV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.cluster.sharedservice.SharedServiceV4Response)

Example 2 with ResourceBasicView

use of com.sequenceiq.cloudbreak.common.dal.ResourceBasicView in project cloudbreak by hortonworks.

the class RepositoryBasedDataCollector method fetchDataFromDbIfNeed.

/**
 * Attaches {@code RESOURCE_ID}, {@code RESOURCE_CRN}, or {@code RESOURCE_NAME} to the provided {@code params} if they're missing.
 *
 * @param params should include exactly one of {@code RESOURCE_CRN} or {@code RESOURCE_NAME}
 */
public void fetchDataFromDbIfNeed(Map<String, String> params) {
    String resourceCrn = params.get(RESOURCE_CRN);
    String resourceName = params.get(RESOURCE_NAME);
    // check each repository for a matching CRN or Name.
    for (Map.Entry<String, AccountAwareResourceRepository<?, ?>> pathRepositoryEntry : pathRepositoryMap.entrySet()) {
        AccountAwareResourceRepository<?, ?> resourceRepository = pathRepositoryEntry.getValue();
        if (resourceCrn == null && resourceName != null) {
            String accountId = ThreadBasedUserCrnProvider.getAccountId();
            Optional<ResourceBasicView> entity = resourceRepository.findResourceBasicViewByNameAndAccountId(resourceName, accountId);
            if (entity.isPresent()) {
                ResourceBasicView resource = entity.get();
                params.put(RESOURCE_ID, Long.toString(resource.getId()));
                params.put(RESOURCE_CRN, resource.getResourceCrn());
                break;
            }
        } else if (resourceName == null) {
            Optional<ResourceBasicView> entity = resourceRepository.findResourceBasicViewByResourceCrn(resourceCrn);
            if (entity.isPresent()) {
                ResourceBasicView resource = entity.get();
                params.put(RESOURCE_ID, Long.toString(resource.getId()));
                params.put(RESOURCE_NAME, resource.getName());
                break;
            }
        }
    }
}
Also used : ResourceBasicView(com.sequenceiq.cloudbreak.common.dal.ResourceBasicView) Optional(java.util.Optional) HashMap(java.util.HashMap) Map(java.util.Map) AccountAwareResourceRepository(com.sequenceiq.cloudbreak.common.dal.repository.AccountAwareResourceRepository)

Example 3 with ResourceBasicView

use of com.sequenceiq.cloudbreak.common.dal.ResourceBasicView in project cloudbreak by hortonworks.

the class DataCollectorComponentTest method testFetchDataFromDbIfNeedWhenCrnNotNullAndNameIsNullAndFound.

@Test
public void testFetchDataFromDbIfNeedWhenCrnNotNullAndNameIsNullAndFound() {
    AccountAwareResourceRepository<AccountAwareResource, Long> repo = mock(AccountAwareResourceRepository.class);
    ResourceBasicView resource = mock(ResourceBasicView.class);
    Optional<ResourceBasicView> entityOpt = Optional.of(resource);
    pathRepositoryMap.put("key", repo);
    Map<String, String> params = new HashMap<>();
    params.put(RESOURCE_CRN, "crn");
    params.put(RESOURCE_NAME, null);
    when(resource.getName()).thenReturn("name-ret");
    when(resource.getId()).thenReturn(342L);
    when(repo.findResourceBasicViewByResourceCrn("crn")).thenReturn(entityOpt);
    ThreadBasedUserCrnProvider.doAs(userCrn, () -> underTest.fetchDataFromDbIfNeed(params));
    Assertions.assertEquals("name-ret", params.get(RESOURCE_NAME));
    Assertions.assertEquals("crn", params.get(RESOURCE_CRN));
    Assertions.assertEquals("342", params.get(RESOURCE_ID));
    verify(repo).findResourceBasicViewByResourceCrn("crn");
}
Also used : ResourceBasicView(com.sequenceiq.cloudbreak.common.dal.ResourceBasicView) HashMap(java.util.HashMap) AccountAwareResource(com.sequenceiq.cloudbreak.common.dal.model.AccountAwareResource) Test(org.junit.jupiter.api.Test)

Example 4 with ResourceBasicView

use of com.sequenceiq.cloudbreak.common.dal.ResourceBasicView in project cloudbreak by hortonworks.

the class DataCollectorComponentTest method testFetchDataFromDbIfNeedWhenNameNotNullAndCrnIsNullAndFound.

@Test
public void testFetchDataFromDbIfNeedWhenNameNotNullAndCrnIsNullAndFound() {
    AccountAwareResourceRepository<AccountAwareResource, Long> repo = mock(AccountAwareResourceRepository.class);
    ResourceBasicView resource = mock(ResourceBasicView.class);
    pathRepositoryMap.put("key", repo);
    Map<String, String> params = new HashMap<>();
    params.put(RESOURCE_CRN, null);
    params.put(RESOURCE_NAME, "name");
    Optional<ResourceBasicView> entityOpt = Optional.of(resource);
    when(resource.getResourceCrn()).thenReturn("crn-ret");
    when(resource.getId()).thenReturn(342L);
    when(repo.findResourceBasicViewByNameAndAccountId("name", "acc")).thenReturn(entityOpt);
    ThreadBasedUserCrnProvider.doAs(userCrn, () -> underTest.fetchDataFromDbIfNeed(params));
    Assertions.assertEquals("name", params.get(RESOURCE_NAME));
    Assertions.assertEquals("crn-ret", params.get(RESOURCE_CRN));
    Assertions.assertEquals("342", params.get(RESOURCE_ID));
    verify(repo).findResourceBasicViewByNameAndAccountId("name", "acc");
}
Also used : ResourceBasicView(com.sequenceiq.cloudbreak.common.dal.ResourceBasicView) HashMap(java.util.HashMap) AccountAwareResource(com.sequenceiq.cloudbreak.common.dal.model.AccountAwareResource) Test(org.junit.jupiter.api.Test)

Example 5 with ResourceBasicView

use of com.sequenceiq.cloudbreak.common.dal.ResourceBasicView in project cloudbreak by hortonworks.

the class DatalakeServiceTest method testAddSharedServiceResponseWhenDatalakeCrnIsNotNull.

@Test
public void testAddSharedServiceResponseWhenDatalakeCrnIsNotNull() {
    ResourceBasicView resourceBasicView = mock(ResourceBasicView.class);
    when(resourceBasicView.getId()).thenReturn(1L);
    when(resourceBasicView.getName()).thenReturn("teststack");
    lenient().when(stackService.getResourceBasicViewByResourceCrn(anyString())).thenReturn(Optional.of(resourceBasicView));
    Stack source = new Stack();
    source.setDatalakeCrn("crn");
    StackV4Response x = new StackV4Response();
    underTest.addSharedServiceResponse(source, x);
    verify(stackService, times(1)).getResourceBasicViewByResourceCrn("crn");
    assertEquals(1L, x.getSharedService().getSharedClusterId());
    assertEquals("teststack", x.getSharedService().getSharedClusterName());
}
Also used : ResourceBasicView(com.sequenceiq.cloudbreak.common.dal.ResourceBasicView) StackV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.StackV4Response) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceBasicView (com.sequenceiq.cloudbreak.common.dal.ResourceBasicView)7 HashMap (java.util.HashMap)3 Test (org.junit.jupiter.api.Test)3 SharedServiceV4Response (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.cluster.sharedservice.SharedServiceV4Response)2 AccountAwareResource (com.sequenceiq.cloudbreak.common.dal.model.AccountAwareResource)2 SharedServiceV4Request (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.cluster.sharedservice.SharedServiceV4Request)1 StackV4Response (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.StackV4Response)1 AccountAwareResourceRepository (com.sequenceiq.cloudbreak.common.dal.repository.AccountAwareResourceRepository)1 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)1 Map (java.util.Map)1 Optional (java.util.Optional)1