Search in sources :

Example 1 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.

the class AWSResetServiceTest method setVMSecurityGroupsToBeDeleted.

private void setVMSecurityGroupsToBeDeleted(Instance instance, ComputeState vm) {
    // This assert is only suitable for real (non-mocking env).
    if (this.isMock) {
        return;
    }
    // Get the SecurityGroupStates that were provided in the request ComputeState
    Collector<SecurityGroupState, ?, Map<String, SecurityGroupState>> convertToMap = Collectors.<SecurityGroupState, String, SecurityGroupState>toMap(sg -> sg.name, sg -> sg);
    Map<String, SecurityGroupState> currentSGNamesToStates = vm.networkInterfaceLinks.stream().map(nicLink -> this.host.getServiceState(null, NetworkInterfaceState.class, UriUtils.buildUri(this.host, nicLink))).<// collect all SecurityGroup States from all NIC states
    SecurityGroupState>flatMap(nicState -> nicState.securityGroupLinks.stream().map(sgLink -> {
        SecurityGroupState sgState = this.host.getServiceState(null, SecurityGroupState.class, UriUtils.buildUri(this.host, sgLink));
        return sgState;
    })).collect(convertToMap);
    for (SecurityGroupState currentSGState : currentSGNamesToStates.values()) {
        // Ensure that the requested SecurityGroup was actually provisioned
        if (currentSGState.name.contains(TestAWSSetupUtils.AWS_NEW_GROUP_PREFIX)) {
            this.sgToCleanUp = currentSGState.id;
        }
    }
}
Also used : Service(com.vmware.xenon.common.Service) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ProvisionComputeTaskService(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService) TestAWSSetupUtils.createAWSComputeHost(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSComputeHost) PhotonModelServices(com.vmware.photon.controller.model.PhotonModelServices) VerificationHost(com.vmware.xenon.common.test.VerificationHost) TestAWSSetupUtils.createAWSResourcePool(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSResourcePool) TestAWSSetupUtils.tearDownTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.tearDownTestVpc) TestAWSSetupUtils.setUpTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setUpTestVpc) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) Utils(com.vmware.xenon.common.Utils) TestAWSSetupUtils.verifyRemovalOfResourceState(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.verifyRemovalOfResourceState) Map(java.util.Map) After(org.junit.After) TestUtils.getExecutor(com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getExecutor) Collector(java.util.stream.Collector) ProvisioningUtils(com.vmware.photon.controller.model.tasks.ProvisioningUtils) AwsNicSpecs(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) StatelessService(com.vmware.xenon.common.StatelessService) AWS_VM_REQUEST_TIMEOUT_MINUTES(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AWS_VM_REQUEST_TIMEOUT_MINUTES) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) TestUtils(com.vmware.photon.controller.model.tasks.TestUtils) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) TestAWSSetupUtils.getVMState(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getVMState) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) TaskState(com.vmware.xenon.common.TaskState) TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client) PhotonModelMetricServices(com.vmware.photon.controller.model.PhotonModelMetricServices) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) HashMap(java.util.HashMap) TestAWSSetupUtils.zoneId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.zoneId) Function(java.util.function.Function) ProvisionComputeTaskState(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService.ProvisionComputeTaskState) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) TestAWSSetupUtils.getAwsInstancesByIds(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getAwsInstancesByIds) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) TestName(org.junit.rules.TestName) Instance(com.amazonaws.services.ec2.model.Instance) Before(org.junit.Before) TestAWSSetupUtils.createAWSAuthentication(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSAuthentication) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) Operation(com.vmware.xenon.common.Operation) TestAWSSetupUtils.getCompute(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getCompute) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) TestAWSSetupUtils.createAWSVMResource(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSVMResource) Rule(org.junit.Rule) TestContext(com.vmware.xenon.common.test.TestContext) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) TestAWSSetupUtils.regionId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.regionId) TestAWSSetupUtils.setAwsClientMockInfo(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setAwsClientMockInfo) Collections(java.util.Collections) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.

the class LongRunEndToEndStatsAggregationTest method verifyResourceMetricEntries.

/**
 * Performs check to verify appropriate resource metrics entries are present for
 * compute resources.
 */
