use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureCostStatsService method setCustomProperty.
private void setCustomProperty(Context context, String key, String value, Stages next) {
context.computeHostDesc.customProperties.put(key, value);
ComputeState accountState = new ComputeState();
accountState.customProperties = new HashMap<>();
accountState.customProperties.put(key, value);
sendRequest(Operation.createPatch(UriUtils.extendUri(getInventoryServiceUri(), context.computeHostDesc.documentSelfLink)).setBody(accountState).setCompletion((operation, exception) -> {
if (exception != null) {
handleError(context, null, exception, false);
return;
}
context.stage = next;
handleRequest(context);
}));
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureDiskEnumerationAdapterService method getManagedDisks.
private void getManagedDisks(DiskEnumContext ctx, DiskEnumStages nextStage) {
logInfo(() -> "Enumerating Azure Managed disks.");
URI uri;
if (ctx.enumNextPageLink == null) {
// TODO- change to use sdk to fetch disks
String uriStr = AdapterUriUtil.expandUriPathTemplate(LIST_DISKS_URI, ctx.endpointAuth.userLink);
uri = UriUtils.extendUriWithQuery(UriUtils.buildUri(uriStr), QUERY_PARAM_API_VERSION, DISK_REST_API_VERSION);
} else {
uri = UriUtils.buildUri(ctx.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 + ctx.credentials.getToken(AzureUtils.getAzureBaseUri()));
} catch (Exception ex) {
handleError(ctx, ex);
return;
}
operation.setCompletion((op, th) -> {
if (th != null) {
handleError(ctx, th);
return;
}
ManagedDiskList results = op.getBody(ManagedDiskList.class);
// Store next page link
ctx.enumNextPageLink = results.nextLink;
logInfo(() -> String.format("Next page link %s", ctx.enumNextPageLink));
List<Disk> diskList = results.value;
if (diskList == null || diskList.size() == 0) {
ctx.subStage = DiskEnumStages.DISASSOCIATE_ENDPOINT_LINKS;
handleSubStage(ctx);
return;
}
logInfo(() -> String.format("Retrieved %d managed disks from Azure", diskList.size()));
// save all disks from Azure to process further
diskList.forEach(disk -> ctx.managedDisks.put(disk.id, disk));
// filter all un-attached disks from diskList
List<Disk> unattachedDisks = diskList.stream().filter(dk -> dk.properties.diskState.equals(DISK_STATUS_UNATTACHED)).collect(Collectors.toList());
// TODO - Remove toLowerCase() after https://github.com/Azure/azure-sdk-for-java/issues/2014 is fixed.
// save all unattached disks in managedDisks map for further processing
unattachedDisks.forEach(disk -> ctx.unattachedDisks.put(disk.id.toLowerCase(), disk));
logInfo(() -> String.format("Processing %d independent disks", ctx.managedDisks.size()));
if (ctx.enumNextPageLink != null) {
ctx.subStage = DiskEnumStages.GET_DISKS;
logFine(() -> String.format("Transition to same stage" + ctx.subStage));
} else {
ctx.subStage = nextStage;
logFine(() -> String.format("Transition to " + nextStage));
}
handleSubStage(ctx);
});
sendRequest(operation);
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureDiskEnumerationAdapterService method disassociateEndpointLinksFromDiskStates.
/**
* Disassociate EndpointLinks from Disk States (of local store) that are no longer existing in Azure.
*/
private void disassociateEndpointLinksFromDiskStates(DiskEnumContext ctx, DiskEnumStages nextStage) {
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(DiskState.class).addFieldClause(DiskState.FIELD_NAME_STATUS, DiskStatus.AVAILABLE).addRangeClause(DiskState.FIELD_NAME_UPDATE_TIME_MICROS, QueryTask.NumericRange.createLessThanRange(ctx.enumerationStartTimeInMicros));
QueryTop<DiskState> queryLocalDiskStates = new QueryTop<>(getHost(), qBuilder.build(), DiskState.class, ctx.parentCompute.tenantLinks, null, ctx.parentCompute.documentSelfLink).setClusterType(ClusterUtil.ServiceTypeCluster.INVENTORY_SERVICE);
final String msg = queryLocalDiskStates.documentClass.getSimpleName() + " - Disassociation of endpoint links from disk states ";
logInfo(() -> msg + " STARTED");
queryLocalDiskStates.queryDocuments(ds -> {
// check for diskState which is managed disk type
if (ds.customProperties != null && ds.customProperties.containsKey(AZURE_MANAGED_DISK_TYPE)) {
if (!ctx.unattachedDisks.containsKey(ds.id.toLowerCase())) {
Operation disassociateOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, ds);
if (disassociateOp != null) {
sendRequest(disassociateOp);
}
}
}
}).thenRun(() -> logInfo(() -> "Disassociation of endpoint link from disk states which " + "are not present in Azure.")).whenComplete((aVoid, th) -> {
if (th != null) {
handleError(ctx, th);
}
logFine(() -> msg + ": SUCCESS");
logFine(() -> String.format("Transition to " + nextStage));
ctx.subStage = nextStage;
handleSubStage(ctx);
});
}
use of com.vmware.xenon.common.Operation 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.xenon.common.Operation in project photon-model by vmware.
the class AzureNetworkEnumerationAdapterService method createUpdateSubnetStates.
/**
* Create new subnet states or updates matching subnet states with the actual state in Azure.
*/
private void createUpdateSubnetStates(NetworkEnumContext context, NetworkEnumStages next) {
if (context.subnets.size() == 0) {
logFine(() -> "No network states available for update.");
handleSubStage(context, next);
return;
}
Stream<Operation> operations = context.subnets.keySet().stream().map(subnetId -> {
SubnetStateWithParentVNetId subnetStateWithParentVNetId = context.subnets.get(subnetId);
SubnetState subnetState = subnetStateWithParentVNetId.subnetState;
if (!context.subnetStates.containsKey(subnetId)) {
// set internal tags as tagLinks for subnets to be newly created.
setTagLinksToResourceState(subnetState, context.subnetInternalTagsMap, false);
} else {
// for already existing subnets, add internal tags only if missing
if (subnetState.tagLinks == null || subnetState.tagLinks.isEmpty()) {
setTagLinksToResourceState(subnetState, context.subnetInternalTagsMap, false);
} else {
context.subnetInternalTagLinksSet.stream().filter(tagLink -> !subnetState.tagLinks.contains(tagLink)).map(tagLink -> subnetState.tagLinks.add(tagLink)).collect(Collectors.toSet());
}
}
// Update networkLink with "latest" (either created or updated)
// NetworkState.documentSelfLink
NetworkState networkState = context.networkStates.get(subnetStateWithParentVNetId.parentVNetId);
if (networkState != null) {
subnetState.networkLink = networkState.documentSelfLink;
} else {
logWarning(() -> String.format("Network state corresponding to subnet with" + " name [%s] was not found. Network Link is left empty.", subnetState.name));
}
if (StringUtils.isEmpty(subnetState.endpointLink)) {
subnetState.endpointLink = context.request.endpointLink;
}
subnetState.computeHostLink = context.parentCompute.documentSelfLink;
AdapterUtils.addToEndpointLinks(subnetState, context.request.endpointLink);
return context.subnetStates.containsKey(subnetId) ? // Update case
Operation.createPatch(this, context.subnetStates.get(subnetId)).setBody(subnetState) : // Create case.
Operation.createPost(getHost(), SubnetService.FACTORY_LINK).setBody(subnetState);
});
OperationJoin.create(operations).setCompletion((ops, failures) -> {
if (failures != null) {
// We don't want to fail the whole data collection if some of the
// operation fails.
failures.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
}
// Process successful operations.
ops.values().stream().filter(operation -> failures != null && !failures.containsKey(operation.getId())).filter(operation -> operation.getStatusCode() != Operation.STATUS_CODE_NOT_MODIFIED).forEach(operation -> {
SubnetState subnetState = operation.getBody(SubnetState.class);
context.subnets.get(subnetState.id).subnetState = subnetState;
});
if (context.enumNextPageLink != null) {
logFine(() -> "Fetch next page of Virtual Networks from Azure.");
handleSubStage(context, NetworkEnumStages.GET_VNETS);
return;
}
logFine(() -> "Finished updating network states");
handleSubStage(context, next);
}).sendWith(this);
}
Aggregations