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));
}
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();
}
}
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;
}
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);
}
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));
}
}
}
}
Aggregations