use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class TestAzureLongRunningEnumeration method getVMComputeStatesWithPrefix.
/**
* Filters and returns all compute states with a VM name starting with a particular prefix.
* @return A map of ComputeStates with a prefix, with each VM name as the key.
*/
private Map<String, ComputeState> getVMComputeStatesWithPrefix() {
URI queryUri = UriUtils.buildExpandLinksQueryUri(createServiceURI(host, host.getUri(), ComputeService.FACTORY_LINK));
// add limit, otherwise the query will not return if there are too many docs or versions
queryUri = UriUtils.extendUriWithQuery(queryUri, UriUtils.URI_PARAM_ODATA_LIMIT, String.valueOf(numOfVMsToTest * 100));
ServiceDocumentQueryResult res = host.getFactoryState(queryUri);
// Add compute states with the prefix to map.
Map<String, ComputeService.ComputeState> filteredComputeStates = new HashMap<>();
for (String key : res.documents.keySet()) {
String json = Utils.toJson(res.documents.get(key));
ComputeService.ComputeState doc = Utils.fromJson(json, ComputeService.ComputeState.class);
if (azureVMNames.contains(doc.name)) {
filteredComputeStates.put(doc.name, doc);
}
}
return filteredComputeStates;
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class AzureLoadBalancerServiceTest method setUpTests.
@Before
public void setUpTests() throws Throwable {
if (computeHost == null) {
ResourcePoolState resourcePool = createDefaultResourcePool(this.host);
endpointState = this.createEndpointState();
// create a compute host for the Azure
computeHost = createDefaultComputeHost(this.host, resourcePool.documentSelfLink, endpointState);
}
AuthCredentialsServiceState azureVMAuth = new AuthCredentialsServiceState();
azureVMAuth.userEmail = AZURE_ADMIN_USERNAME;
azureVMAuth.privateKey = AZURE_ADMIN_PASSWORD;
azureVMAuth = TestUtils.doPost(host, azureVMAuth, AuthCredentialsServiceState.class, UriUtils.buildUri(host, AuthCredentialsService.FACTORY_LINK));
ComputeDescription compDesc = buildComputeDescription(host, computeHost, endpointState, azureVMAuth);
this.rgName = AzureUtils.DEFAULT_GROUP_PREFIX + compDesc.documentCreationTimeMicros / 1000;
ResourceGroupState rgState = createDefaultResourceGroupState(this.host, this.rgName, computeHost, endpointState, ResourceGroupStateType.AzureResourceGroup);
networkState = createNetworkState(rgState.documentSelfLink);
vmStates = new ArrayList<>();
for (int i = 0; i < poolSize; i++) {
ComputeState vmState = createDefaultVMResource(getHost(), this.rgName + "VM" + i, computeHost, endpointState, NO_PUBLIC_IP_NIC_SPEC, null, 0, compDesc, this.rgName);
vmStates.add(vmState);
}
subnetState = createSubnetState(this.subnetName);
if (!this.isMock) {
this.loadBalancerClient = getAzureSdkClients().getNetworkManagementClientImpl().loadBalancers();
this.rgOpsClient = getAzureSdkClients().getResourceManagementClientImpl().resourceGroups();
ResourceGroupInner rg = new ResourceGroupInner();
rg.withName(this.rgName);
rg.withLocation(this.regionId);
this.rgOpsClient.createOrUpdate(this.rgName, rg);
VirtualNetworkInner vNet = new VirtualNetworkInner();
AddressSpace addressSpace = new AddressSpace();
addressSpace.withAddressPrefixes(Collections.singletonList(AZURE_DEFAULT_VPC_CIDR));
vNet.withAddressSpace(addressSpace);
vNet.withLocation(this.regionId);
VirtualNetworksInner vNetClient = getAzureSdkClients().getNetworkManagementClientImpl().virtualNetworks();
vNetClient.createOrUpdate(this.rgName, this.vNetName, vNet);
kickOffComputeProvision();
kickOffSubnetProvision(InstanceRequestType.CREATE, subnetState, TaskStage.FINISHED);
}
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class EndpointAdapterUtils method configureEndpoint.
private static void configureEndpoint(StatelessService service, EndpointConfigRequest body, BiConsumer<AuthCredentialsServiceState, Retriever> credEnhancer, BiConsumer<ComputeDescription, Retriever> descEnhancer, BiConsumer<ComputeState, Retriever> compEnhancer, BiConsumer<EndpointState, Retriever> endpointEnhancer) {
TaskManager tm = new TaskManager(service, body.taskReference, body.resourceLink());
Consumer<Throwable> onFailure = tm::patchTaskToFailure;
Consumer<Operation> onSuccess = (op) -> {
EndpointState endpoint = op.getBody(EndpointState.class);
op.complete();
AuthCredentialsServiceState authState = new AuthCredentialsServiceState();
Map<String, String> props = new HashMap<>(body.endpointProperties);
props.put(MOCK_REQUEST, String.valueOf(body.isMockRequest));
props.put(ENDPOINT_REFERENCE_URI, body.resourceReference.toString());
Retriever r = Retriever.of(props);
try {
credEnhancer.accept(authState, r);
ComputeDescription cd = new ComputeDescription();
descEnhancer.accept(cd, r);
ComputeState cs = new ComputeState();
cs.powerState = PowerState.ON;
compEnhancer.accept(cs, r);
EndpointState es = new EndpointState();
es.endpointProperties = new HashMap<>();
es.regionId = r.get(EndpointConfigRequest.REGION_KEY).orElse(null);
endpointEnhancer.accept(es, r);
Stream<Operation> operations = Stream.of(Pair.of(authState, endpoint.authCredentialsLink), Pair.of(cd, endpoint.computeDescriptionLink), Pair.of(cs, endpoint.computeLink), Pair.of(es, endpoint.documentSelfLink)).map((p) -> Operation.createPatch(createInventoryUri(service.getHost(), p.right)).setBody(p.left).setReferer(service.getUri()));
applyChanges(tm, service, endpoint, operations);
} catch (Exception e) {
tm.patchTaskToFailure(e);
}
};
AdapterUtils.getServiceState(service, body.resourceReference, onSuccess, onFailure);
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class GCPTestUtil method queryComputeStatesWithPowerState.
/**
* Method to query the number of local compute states with given power state.
* @param host The test service host.
* @param resourcePool The default resource pool.
* @param parentCompute The default compute host.
* @param powerState The given power state.
* @param instanceNames The assumed names of compute states.
*/
private static void queryComputeStatesWithPowerState(VerificationHost host, ResourcePoolState resourcePool, ComputeState parentCompute, PowerState powerState, Set<String> instanceNames) {
Query query = QueryTask.Query.Builder.create().addKindFieldClause(ComputeState.class).addFieldClause(ComputeState.FIELD_NAME_RESOURCE_POOL_LINK, resourcePool.documentSelfLink).addFieldClause(ComputeState.FIELD_NAME_PARENT_LINK, parentCompute.documentSelfLink).build();
QueryTask q = QueryTask.Builder.createDirectTask().addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).setQuery(query).build();
host.sendRequest(QueryUtils.createQueryTaskOperation(host, q, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(host.getUri()).setCompletion((o, e) -> {
if (e != null) {
host.log(Level.WARNING, String.format("Error: %s", e.getMessage()));
return;
}
QueryTask queryTask = o.getBody(QueryTask.class);
if (queryTask.results.documentCount > 0) {
queryTask.results.documents.values().forEach(s -> {
ComputeState computeState = Utils.fromJson(s, ComputeState.class);
if (computeState.powerState == powerState) {
instanceNames.remove(computeState.name);
}
});
}
}));
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class AzureUtils method constructAzureSubscriptionComputeState.
public static ComputeState constructAzureSubscriptionComputeState(String endpointLink, String descriptionLink, List<String> tenantLinks, String entityName, String resourcePoolLink, Map<String, String> customProperties, String name, String parentLink) {
ComputeState cs = new ComputeState();
cs.id = UUID.randomUUID().toString();
cs.name = name != null ? name : entityName;
cs.tenantLinks = tenantLinks;
cs.endpointLink = endpointLink;
cs.computeHostLink = parentLink;
AdapterUtils.addToEndpointLinks(cs, endpointLink);
if (customProperties == null) {
customProperties = new HashMap<>();
}
customProperties.put(EndpointAllocationTaskService.CUSTOM_PROP_ENPOINT_TYPE, EndpointType.azure.name());
cs.customProperties = customProperties;
cs.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
cs.type = ComputeType.ENDPOINT_HOST;
cs.descriptionLink = descriptionLink;
cs.resourcePoolLink = resourcePoolLink;
cs.creationTimeMicros = Utils.getNowMicrosUtc();
return cs;
}
Aggregations