Search in sources :

Example 1 with ManagedObjectReference

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);
}
Also used : SDDCSoftwareConfig(com.vmware.flowgate.common.model.SDDCSoftwareConfig) HashMap(java.util.HashMap) ServerMapping(com.vmware.flowgate.common.model.ServerMapping) HostSystem(com.vmware.vim.binding.vim.HostSystem) ArrayList(java.util.ArrayList) Asset(com.vmware.flowgate.common.model.Asset) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ManagedObjectReference(com.vmware.vim.binding.vmodl.ManagedObjectReference) Test(org.junit.Test)

Example 2 with ManagedObjectReference

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);
}
Also used : HashMap(java.util.HashMap) ServerMapping(com.vmware.flowgate.common.model.ServerMapping) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SDDCSoftwareConfig(com.vmware.flowgate.common.model.SDDCSoftwareConfig) HostSystem(com.vmware.vim.binding.vim.HostSystem) Asset(com.vmware.flowgate.common.model.Asset) HostInfo(com.vmware.flowgate.vcworker.model.HostInfo) ManagedObjectReference(com.vmware.vim.binding.vmodl.ManagedObjectReference) Test(org.junit.Test)

Example 3 with ManagedObjectReference

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);
}
Also used : ClusterComputeResource(com.vmware.vim.binding.vim.ClusterComputeResource) HashMap(java.util.HashMap) ServerMapping(com.vmware.flowgate.common.model.ServerMapping) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SDDCSoftwareConfig(com.vmware.flowgate.common.model.SDDCSoftwareConfig) HostSystem(com.vmware.vim.binding.vim.HostSystem) Asset(com.vmware.flowgate.common.model.Asset) HostInfo(com.vmware.flowgate.vcworker.model.HostInfo) ManagedObjectReference(com.vmware.vim.binding.vmodl.ManagedObjectReference) Test(org.junit.Test)

Example 4 with ManagedObjectReference

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();
        }
    }
}
Also used : ServiceInstanceContent(com.vmware.vim.binding.vim.ServiceInstanceContent) HttpConfigurationImpl(com.vmware.vim.vmomi.client.http.impl.HttpConfigurationImpl) SessionManager(com.vmware.vim.binding.vim.SessionManager) UserSession(com.vmware.vim.binding.vim.UserSession) ServiceInstance(com.vmware.vim.binding.vim.ServiceInstance) WormholeException(com.vmware.flowgate.common.exception.WormholeException) HttpConfiguration(com.vmware.vim.vmomi.client.http.HttpConfiguration) Client(com.vmware.vim.vmomi.client.Client) URI(java.net.URI) HttpClientConfiguration(com.vmware.vim.vmomi.client.http.HttpClientConfiguration) ManagedObjectReference(com.vmware.vim.binding.vmodl.ManagedObjectReference)

Example 5 with ManagedObjectReference

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;
}
Also used : ClusterComputeResource(com.vmware.vim.binding.vim.ClusterComputeResource) ConfigInfoEx(com.vmware.vim.binding.vim.cluster.ConfigInfoEx) EsxiMetadata(com.vmware.flowgate.vcworker.model.EsxiMetadata) DpmHostConfigInfo(com.vmware.vim.binding.vim.cluster.DpmHostConfigInfo) ManagedObjectReference(com.vmware.vim.binding.vmodl.ManagedObjectReference)

Aggregations

ManagedObjectReference (com.vmware.vim.binding.vmodl.ManagedObjectReference)12 Test (org.junit.Test)8 HostSystem (com.vmware.vim.binding.vim.HostSystem)7 ArrayList (java.util.ArrayList)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 Asset (com.vmware.flowgate.common.model.Asset)4 SDDCSoftwareConfig (com.vmware.flowgate.common.model.SDDCSoftwareConfig)4 ServerMapping (com.vmware.flowgate.common.model.ServerMapping)4 HostInfo (com.vmware.flowgate.vcworker.model.HostInfo)4 ClusterComputeResource (com.vmware.vim.binding.vim.ClusterComputeResource)4 ConfigInfoEx (com.vmware.vim.binding.vim.cluster.ConfigInfoEx)3 DpmHostConfigInfo (com.vmware.vim.binding.vim.cluster.DpmHostConfigInfo)3 HashMap (java.util.HashMap)3 EsxiMetadata (com.vmware.flowgate.vcworker.model.EsxiMetadata)2 DpmConfigInfo (com.vmware.vim.binding.vim.cluster.DpmConfigInfo)2 DrsConfigInfo (com.vmware.vim.binding.vim.cluster.DrsConfigInfo)2 Capability (com.vmware.vim.binding.vim.host.Capability)2 ConfigInfo (com.vmware.vim.binding.vim.host.ConfigInfo)2 WormholeException (com.vmware.flowgate.common.exception.WormholeException)1 EventMessage (com.vmware.flowgate.common.model.redis.message.EventMessage)1