Search in sources :

Example 1 with VirtualNetwork

use of com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetwork in project photon-model by vmware.

the class AzureNetworkEnumerationAdapterService method createUpdateNetworkStates.

/**
 * Create new network states or update matching network states with the actual state in Azure.
 */
private void createUpdateNetworkStates(NetworkEnumContext context, NetworkEnumStages next) {
    logFine(() -> "Create or update Network States with the actual state in Azure.");
    if (context.virtualNetworks.size() == 0) {
        logFine(() -> "No virtual networks found to create/update.");
        handleSubStage(context, next);
        return;
    }
    Stream<Operation> operations = context.virtualNetworks.values().stream().map(virtualNetwork -> {
        NetworkState existingNetworkState = context.networkStates.get(virtualNetwork.id);
        NetworkState networkState = buildNetworkState(context, virtualNetwork, existingNetworkState);
        setTagLinksToResourceState(networkState, virtualNetwork.tags, true);
        if (existingNetworkState == null) {
            // set internal tags as tagLinks for networks to be newly created.
            setTagLinksToResourceState(networkState, context.networkInternalTagsMap, false);
        } else {
            // for already existing networks, add internal tags only if missing
            if (networkState.tagLinks == null || networkState.tagLinks.isEmpty()) {
                setTagLinksToResourceState(networkState, context.networkInternalTagsMap, false);
            } else {
                context.networkInternalTagLinksSet.stream().filter(tagLink -> !networkState.tagLinks.contains(tagLink)).map(tagLink -> networkState.tagLinks.add(tagLink)).collect(Collectors.toSet());
            }
        }
        CompletionHandler handler = (completedOp, failure) -> {
            if (failure != null) {
                // Process successful operations only.
                logWarning(() -> String.format("Error: %s", failure.getMessage()));
                return;
            }
            NetworkState result;
            if (completedOp.getStatusCode() == Operation.STATUS_CODE_NOT_MODIFIED) {
                // Use the original networkState as result
                result = networkState;
            } else {
                result = completedOp.getBody(NetworkState.class);
            }
            context.networkStates.put(result.id, result);
        };
        return existingNetworkState != null ? // Update case.
        Operation.createPatch(this, networkState.documentSelfLink).setBody(networkState).setCompletion(handler) : // Create case.
        Operation.createPost(getHost(), NetworkService.FACTORY_LINK).setBody(networkState).setCompletion(handler);
    });
    OperationJoin.create(operations).setCompletion((ops, failures) -> {
        // We don't want to fail the whole data collection if some of the so we don't care of
        // any potential operation failures. They are already logged at individual operation
        // level.
        logFine(() -> "Finished updating network states.");
        handleSubStage(context, next);
    }).sendWith(this);
}
Also used : QUERY_PARAM_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.QUERY_PARAM_API_VERSION) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) SubnetStateWithParentVNetId(com.vmware.photon.controller.model.adapters.azure.enumeration.AzureNetworkEnumerationAdapterService.NetworkEnumContext.SubnetStateWithParentVNetId) LifecycleState(com.vmware.photon.controller.model.support.LifecycleState) StringUtils(org.apache.commons.lang3.StringUtils) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) Utils(com.vmware.xenon.common.Utils) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) VirtualNetworkListResult(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetworkListResult) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) StatelessService(com.vmware.xenon.common.StatelessService) DEFAULT_INSTANCE_ADAPTER_REFERENCE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DEFAULT_INSTANCE_ADAPTER_REFERENCE) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) Collectors(java.util.stream.Collectors) NETWORK_REST_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.NETWORK_REST_API_VERSION) List(java.util.List) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) Stream(java.util.stream.Stream) TagService(com.vmware.photon.controller.model.resources.TagService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) AddressSpace(com.vmware.photon.controller.model.adapters.azure.model.network.AddressSpace) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) Entry(java.util.Map.Entry) TagsUtil.setTagLinksToResourceState(com.vmware.photon.controller.model.adapters.util.TagsUtil.setTagLinksToResourceState) AzureUtils.getAzureConfig(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getAzureConfig) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) LIST_VIRTUAL_NETWORKS_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.LIST_VIRTUAL_NETWORKS_URI) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceGroupStateType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.ResourceGroupStateType) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) AUTH_HEADER_BEARER_PREFIX(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTH_HEADER_BEARER_PREFIX) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) TagsUtil.updateLocalTagStates(com.vmware.photon.controller.model.adapters.util.TagsUtil.updateLocalTagStates) VirtualNetwork(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetwork) BiConsumer(java.util.function.BiConsumer) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AzureResourceType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AzureResourceType) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Subnet(com.vmware.photon.controller.model.adapters.azure.model.network.Subnet) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) Operation(com.vmware.xenon.common.Operation) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState)

Example 2 with VirtualNetwork

use of com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetwork in project photon-model by vmware.

the class AzureNetworkEnumerationAdapterService method updateNetworkTagLinks.

