use of com.vmware.photon.controller.model.ComputeProperties.ENDPOINT_LINK_PROP_NAME in project photon-model by vmware.
the class EndpointAllocationTaskService method doTriggerEnumeration.
private void doTriggerEnumeration(EndpointAllocationTaskState currentState, SubStage next, URI adapterManagementReference) {
EndpointState endpoint = currentState.endpointState;
long intervalMicros = currentState.enumerationRequest.refreshIntervalMicros != null ? currentState.enumerationRequest.refreshIntervalMicros : DEFAULT_SCHEDULED_TASK_INTERVAL_MICROS;
// Use endpoint documentSelfLink's last part as convention, so that we are able to stop
// enumeration during endpoint update.
String id = UriUtils.getLastPathSegment(endpoint.documentSelfLink);
ResourceEnumerationTaskState enumTaskState = new ResourceEnumerationTaskState();
enumTaskState.parentComputeLink = endpoint.computeLink;
enumTaskState.endpointLink = endpoint.documentSelfLink;
enumTaskState.resourcePoolLink = currentState.enumerationRequest.resourcePoolLink;
enumTaskState.adapterManagementReference = adapterManagementReference;
enumTaskState.tenantLinks = endpoint.tenantLinks;
enumTaskState.options = EnumSet.of(TaskOption.SELF_DELETE_ON_COMPLETION);
if (currentState.options.contains(TaskOption.IS_MOCK)) {
enumTaskState.options.add(TaskOption.IS_MOCK);
}
if (currentState.options.contains(TaskOption.PRESERVE_MISSING_RESOUCES)) {
enumTaskState.options.add(TaskOption.PRESERVE_MISSING_RESOUCES);
}
ScheduledTaskState scheduledTaskState = new ScheduledTaskState();
scheduledTaskState.documentSelfLink = id;
scheduledTaskState.factoryLink = ResourceEnumerationTaskService.FACTORY_LINK;
scheduledTaskState.initialStateJson = Utils.toJson(enumTaskState);
scheduledTaskState.intervalMicros = intervalMicros;
scheduledTaskState.delayMicros = currentState.enumerationRequest.delayMicros;
scheduledTaskState.noDelayOnInitialExecution = Boolean.TRUE;
scheduledTaskState.tenantLinks = endpoint.tenantLinks;
scheduledTaskState.customProperties = new HashMap<>();
scheduledTaskState.customProperties.put(ENDPOINT_LINK_PROP_NAME, endpoint.documentSelfLink);
Operation.createPost(this, ScheduledTaskService.FACTORY_LINK).addPragmaDirective(Operation.PRAGMA_DIRECTIVE_FORCE_INDEX_UPDATE).setBody(scheduledTaskState).setCompletion((o, e) -> {
if (e != null) {
logWarning(() -> String.format("Error triggering Enumeration task, reason:" + " %s", e.getMessage()));
}
sendSelfPatch(createUpdateSubStageTask(next));
}).sendWith(this);
}
use of com.vmware.photon.controller.model.ComputeProperties.ENDPOINT_LINK_PROP_NAME in project photon-model by vmware.
the class EndpointAllocationTaskService method createEndpoint.
private void createEndpoint(EndpointAllocationTaskState currentState) {
List<String> createdDocumentLinks = new ArrayList<>();
EndpointState es = currentState.endpointState;
Map<String, String> endpointProperties = currentState.endpointState.endpointProperties;
es.endpointProperties = null;
if (es.documentSelfLink == null) {
es.documentSelfLink = UriUtils.buildUriPath(EndpointService.FACTORY_LINK, this.getHost().nextUUID());
}
// merge endpoint and task tenant links
if (es.tenantLinks == null || es.tenantLinks.isEmpty()) {
es.tenantLinks = currentState.tenantLinks;
} else if (currentState.tenantLinks != null) {
currentState.tenantLinks.forEach(tl -> {
if (!es.tenantLinks.contains(tl)) {
es.tenantLinks.add(tl);
}
});
}
Operation endpointOp = Operation.createPost(this, EndpointService.FACTORY_LINK);
ComputeDescription computeDescription = configureDescription(currentState, es);
ComputeState computeState = configureCompute(currentState, es, endpointProperties);
Operation cdOp = createComputeDescriptionOp(currentState, computeDescription.documentSelfLink);
Operation compOp = createComputeStateOp(currentState, computeState.documentSelfLink);
// pool link
if (currentState.enumerationRequest != null && currentState.enumerationRequest.resourcePoolLink != null) {
es.resourcePoolLink = currentState.enumerationRequest.resourcePoolLink;
computeState.resourcePoolLink = es.resourcePoolLink;
}
OperationSequence sequence;
if (es.authCredentialsLink == null) {
AuthCredentialsServiceState auth = configureAuth(es);
Operation authOp = Operation.createPost(createInventoryUri(this.getHost(), AuthCredentialsService.FACTORY_LINK)).setBody(auth);
sequence = OperationSequence.create(authOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create auth credentials: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation o = ops.get(authOp.getId());
AuthCredentialsServiceState authState = o.getBody(AuthCredentialsServiceState.class);
computeDescription.authCredentialsLink = authState.documentSelfLink;
es.authCredentialsLink = authState.documentSelfLink;
cdOp.setBody(computeDescription);
}).next(cdOp);
} else {
cdOp.setBody(computeDescription);
sequence = OperationSequence.create(cdOp);
}
sequence = sequence.setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create compute description: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation o = ops.get(cdOp.getId());
ComputeDescription desc = o.getBody(ComputeDescription.class);
if (!currentState.accountAlreadyExists) {
createdDocumentLinks.add(desc.documentSelfLink);
}
computeState.descriptionLink = desc.documentSelfLink;
es.computeDescriptionLink = desc.documentSelfLink;
});
// Don't create resource pool, if a resource pool link was passed.
if (es.resourcePoolLink == null) {
Operation poolOp = createResourcePoolOp(es);
sequence = sequence.next(poolOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error creating resource" + " pool: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation o = ops.get(poolOp.getId());
ResourcePoolState poolState = o.getBody(ResourcePoolState.class);
createdDocumentLinks.add(poolState.documentSelfLink);
es.resourcePoolLink = poolState.documentSelfLink;
computeState.resourcePoolLink = es.resourcePoolLink;
compOp.setBody(computeState);
});
} else {
Operation getPoolOp = Operation.createGet(this, es.resourcePoolLink);
sequence = sequence.next(getPoolOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error retrieving resource" + " pool: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation o = ops.get(getPoolOp.getId());
ResourcePoolState poolState = o.getBody(ResourcePoolState.class);
if (poolState.customProperties != null) {
String endpointLink = poolState.customProperties.get(ENDPOINT_LINK_PROP_NAME);
if (endpointLink != null && endpointLink.equals(es.documentSelfLink)) {
sendFailurePatch(this, currentState, new IllegalStateException("Passed resource pool is associated with a different endpoint."));
return;
}
}
es.resourcePoolLink = poolState.documentSelfLink;
computeState.resourcePoolLink = es.resourcePoolLink;
compOp.setBody(computeState);
});
}
sequence.next(compOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create compute state: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation csOp = ops.get(compOp.getId());
ComputeState c = csOp.getBody(ComputeState.class);
if (!currentState.accountAlreadyExists) {
createdDocumentLinks.add(c.documentSelfLink);
}
es.computeLink = c.documentSelfLink;
endpointOp.setBody(es);
}).next(endpointOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create endpoint state: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation esOp = ops.get(endpointOp.getId());
EndpointState endpoint = esOp.getBody(EndpointState.class);
createdDocumentLinks.add(endpoint.documentSelfLink);
// propagate the endpoint properties to the next stage
endpoint.endpointProperties = endpointProperties;
EndpointAllocationTaskState state = createUpdateSubStageTask(SubStage.INVOKE_ADAPTER);
state.endpointState = endpoint;
state.createdDocumentLinks = createdDocumentLinks;
sendSelfPatch(state);
}).sendWith(this);
}
Aggregations