Search in sources :

Example 6 with ServiceNotFoundException

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

the class TestProvisionAWSSecurityGroup method testDeleteAWSSecurityGroupWithDependency.

@Test
public void testDeleteAWSSecurityGroupWithDependency() throws Throwable {
    // create credentials
    Operation authResponse = new Operation();
    TestUtils.postCredentials(this.host, authResponse, this.privateKey, this.privateKeyId);
    AuthCredentialsServiceState creds = authResponse.getBody(AuthCredentialsServiceState.class);
    // create resource pool
    Operation poolResponse = new Operation();
    TestUtils.postResourcePool(this.host, poolResponse);
    ResourcePoolState pool = poolResponse.getBody(ResourcePoolState.class);
    // create sg service
    Operation securityGroupResponse = new Operation();
    SecurityGroupState initialSecurityGroupState = buildSecurityGroupState(creds, pool);
    TestUtils.postSecurityGroup(this.host, initialSecurityGroupState, securityGroupResponse);
    SecurityGroupState securityGroupState = securityGroupResponse.getBody(SecurityGroupState.class);
    // set up security group task state
    ProvisionSecurityGroupTaskState task = new ProvisionSecurityGroupTaskState();
    task.requestType = SecurityGroupInstanceRequest.InstanceRequestType.CREATE;
    task.securityGroupDescriptionLinks = Stream.of(securityGroupState.documentSelfLink).collect(Collectors.toSet());
    task.customProperties = new HashMap<>();
    task.customProperties.put(NETWORK_STATE_ID_PROP_NAME, this.vpcId);
    Operation provision = new Operation();
    provisionSecurityGroup(task, provision);
    ProvisionSecurityGroupTaskState ps = provision.getBody(ProvisionSecurityGroupTaskState.class);
    waitForTaskCompletion(this.host, UriUtils.buildUri(this.host, ps.documentSelfLink));
    securityGroupState = getServiceSynchronously(securityGroupState.documentSelfLink, SecurityGroupState.class);
    // provision machine on the newly created SG
    String vm = provisionAWSVMWithEC2Client(this.host, this.ec2client, EC2_LINUX_AMI, this.subnetId, securityGroupState.id);
    // reuse previous task, but switch to a delete
    task.requestType = SecurityGroupInstanceRequest.InstanceRequestType.DELETE;
    Operation remove = new Operation();
    provisionSecurityGroup(task, remove);
    // delete the newly provisioned machine after a small delay
    Runnable deleteMachine = () -> {
        try {
            Thread.sleep(2000);
            deleteVMsUsingEC2Client(this.ec2client, this.host, Collections.singletonList(vm));
        } catch (Throwable t) {
            assertNotNull(t);
        }
    };
    deleteMachine.run();
    ProvisionSecurityGroupTaskState removeTask = remove.getBody(ProvisionSecurityGroupTaskState.class);
    waitForTaskCompletion(this.host, UriUtils.buildUri(this.host, removeTask.documentSelfLink));
    // verify security group state is gone
    try {
        getSecurityGroupState(securityGroupState.documentSelfLink);
    } catch (Exception ex) {
        assertTrue(ex instanceof ServiceNotFoundException);
    }
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) Operation(com.vmware.xenon.common.Operation) TimeoutException(java.util.concurrent.TimeoutException) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) CompletionException(java.util.concurrent.CompletionException) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 7 with ServiceNotFoundException

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

the class TestProvisionAWSSecurityGroup method testProvisionAWSSecurityGroup.

@Test
public void testProvisionAWSSecurityGroup() throws Throwable {
    // create credentials
    Operation authResponse = new Operation();
    TestUtils.postCredentials(this.host, authResponse, this.privateKey, this.privateKeyId);
    AuthCredentialsServiceState creds = authResponse.getBody(AuthCredentialsServiceState.class);
    // create resource pool
    Operation poolResponse = new Operation();
    TestUtils.postResourcePool(this.host, poolResponse);
    ResourcePoolState pool = poolResponse.getBody(ResourcePoolState.class);
    // create sg service
    Operation securityGroupResponse = new Operation();
    SecurityGroupState initialSecurityGroupState = buildSecurityGroupState(creds, pool);
    TestUtils.postSecurityGroup(this.host, initialSecurityGroupState, securityGroupResponse);
    SecurityGroupState securityGroupState = securityGroupResponse.getBody(SecurityGroupState.class);
    // set up security group task state
    ProvisionSecurityGroupTaskState task = new ProvisionSecurityGroupTaskState();
    task.requestType = SecurityGroupInstanceRequest.InstanceRequestType.CREATE;
    task.securityGroupDescriptionLinks = Stream.of(securityGroupState.documentSelfLink).collect(Collectors.toSet());
    task.customProperties = new HashMap<>();
    task.customProperties.put(NETWORK_STATE_ID_PROP_NAME, this.vpcId);
    Operation provision = new Operation();
    provisionSecurityGroup(task, provision);
    ProvisionSecurityGroupTaskState ps = provision.getBody(ProvisionSecurityGroupTaskState.class);
    waitForTaskCompletion(this.host, UriUtils.buildUri(this.host, ps.documentSelfLink));
    validateAWSArtifacts(securityGroupState.documentSelfLink, creds);
    // reuse previous task, but switch to a delete
    task.requestType = SecurityGroupInstanceRequest.InstanceRequestType.DELETE;
    Operation remove = new Operation();
    provisionSecurityGroup(task, remove);
    ProvisionSecurityGroupTaskState removeTask = remove.getBody(ProvisionSecurityGroupTaskState.class);
    waitForTaskCompletion(this.host, UriUtils.buildUri(this.host, removeTask.documentSelfLink));
    // verify security group state is gone
    try {
        getSecurityGroupState(securityGroupState.documentSelfLink);
    } catch (Exception ex) {
        assertTrue(ex instanceof ServiceNotFoundException);
    }
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) Operation(com.vmware.xenon.common.Operation) TimeoutException(java.util.concurrent.TimeoutException) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) CompletionException(java.util.concurrent.CompletionException) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 8 with ServiceNotFoundException

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

