Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing vc-worker formula #793

Merged
merged 1 commit into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -386,39 +386,43 @@ public void queryHostMetrics(SDDCSoftwareConfig vc) {
}

private void feedAssetMetricsFormulars(Asset asset) {

String assetId = asset.getId();
List<String> vcHostMetricsFormulaKeyList = new ArrayList<>(16);
// cpu
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_CPUUSAGE);
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_CPUUSEDINMHZ);
// memory
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_ACTIVEMEMORY);
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_BALLOONMEMORY);
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_CONSUMEDMEMORY);
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_SHAREDMEMORY);
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_SWAPMEMORY);
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_MEMORYUSAGE);
// storage
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_STORAGEIORATEUSAGE);
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_STORAGEUSED);
// power
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_POWER);
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_MINIMUM_USED_POWER);
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_PEAK_USED_POWER);
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_AVERAGE_USED_POWER);
vcHostMetricsFormulaKeyList.add(MetricName.SERVER_ENERGY_CONSUMPTION);

Map<String, String> metricsFormulas = asset.getMetricsformulars();
Map<String, String> metrics = null;
Map<String, String> hostMetricsFormula = new HashMap<>();
if (StringUtils.isNotBlank(metricsFormulas.get(FlowgateConstant.HOST_METRICS))) {
metrics = asset.metricsFormulaToMap(metricsFormulas.get(FlowgateConstant.HOST_METRICS), new TypeReference<Map<String, String>>() {});
hostMetricsFormula = asset.metricsFormulaToMap(metricsFormulas.get(FlowgateConstant.HOST_METRICS), new TypeReference<Map<String, String>>() {});
}
if (metrics == null || metrics.isEmpty()) {
metrics = new HashMap<>();
// cpu
metrics.put(MetricName.SERVER_CPUUSAGE, assetId);
metrics.put(MetricName.SERVER_CPUUSEDINMHZ, assetId);

// memory
metrics.put(MetricName.SERVER_ACTIVEMEMORY, assetId);
metrics.put(MetricName.SERVER_BALLOONMEMORY, assetId);
metrics.put(MetricName.SERVER_CONSUMEDMEMORY, assetId);
metrics.put(MetricName.SERVER_SHAREDMEMORY, assetId);
metrics.put(MetricName.SERVER_SWAPMEMORY, assetId);
metrics.put(MetricName.SERVER_MEMORYUSAGE, assetId);

// storage
metrics.put(MetricName.SERVER_STORAGEIORATEUSAGE, assetId);
metrics.put(MetricName.SERVER_STORAGEUSAGE, assetId);
metrics.put(MetricName.SERVER_STORAGEUSED, assetId);

// power
metrics.put(MetricName.SERVER_POWER, assetId);
metrics.put(MetricName.SERVER_MINIMUM_USED_POWER, assetId);
metrics.put(MetricName.SERVER_PEAK_USED_POWER, assetId);
metrics.put(MetricName.SERVER_AVERAGE_USED_POWER, assetId);
metrics.put(MetricName.SERVER_ENERGY_CONSUMPTION, assetId);
metricsFormulas.put(FlowgateConstant.HOST_METRICS, asset.metricsFormulaToString(metrics));
boolean isUpdated = false;
for (String vcHostMetricsFormulaKey : vcHostMetricsFormulaKeyList) {
if (!hostMetricsFormula.containsKey(vcHostMetricsFormulaKey)) {
hostMetricsFormula.put(vcHostMetricsFormulaKey, assetId);
isUpdated = true;
}
}
if (isUpdated) {
metricsFormulas.put(FlowgateConstant.HOST_METRICS, asset.metricsFormulaToString(hostMetricsFormula));
asset.setMetricsformulars(metricsFormulas);
restClient.saveAssets(asset);
}
}
Expand Down Expand Up @@ -1126,9 +1130,7 @@ public boolean feedHostMetaData(HostSystem host, HostInfo hostInfo) {
private void syncCustomAttributes(SDDCSoftwareConfig vc) {
// TODO need to allow only update 1 vcenter instead of all the vcenter.

try (VsphereClient vsphereClient =
VsphereClient.connect(String.format(VCConstants.SDKURL, vc.getServerURL()),
vc.getUserName(), vc.getPassword(), !vc.isVerifyCert());) {
try (VsphereClient vsphereClient = connectVsphere(vc)) {
for (String key : VCConstants.hostCustomAttrMapping.values()) {
vsphereClient.createCustomAttribute(key, VCConstants.HOSTSYSTEM);
}
Expand Down Expand Up @@ -1180,9 +1182,7 @@ private void syncCustomAttributes(SDDCSoftwareConfig vc) {
private void syncCustomerAttrsData(SDDCSoftwareConfig vcInfo) {
restClient.setServiceKey(serviceKeyConfig.getServiceKey());

try (VsphereClient vsphereClient =
VsphereClient.connect(String.format(VCConstants.SDKURL, vcInfo.getServerURL()),
vcInfo.getUserName(), vcInfo.getPassword(), !vcInfo.isVerifyCert());) {
try (VsphereClient vsphereClient = connectVsphere(vcInfo)) {
ServerMapping[] mappings = null;
try {
mappings = restClient.getServerMappingsByVC(vcInfo.getId()).getBody();
Expand Down Expand Up @@ -1297,10 +1297,7 @@ private void feedData(Map<String, Asset> assetDictionary, List<ServerMapping> va
for (ServerMapping validServer : validMapping) {
HostSystem host = hostDictionary.get(validServer.getVcMobID());
Asset asset = assetDictionary.get(validServer.getAsset());
String hostMetricsMap = asset.getMetricsformulars().get(FlowgateConstant.HOST_METRICS);
if (hostMetricsMap == null || hostMetricsMap.isEmpty()) {
feedAssetMetricsFormulars(asset);
}
feedAssetMetricsFormulars(asset);
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(asset);
for (String key : VCConstants.hostCustomAttrMapping.keySet()) {
host.setCustomValue(VCConstants.hostCustomAttrMapping.get(key),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
Expand All @@ -16,6 +17,9 @@
import java.util.List;
import java.util.Map;

import com.vmware.flowgate.common.model.redis.message.EventMessage;
import com.vmware.flowgate.common.model.redis.message.EventType;
import com.vmware.flowgate.common.model.redis.message.impl.EventMessageUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -24,6 +28,8 @@
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

Expand Down Expand Up @@ -103,6 +109,9 @@ public class VCDataServiceTest {
@Mock
private VsphereClient vsphereClient;

@Mock
private StringRedisTemplate template;

private ObjectMapper mapper = new ObjectMapper();

@Before
Expand Down Expand Up @@ -860,4 +869,56 @@ public void testGetStatisticsValueUnit() {
}
}

@Test
public void testSyncCustomerAttrsData() throws Exception {
String assetId = "QONVN1098G1NVN01NG01";
String vcMobID = "host-11";

SDDCSoftwareConfig sddcSoftwareConfig = new SDDCSoftwareConfig();
sddcSoftwareConfig.setType(SDDCSoftwareConfig.SoftwareType.VCENTER);
sddcSoftwareConfig.setServerURL("https://1.1.1.1");
sddcSoftwareConfig.setPassword("ASDFGAGAHAHwegqhwrjw");
sddcSoftwareConfig.setVerifyCert(false);
EventMessage eventMessage = EventMessageUtil.createEventMessage(EventType.VCenter, EventMessageUtil.VCENTER_SyncCustomerAttrsData, mapper.writeValueAsString(sddcSoftwareConfig));

ListOperations<String, String> listOperations = Mockito.mock(ListOperations.class);
doReturn(listOperations).when(template).opsForList();
doReturn(mapper.writeValueAsString(eventMessage), null).when(listOperations).rightPop(EventMessageUtil.vcJobList);
doReturn(vsphereClient).when(service).connectVsphere(any());

ServerMapping serverMapping = new ServerMapping();
serverMapping.setVcMobID(vcMobID);
serverMapping.setVcHostName("server-1");
serverMapping.setAsset(assetId);
ServerMapping[] serverMappings = { serverMapping };
doReturn(new ResponseEntity<>(serverMappings, HttpStatus.OK)).when(restClient).getServerMappingsByVC(any());

Collection<HostSystem> hostSystems = new ArrayList<>();
HostSystem hostSystem = mock(HostSystem.class);
ManagedObjectReference managedObjectReference = mock(ManagedObjectReference.class);
hostSystems.add(hostSystem);
doReturn(managedObjectReference).when(hostSystem)._getRef();
doReturn("server-1").when(hostSystem).getName();
doReturn(vcMobID).when(managedObjectReference).getValue();
doReturn(hostSystems).when(vsphereClient).getAllHost();
Asset asset = new Asset();
asset.setId(assetId);
Map<String, String> metricsFormulas = new HashMap<>();
Map<String, String> hostMetricsFormula = new HashMap<>();
hostMetricsFormula.put(MetricName.SERVER_TEMPERATURE, asset.getId());
hostMetricsFormula.put(MetricName.SERVER_PEAK_TEMPERATURE, asset.getId());
hostMetricsFormula.put(MetricName.SERVER_AVERAGE_TEMPERATURE, asset.getId());
hostMetricsFormula.put(MetricName.SERVER_ENERGY_CONSUMPTION, asset.getId());
hostMetricsFormula.put(MetricName.SERVER_POWER, asset.getId());
hostMetricsFormula.put(MetricName.SERVER_AVERAGE_USED_POWER, asset.getId());
hostMetricsFormula.put(MetricName.SERVER_PEAK_USED_POWER, asset.getId());
hostMetricsFormula.put(MetricName.SERVER_MINIMUM_USED_POWER, asset.getId());
metricsFormulas.put(FlowgateConstant.HOST_METRICS, asset.metricsFormulaToString(hostMetricsFormula));
asset.setMetricsformulars(metricsFormulas);
Asset[] assets = { asset };
doReturn(new ResponseEntity<>(assets, HttpStatus.OK)).when(restClient).getAssetsByVCID(any());
doReturn(new ResponseEntity<Void>(HttpStatus.OK)).when(restClient).saveAssets(any(Asset.class));
service.executeAsync(EventMessageUtil.createEventMessage(EventType.VCenter, EventMessageUtil.VCENTER_SyncData, null));
}

}