Search in sources :

Example 11 with Service

use of com.vmware.xenon.common.Service in project photon-model by vmware.

the class ResourceRemovalTaskService method sendInstanceDelete.

private void sendInstanceDelete(String resourceLink, String subTaskLink, Operation o, ResourceRemovalTaskState currentState) {
    ComputeStateWithDescription chd = o.getBody(ComputeStateWithDescription.class);
    if (chd.description.instanceAdapterReference != null) {
        ComputeInstanceRequest deleteReq = new ComputeInstanceRequest();
        deleteReq.resourceReference = createInventoryUri(this.getHost(), resourceLink);
        deleteReq.taskReference = UriUtils.buildUri(getHost(), subTaskLink);
        deleteReq.requestType = ComputeInstanceRequest.InstanceRequestType.DELETE;
        deleteReq.isMockRequest = currentState.isMockRequest;
        sendRequest(Operation.createPatch(chd.description.instanceAdapterReference).setBody(deleteReq).setCompletion((deleteOp, e) -> {
            if (e != null) {
                logWarning(() -> String.format("PATCH to instance service %s, failed: %s", deleteOp.getUri(), e.toString()));
                ResourceOperationResponse fail = ResourceOperationResponse.fail(resourceLink, e);
                sendPatch(subTaskLink, fail);
                return;
            }
        }));
    } else {
        logWarning(() -> String.format("Compute instance %s doesn't not have configured" + " instanceAdapter. Only local resource will be deleted.", resourceLink));
        ResourceOperationResponse subTaskPatchBody = ResourceOperationResponse.finish(resourceLink);
        sendPatch(subTaskLink, subTaskPatchBody);
    }
}
Also used : Service(com.vmware.xenon.common.Service) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeInstanceRequest(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) Utils(com.vmware.xenon.common.Utils) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) URI(java.net.URI) EnumSet(java.util.EnumSet) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) Operation(com.vmware.xenon.common.Operation) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ResourceIPDeallocationTaskState(com.vmware.photon.controller.model.tasks.ResourceIPDeallocationTaskService.ResourceIPDeallocationTaskState) List(java.util.List) Stream(java.util.stream.Stream) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ServiceTaskCallbackResponse(com.vmware.photon.controller.model.tasks.ServiceTaskCallback.ServiceTaskCallbackResponse) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) TaskState(com.vmware.xenon.common.TaskState) TaskService(com.vmware.xenon.services.common.TaskService) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ComputeInstanceRequest(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest)

Example 12 with Service

use of com.vmware.xenon.common.Service in project photon-model by vmware.

the class BaseTestCase method verifyService.

protected void verifyService(URI factoryUri, FactoryService factoryInstance, Class<? extends ServiceDocument> serviceDocumentType, TestServiceDocumentInitialization serviceDocumentInit, TestServiceDocumentAssertion assertion) throws Throwable {
    int childCount = 1;
    TestContext ctx = testCreate(childCount);
    String prefix = "example-";
    URI[] childURIs = new URI[childCount];
    for (int i = 0; i < childCount; i++) {
        ServiceDocument serviceDocument = serviceDocumentInit.create(prefix, i);
        final int finalI = i;
        // create a ServiceDocument instance.
        Operation createPost = createForcedPost(factoryUri).setBody(serviceDocument).setCompletion((o, e) -> {
            if (e != null) {
                ctx.failIteration(e);
                return;
            }
            ServiceDocument rsp = o.getBody(serviceDocumentType);
            childURIs[finalI] = UriUtils.buildUri(this.host, rsp.documentSelfLink);
            ctx.completeIteration();
        });
        this.host.send(createPost);
    }
    try {
        // verify factory and service instance wiring.
        factoryInstance.setHost(this.host);
        Service serviceInstance = factoryInstance.createServiceInstance();
        serviceInstance.setHost(this.host);
        assertNotNull(serviceInstance);
        ctx.await();
        // do GET on all child URIs
        Map<URI, ? extends ServiceDocument> childStates = this.host.getServiceState(null, serviceDocumentType, childURIs);
        for (ServiceDocument s : childStates.values()) {
            assertion.assertState(prefix, s);
        }
        // verify template GET works on factory
        ServiceDocumentQueryResult templateResult = this.host.getServiceState(null, ServiceDocumentQueryResult.class, UriUtils.extendUri(factoryUri, ServiceHost.SERVICE_URI_SUFFIX_TEMPLATE));
        assertTrue(templateResult.documentLinks.size() == templateResult.documents.size());
        ServiceDocument childTemplate = Utils.fromJson(templateResult.documents.get(templateResult.documentLinks.iterator().next()), serviceDocumentType);
        assertTrue(childTemplate.documentDescription != null);
        assertTrue(childTemplate.documentDescription.propertyDescriptions != null && childTemplate.documentDescription.propertyDescriptions.size() > 0);
    } catch (Throwable t) {
        if (t instanceof RuntimeException) {
            throw t;
        }
        throw new RuntimeException(t);
    }
}
Also used : TestContext(com.vmware.xenon.common.test.TestContext) Service(com.vmware.xenon.common.Service) StatefulService(com.vmware.xenon.common.StatefulService) FactoryService(com.vmware.xenon.common.FactoryService) ServiceDocument(com.vmware.xenon.common.ServiceDocument) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult)

Aggregations

Service (com.vmware.xenon.common.Service)12 Operation (com.vmware.xenon.common.Operation)10 UriUtils (com.vmware.xenon.common.UriUtils)9 Utils (com.vmware.xenon.common.Utils)9 URI (java.net.URI)9 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)7 List (java.util.List)7 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)6 OperationJoin (com.vmware.xenon.common.OperationJoin)6 EnumSet (java.util.EnumSet)6 Consumer (java.util.function.Consumer)6 Query (com.vmware.xenon.services.common.QueryTask.Query)5 UriPaths (com.vmware.photon.controller.model.UriPaths)4 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)4 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)4 ResourceState (com.vmware.photon.controller.model.resources.ResourceState)4 Collections (java.util.Collections)4 TimeUnit (java.util.concurrent.TimeUnit)4 IAAS_API_ENABLED (com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED)3 ComputeInstanceRequest (com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest)3