the class StatsCollectionTaskService method initializeQuery.

private void initializeQuery(Operation op, StatsCollectionTaskState currentState, ResourcePoolState resourcePoolState) {
    // load the RP state, if not already
    if (resourcePoolState == null) {
        sendRequest(Operation.createGet(UriUtils.extendUri(ClusterUtil.getClusterUri(getHost(), ServiceTypeCluster.INVENTORY_SERVICE), currentState.resourcePoolLink)).setCompletion((o, e) -> {
            if (e != null) {
                if (e instanceof ServiceNotFoundException || o.getStatusCode() == Operation.STATUS_CODE_NOT_FOUND) {
                    logInfo(() -> String.format("Resource pool %s seems to have been deleted", currentState.resourcePoolLink));
                } else {
                    logWarning(() -> String.format("Error retrieving resource pool %s: %s", currentState.resourcePoolLink, Utils.toString(e)));
                }
                sendSelfPatch(new StatsCollectionTaskState(), TaskStage.FAILED, patchBody -> {
                    patchBody.taskInfo.failure = Utils.toServiceErrorResponse(e);
                });
                return;
            }
            ResourcePoolState loadedRpState = o.getBody(ResourcePoolState.class);
            initializeQuery(op, currentState, loadedRpState);
        }));
        return;
    }
    int resultLimit = DEFAULT_QUERY_RESULT_LIMIT;
    try {
        resultLimit = (QUERY_RESULT_LIMIT != null) ? Integer.valueOf(QUERY_RESULT_LIMIT) : DEFAULT_QUERY_RESULT_LIMIT;
    } catch (NumberFormatException e) {
        // use the default;
        logWarning(STATS_QUERY_RESULT_LIMIT + " is not a number; Using a default value of " + DEFAULT_QUERY_RESULT_LIMIT);
    }
    Query resourcePoolStateQuery = resourcePoolState.query;
    // Customize default resource pool Query.
    if (currentState.customizationClauses != null && !currentState.customizationClauses.isEmpty()) {
        currentState.customizationClauses.stream().forEach(q -> {
            resourcePoolStateQuery.addBooleanClause(q);
        });
    }
    QueryTask.Builder queryTaskBuilder = QueryTask.Builder.createDirectTask().setQuery(resourcePoolStateQuery).setResultLimit(resultLimit);
    QueryTask qTask = queryTaskBuilder.build();
    QueryUtils.startInventoryQueryTask(this, qTask).whenComplete((queryRsp, queryEx) -> {
        if (queryEx != null) {
            TaskUtils.sendFailurePatch(this, new StatsCollectionTaskState(), queryEx);
            return;
        }
        StatsCollectionTaskState patchBody = new StatsCollectionTaskState();
        if (queryRsp.results.nextPageLink == null) {
            patchBody.taskInfo = TaskUtils.createTaskState(TaskStage.FINISHED);
        } else {
            patchBody.taskInfo = TaskUtils.createTaskState(TaskStage.STARTED);
            patchBody.taskSubStage = StatsCollectionStage.GET_RESOURCES;
            patchBody.nextPageLink = queryRsp.results.nextPageLink;
        }
        TaskUtils.sendPatch(this, patchBody);
    });
}
Also used : Service(com.vmware.xenon.common.Service) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) SubTaskService(com.vmware.photon.controller.model.tasks.SubTaskService) Utils(com.vmware.xenon.common.Utils) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) TaskFactoryService(com.vmware.xenon.services.common.TaskFactoryService) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) URI(java.net.URI) EnumSet(java.util.EnumSet) SubTaskState(com.vmware.photon.controller.model.tasks.SubTaskService.SubTaskState) ServiceTaskCallback(com.vmware.photon.controller.model.tasks.ServiceTaskCallback) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ServiceTaskCallbackResponse(com.vmware.photon.controller.model.tasks.ServiceTaskCallback.ServiceTaskCallbackResponse) UriUtils(com.vmware.xenon.common.UriUtils) TaskState(com.vmware.xenon.common.TaskState) TaskOption(com.vmware.photon.controller.model.tasks.TaskOption) FactoryService(com.vmware.xenon.common.FactoryService) TaskService(com.vmware.xenon.services.common.TaskService) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) TaskUtils(com.vmware.photon.controller.model.tasks.TaskUtils) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState)

Aggregations

ServiceNotFoundException (com.vmware.xenon.common.ServiceHost.ServiceNotFoundException)8 Operation (com.vmware.xenon.common.Operation)6 SecurityGroupState (com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)5 Test (org.junit.Test)5 ResourcePoolState (com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState)4 BaseModelTest (com.vmware.photon.controller.model.helpers.BaseModelTest)3 ProvisionSecurityGroupTaskState (com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState)3 UriUtils (com.vmware.xenon.common.UriUtils)3 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)3 CompletionException (java.util.concurrent.CompletionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 AzureBaseTest (com.vmware.photon.controller.model.adapters.azure.base.AzureBaseTest)2 AzureTestUtil.getSecurityGroupState (com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.getSecurityGroupState)2 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)2 ComputeDescriptionService (com.vmware.photon.controller.model.resources.ComputeDescriptionService)2 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)2 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)2 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)2 QueryTask (com.vmware.xenon.services.common.QueryTask)2 List (java.util.List)2