use of com.vmware.photon.controller.model.adapters.azure.model.storage.Disk 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.photon.controller.model.adapters.azure.model.storage.Disk in project photon-model by vmware.
the class AzureDiskEnumerationAdapterService method createUpdateDiskStates.
private void createUpdateDiskStates(DiskEnumContext ctx, DiskEnumStages nextStage) {
if (ctx.unattachedDisks.isEmpty()) {
ctx.subStage = nextStage;
handleSubStage(ctx);
return;
}
Collection<Operation> opCollection = new ArrayList<>();
ctx.unattachedDisks.entrySet().stream().forEach(entry -> {
DiskState diskState = ctx.localDiskStates.get(entry.getKey());
Operation diskOp = null;
if (diskState != null) {
diskState.status = DiskStatus.AVAILABLE;
if (diskState.endpointLinks != null && !diskState.endpointLinks.contains(ctx.request.endpointLink)) {
AdapterUtils.addToEndpointLinks(diskState, ctx.request.endpointLink);
}
if (diskState.endpointLink == null || diskState.endpointLink.equals("")) {
diskState.endpointLink = ctx.request.endpointLink;
}
if (diskState.customProperties != null && diskState.customProperties.containsKey(DISK_CONTROLLER_NUMBER)) {
diskState.customProperties.remove(DISK_CONTROLLER_NUMBER);
}
if (diskState.tagLinks == null) {
diskState.tagLinks = new HashSet<>();
}
diskState.tagLinks.addAll(ctx.internalTagLinks);
diskState.regionId = entry.getValue().location;
diskOp = Operation.createPatch(createInventoryUri(getHost(), diskState.documentSelfLink)).setBody(diskState);
} else {
diskState = createLocalDiskState(ctx, entry.getValue());
diskOp = Operation.createPost(createInventoryUri(getHost(), DiskService.FACTORY_LINK)).setBody(diskState);
}
ctx.localDiskStates.put(diskState.id, diskState);
opCollection.add(diskOp);
});
if (opCollection.isEmpty()) {
ctx.subStage = nextStage;
handleSubStage(ctx);
return;
}
logInfo(() -> "Create and/or update disk states in local document store.");
OperationJoin.create(opCollection).setCompletion((ops, exs) -> {
if (exs != null) {
exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
return;
}
logFine(() -> String.format("Transition to " + nextStage));
ctx.subStage = nextStage;
handleSubStage(ctx);
}).sendWith(this);
}
Aggregations