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);
}
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);
}
}
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);
}
Aggregations