private void verifyResourceMetricEntries(ServiceDocumentQueryResult res) {
    for (Map.Entry<String, Object> resourceMap : res.documents.entrySet()) {
        ComputeState state = Utils.fromJson(resourceMap.getValue(), ComputeState.class);
        if (state.type == ComputeType.ENDPOINT_HOST) {
            assertNotNull(getResourceMetrics(resourceMap.getKey(), PhotonModelConstants.ESTIMATED_CHARGES));
        } else if (state.type == ComputeType.VM_GUEST) {
            long micros = Utils.getNowMicrosUtc() - state.creationTimeMicros;
            if (state.powerState == PowerState.ON && micros > TimeUnit.MINUTES.toMicros(initTimeForStatsAvailInMin)) {
                assertNotNull(getResourceMetrics(resourceMap.getKey(), PhotonModelConstants.CPU_UTILIZATION_PERCENT));
            }
        } else if (state.type == ComputeType.ZONE) {
            assertNotNull(getResourceMetrics(resourceMap.getKey(), PhotonModelConstants.ESTIMATED_CHARGES));
        } else {
            assertNull(getResourceMetrics(resourceMap.getKey(), PhotonModelConstants.ESTIMATED_CHARGES));
            assertNull(getResourceMetrics(resourceMap.getKey(), PhotonModelConstants.CPU_UTILIZATION_PERCENT));
        }
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Map(java.util.Map)

Example 3 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.

the class TestAWSEnumerationDocumentCountInLongRun method storeDocumentLinksFromComputeStates.

/**
 * Gets and stores resource pool links and network interface links by querying
 * given instance IDs.
 * @param instanceIdList List of instance IDs provisioned by the test
 */
private void storeDocumentLinksFromComputeStates(List<String> instanceIdList) {
    // Query to get all compute state documents associated with list of instance IDs.
    QueryTask.Query computeStateQuery = QueryTask.Query.Builder.create().addKindFieldClause(ComputeState.class).addInClause(ComputeState.FIELD_NAME_ID, instanceIdList).build();
    QueryTask q = QueryTask.Builder.createDirectTask().setQuery(computeStateQuery).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).build();
    Operation queryComputeState = QueryUtils.createQueryTaskOperation(this.host, q, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(this.host.getUri());
    Operation queryResponse = this.host.waitForResponse(queryComputeState);
    Assert.assertTrue("Error retrieving compute states", queryResponse.getStatusCode() == 200);
    QueryTask qt = queryResponse.getBody(QueryTask.class);
    // Store all compute links
    this.computeStateLinks.addAll(qt.results.documentLinks);
    // Store resource pool links and network links from all compute states.
    for (String documentLink : this.computeStateLinks) {
        ComputeState cs = Utils.fromJson(qt.results.documents.get(documentLink), ComputeState.class);
        this.resourcePoolLinks.add(cs.resourcePoolLink);
        this.networkInterfaceLinks.addAll(cs.networkInterfaceLinks);
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) Operation(com.vmware.xenon.common.Operation)

Example 4 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.

the class AWSEnumerationAndDeletionAdapterService method populateLocalInstanceInformationFromQueryResults.

/**
 * Populates the local instance information from the query results.
 */
public QueryTask populateLocalInstanceInformationFromQueryResults(EnumerationDeletionContext context, QueryTask queryTask) {
    for (Object s : queryTask.results.documents.values()) {
        ComputeState localInstance = Utils.fromJson(s, ComputeState.class);
        if (!localInstance.id.startsWith(AWS_INSTANCE_ID_PREFIX)) {
            continue;
        }
        context.localInstanceIds.put(localInstance.id, localInstance);
    }
    context.pageNo++;
    context.nextPageLink = queryTask.results.nextPageLink;
    logFine(() -> String.format("Next page link %s", context.nextPageLink));
    return queryTask;
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState)

Example 5 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.

the class AWSEnumerationAndDeletionAdapterService method retireComputeStates.

/**
 * Creates operations to retire all the compute states in the local system for which the AWS
 * instance has been terminated/missing from the remote instance.
 */
private void retireComputeStates(EnumerationDeletionContext context) {
    List<Operation> operations = new ArrayList<>();
    // Disks.
    for (ComputeState cs : context.instancesToBeDeleted) {
        ComputeState cps = new ComputeState();
        cps.powerState = PowerState.OFF;
        cps.lifecycleState = LifecycleState.RETIRED;
        Operation operation = Operation.createPatch(this.getHost(), cs.documentSelfLink).setBody(cps).setReferer(getHost().getUri());
        operations.add(operation);
    }
    // Kick off patch operations with a join handler.
    if (operations == null || operations.size() == 0) {
        logFine(() -> "No local compute states to be deleted.");
        deleteResourcesInLocalSystem(context);
        return;
    }
    OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
        if (exc != null) {
            logSevere(() -> String.format("Failure retiring local compute states: %s ", Utils.toString(exc)));
            deleteResourcesInLocalSystem(context);
            return;
        }
        logFine(() -> "Successfully retired local compute states.");
        deleteResourcesInLocalSystem(context);
        return;
    };
    OperationJoin joinOp = OperationJoin.create(operations);
    joinOp.setCompletion(joinCompletion);
    joinOp.sendWith(getHost());
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) Arrays(java.util.Arrays) OperationContext(com.vmware.xenon.common.OperationContext) QueryTask(com.vmware.xenon.services.common.QueryTask) LifecycleState(com.vmware.photon.controller.model.resources.ComputeService.LifecycleState) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) Utils(com.vmware.xenon.common.Utils) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) Filter(com.amazonaws.services.ec2.model.Filter) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) Instance(com.amazonaws.services.ec2.model.Instance) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AWSConstants.getQueryResultLimit(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.getQueryResultLimit) Reservation(com.amazonaws.services.ec2.model.Reservation) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) Set(java.util.Set) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) AWS_INVALID_INSTANCE_ID_ERROR_CODE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_INVALID_INSTANCE_ID_ERROR_CODE) AWS_INSTANCE_ID_PREFIX(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_INSTANCE_ID_PREFIX) List(java.util.List) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) AWSUtils.getAWSNonTerminatedInstancesFilter(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) AsyncHandler(com.amazonaws.handlers.AsyncHandler) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) OperationJoin(com.vmware.xenon.common.OperationJoin) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation)

Aggregations

ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)214 Operation (com.vmware.xenon.common.Operation)93 ArrayList (java.util.ArrayList)63 QueryTask (com.vmware.xenon.services.common.QueryTask)58 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)57 HashMap (java.util.HashMap)54 List (java.util.List)51 Map (java.util.Map)50 Utils (com.vmware.xenon.common.Utils)45 Test (org.junit.Test)45 UriUtils (com.vmware.xenon.common.UriUtils)44 URI (java.net.URI)42 Collectors (java.util.stream.Collectors)42 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)40 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)40 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)40 StatelessService (com.vmware.xenon.common.StatelessService)40 Query (com.vmware.xenon.services.common.QueryTask.Query)40 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)38 ComputeType (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType)36