use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class AWSCostStatsService method getMarkerMetricsOp.
private Operation getMarkerMetricsOp(AWSCostStatsCreationContext context, ComputeState accComputeState) {
QueryTask qTask = getQueryTaskForMetric(accComputeState);
Operation.CompletionHandler completionHandler = (operation, exception) -> {
if (exception != null) {
logWarning(() -> String.format("Failed to get bill processed time for account: %s", accComputeState.documentSelfLink));
getFailureConsumer(context).accept(exception);
return;
}
QueryTask body = operation.getBody(QueryTask.class);
String accountId = accComputeState.customProperties.get(AWS_ACCOUNT_ID_KEY);
if (body.results.documentCount == 0) {
ResourceMetrics markerMetrics = new ResourceMetrics();
markerMetrics.timestampMicrosUtc = getCurrentMonthStartTimeMicros();
markerMetrics.entries = new HashMap<>();
markerMetrics.entries.put(AWSConstants.AWS_ACCOUNT_BILL_PROCESSED_TIME_MILLIS, 0d);
markerMetrics.documentSelfLink = StatsUtil.getMetricKey(accComputeState.documentSelfLink, Utils.getNowMicrosUtc());
context.accountsMarkersMap.put(accountId, markerMetrics);
} else {
ResourceMetrics markerMetrics = body.results.documents.values().stream().map(o -> Utils.fromJson(o, ResourceMetrics.class)).collect(Collectors.toList()).get(0);
context.accountsMarkersMap.putIfAbsent(accountId, markerMetrics);
}
};
return QueryUtils.createQueryTaskOperation(this, qTask, ServiceTypeCluster.METRIC_SERVICE).setExpiration(Utils.fromNowMicrosUtc(TimeUnit.SECONDS.toMicros(INTERNAL_REQUEST_TIMEOUT_SECONDS))).setCompletion(completionHandler);
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class AWSEndpointAdapterService method checkIfAccountExistsAndGetExistingDocuments.
private void checkIfAccountExistsAndGetExistingDocuments(EndpointConfigRequest req, Operation op) {
if (req.isMockRequest) {
req.accountAlreadyExists = false;
op.setBody(req);
op.complete();
return;
}
String accountId = getAccountId(req.endpointProperties.get(ARN_KEY), req.endpointProperties.get(EndpointConfigRequest.PRIVATE_KEYID_KEY), req.endpointProperties.get(EndpointConfigRequest.PRIVATE_KEY_KEY));
if (accountId != null && !accountId.isEmpty()) {
QueryTask queryTask = QueryUtils.createAccountQuery(accountId, PhotonModelConstants.EndpointType.aws.name(), req.tenantLinks);
queryTask.tenantLinks = req.tenantLinks;
QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((qrt, e) -> {
if (e != null) {
logSevere(() -> String.format("Failure retrieving query results for compute host corresponding to" + "the account ID: %s", e.toString()));
op.fail(e);
return;
}
if (qrt.results.documentCount > 0) {
req.accountAlreadyExists = true;
Object state = qrt.results.documents.values().iterator().next();
ComputeState computeHost = Utils.fromJson(state, ComputeState.class);
req.existingComputeState = computeHost;
getComputeDescription(req, computeHost.descriptionLink, op);
} else {
req.accountAlreadyExists = false;
op.setBody(req);
op.complete();
return;
}
});
} else {
// If the account Id cannot be looked up with the given set of credentials then de duplication is not possible.
req.accountAlreadyExists = false;
op.setBody(req);
op.complete();
}
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method createComputeInstanceForRegion.
private ComputeState createComputeInstanceForRegion(EnumerationContext context, RegionInfo regionInfo) {
ComputeService.ComputeState computeState = new ComputeService.ComputeState();
computeState.name = regionInfo.name;
computeState.id = regionInfo.regionId;
computeState.adapterManagementReference = context.parentCompute.adapterManagementReference;
computeState.instanceAdapterReference = context.parentCompute.description.instanceAdapterReference;
computeState.statsAdapterReference = context.parentCompute.description.statsAdapterReference;
computeState.parentLink = context.parentCompute.documentSelfLink;
computeState.computeHostLink = context.parentCompute.documentSelfLink;
computeState.resourcePoolLink = context.request.resourcePoolLink;
computeState.endpointLink = context.request.endpointLink;
AdapterUtils.addToEndpointLinks(computeState, context.request.endpointLink);
String descriptionLinkId = generateRegionComputeDescriptionLinkId(regionInfo.regionId, context.request.endpointLink);
computeState.descriptionLink = UriUtils.buildUriPath(ComputeDescriptionService.FACTORY_LINK, descriptionLinkId);
computeState.type = ComputeType.ZONE;
computeState.regionId = regionInfo.regionId;
computeState.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
computeState.powerState = PowerState.ON;
computeState.customProperties = context.parentCompute.customProperties;
if (computeState.customProperties == null) {
computeState.customProperties = new HashMap<>();
}
computeState.customProperties.put(SOURCE_TASK_LINK, ResourceEnumerationTaskService.FACTORY_LINK);
computeState.tenantLinks = context.parentCompute.tenantLinks;
return computeState;
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method disassociateOrRetireHelper.
/**
* Helper method to paginate through resources to be deleted.
*/
private void disassociateOrRetireHelper(EnumerationContext ctx, ComputeEnumerationSubStages next) {
if (ctx.deletionNextPageLink == null) {
logFine(() -> String.format("Finished %s of compute states for Azure", ctx.request.preserveMissing ? "retiring" : "deletion"));
ctx.subStage = next;
handleSubStage(ctx);
return;
}
CompletionHandler completionHandler = (o, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
QueryTask queryTask = o.getBody(QueryTask.class);
ctx.deletionNextPageLink = queryTask.results.nextPageLink;
List<Operation> operations = new ArrayList<>();
for (Object s : queryTask.results.documents.values()) {
ComputeState computeState = Utils.fromJson(s, ComputeState.class);
String vmId = computeState.id;
// present in Azure but have older timestamp in local repository.
if (ctx.vmIds.contains(vmId) || ctx.regionIds.contains(vmId)) {
continue;
}
if (ctx.request.preserveMissing) {
logFine(() -> String.format("Retiring compute state %s", computeState.documentSelfLink));
ComputeState cs = new ComputeState();
cs.powerState = PowerState.OFF;
cs.lifecycleState = LifecycleState.RETIRED;
operations.add(Operation.createPatch(this, computeState.documentSelfLink).setBody(cs));
} else {
// Deleting the localResourceState is done by disassociating the endpointLink from the
// localResourceState. If the localResourceState isn't associated with any other
// endpointLink, we issue a delete then
Operation dOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, computeState);
if (dOp != null) {
dOp.sendWith(getHost());
logFine(() -> String.format("Deleting compute state %s", computeState.documentSelfLink));
}
if (computeState.diskLinks != null && !computeState.diskLinks.isEmpty()) {
computeState.diskLinks.forEach(dl -> {
sendRequest(Operation.createGet(this, dl).setCompletion((op, ex) -> {
if (ex != null) {
logWarning(() -> String.format("Error retrieving " + "diskState: %s", ex.getMessage()));
} else {
DiskState diskState = op.getBody(DiskState.class);
Operation diskOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, diskState);
if (diskOp != null) {
diskOp.sendWith(getHost());
logFine(() -> String.format("Deleting disk state %s of machine %s", dl, computeState.documentSelfLink));
}
}
}));
});
}
if (computeState.networkInterfaceLinks != null && !computeState.networkInterfaceLinks.isEmpty()) {
computeState.networkInterfaceLinks.forEach(nil -> {
sendRequest(Operation.createGet(this, nil).setCompletion((op, ex) -> {
if (ex != null) {
logWarning(() -> String.format("Error retrieving NetworkInterface state: %s", ex.getMessage()));
} else {
NetworkInterfaceState networkInterfaceState = op.getBody(NetworkInterfaceState.class);
Operation nicOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, networkInterfaceState);
if (nicOp != null) {
nicOp.sendWith(getHost());
logFine(() -> String.format("Deleting NetworkInterface state %s of machine %s", nil, computeState.documentSelfLink));
}
}
}));
});
}
}
}
if (operations.size() == 0) {
logFine(() -> String.format("No compute/disk states to %s", ctx.request.preserveMissing ? "retire" : "delete"));
disassociateOrRetireHelper(ctx, next);
return;
}
OperationJoin.create(operations).setCompletion((ops, exs) -> {
if (exs != null) {
// We don't want to fail the whole data collection if some of the
// operation fails.
exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
}
disassociateOrRetireHelper(ctx, next);
}).sendWith(this);
};
logFine(() -> String.format("Querying page [%s] for resources to be %s", ctx.deletionNextPageLink, ctx.request.preserveMissing ? "retire" : "delete"));
sendRequest(Operation.createGet(createInventoryUri(this.getHost(), ctx.deletionNextPageLink)).setCompletion(completionHandler));
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class AzureComputeDiskDay2Service method updateComputeState.
/**
* Update the diskLink of DiskState in ComputeState
*/
private DeferredResult<Operation> updateComputeState(AzureComputeDiskDay2Context context) {
Map<String, Collection<Object>> collectionsToModify = Collections.singletonMap(ComputeState.FIELD_NAME_DISK_LINKS, Collections.singletonList(context.diskState.documentSelfLink));
Map<String, Collection<Object>> collectionsToAdd = null;
Map<String, Collection<Object>> collectionsToRemove = null;
ComputeState computeState = context.computeState;
if (context.request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
collectionsToAdd = collectionsToModify;
} else if (context.request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
collectionsToRemove = collectionsToModify;
}
ServiceStateCollectionUpdateRequest updateDiskLinksRequest = ServiceStateCollectionUpdateRequest.create(collectionsToAdd, collectionsToRemove);
Operation computeStateOp = Operation.createPatch(createInventoryUri(this.getHost(), computeState.documentSelfLink)).setBody(updateDiskLinksRequest).setReferer(this.getUri());
return this.sendWithDeferredResult(computeStateOp);
}
Aggregations