private DeferredResult<NetworkEnumContext> updateNetworkTagLinks(NetworkEnumContext context) {
    logFine(() -> "Create or update Network States' tags with the actual tags in Azure.");
    if (context.virtualNetworks.size() == 0) {
        logFine(() -> "No local networks to be updated so there are no tags to update.");
        return DeferredResult.completed(context);
    } else {
        List<DeferredResult<Set<String>>> updateNetwLinksOps = new ArrayList<>();
        // update tag links for the existing NetworkStates
        for (String vnetId : context.networkStates.keySet()) {
            if (!context.virtualNetworks.containsKey(vnetId)) {
                // this is not a network to update
                continue;
            }
            VirtualNetwork vNet = context.virtualNetworks.get(vnetId);
            NetworkState existingNetworkState = context.networkStates.get(vnetId);
            Map<String, String> remoteTags = new HashMap<>();
            if (vNet.tags != null && !vNet.tags.isEmpty()) {
                for (Entry<String, String> vNetTagEntry : vNet.tags.entrySet()) {
                    remoteTags.put(vNetTagEntry.getKey(), vNetTagEntry.getValue());
                }
            }
            updateNetwLinksOps.add(updateLocalTagStates(this, existingNetworkState, remoteTags, null));
        }
        return DeferredResult.allOf(updateNetwLinksOps).thenApply(gnore -> context);
    }
}
Also used : VirtualNetwork(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetwork) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 3 with VirtualNetwork

use of com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetwork in project photon-model by vmware.

the class AzureNetworkEnumerationAdapterService method getVirtualNetworks.

/**
 * Retrieve a page of Virtual Network resources from azure, based on the provided compute host
 * description.
 */
private void getVirtualNetworks(NetworkEnumContext context, NetworkEnumStages next) {
    logFine(() -> "Enumerating Virtual Networks from Azure.");
    URI uri;
    if (context.enumNextPageLink == null) {
        // First request to fetch Virtual Networks from Azure.
        String uriStr = AdapterUriUtil.expandUriPathTemplate(LIST_VIRTUAL_NETWORKS_URI, context.endpointAuth.userLink);
        uri = UriUtils.extendUriWithQuery(UriUtils.buildUri(uriStr), QUERY_PARAM_API_VERSION, NETWORK_REST_API_VERSION);
    } else {
        // Request to fetch next page of Virtual Networks from Azure.
        uri = UriUtils.buildUri(context.enumNextPageLink);
    }
    final Operation operation = Operation.createGet(uri);
    operation.addRequestHeader(Operation.ACCEPT_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
    operation.addRequestHeader(Operation.CONTENT_TYPE_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
    try {
        operation.addRequestHeader(Operation.AUTHORIZATION_HEADER, AUTH_HEADER_BEARER_PREFIX + context.credentials.getToken(AzureUtils.getAzureBaseUri()));
    } catch (Exception ex) {
        handleError(context, ex);
        return;
    }
    operation.setCompletion((op, ex) -> {
        op.complete();
        if (ex != null) {
            handleError(context, ex);
            return;
        }
        VirtualNetworkListResult results = op.getBody(VirtualNetworkListResult.class);
        List<VirtualNetwork> virtualNetworks = results.value;
        // If there are no Virtual Networks in Azure we directly skip over to deletion phase.
        if (virtualNetworks == null || virtualNetworks.size() == 0) {
            handleSubStage(context, NetworkEnumStages.DELETE_NETWORK_STATES);
            return;
        }
        // Store next page link.
        context.enumNextPageLink = results.nextLink;
        logFine(() -> String.format("Retrieved %d Virtual Networks from Azure", virtualNetworks.size()));
        logFine(() -> String.format("Next page link %s", context.enumNextPageLink));
        // Store virtual networks for further processing during the next stages.
        virtualNetworks.forEach(virtualNetwork -> {
            context.addVirtualNetwork(virtualNetwork);
        });
        logFine(() -> String.format("Processing %d virtual networks", context.virtualNetworks.size()));
        handleSubStage(context, next);
    });
    sendRequest(operation);
}
Also used : VirtualNetwork(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetwork) VirtualNetworkListResult(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetworkListResult) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) LIST_VIRTUAL_NETWORKS_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.LIST_VIRTUAL_NETWORKS_URI)

Aggregations

VirtualNetwork (com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetwork)3 LIST_VIRTUAL_NETWORKS_URI (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.LIST_VIRTUAL_NETWORKS_URI)2 VirtualNetworkListResult (com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetworkListResult)2 NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)2 DeferredResult (com.vmware.xenon.common.DeferredResult)2 Operation (com.vmware.xenon.common.Operation)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ApplicationTokenCredentials (com.microsoft.azure.credentials.ApplicationTokenCredentials)1 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)1 ComputeEnumerateResourceRequest (com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)1 EnumerationAction (com.vmware.photon.controller.model.adapterapi.EnumerationAction)1 AzureUriPaths (com.vmware.photon.controller.model.adapters.azure.AzureUriPaths)1 AzureConstants (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants)1 AUTH_HEADER_BEARER_PREFIX (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTH_HEADER_BEARER_PREFIX)1 AzureResourceType (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AzureResourceType)1 DEFAULT_INSTANCE_ADAPTER_REFERENCE (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DEFAULT_INSTANCE_ADAPTER_REFERENCE)1 NETWORK_REST_API_VERSION (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.NETWORK_REST_API_VERSION)1