Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
cmdb remoteCreate accepting business service ID as alternative; dashb…
Browse files Browse the repository at this point in the history
…oard remoteCreate behaves as remoteUpdate when existing (#155)

* cmdb remoteCreate accepting business service ID as alternative; dashboard remoteCreate behaves as remoteUpdate when existing

* Up version to 3.2.7-SNAPSHOT

* Fix review comments
  • Loading branch information
benj58xu authored Aug 10, 2020
1 parent 380db7a commit 63412d5
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>api</artifactId>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<version>3.2.6-SNAPSHOT</version>
<version>3.2.7-SNAPSHOT</version>
<description>Hygieia Rest API Layer</description>
<url>https://github.com/Hygieia/api</url>

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/capitalone/dashboard/request/CmdbRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class CmdbRequest {
private String assignmentGroup;
@NotNull
private String ownerDept;

private String businessService;

/**
* commonName Human readable value of the configurationItem
*/
Expand Down Expand Up @@ -92,4 +95,11 @@ public String getToolName() {
public void setToolName(String toolName) {
this.toolName = toolName;
}

public String getBusinessService() {
return businessService;
}

public void setBusinessService(String businessService) { this.businessService = businessService; }

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.capitalone.dashboard.service.CmdbRemoteService;
import com.capitalone.dashboard.service.CmdbService;
import com.capitalone.dashboard.util.PaginationHeaderUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -31,6 +33,8 @@ public class CmdbController {
private final CmdbRemoteService cmdbRemoteService;
private PaginationHeaderUtility paginationHeaderUtility;

private static final Logger LOGGER = LoggerFactory.getLogger(CmdbController.class);

@Autowired
public CmdbController(CmdbService cmdbService, PaginationHeaderUtility paginationHeaderUtility, CmdbRemoteService cmdbRemoteService ) {

Expand Down Expand Up @@ -62,6 +66,7 @@ public ResponseEntity<Object> remoteCreateDashboard( @Valid @RequestBody CmdbReq
.status( HttpStatus.CREATED )
.body( cmdbRemoteService.remoteCreate( request ) );
} catch (HygieiaException he) {
LOGGER.error("Failed to create cmdb entry", he);
return ResponseEntity
.status( HttpStatus.BAD_REQUEST )
.body( he.getMessage() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.capitalone.dashboard.model.Dashboard;
import com.capitalone.dashboard.request.DashboardRemoteRequest;
import com.capitalone.dashboard.service.DashboardRemoteService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -20,9 +23,9 @@
@RestController
public class DashboardRemoteController {

// private static final Logger LOGGER = LoggerFactory.getLogger(DashboardRemoteController.class);
private final DashboardRemoteService dashboardRemoteService;
private static final Logger LOGGER = LoggerFactory.getLogger(DashboardRemoteController.class);

private final DashboardRemoteService dashboardRemoteService;

@Autowired
public DashboardRemoteController(DashboardRemoteService dashboardRemoteService) {
Expand All @@ -37,7 +40,8 @@ public ResponseEntity<String> remoteCreateDashboard(@Valid @RequestBody Dashboar
return ResponseEntity
.status(HttpStatus.CREATED)
.body("Successfully created dashboard: id =" + dashboard.getId());
} catch (HygieiaException he) {
} catch (Exception he) {
LOGGER.error("RemoteCreate receives exception", he);
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body("Failed to create dashboard. Error: " + he.getMessage());
Expand All @@ -52,7 +56,8 @@ public ResponseEntity<String> remoteUpdateDashboard(@Valid @RequestBody Dashboar
return ResponseEntity
.status(HttpStatus.CREATED)
.body("Successfully updated dashboard: id =" + dashboard.getId());
} catch (HygieiaException he) {
} catch (Exception he) {
LOGGER.error("RemoteUpdate receives exception", he);
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body("Failed to update dashboard. Error: " + he.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public CmdbRemoteServiceImpl(
@Override
public Cmdb remoteCreate(CmdbRequest request ) throws HygieiaException {

Cmdb businessService = findBusinessService(request);

validateRequest(request);

updateRelationship(request);
Expand Down Expand Up @@ -77,18 +79,33 @@ private void updateRelationship( CmdbRequest request ) {
}
}

private Cmdb findBusinessService(CmdbRequest request) throws HygieiaException {
String businessService = request.getBusinessService();
String busServiceName = request.getConfigurationItemBusServName();
Cmdb cmdb = null;
if (!StringUtils.isEmpty(businessService)) {
cmdb = cmdbRepository.findByConfigurationItemAndItemType(businessService, APP_TYPE);
if (cmdb == null) {
throw new HygieiaException("Configuration Item " + businessService + " does not exist", HygieiaException.BAD_DATA);
}
} else if (!StringUtils.isEmpty(busServiceName)) {
cmdb = cmdbRepository.findByConfigurationItemAndItemType(busServiceName, APP_TYPE);
if (cmdb == null) {
throw new HygieiaException("Configuration Item " + busServiceName + " does not exist", HygieiaException.BAD_DATA);
}
}
return cmdb;
}

/**
* Validates CmdbRequest for errors
* @param request
* @throws HygieiaException
*/
private void validateRequest(CmdbRequest request) throws HygieiaException {
String busServiceName = request.getConfigurationItemBusServName();
if(!StringUtils.isEmpty( busServiceName ) && cmdbRepository.findByConfigurationItemAndItemType( busServiceName, APP_TYPE ) == null){
throw new HygieiaException("Configuration Item " + busServiceName + " does not exist", HygieiaException.BAD_DATA);
}

Cmdb cmdb = cmdbRepository.findByConfigurationItemIgnoreCaseOrCommonNameIgnoreCase(request.getConfigurationItem(), request.getCommonName());

if(cmdb != null){
throw new HygieiaException("Configuration Item " + cmdb.getConfigurationItem() + " already exists", HygieiaException.DUPLICATE_DATA);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,19 @@ public Dashboard remoteCreate(DashboardRemoteRequest request, boolean isUpdate)

List<Dashboard> dashboards = findExistingDashboardsFromRequest( request );
if (!CollectionUtils.isEmpty(dashboards)) {
dashboard = dashboards.get(0);
if (dashboards.size()==1) {
dashboard = dashboards.get(0);
} else {
dashboard = chooseDashboard(dashboards, request);
}
Set<Owner> uniqueOwners = new HashSet<Owner>(validOwners);
uniqueOwners.addAll(dashboard.getOwners());
dashboard.setOwners(new ArrayList<Owner>(uniqueOwners));
dashboard.setConfigurationItemBusAppName(request.getMetaData().getBusinessApplication());
dashboard.setConfigurationItemBusServName(request.getMetaData().getBusinessService());
if (!isUpdate) {
throw new HygieiaException("Dashboard " + dashboard.getTitle() + " (id =" + dashboard.getId() + ") already exists", HygieiaException.DUPLICATE_DATA);
}
// if (!isUpdate) {
// throw new HygieiaException("Dashboard " + dashboard.getTitle() + " (id =" + dashboard.getId() + ") already exists", HygieiaException.DUPLICATE_DATA);
// }
dashboardService.update(dashboard);
//Save the widgets
for (Widget w : dashboard.getWidgets()) {
Expand Down Expand Up @@ -194,6 +198,27 @@ public Dashboard remoteCreate(DashboardRemoteRequest request, boolean isUpdate)
return (dashboard != null) ? dashboardService.get(dashboard.getId()) : null;
}

private Dashboard chooseDashboard(List<Dashboard> dashboards, DashboardRemoteRequest request) {
Dashboard dashboard = null;
String businessService = request.getMetaData().getBusinessService();
String businessApplication = request.getMetaData().getBusinessApplication();
String title = request.getMetaData().getTitle();
for (Dashboard one : dashboards) {
if (dashboard==null) {
dashboard = one;
} else if (one.getUpdatedAt()>dashboard.getUpdatedAt()) {
dashboard = one;
} else if (one.getUpdatedAt()==dashboard.getUpdatedAt()) {
if (one.getCreatedAt()>dashboard.getCreatedAt()) {
dashboard = one;
}
}
}
LOG.warn(String.format("MultipleDashboards=%d, businessService=%s, businessApplication=%s, title=%s, selected=%s",
dashboards.size(), businessService, businessApplication, title, dashboard.getId()));
return dashboard;
}

/**
* Generates a Widget Request list of Widgets to be created from the request
* @param entries
Expand Down Expand Up @@ -244,7 +269,7 @@ private List< Dashboard > findExistingDashboardsFromRequest( DashboardRemoteRequ
List<Dashboard> existing = new ArrayList<>();
if( !StringUtils.isEmpty( businessService ) && !StringUtils.isEmpty( businessApplication ) ){
existing.addAll(dashboardRepository.findAllByConfigurationItemBusServNameContainingIgnoreCaseAndConfigurationItemBusAppNameContainingIgnoreCase( businessService, businessApplication ));
}if(StringUtils.isNotEmpty(title)) {
} else if (StringUtils.isNotEmpty(title)) {
existing.addAll(dashboardRepository.findByTitle( request.getMetaData().getTitle() ));
}
return existing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ private Dashboard create(Dashboard dashboard, boolean isUpdate) throws HygieiaEx
Iterable<Component> components = null;

if(!isUpdate) {
dashboard.setCreatedAt(System.currentTimeMillis());
components = componentRepository.save(dashboard.getApplication().getComponents());
} else {
dashboard.setUpdatedAt(System.currentTimeMillis());
}
dashboard.setUpdatedAt(System.currentTimeMillis());

try {
duplicateDashboardErrorCheck(dashboard);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void remoteCreate() throws HygieiaException {
ObjectId objectId = ObjectId.get();
expected.setId(objectId);
CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype",
"type", "assignmentgroup","owner", "BAPTEST", "", "cmdbCollector");
"type", "assignmentgroup","owner", "BAPTEST", null, "", "cmdbCollector");
when(cmdbRepository.findByConfigurationItemAndItemType("","" )).thenReturn(null);
when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem());
when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB));
Expand All @@ -69,7 +69,7 @@ public void remoteCreateInvalidBusService() throws HygieiaException {
ObjectId objectId = ObjectId.get();
expected.setId(objectId);
CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype",
"type", "assignmentgroup","owner", "BAPTEST", "ASVTEST", "cmdbCollector");
"type", "assignmentgroup","owner", "BAPTEST", null,"ASVTEST", "cmdbCollector");
when(cmdbRepository.findByConfigurationItemAndItemType("","" )).thenReturn(null);
when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem());
when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB));
Expand All @@ -84,6 +84,29 @@ public void remoteCreateInvalidBusService() throws HygieiaException {
assertEquals(excep.getMessage(), e.getMessage());
}
}

/**
* Test the use of businessService instead of configurationItemBusServName in the request.
* @throws HygieiaException
*/
@Test
public void remoteCreateUsingBusinessService() throws HygieiaException {
Cmdb businessServiceItem = makeCmdbItem("CI123456", "subtype",
"type", "assignmentgroup","owner", "ASVTEST");
Cmdb expected = makeCmdbItem("BAPTEST", "subtype",
"type", "assignmentgroup","owner", "BAPTEST");
ObjectId objectId = ObjectId.get();
expected.setId(objectId);
CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype",
"type", "assignmentgroup","owner", "BAPTEST", "CI123456", "", "cmdbCollector");
when(cmdbRepository.findByConfigurationItemAndItemType("CI123456","app" )).thenReturn(businessServiceItem);
when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem());
when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB));
when(cmdbRepository.save(Matchers.any(Cmdb.class))).thenReturn(expected);

assertThat(cmdbRemoteService.remoteCreate(request), is(expected));
}

/**
* Tests remoteCreate functionality ConfigurationItemBusServName doesn't have existing relationships
* @throws HygieiaException
Expand All @@ -97,7 +120,7 @@ public void remoteCreateRelationshipUpdateExistingCompsNull() throws HygieiaExce
ObjectId objectId = ObjectId.get();
expected.setId(objectId);
CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype",
"type", "assignmentgroup","owner", "BAPTEST", "ASVTEST", "cmdbCollector");
"type", "assignmentgroup","owner", "BAPTEST", null,"ASVTEST", "cmdbCollector");
when(cmdbRepository.findByConfigurationItemAndItemType("","" )).thenReturn(null);
when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem());
when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB));
Expand All @@ -122,7 +145,7 @@ public void remoteCreateRelationshipUpdateExistingComps() throws HygieiaExceptio
ObjectId objectId = ObjectId.get();
expected.setId(objectId);
CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype",
"type", "assignmentgroup","owner", "BAPTEST", "ASVTEST", "cmdbCollector");
"type", "assignmentgroup","owner", "BAPTEST", null,"ASVTEST", "cmdbCollector");
when(cmdbRepository.findByConfigurationItemAndItemType("","" )).thenReturn(null);
when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem());
when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB));
Expand Down Expand Up @@ -154,6 +177,7 @@ private CmdbRequest makeCmdbRequest(String configurationItem,
String assignmentGroup,
String ownerDept,
String commonName,
String businessService,
String configurationItemBusServName,
String toolName){

Expand All @@ -164,6 +188,7 @@ private CmdbRequest makeCmdbRequest(String configurationItem,
request.setAssignmentGroup(assignmentGroup);
request.setOwnerDept(ownerDept);
request.setCommonName(commonName);
request.setBusinessService(businessService);
request.setConfigurationItemBusServName(configurationItemBusServName);
request.setToolName(toolName);
return request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertNotNull;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ApiTestConfig.class, FongoConfig.class})
Expand Down Expand Up @@ -210,18 +211,15 @@ public void remoteCreateInvalidCompAndApp() throws IOException {
}
}
@Test
public void remoteCreateDuplicateDashboard() throws IOException {
public void remoteCreateDuplicateDashboard() throws HygieiaException, IOException {
DashboardRemoteRequest request = getRemoteRequest("./dashboardRemoteRequests/0-Remote-Update-Repo.json");
Dashboard dashboard = dashboardRepository.findByTitle(request.getMetaData().getTitle()).get(0);
Throwable t = new Throwable();
RuntimeException excep = new RuntimeException("Dashboard "+dashboard.getTitle()+" (id =" + dashboard.getId() + ") already exists", t);

try {
dashboardRemoteService.remoteCreate(request, false);
fail("Should throw RuntimeException");
} catch(Exception e) {
assertEquals(excep.getMessage(), e.getMessage());
}
Dashboard found = dashboardRemoteService.remoteCreate(request, false);
assertEquals(dashboard.getId(), found.getId());
assertTrue(found.getUpdatedAt()>dashboard.getUpdatedAt());
}
@Test
public void remoteCreate() throws HygieiaException, IOException {
Expand Down

0 comments on commit 63412d5

Please sign in to comment.