Search in sources :

Example 31 with ComputeState

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

the class TestVSphereEnumerationTask method verifyCIGapForComputeResourcesAndVMs.

private void verifyCIGapForComputeResourcesAndVMs() throws Throwable {
    ComputeState cd = findRandomVm();
    assertNotNull(cd.customProperties.get(CustomProperties.VM_SOFTWARE_NAME));
    assertNotNull(cd.customProperties.get(CustomProperties.DATACENTER_SELF_LINK));
    ComputeState host = findRandomHost();
    assertNotNull(host.customProperties.get(CustomProperties.MODEL_NAME));
    assertNotNull(host.customProperties.get(CustomProperties.MANUFACTURER));
    assertNotNull(host.customProperties.get(CustomProperties.DATACENTER_SELF_LINK));
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState)

Example 32 with ComputeState

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

the class TestVSphereEnumerationTask method testRefresh.

@Test
public void testRefresh() throws Throwable {
    // Create a resource pool where the VM will be housed
    this.resourcePool = createResourcePool();
    this.auth = createAuth();
    this.computeHostDescription = createComputeDescription();
    this.computeHost = createComputeHost(this.computeHostDescription);
    // Always create endpoint, so that there is no bogus
    // endpointLink. This enumeration task is started on a real
    // endpoint. This will also help tango based adapters
    // as the existence of endpoint is more critical there to
    // extract data collector identifier.
    EndpointState ep = createEndpointState(this.computeHost, this.computeHostDescription);
    this.endpoint = TestUtils.doPost(this.host, ep, EndpointState.class, UriUtils.buildUri(this.host, EndpointService.FACTORY_LINK));
    refreshAndRetire();
    if (!isMock()) {
        ComputeState vm = findRandomVm();
        assertInternalPropertiesSet(vm);
        assertNotNull(vm.endpointLink);
        assertNotNull(vm.tenantLinks);
    }
    captureFactoryState("initial");
    String aComputeLink = null;
    String anUsedHostLink = null;
    String anUnusedHostLink = null;
    if (!isMock()) {
        // clone a random compute and save it under different id
        ComputeState randVm = findRandomVm();
        ComputeState vm = Utils.clone(randVm);
        vm.documentSelfLink = null;
        vm.id = "fake-vm-" + vm.id;
        vm.documentSelfLink = null;
        vm = TestUtils.doPost(this.host, vm, ComputeState.class, UriUtils.buildUri(this.host, ComputeService.FACTORY_LINK));
        aComputeLink = vm.documentSelfLink;
        ComputeState randomHost = findRandomHost();
        {
            ComputeState host = Utils.clone(randomHost);
            host.documentSelfLink = null;
            host.powerState = PowerState.ON;
            host.id = "fake-host-" + host.id;
            host.documentSelfLink = null;
            host = TestUtils.doPost(this.host, host, ComputeState.class, UriUtils.buildUri(this.host, ComputeService.FACTORY_LINK));
            anUsedHostLink = host.documentSelfLink;
            ComputeState update = new ComputeState();
            update.customProperties = new HashMap<>();
            update.customProperties.put(ComputeProperties.PLACEMENT_LINK, host.documentSelfLink);
            TestUtils.doPatch(this.host, update, ComputeState.class, UriUtils.buildUri(this.host, randVm.documentSelfLink));
        }
        {
            ComputeState host = Utils.clone(randomHost);
            host.documentSelfLink = null;
            host.powerState = PowerState.ON;
            host.id = "fake-host-unused" + host.id;
            host.documentSelfLink = null;
            host = TestUtils.doPost(this.host, host, ComputeState.class, UriUtils.buildUri(this.host, ComputeService.FACTORY_LINK));
            anUnusedHostLink = host.documentSelfLink;
        }
    }
    // do a second refresh to test update path
    refreshAndRetire();
    captureFactoryState("updated");
    snapshotFactoryState("metrics", ResourceMetricsService.class);
    if (aComputeLink != null) {
        // the second enumeration marked the fake vm as retired
        Operation op = Operation.createGet(this.host, aComputeLink);
        op = this.host.waitForResponse(op);
        ComputeState compute = op.getBody(ComputeState.class);
        assertEquals(compute.lifecycleState, LifecycleState.RETIRED);
        assertEquals(PowerState.OFF, compute.powerState);
    }
    if (anUsedHostLink != null) {
        // the second enumeration marked the fake vm as retired
        Operation op = Operation.createGet(this.host, anUsedHostLink);
        op = this.host.waitForResponse(op);
        ComputeState compute = op.getBody(ComputeState.class);
        assertEquals(compute.lifecycleState, LifecycleState.RETIRED);
        assertEquals(PowerState.OFF, compute.powerState);
    }
    if (anUnusedHostLink != null) {
        // the unused host is wiped out unconditionally
        Operation op = Operation.createGet(this.host, anUnusedHostLink);
        op = this.host.waitForResponse(op);
        assertEquals(op.getStatusCode(), 404);
    }
    verifyDatastoreAndStoragePolicy();
    if (!isMock()) {
        verifyCIGapForComputeResourcesAndVMs();
        verifyCIGapForDatacenterOrFolder(VimNames.TYPE_DATACENTER);
        verifyCIGapForDatacenterOrFolder(VimNames.TYPE_FOLDER);
        verifyCIGapForDatastore();
        verifyCIGapForComputeResource();
    }
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) HashMap(java.util.HashMap) Operation(com.vmware.xenon.common.Operation) Test(org.junit.Test)

