use of com.vmware.flowgate.common.model.AssetIPMapping in project flowgate by vmware.
the class AssetControllerTest method updateHostNameIPMappingFailureExample.
@Test
public void updateHostNameIPMappingFailureExample() throws Exception {
SetOperations<String, String> setOperations = Mockito.mock(SetOperations.class);
when(template.hasKey(anyString())).thenReturn(false);
when(template.opsForSet()).thenReturn(setOperations);
when(setOperations.add(anyString(), any())).thenReturn(1l);
Asset server = createAsset();
server.setCategory(AssetCategory.Server);
server.setAssetName("cloud-sha1-esx2");
server = assetRepository.save(server);
Asset server1 = createAsset();
server1.setCategory(AssetCategory.Server);
server1.setAssetName("cloud-sha1-esx8");
server1 = assetRepository.save(server1);
AssetIPMapping assetipmapping = createAssetIPMapping();
assetipmapping.setAssetname(server.getAssetName());
assetipmapping = assetIPMappingRepository.save(assetipmapping);
AssetIPMapping newAssetIPMapping = createAssetIPMapping();
newAssetIPMapping.setId(assetipmapping.getId());
newAssetIPMapping.setAssetname("cloud-server1");
newAssetIPMapping.setIp("192.168.0.1");
expectedEx.expect(WormholeRequestException.class);
expectedEx.expectMessage("Can't find any asset with the name : " + newAssetIPMapping.getAssetname());
MvcResult result = this.mockMvc.perform(put("/v1/assets/mapping/hostnameip").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(newAssetIPMapping))).andExpect(status().is5xxServerError()).andReturn();
if (result.getResolvedException() != null) {
assetRepository.deleteById(server.getId());
assetRepository.deleteById(server1.getId());
assetIPMappingRepository.deleteById(assetipmapping.getId());
throw result.getResolvedException();
}
}
use of com.vmware.flowgate.common.model.AssetIPMapping in project flowgate by vmware.
the class AssetControllerTest method updateHostNameIPMappingMacIsNullExample.
@Test
public void updateHostNameIPMappingMacIsNullExample() throws Exception {
SetOperations<String, String> setOperations = Mockito.mock(SetOperations.class);
when(template.hasKey(anyString())).thenReturn(false);
when(template.opsForSet()).thenReturn(setOperations);
when(setOperations.add(anyString(), any())).thenReturn(1l);
Asset server = createAsset();
server.setCategory(AssetCategory.Server);
server.setAssetName("cloud-sha1-esx2");
server = assetRepository.save(server);
Asset server1 = createAsset();
server1.setCategory(AssetCategory.Server);
server1.setAssetName("cloud-sha1-esx8");
server1 = assetRepository.save(server1);
AssetIPMapping assetipmapping = createAssetIPMapping();
assetipmapping.setAssetname(server.getAssetName());
assetipmapping = assetIPMappingRepository.save(assetipmapping);
AssetIPMapping newAssetIPMapping = createAssetIPMapping();
newAssetIPMapping.setAssetname(server1.getAssetName());
newAssetIPMapping.setId(assetipmapping.getId());
newAssetIPMapping.setMacAddress(null);
this.mockMvc.perform(put("/v1/assets/mapping/hostnameip").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(newAssetIPMapping))).andExpect(status().isOk());
assetipmapping = assetIPMappingRepository.findById(assetipmapping.getId()).get();
assetRepository.deleteById(server.getId());
assetRepository.deleteById(server1.getId());
assetIPMappingRepository.deleteById(assetipmapping.getId());
TestCase.assertEquals(server1.getAssetName(), assetipmapping.getAssetname());
TestCase.assertNull(assetipmapping.getMacAddress());
}
use of com.vmware.flowgate.common.model.AssetIPMapping in project flowgate by vmware.
the class AssetControllerTest method getHostNameByIPExample.
@Test
public void getHostNameByIPExample() throws Exception {
AssetIPMapping mapping1 = createAssetIPMapping();
assetIPMappingRepository.save(mapping1);
AssetIPMapping mapping2 = createAssetIPMapping();
assetIPMappingRepository.save(mapping2);
try {
FieldDescriptor[] fieldpath = new FieldDescriptor[] { fieldWithPath("id").description("ID of the AssetIPMapping, created by flowgate"), fieldWithPath("ip").description("IP of AssetIPMapping."), fieldWithPath("macAddress").description("macAddress of IP."), fieldWithPath("assetname").description("name of asset.") };
this.mockMvc.perform(get("/v1/assets/mapping/hostnameip/ip/{ip}", mapping1.getIp())).andExpect(status().isOk()).andDo(document("assets-getHostNameByIP-example", pathParameters(parameterWithName("ip").description("IP of AssetIPMapping.")), responseFields(fieldWithPath("[]").description("An array of AssetIPMapping")).andWithPrefix("[].", fieldpath)));
} finally {
assetIPMappingRepository.deleteById(mapping1.getId());
assetIPMappingRepository.deleteById(mapping2.getId());
}
}
use of com.vmware.flowgate.common.model.AssetIPMapping in project flowgate by vmware.
the class InfoBloxService method executeAsync.
@Override
@Async("asyncServiceExecutor")
public void executeAsync(EventMessage eventMessage) {
// The data format of the message. should be an ip address.
// Do the business here.
String message = eventMessage.getContent();
logger.info(String.format("Try to find hostname for ip: %s", message));
// check message , make sure it is an valid ip address;
wormholeAPIClient.setServiceKey(serviceKeyConfig.getServiceKey());
FacilitySoftwareConfig[] infoBloxes = wormholeAPIClient.getFacilitySoftwareInternalByType(SoftwareType.InfoBlox).getBody();
List<InfoBloxIPInfoResult> infoBloxIPInfoResults = new ArrayList<>();
List<InfoBloxIPInfoResult> hostRecordResults = null;
List<InfoBloxIPInfoResult> ipv4addressResults = null;
for (FacilitySoftwareConfig infoblox : infoBloxes) {
if (!infoblox.checkIsActive()) {
continue;
}
InfobloxClient client = buildInfobloxClient(infoblox);
IntegrationStatus integrationStatus = infoblox.getIntegrationStatus();
IntegrationStatus.Status status = integrationStatus == null ? null : integrationStatus.getStatus();
try {
if (status == null || IntegrationStatus.Status.ACTIVE.equals(status)) {
hostRecordResults = client.queryHostRecordByIP(message);
logger.debug("queryHostRecordByIP: {}", hostRecordResults);
if (hostRecordResults != null && !hostRecordResults.isEmpty()) {
infoBloxIPInfoResults.addAll(hostRecordResults);
}
}
if (hostRecordResults == null || hostRecordResults.isEmpty()) {
ipv4addressResults = client.queryIpv4addressByIP(message);
logger.debug("queryHostNamesByIP: {}", ipv4addressResults);
if (ipv4addressResults != null && !ipv4addressResults.isEmpty()) {
infoBloxIPInfoResults.addAll(ipv4addressResults);
}
if (!IntegrationStatus.Status.WARNING.equals(status) && ipv4addressResults != null) {
updateIntegrationStatusToWarning(infoblox, message);
}
}
} catch (ResourceAccessException e) {
if (e.getCause().getCause() instanceof ConnectException) {
checkAndUpdateIntegrationStatus(infoblox, e.getCause().getCause().getMessage());
continue;
}
} catch (HttpClientErrorException e1) {
logger.error("Failed to query data from Infoblox", e1);
if (integrationStatus == null) {
integrationStatus = new IntegrationStatus();
}
integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
integrationStatus.setDetail(e1.getMessage());
integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
infoblox.setIntegrationStatus(integrationStatus);
updateIntegrationStatus(infoblox);
continue;
}
if (integrationStatus != null && integrationStatus.getRetryCounter() > 0) {
integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
updateIntegrationStatus(infoblox);
}
if (!infoBloxIPInfoResults.isEmpty()) {
for (InfoBloxIPInfoResult infoBloxIPInfoResult : infoBloxIPInfoResults) {
try {
Asset asset = wormholeAPIClient.getAssetByName(infoBloxIPInfoResult.getHostName()).getBody();
if (asset == null) {
logger.info(String.format("hostname (%s) no found!", infoBloxIPInfoResult.getHostName()));
continue;
}
} catch (HttpClientErrorException e) {
logger.error(String.format("Error when searching %s", infoBloxIPInfoResult.getHostName()), e);
continue;
}
AssetIPMapping tempMapping = new AssetIPMapping();
tempMapping.setAssetname(infoBloxIPInfoResult.getHostName());
tempMapping.setMacAddress(infoBloxIPInfoResult.getMacAddress());
tempMapping.setIp(message);
AssetIPMapping[] mappings = wormholeAPIClient.getHostnameIPMappingByIP(message).getBody();
boolean isNewMapping = true;
if (null != mappings && mappings.length > 0) {
for (AssetIPMapping mapping : mappings) {
if (tempMapping.getAssetname().equals(mapping.getAssetname())) {
if (!StringUtils.equals(mapping.getMacAddress(), tempMapping.getMacAddress())) {
mapping.setMacAddress(tempMapping.getMacAddress());
wormholeAPIClient.updateHostnameIPMapping(mapping);
}
isNewMapping = false;
break;
}
}
}
if (isNewMapping) {
wormholeAPIClient.createHostnameIPMapping(tempMapping);
}
logger.info(String.format("Find hostname %s for ip %s", infoBloxIPInfoResult.getHostName(), message));
return;
}
}
}
logger.info(String.format("Cannot find the hostname for IP: %s", message));
}
Aggregations