use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.
the class AWSRebootServiceTest method createTaskResultListener.
private void createTaskResultListener(VerificationHost host, String taskLink, Function<Operation, Boolean> h) {
StatelessService service = new StatelessService() {
@Override
public void handleRequest(Operation update) {
if (!h.apply(update)) {
super.handleRequest(update);
}
}
};
Operation startOp = Operation.createPost(host, taskLink).setCompletion(this.host.getCompletion()).setReferer(this.host.getReferer());
this.host.startService(startOp, service);
}
use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.
the class AWSPowerServiceTest method createTaskResultListener.
private void createTaskResultListener(VerificationHost host, String taskLink, Function<Operation, Boolean> h) {
StatelessService service = new StatelessService() {
@Override
public void handleRequest(Operation update) {
if (!h.apply(update)) {
super.handleRequest(update);
}
}
};
TestContext ctx = this.host.testCreate(1);
Operation startOp = Operation.createPost(host, taskLink).setCompletion((o, e) -> {
if (e != null) {
ctx.failIteration(e);
return;
}
ctx.completeIteration();
}).setReferer(this.host.getReferer());
this.host.startService(startOp, service);
ctx.await();
}
use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.
the class TestAzureProvisionTask method issueStatsRequest.
private void issueStatsRequest(ComputeState vm) throws Throwable {
// spin up a stateless service that acts as the parent link to patch back to
StatelessService parentService = new StatelessService() {
@Override
public void handleRequest(Operation op) {
if (op.getAction() == Action.PATCH) {
if (!TestAzureProvisionTask.this.isMock) {
ComputeStatsResponse resp = op.getBody(ComputeStatsResponse.class);
if (resp.statsList.size() != 1) {
TestAzureProvisionTask.this.host.failIteration(new IllegalStateException("response size was incorrect."));
return;
}
if (resp.statsList.get(0).statValues.size() == 0) {
TestAzureProvisionTask.this.host.failIteration(new IllegalStateException("incorrect number of metrics received."));
return;
}
if (!resp.statsList.get(0).computeLink.equals(vm.documentSelfLink)) {
TestAzureProvisionTask.this.host.failIteration(new IllegalStateException("Incorrect computeReference returned."));
return;
}
}
TestAzureProvisionTask.this.host.completeIteration();
}
}
};
String servicePath = UUID.randomUUID().toString();
Operation startOp = Operation.createPost(UriUtils.buildUri(getHost(), servicePath));
getHost().startService(startOp, parentService);
ComputeStatsRequest statsRequest = new ComputeStatsRequest();
statsRequest.resourceReference = UriUtils.buildUri(getHost(), vm.documentSelfLink);
statsRequest.isMockRequest = this.isMock;
statsRequest.taskReference = UriUtils.buildUri(getHost(), servicePath);
getHost().sendAndWait(Operation.createPatch(UriUtils.buildUri(getHost(), AzureUriPaths.AZURE_STATS_ADAPTER)).setBody(statsRequest).setReferer(getHost().getUri()));
}
use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.
the class TestAzureEnumerationTask method issueStatsRequest.
private void issueStatsRequest(String selfLink, boolean isComputeHost) throws Throwable {
// spin up a stateless service that acts as the parent link to patch back to
StatelessService parentService = new StatelessService() {
@Override
public void handleRequest(Operation op) {
if (op.getAction() == Action.PATCH) {
if (!TestAzureEnumerationTask.this.isMock) {
ComputeStatsResponse resp = op.getBody(ComputeStatsResponse.class);
if (resp == null) {
TestAzureEnumerationTask.this.host.failIteration(new IllegalStateException("response was null."));
return;
}
if (resp.statsList.size() != 1) {
TestAzureEnumerationTask.this.host.failIteration(new IllegalStateException("response size was incorrect."));
return;
}
if (resp.statsList.get(0).statValues.size() == 0) {
TestAzureEnumerationTask.this.host.failIteration(new IllegalStateException("incorrect number of metrics received."));
return;
}
if (!resp.statsList.get(0).computeLink.equals(selfLink)) {
TestAzureEnumerationTask.this.host.failIteration(new IllegalStateException("Incorrect resourceReference returned."));
return;
}
// Verify all the stats are obtained
verifyStats(resp, isComputeHost);
// Persist stats on Verification Host for testing the computeHost stats.
URI persistStatsUri = UriUtils.buildUri(getHost(), ResourceMetricsService.FACTORY_LINK);
ResourceMetricsService.ResourceMetrics resourceMetric = new ResourceMetricsService.ResourceMetrics();
resourceMetric.documentSelfLink = StatsUtil.getMetricKey(selfLink, Utils.getNowMicrosUtc());
resourceMetric.entries = new HashMap<>();
resourceMetric.timestampMicrosUtc = Utils.getNowMicrosUtc();
for (String key : resp.statsList.get(0).statValues.keySet()) {
List<ServiceStat> stats = resp.statsList.get(0).statValues.get(key);
for (ServiceStat stat : stats) {
if (stat == null) {
continue;
}
resourceMetric.entries.put(key, stat.latestValue);
}
}
TestAzureEnumerationTask.this.host.sendRequest(Operation.createPost(persistStatsUri).setReferer(TestAzureEnumerationTask.this.host.getUri()).setBodyNoCloning(resourceMetric));
}
TestAzureEnumerationTask.this.host.completeIteration();
}
}
};
String servicePath = UUID.randomUUID().toString();
Operation startOp = Operation.createPost(UriUtils.buildUri(this.host, servicePath));
this.host.startService(startOp, parentService);
ComputeStatsRequest statsRequest = new ComputeStatsRequest();
statsRequest.resourceReference = UriUtils.buildUri(this.host, selfLink);
statsRequest.nextStage = SingleResourceTaskCollectionStage.UPDATE_STATS.name();
statsRequest.isMockRequest = this.isMock;
statsRequest.taskReference = UriUtils.buildUri(this.host, servicePath);
this.host.sendAndWait(Operation.createPatch(UriUtils.buildUri(this.host, AzureUriPaths.AZURE_STATS_ADAPTER)).setBody(statsRequest).setReferer(this.host.getUri()));
}
use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.
the class EndpointAdapterUtils method configureEndpoint.
private static void configureEndpoint(StatelessService service, EndpointConfigRequest body, BiConsumer<AuthCredentialsServiceState, Retriever> credEnhancer, BiConsumer<ComputeDescription, Retriever> descEnhancer, BiConsumer<ComputeState, Retriever> compEnhancer, BiConsumer<EndpointState, Retriever> endpointEnhancer) {
TaskManager tm = new TaskManager(service, body.taskReference, body.resourceLink());
Consumer<Throwable> onFailure = tm::patchTaskToFailure;
Consumer<Operation> onSuccess = (op) -> {
EndpointState endpoint = op.getBody(EndpointState.class);
op.complete();
AuthCredentialsServiceState authState = new AuthCredentialsServiceState();
Map<String, String> props = new HashMap<>(body.endpointProperties);
props.put(MOCK_REQUEST, String.valueOf(body.isMockRequest));
props.put(ENDPOINT_REFERENCE_URI, body.resourceReference.toString());
Retriever r = Retriever.of(props);
try {
credEnhancer.accept(authState, r);
ComputeDescription cd = new ComputeDescription();
descEnhancer.accept(cd, r);
ComputeState cs = new ComputeState();
cs.powerState = PowerState.ON;
compEnhancer.accept(cs, r);
EndpointState es = new EndpointState();
es.endpointProperties = new HashMap<>();
es.regionId = r.get(EndpointConfigRequest.REGION_KEY).orElse(null);
endpointEnhancer.accept(es, r);
Stream<Operation> operations = Stream.of(Pair.of(authState, endpoint.authCredentialsLink), Pair.of(cd, endpoint.computeDescriptionLink), Pair.of(cs, endpoint.computeLink), Pair.of(es, endpoint.documentSelfLink)).map((p) -> Operation.createPatch(createInventoryUri(service.getHost(), p.right)).setBody(p.left).setReferer(service.getUri()));
applyChanges(tm, service, endpoint, operations);
} catch (Exception e) {
tm.patchTaskToFailure(e);
}
};
AdapterUtils.getServiceState(service, body.resourceReference, onSuccess, onFailure);
}
Aggregations