Example 33 with ComputeState

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

the class TestVSphereOvfImport method createComputeHost.

/**
 * Create a compute host representing a vcenter server
 */
private ComputeState createComputeHost() throws Throwable {
    ComputeState computeState = new ComputeState();
    computeState.id = nextName("vm");
    computeState.documentSelfLink = computeState.id;
    computeState.descriptionLink = this.computeHostDescription.documentSelfLink;
    computeState.resourcePoolLink = this.resourcePool.documentSelfLink;
    computeState.adapterManagementReference = getAdapterManagementReference();
    ComputeState returnState = TestUtils.doPost(this.host, computeState, ComputeState.class, UriUtils.buildUri(this.host, ComputeService.FACTORY_LINK));
    return returnState;
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState)

Example 34 with ComputeState

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

the class ResourceUtilsTest method testExpandTags.

@Test
public void testExpandTags() throws Throwable {
    TagState tag1 = new TagState();
    tag1.key = "A";
    tag1.value = "1";
    tag1 = postServiceSynchronously(TagService.FACTORY_LINK, tag1, TagState.class);
    TagState tag2 = new TagState();
    tag2.key = "A";
    tag2.value = "2";
    tag2 = postServiceSynchronously(TagService.FACTORY_LINK, tag2, TagState.class);
    TagState tag3 = new TagState();
    tag3.key = "A";
    tag3.value = "3";
    tag3 = postServiceSynchronously(TagService.FACTORY_LINK, tag3, TagState.class);
    // validate expansion on POST
    ComputeState compute = new ComputeState();
    compute.descriptionLink = "cdLink";
    compute.tagLinks = new HashSet<>();
    compute.tagLinks.add(tag1.documentSelfLink);
    compute.tagLinks.add(tag2.documentSelfLink);
    compute = postServiceSynchronously(ComputeService.FACTORY_LINK, compute, ComputeState.class);
    Collection<String> tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(2, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n2")));
    // validate tags cannot be modified directly
    compute.expandedTags.remove(1);
    assertEquals(1, compute.expandedTags.size());
    putServiceSynchronously(compute.documentSelfLink, compute);
    compute = getServiceSynchronously(compute.documentSelfLink, ComputeState.class);
    tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(2, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n2")));
    // validate expansion on PUT
    compute.tagLinks.remove(tag2.documentSelfLink);
    compute.tagLinks.add(tag3.documentSelfLink);
    putServiceSynchronously(compute.documentSelfLink, compute);
    compute = getServiceSynchronously(compute.documentSelfLink, ComputeState.class);
    tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(2, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n3")));
    // validate expansion on PATCH
    ComputeState patchState = new ComputeState();
    patchState.tagLinks = new HashSet<>();
    patchState.tagLinks.add(tag2.documentSelfLink);
    compute = patchServiceSynchronously(compute.documentSelfLink, patchState, ComputeState.class);
    tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(3, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n2", "A\n3")));
    // validate expansion through custom PATCH body
    Map<String, Collection<Object>> itemsToRemove = new HashMap<>();
    itemsToRemove.put(ResourceState.FIELD_NAME_TAG_LINKS, Arrays.asList(tag2.documentSelfLink, tag3.documentSelfLink));
    patchServiceSynchronously(compute.documentSelfLink, ServiceStateCollectionUpdateRequest.create(null, itemsToRemove));
    compute = getServiceSynchronously(compute.documentSelfLink, ComputeState.class);
    tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(1, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1")));
    // validate query (case-insensitive) (Note: only 1 tag can be found with Xenon 1.6.1)
    Query tagQuery = Query.Builder.create().addFieldClause(TagInfo.COMPOSITE_FIELD_NAME_TAG, "a*", MatchType.WILDCARD).build();
    QueryTask tagQueryTask = QueryTask.Builder.createDirectTask().setQuery(tagQuery).addOption(QueryOption.EXPAND_CONTENT).build();
    tagQueryTask = postServiceSynchronously(ServiceUriPaths.CORE_LOCAL_QUERY_TASKS, tagQueryTask, QueryTask.class);
    assertEquals(1, tagQueryTask.results.documentLinks.size());
    assertEquals(1, tagQueryTask.results.documents.size());
    assertEquals(compute.documentSelfLink, tagQueryTask.results.documentLinks.get(0));
    ComputeState foundCompute = Utils.fromJson(tagQueryTask.results.documents.values().iterator().next(), ComputeState.class);
    assertEquals(1, foundCompute.expandedTags.size());
    assertEquals("A\n1", foundCompute.expandedTags.get(0).tag);
}
Also used : Arrays(java.util.Arrays) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) QueryTask(com.vmware.xenon.services.common.QueryTask) HashMap(java.util.HashMap) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) ArrayList(java.util.ArrayList) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) HashSet(java.util.HashSet) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) Utils(com.vmware.xenon.common.Utils) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Map(java.util.Map) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) Assert.assertTrue(org.junit.Assert.assertTrue) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Assert.assertNull(org.junit.Assert.assertNull) TagInfo(com.vmware.photon.controller.model.resources.ResourceState.TagInfo) UriUtils(com.vmware.xenon.common.UriUtils) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) ServiceDocumentDescription(com.vmware.xenon.common.ServiceDocumentDescription) Assert.assertEquals(org.junit.Assert.assertEquals) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) HashMap(java.util.HashMap) Collection(java.util.Collection) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 35 with ComputeState

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

