use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.
the class AWSNetworkService method deleteSubnetStates.
/**
* Delete all subnet states that refer the NetworkState we are about to delete.
*/
private void deleteSubnetStates(AWSNetworkContext context, AWSNetworkStage next) {
Query queryForReferrers = QueryUtils.queryForReferrers(context.network.documentSelfLink, SubnetState.class, SubnetState.FIELD_NAME_NETWORK_LINK);
QueryByPages<SubnetState> subnetStates = new QueryByPages<>(getHost(), queryForReferrers, SubnetState.class, context.network.tenantLinks, context.network.endpointLink);
subnetStates.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
DeferredResult<Void> query = subnetStates.queryDocuments(subnetState -> {
// First delete Subnet in AWS
try {
context.client.deleteSubnet(subnetState.id);
} catch (AmazonEC2Exception ex) {
if (AWSNetworkClient.STATUS_CODE_SUBNET_NOT_FOUND.equals(ex.getErrorCode())) {
// Ignore exception if the subnet is no longer available in AWS.
this.logWarning(() -> "Unable to delete the subnet in AWS. Reason: " + ex.getMessage());
} else {
throw ex;
}
}
// Then delete tracking SubnetState
Operation.createDelete(this, subnetState.documentSelfLink).sendWith(this);
});
query.whenComplete((v, e) -> {
if (e != null) {
handleStages(context, e);
} else {
handleStages(context, next);
}
});
}
use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method doCreateUpdateNics.
/**
* Manages creating and updating Network Interfaces resources based on network interfaces
* associated with virtual machines.
*/
private DeferredResult<List<NetworkInterfaceState>> doCreateUpdateNics(EnumerationContext ctx, Map<String, String> subnetPerNicId, List<Pair<NetworkInterfaceInner, String>> remoteNics) {
Map<String, Pair<NetworkInterfaceInner, String>> remoteStates = remoteNics.stream().filter(p -> p.getLeft() != null).collect(Collectors.toMap(p -> p.getLeft().id(), p -> p));
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(NetworkInterfaceState.class).addInClause(NetworkInterfaceState.FIELD_NAME_ID, remoteStates.keySet());
QueryByPages<NetworkInterfaceState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), NetworkInterfaceState.class, ctx.parentCompute.tenantLinks, // endpointLink
null, ctx.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
return queryLocalStates.collectDocuments(Collectors.toList()).thenCompose(localNics -> requestCreateUpdateNic(localNics, remoteStates, ctx, subnetPerNicId, remoteNics));
}
use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method loadSubnets.
private DeferredResult<Map<String, String>> loadSubnets(EnumerationContext ctx, List<Pair<NetworkInterfaceInner, String>> remoteNics) {
Map<String, List<Pair<NetworkInterfaceInner, String>>> nicsPerSubnet = remoteNics.stream().filter(p -> p.getLeft() != null && p.getLeft().ipConfigurations() != null && !p.getLeft().ipConfigurations().isEmpty() && p.getLeft().ipConfigurations().get(0).subnet() != null).collect(java.util.stream.Collectors.groupingBy(p -> p.getLeft().ipConfigurations().get(0).subnet().id()));
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(SubnetState.class).addInClause(NetworkInterfaceState.FIELD_NAME_ID, nicsPerSubnet.keySet().stream().collect(Collectors.toList()));
QueryByPages<SubnetState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), SubnetState.class, ctx.parentCompute.tenantLinks, // endpointLink
null, ctx.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
Map<String, String> subnetLinkPerNicId = new HashMap<>();
return queryLocalStates.queryDocuments(subnet -> nicsPerSubnet.get(subnet.id).forEach(p -> subnetLinkPerNicId.put(p.getLeft().id(), subnet.documentSelfLink))).thenApply(ignore -> subnetLinkPerNicId);
}
use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method queryForDiskStates.
/**
* Get all disk states related to given VMs
*/
private void queryForDiskStates(EnumerationContext ctx, ComputeEnumerationSubStages next) {
if (ctx.virtualMachines.size() == 0) {
logFine(() -> "No virtual machines found to be associated with local disks");
if (ctx.regions.isEmpty()) {
ctx.subStage = ComputeEnumerationSubStages.DISASSOCIATE_COMPUTE_STATES;
handleSubStage(ctx);
return;
} else {
ctx.subStage = ComputeEnumerationSubStages.UPDATE_COMPUTE_STATES;
handleSubStage(ctx);
return;
}
}
ctx.diskStates.clear();
List<String> diskIdList = new ArrayList<>();
for (String instanceId : ctx.virtualMachines.keySet()) {
VirtualMachineInner virtualMachine = ctx.virtualMachines.get(instanceId);
String diskId = getVhdUri(virtualMachine);
if (diskId == null) {
continue;
}
diskIdList.add(diskId);
List<String> dataDiskIDList = getDataDisksID(virtualMachine, AzureUtils.isDiskManaged(virtualMachine));
if (null != dataDiskIDList && dataDiskIDList.size() > 0) {
diskIdList.addAll(dataDiskIDList);
}
}
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(DiskState.class).addInClause(DiskState.FIELD_NAME_ID, diskIdList, Occurance.SHOULD_OCCUR);
QueryByPages<DiskState> queryDiskStates = new QueryByPages<>(getHost(), qBuilder.build(), DiskState.class, ctx.parentCompute.tenantLinks, // endpointLink
null, ctx.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
queryDiskStates.collectDocuments(Collectors.toList()).whenComplete((diskStates, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
if (diskStates == null) {
return;
}
diskStates.forEach(diskState -> ctx.diskStates.put(diskState.id, diskState));
ctx.subStage = next;
handleSubStage(ctx);
});
}
use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method disassociateNicHelper.
/**
* Helper for deleting stale network interfaces.
*/
private DeferredResult<List<Operation>> disassociateNicHelper(List<String> remoteNicIds, EnumerationContext ctx) {
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(NetworkInterfaceState.class);
QueryByPages<NetworkInterfaceState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), NetworkInterfaceState.class, ctx.parentCompute.tenantLinks, ctx.request.endpointLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
return queryLocalStates.collectDocuments(Collectors.toList()).thenCompose(allLocalNics -> disassociateNics(ctx, remoteNicIds, allLocalNics));
}
Aggregations