Search in sources :

Example 1 with HostTagClient

use of com.vmware.flowgate.vcworker.client.HostTagClient in project flowgate by vmware.

the class VCDataService method syncCustomAttributes.

private void syncCustomAttributes(SDDCSoftwareConfig vc) {
    try (VsphereClient vsphereClient = connectVsphere(vc)) {
        for (String key : VCConstants.hostCustomAttrMapping.values()) {
            vsphereClient.createCustomAttribute(key, VCConstants.HOSTSYSTEM);
        }
        // Add the PDU information;
        vsphereClient.createCustomAttribute(VCConstants.ASSET_PDUs, VCConstants.HOSTSYSTEM);
        // Add host switch information;
        vsphereClient.createCustomAttribute(VCConstants.ASSET_SWITCHs, VCConstants.HOSTSYSTEM);
    } catch (ConnectionException connectionException) {
        checkAndUpdateIntegrationStatus(vc, connectionException.getMessage());
        return;
    } catch (ExecutionException executionException) {
        if (executionException.getCause() instanceof InvalidLogin) {
            logger.error("Failed to push data to " + vc.getServerURL(), executionException);
            IntegrationStatus integrationStatus = vc.getIntegrationStatus();
            if (integrationStatus == null) {
                integrationStatus = new IntegrationStatus();
            }
            integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
            integrationStatus.setDetail("Invalid username or password.");
            integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
            updateIntegrationStatus(vc);
            return;
        }
    } catch (Exception exception) {
        logger.error("Failed to sync the host metadata to VC ", exception);
        return;
    }
    try (HostTagClient client = new HostTagClient(vc.getServerURL(), vc.getUserName(), vc.getPassword(), !vc.isVerifyCert())) {
        client.initConnection();
        TagModel tag = client.getTagByName(VCConstants.locationAntiAffinityTagName);
        String categoryID;
        if (tag == null) {
            CategoryModel category = client.getTagCategoryByName(VCConstants.categoryName);
            if (category == null) {
                categoryID = client.createTagCategory(VCConstants.categoryName, VCConstants.categoryDescription, Cardinality.MULTIPLE);
            } else {
                categoryID = category.getId();
            }
            client.createTag(VCConstants.locationAntiAffinityTagName, VCConstants.locationAntiAffinityTagDescription, categoryID);
        }
    } catch (Exception exception) {
        logger.error("Faild to check the predefined tag information", exception);
    }
}
Also used : VsphereClient(com.vmware.flowgate.vcworker.client.VsphereClient) IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) InvalidLogin(com.vmware.vim.binding.vim.fault.InvalidLogin) HostTagClient(com.vmware.flowgate.vcworker.client.HostTagClient) CategoryModel(com.vmware.cis.tagging.CategoryModel) ExecutionException(java.util.concurrent.ExecutionException) TagModel(com.vmware.cis.tagging.TagModel) ConnectionException(com.vmware.vim.vmomi.client.exception.ConnectionException) ConnectionException(com.vmware.vim.vmomi.client.exception.ConnectionException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException)

Example 2 with HostTagClient

use of com.vmware.flowgate.vcworker.client.HostTagClient in project flowgate by vmware.

the class VCDataService method validClusterHostsLocationAntiaffinity.

private void validClusterHostsLocationAntiaffinity(SDDCSoftwareConfig vcInfo, Map<String, Asset> assetDictionary, List<ServerMapping> validMapping) {
    Map<String, Set<Asset>> assetsByCluster = new HashMap<String, Set<Asset>>();
    for (ServerMapping validServer : validMapping) {
        Asset asset = assetDictionary.get(validServer.getAsset());
        if (null != validServer.getVcClusterMobID()) {
            if (!assetsByCluster.containsKey(validServer.getVcClusterMobID())) {
                assetsByCluster.put(validServer.getVcClusterMobID(), new HashSet<Asset>());
            }
            assetsByCluster.get(validServer.getVcClusterMobID()).add(asset);
        }
    }
    Set<Asset> needTagHost = new HashSet<>();
    for (String clusterMob : assetsByCluster.keySet()) {
        if (assetsByCluster.get(clusterMob).size() > 1) {
            String location = "%s-%s-%s-%s-%s-%s-%d";
            Map<String, Set<Asset>> assetsByLocation = new HashMap<String, Set<Asset>>();
            for (Asset asset : assetsByCluster.get(clusterMob)) {
                String assetLocation = String.format(location, asset.getRegion(), asset.getCountry(), asset.getCity(), asset.getBuilding(), asset.getFloor(), asset.getCabinetAssetNumber(), asset.getCabinetUnitPosition());
                if (!assetsByLocation.containsKey(assetLocation)) {
                    assetsByLocation.put(assetLocation, new HashSet<Asset>());
                }
                assetsByLocation.get(assetLocation).add(asset);
            }
            for (String local : assetsByLocation.keySet()) {
                if (assetsByLocation.get(local).size() > 1) {
                    // now we need to tag the hosts
                    needTagHost.addAll(assetsByLocation.get(local));
                }
            }
        }
    }
    if (!needTagHost.isEmpty()) {
        Map<String, ServerMapping> assetIDMapping = new HashMap<String, ServerMapping>();
        for (ServerMapping mapping : validMapping) {
            assetIDMapping.put(mapping.getAsset(), mapping);
        }
        try (HostTagClient client = new HostTagClient(vcInfo.getServerURL(), vcInfo.getUserName(), vcInfo.getPassword(), !vcInfo.isVerifyCert())) {
            client.initConnection();
            TagModel locationTag = client.getTagByName(VCConstants.locationAntiAffinityTagName);
            for (Asset a : needTagHost) {
                client.attachTagToHost(locationTag.getId(), assetIDMapping.get(a.getId()).getVcMobID());
            }
        } catch (Exception exception) {
            logger.warn("Failed to tag the host, will try to tag it in next run.", exception);
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ServerMapping(com.vmware.flowgate.common.model.ServerMapping) ConnectionException(com.vmware.vim.vmomi.client.exception.ConnectionException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Asset(com.vmware.flowgate.common.model.Asset) HostTagClient(com.vmware.flowgate.vcworker.client.HostTagClient) TagModel(com.vmware.cis.tagging.TagModel) HashSet(java.util.HashSet)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 TagModel (com.vmware.cis.tagging.TagModel)2 HostTagClient (com.vmware.flowgate.vcworker.client.HostTagClient)2 ConnectionException (com.vmware.vim.vmomi.client.exception.ConnectionException)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)2 CategoryModel (com.vmware.cis.tagging.CategoryModel)1 Asset (com.vmware.flowgate.common.model.Asset)1 IntegrationStatus (com.vmware.flowgate.common.model.IntegrationStatus)1 ServerMapping (com.vmware.flowgate.common.model.ServerMapping)1 VsphereClient (com.vmware.flowgate.vcworker.client.VsphereClient)1 InvalidLogin (com.vmware.vim.binding.vim.fault.InvalidLogin)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1