the class QueryUtilsTest method doTest.

private void doTest(ComputeDescription cd, Set<String> expected, List<String> tenantLinks) {
    Query queryForReferrers = QueryUtils.queryForReferrers(cd.documentSelfLink, ComputeState.class, ComputeState.FIELD_NAME_DESCRIPTION_LINK);
    // The classes under testing: QueryByPages and QueryTop
    List<QueryTemplate<?, ComputeState>> queryStrategies = Arrays.asList(new QueryByPages<>(getHost(), queryForReferrers, ComputeState.class, tenantLinks), new QueryTop<>(getHost(), queryForReferrers, ComputeState.class, tenantLinks));
    // Test collectDocuments/queryDocuments/collectLinks/queryLinks per strategy
    for (QueryTemplate<?, ComputeState> queryStrategy : queryStrategies) {
        for (boolean isDirect : Arrays.asList(true, false)) {
            final String msg = queryStrategy.getClass().getSimpleName() + ":" + isDirect;
            {
                // Test collectDocuments, which internally also tests queryDocuments
                DeferredResult<Set<String>> documentLinksDR = queryStrategy.setDirect(isDirect).collectDocuments(mapping(cs -> cs.documentSelfLink, toSet()));
                Set<String> actual = waitToComplete(documentLinksDR);
                assertThat(msg, actual, equalTo(expected));
            }
            {
                // Test collectLinks, which internally also tests queryLinks
                DeferredResult<Set<String>> documentLinksDR = queryStrategy.setDirect(isDirect).collectLinks(toSet());
                Set<String> actual = waitToComplete(documentLinksDR);
                assertThat(msg, actual, equalTo(expected));
            }
        }
    }
}
Also used : QueryTemplate(com.vmware.photon.controller.model.query.QueryUtils.QueryTemplate) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Set(java.util.Set) HashSet(java.util.HashSet) Collectors.toSet(java.util.stream.Collectors.toSet) Query(com.vmware.xenon.services.common.QueryTask.Query) DeferredResult(com.vmware.xenon.common.DeferredResult)

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