use of com.vmware.vim.binding.vmodl.ManagedObjectReference in project flowgate by vmware.
the class VCDataServiceTest method testQueryHostMetrics.
@Test
public void testQueryHostMetrics() throws Exception {
SDDCSoftwareConfig vc = Mockito.mock(SDDCSoftwareConfig.class);
HashMap<String, ServerMapping> serverMappingMap = new HashMap<String, ServerMapping>();
ServerMapping mapping1 = new ServerMapping();
mapping1.setVcMobID("vc1");
mapping1.setAsset("asset1");
serverMappingMap.put(mapping1.getVcMobID(), mapping1);
ServerMapping mapping2 = new ServerMapping();
mapping2.setVcMobID("vc2");
mapping2.setAsset("asset2");
serverMappingMap.put(mapping2.getVcMobID(), mapping2);
doReturn(serverMappingMap).when(service).getVaildServerMapping(any());
doReturn(vsphereClient).when(service).connectVsphere(any());
HostSystem host1 = Mockito.mock(HostSystem.class);
HostSystem host2 = Mockito.mock(HostSystem.class);
Collection<HostSystem> hosts = new ArrayList<>();
hosts.add(host1);
hosts.add(host2);
when(vsphereClient.getAllHost()).thenReturn(hosts);
ManagedObjectReference mor1 = Mockito.mock(ManagedObjectReference.class);
ManagedObjectReference mor2 = Mockito.mock(ManagedObjectReference.class);
when(host1._getRef()).thenReturn(mor1);
when(mor1.getValue()).thenReturn("vc1");
when(host2._getRef()).thenReturn(mor2);
when(mor2.getValue()).thenReturn("vc2");
Asset asset = new Asset();
asset.setId("asset1");
Map<String, String> metricsFormulars = new HashMap<>();
Map<String, String> metrics = new HashMap<>();
metricsFormulars.put(FlowgateConstant.HOST_METRICS, mapper.writeValueAsString(metrics));
asset.setMetricsformulars(metricsFormulars);
ResponseEntity<Asset> assets = Mockito.mock(ResponseEntity.class);
when(restClient.getAssetByID(anyString())).thenReturn(assets);
when(assets.getBody()).thenReturn(asset);
doNothing().when(service).feedHostUsageData(any(), any(), any());
service.queryHostMetrics(vc);
}
use of com.vmware.vim.binding.vmodl.ManagedObjectReference in project flowgate by vmware.
the class VCDataServiceTest method testQueryHostMetaDataForclustersIsNull.
@Test
public void testQueryHostMetaDataForclustersIsNull() throws Exception {
SDDCSoftwareConfig vc = Mockito.mock(SDDCSoftwareConfig.class);
HashMap<String, ServerMapping> serverMappingMap = new HashMap<String, ServerMapping>();
ServerMapping mapping1 = new ServerMapping();
mapping1.setVcMobID("vc1");
mapping1.setAsset("asset1");
serverMappingMap.put(mapping1.getVcMobID(), mapping1);
ServerMapping mapping2 = new ServerMapping();
mapping2.setVcMobID("vc2");
mapping2.setAsset("asset2");
serverMappingMap.put(mapping2.getVcMobID(), mapping2);
doReturn(serverMappingMap).when(service).getVaildServerMapping(any());
doReturn(vsphereClient).when(service).connectVsphere(any());
HostSystem host1 = Mockito.mock(HostSystem.class);
HostSystem host2 = Mockito.mock(HostSystem.class);
Collection<HostSystem> hosts = new ArrayList<>();
hosts.add(host1);
hosts.add(host2);
when(vsphereClient.getAllHost()).thenReturn(hosts);
when(vsphereClient.getAllClusterComputeResource()).thenReturn(null);
ManagedObjectReference mor1 = Mockito.mock(ManagedObjectReference.class);
ManagedObjectReference mor2 = Mockito.mock(ManagedObjectReference.class);
when(host1._getRef()).thenReturn(mor1);
when(mor1.getValue()).thenReturn("vc1");
when(host2._getRef()).thenReturn(mor2);
when(mor2.getValue()).thenReturn("vc2");
Asset asset = new Asset();
asset.setId("asset1");
HashMap<String, String> hostJustification = new HashMap<String, String>();
HostInfo hostInfo = new HostInfo();
String vcHostObjStr = mapper.writeValueAsString(hostInfo);
hostJustification.put(FlowgateConstant.HOST_METADATA, vcHostObjStr);
asset.setJustificationfields(hostJustification);
ResponseEntity<Asset> assets = Mockito.mock(ResponseEntity.class);
when(restClient.getAssetByID(anyString())).thenReturn(assets);
when(assets.getBody()).thenReturn(asset);
doReturn(true).when(service).feedHostMetaData(any(), any());
doReturn(true).when(service).feedClusterMetaData(any(), any(), any(), anyString());
service.queryHostMetaData(vc);
}
use of com.vmware.vim.binding.vmodl.ManagedObjectReference in project flowgate by vmware.
the class VCDataServiceTest method testQueryHostMetaData.
@Test
public void testQueryHostMetaData() throws Exception {
SDDCSoftwareConfig vc = Mockito.mock(SDDCSoftwareConfig.class);
HashMap<String, ServerMapping> serverMappingMap = new HashMap<String, ServerMapping>();
ServerMapping mapping1 = new ServerMapping();
mapping1.setVcMobID("vc1");
mapping1.setAsset("asset1");
serverMappingMap.put(mapping1.getVcMobID(), mapping1);
ServerMapping mapping2 = new ServerMapping();
mapping2.setVcMobID("vc2");
mapping2.setAsset("asset2");
serverMappingMap.put(mapping2.getVcMobID(), mapping2);
doReturn(serverMappingMap).when(service).getVaildServerMapping(any());
doReturn(vsphereClient).when(service).connectVsphere(any());
HostSystem host1 = Mockito.mock(HostSystem.class);
HostSystem host2 = Mockito.mock(HostSystem.class);
Collection<HostSystem> hosts = new ArrayList<>();
hosts.add(host1);
hosts.add(host2);
when(vsphereClient.getAllHost()).thenReturn(hosts);
Collection<ClusterComputeResource> clusterComputeResources = new ArrayList<>();
ClusterComputeResource cluster = Mockito.mock(ClusterComputeResource.class);
clusterComputeResources.add(cluster);
clusterComputeResources.add(cluster);
when(vsphereClient.getAllClusterComputeResource()).thenReturn(clusterComputeResources);
ManagedObjectReference mor1 = Mockito.mock(ManagedObjectReference.class);
ManagedObjectReference mor2 = Mockito.mock(ManagedObjectReference.class);
when(host1._getRef()).thenReturn(mor1);
when(mor1.getValue()).thenReturn("vc1");
when(host2._getRef()).thenReturn(mor2);
when(mor2.getValue()).thenReturn("vc2");
Asset asset = new Asset();
asset.setId("asset1");
HashMap<String, String> hostJustification = new HashMap<String, String>();
HostInfo hostInfo = new HostInfo();
String vcHostObjStr = mapper.writeValueAsString(hostInfo);
hostJustification.put(FlowgateConstant.HOST_METADATA, vcHostObjStr);
asset.setJustificationfields(hostJustification);
ResponseEntity<Asset> assets = Mockito.mock(ResponseEntity.class);
when(restClient.getAssetByID(anyString())).thenReturn(assets);
when(assets.getBody()).thenReturn(asset);
doReturn(true).when(service).feedHostMetaData(any(), any());
doReturn(true).when(service).feedClusterMetaData(any(), any(), any(), anyString());
service.queryHostMetaData(vc);
}
use of com.vmware.vim.binding.vmodl.ManagedObjectReference in project flowgate by vmware.
the class AuthVcUser method authenticateUser.
public void authenticateUser(String name, String password) throws URISyntaxException, InvalidLogin, InvalidLocale, SSLException {
Client vmomiClient = null;
SessionManager sessionManager = null;
try {
URI uri = new URI(serviceUrl);
HttpConfiguration httpConfig = new HttpConfigurationImpl();
httpConfig.setThumbprintVerifier(getThumbprintVerifier());
HttpClientConfiguration clientConfig = HttpClientConfiguration.Factory.newInstance();
// set customized SSL protocols
// TlsClientConfiguration tlsClientConfiguration = new TlsClientConfiguration();
httpConfig.setEnabledProtocols(new String[] { "TLSv1.1", "TLSv1.2" });
clientConfig.setHttpConfiguration(httpConfig);
vmomiClient = Client.Factory.createClient(uri, version, clientConfig);
ManagedObjectReference svcRef = new ManagedObjectReference();
svcRef.setType("ServiceInstance");
svcRef.setValue("ServiceInstance");
if (name == null || name.isEmpty()) {
// VC session token auth
vmomiClient.getBinding().setSession(vmomiClient.getBinding().createSession(password));
}
ServiceInstance instance = vmomiClient.createStub(ServiceInstance.class, svcRef);
ServiceInstanceContent instanceContent = instance.retrieveContent();
sessionManager = vmomiClient.createStub(SessionManager.class, instanceContent.getSessionManager());
if (name != null && !name.isEmpty()) {
// username/passowrd auth
sessionManager.login(name, password, sessionManager.getDefaultLocale());
sessionManager.logout();
} else {
// VC session token auth
UserSession session = sessionManager.getCurrentSession();
if (session == null) {
throw new WormholeException("invalid vc session.");
} else {
logger.info(session.getUserName() + " is authenticated");
}
}
} finally {
if (vmomiClient != null) {
vmomiClient.shutdown();
}
}
}
use of com.vmware.vim.binding.vmodl.ManagedObjectReference in project flowgate by vmware.
the class VCDataService method feedClusterMetaData.
public boolean feedClusterMetaData(Map<String, ClusterComputeResource> clusterMap, HostSystem host, HostInfo hostInfo, String vcInstanceUUID) {
ManagedObjectReference hostParent = host.getParent();
if (hostParent == null || !hostParent.getType().equals(VCConstants.CLUSTERCOMPUTERESOURCE)) {
return false;
}
String clusterMobId = hostParent.getValue();
ClusterComputeResource cluster = clusterMap.get(clusterMobId);
boolean needUpdate = false;
EsxiMetadata oldEsxiMetadata = hostInfo.getEsxiMetadata();
if (oldEsxiMetadata == null) {
needUpdate = true;
oldEsxiMetadata = new EsxiMetadata();
}
String hostMobId = host._getRef().getValue();
ConfigInfoEx configInfoExtension = (ConfigInfoEx) cluster.getConfigurationEx();
String oldEsxiMetadataClusterName = oldEsxiMetadata.getClusterName();
String esxiMetadataClusterName = cluster.getName();
if (!StringUtils.equals(oldEsxiMetadataClusterName, esxiMetadataClusterName)) {
needUpdate = true;
oldEsxiMetadata.setClusterName(esxiMetadataClusterName);
}
if (!configInfoExtension.getDpmConfigInfo().getEnabled().equals(oldEsxiMetadata.isClusterDPMenabled())) {
needUpdate = true;
oldEsxiMetadata.setClusterDPMenabled(configInfoExtension.getDpmConfigInfo().getEnabled());
}
String oldEsxiMetadataClusterDRSBehavior = oldEsxiMetadata.getClusterDRSBehavior();
String esxiMetadataClusterDRSBehavior = configInfoExtension.getDrsConfig().getDefaultVmBehavior().toString();
if (!StringUtils.equals(oldEsxiMetadataClusterDRSBehavior, esxiMetadataClusterDRSBehavior)) {
needUpdate = true;
oldEsxiMetadata.setClusterDRSBehavior(esxiMetadataClusterDRSBehavior);
}
if (cluster.getSummary().getNumEffectiveHosts() != oldEsxiMetadata.getClusterEffectiveHostsNum()) {
needUpdate = true;
oldEsxiMetadata.setClusterEffectiveHostsNum(cluster.getSummary().getNumEffectiveHosts());
}
if (cluster.getSummary().getNumHosts() != oldEsxiMetadata.getClusterHostsNum()) {
needUpdate = true;
oldEsxiMetadata.setClusterHostsNum(cluster.getSummary().getNumHosts());
}
if (cluster.getSummary().getTotalCpu() != oldEsxiMetadata.getClusterTotalCpu()) {
needUpdate = true;
oldEsxiMetadata.setClusterTotalCpu(cluster.getSummary().getTotalCpu());
}
if (cluster.getSummary().getNumCpuCores() != oldEsxiMetadata.getClusterTotalCpuCores()) {
needUpdate = true;
oldEsxiMetadata.setClusterTotalCpuCores(cluster.getSummary().getNumCpuCores());
}
if (cluster.getSummary().getNumCpuThreads() != oldEsxiMetadata.getClusterTotalCpuThreads()) {
needUpdate = true;
oldEsxiMetadata.setClusterTotalCpuThreads(cluster.getSummary().getNumCpuThreads());
}
if (cluster.getSummary().getTotalMemory() != oldEsxiMetadata.getClusterTotalMemory()) {
needUpdate = true;
oldEsxiMetadata.setClusterTotalMemory(cluster.getSummary().getTotalMemory());
}
if (!configInfoExtension.getVsanConfigInfo().getEnabled().equals(oldEsxiMetadata.isHostVSANenabled())) {
needUpdate = true;
oldEsxiMetadata.setClusterVSANenabled(configInfoExtension.getVsanConfigInfo().getEnabled());
}
DpmHostConfigInfo[] dpmHostConfigInfos = configInfoExtension.getDpmHostConfig();
if (dpmHostConfigInfos != null && dpmHostConfigInfos.length > 0) {
for (DpmHostConfigInfo dpmHostConfigInfo : dpmHostConfigInfos) {
if (hostMobId.equals(dpmHostConfigInfo.getKey().getValue())) {
if (!dpmHostConfigInfo.getEnabled().equals(oldEsxiMetadata.isHostDPMenabled())) {
needUpdate = true;
oldEsxiMetadata.setHostDPMenabled(dpmHostConfigInfo.getEnabled());
}
}
}
}
String oldEsxiMetadataClusterMobid = oldEsxiMetadata.getClusterMobid();
String esxiMetadataClusterMobid = cluster._getRef().getValue();
if (!StringUtils.equals(oldEsxiMetadataClusterMobid, esxiMetadataClusterMobid)) {
needUpdate = true;
oldEsxiMetadata.setClusterMobid(esxiMetadataClusterMobid);
}
String oldEsxiMetadataInstanceId = oldEsxiMetadata.getInstanceId();
if (!StringUtils.equals(oldEsxiMetadataInstanceId, vcInstanceUUID)) {
needUpdate = true;
oldEsxiMetadata.setInstanceId(vcInstanceUUID);
}
String oldEsxiMetadataHostName = oldEsxiMetadata.getHostName();
String esxiMetadataHostName = host.getName();
if (!StringUtils.equals(oldEsxiMetadataHostName, esxiMetadataHostName)) {
needUpdate = true;
oldEsxiMetadata.setHostName(host.getName());
}
if (!host.getConfig().getVsanHostConfig().getEnabled().equals(oldEsxiMetadata.isHostVSANenabled())) {
needUpdate = true;
oldEsxiMetadata.setHostVSANenabled(host.getConfig().getVsanHostConfig().getEnabled());
}
if (!host.getCapability().getVsanSupported().equals(oldEsxiMetadata.isHostVsanSupported())) {
needUpdate = true;
oldEsxiMetadata.setHostVsanSupported(host.getCapability().getVsanSupported());
}
oldEsxiMetadata.setHostMobid(hostMobId);
hostInfo.setEsxiMetadata(oldEsxiMetadata);
return needUpdate;
}
Aggregations