use of com.vmware.photon.controller.model.resources.ResourceState in project photon-model by vmware.
the class TestAWSEnumerationDocumentCountInLongRun method verifyResourceDuplicates.
/**
* Verify documents for duplicates after multiple enumerations.
*/
private void verifyResourceDuplicates() {
int total_dup_resource_count = 0;
for (Class resource : resourcesList) {
QueryTask.Query.Builder qBuilder = QueryTask.Query.Builder.create().addKindFieldClause(resource).addFieldClause("endpointLinks.item", this.endpointState.documentSelfLink, QueryTask.QueryTerm.MatchType.TERM, QueryTask.Query.Occurance.MUST_OCCUR);
if (resource.getSimpleName().equals("ComputeState")) {
qBuilder.addFieldClause("type", "VM_GUEST", QueryTask.QueryTerm.MatchType.TERM, QueryTask.Query.Occurance.MUST_OCCUR);
}
QueryTask resourceQt = QueryTask.Builder.createDirectTask().setQuery(qBuilder.build()).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).addOption(QueryTask.QuerySpecification.QueryOption.TOP_RESULTS).setResultLimit(10000).build();
Operation queryDocuments = QueryUtils.createQueryTaskOperation(this.host, resourceQt, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(this.host.getUri());
Operation queryResponse = this.host.waitForResponse(queryDocuments);
Assert.assertTrue("Error retrieving enumerated documents", queryResponse.getStatusCode() == 200);
QueryTask qt = queryResponse.getBody(QueryTask.class);
Set<String> resourceIdSet = new HashSet<>();
if (qt.results != null && qt.results.documentLinks != null && qt.results.documentLinks.size() > 0) {
this.host.log("Number of %s docs: %d", resource.getSimpleName(), qt.results.documentLinks.size());
for (String resourceDocumentLink : qt.results.documentLinks) {
Object object = qt.results.documents.get(resourceDocumentLink);
ResourceState resourceState = Utils.fromJson(object, ResourceState.class);
String resourceId = resourceState.id;
if (!resourceIdSet.contains(resourceId)) {
resourceIdSet.add(resourceId);
} else {
this.host.log("duplicate %s id = %s, with state: ", resource.getSimpleName(), resourceId, Utils.toJsonHtml(resourceState));
total_dup_resource_count++;
}
}
}
}
assertEquals("Duplicate resources found: ", 0, total_dup_resource_count);
}
use of com.vmware.photon.controller.model.resources.ResourceState in project photon-model by vmware.
the class AWSNetworkStateEnumerationAdapterService method deleteStaleLocalStates.
private DeferredResult<AWSNetworkStateCreationContext> deleteStaleLocalStates(AWSNetworkStateCreationContext context, Class<? extends ResourceState> localStateClass, Set<String> remoteResourcesKeys) {
final String msg = "Delete %ss that no longer exist in the endpoint: %s";
logFine(() -> String.format(msg, localStateClass.getSimpleName(), "STARTING"));
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(localStateClass).addFieldClause("lifecycleState", LifecycleState.PROVISIONING.toString(), Occurance.MUST_NOT_OCCUR).addRangeClause(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS, createLessThanRange(context.enumStartTimeInMicros));
if (context.request.regionId != null) {
// Delete resources only in this End-point region
qBuilder.addFieldClause(ResourceState.FIELD_NAME_REGION_ID, context.request.regionId);
}
if (!remoteResourcesKeys.isEmpty() && remoteResourcesKeys.size() <= MAX_RESOURCES_TO_QUERY_ON_DELETE) {
// do not load resources from enumExternalResourcesIds
qBuilder.addInClause(ResourceState.FIELD_NAME_ID, remoteResourcesKeys, Occurance.MUST_NOT_OCCUR);
}
QueryByPages<? extends ResourceState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), localStateClass, context.request.tenantLinks, context.request.request.endpointLink);
queryLocalStates.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
List<DeferredResult<Operation>> disassociateDRs = new ArrayList<>();
// Delete stale resources.
return queryLocalStates.queryDocuments(localState -> {
if (remoteResourcesKeys.contains(localState.id)) {
return;
}
// Deleting the localResourceState is done by disassociating the
// endpointLink from the localResourceState. If the localResourceState
// isn't associated with any other endpointLink, it should be eventually
// deleted by the groomer task
Operation disassociateOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, context.request.request.endpointLink, localState);
if (disassociateOp == null) {
return;
}
// NOTE: The original Op is set with completion that must be executed.
// Since sendWithDeferredResult is used we must manually call it, otherwise it's
// just ignored.
CompletionHandler disassociateOpCompletion = disassociateOp.getCompletion();
DeferredResult<Operation> disassociateDR = sendWithDeferredResult(disassociateOp).whenComplete(disassociateOpCompletion::handle).whenComplete((o, e) -> {
final String message = "Disassociate stale %s state";
if (e != null) {
logWarning(message + ": FAILED with %s", localState.documentSelfLink, Utils.toString(e));
} else {
log(Level.FINEST, message + ": SUCCESS", localState.documentSelfLink);
}
});
disassociateDRs.add(disassociateDR);
}).thenCompose(ignore -> DeferredResult.allOf(disassociateDRs)).thenApply(ignore -> context);
}
use of com.vmware.photon.controller.model.resources.ResourceState in project photon-model by vmware.
the class ResourceOperationUtils method isAvailable.
/**
* Evaluates provided {@code spec}'s target criteria against the specified {@code resourceState}
* and returns if the {@link ResourceOperationSpec} is applicable for the given {@link
* ResourceState}
* @param resourceState
* the resource state for which to check whether given {@code spec} is available
* @param spec
* the {@link ResourceOperationSpec} which to check whether is available for the given
* {@code resourceState}
* @return {@literal true} only in case there is targetCriteria, and the targetCriteria is
* evaluated to {@literal true} for the given {@code resourceState}
*/
public static boolean isAvailable(ResourceState resourceState, ResourceOperationSpec spec) {
AssertUtil.assertNotNull(spec, "'spec' must be set.");
if (spec.targetCriteria == null) {
return true;
}
ScriptEngine engine = new ScriptEngineManager().getEngineByName(SCRIPT_ENGINE_NAME_JS);
if (resourceState != null) {
// Clone original object to avoid changing props of original object from vulnerable
// targetCriteria
ResourceState clone = Utils.cloneObject(resourceState);
engine.getBindings(ScriptContext.ENGINE_SCOPE).put(SCRIPT_CONTEXT_RESOURCE, clone);
}
try {
Object res = engine.eval(spec.targetCriteria);
if (res instanceof Boolean) {
return ((Boolean) res).booleanValue();
} else {
Utils.log(ResourceOperationUtils.class, "isAvailable", Level.WARNING, "Expect boolean result when evaluate targetCriteria \"%s\" of " + "endpointType: %s, resourceType: %s, operation: %s, " + "adapterReference: %s. Result: %s", spec.targetCriteria, spec.endpointType, spec.resourceType, spec.operation, spec.adapterReference, res);
}
} catch (ScriptException e) {
Utils.log(ResourceOperationUtils.class, "isAvailable", Level.SEVERE, "Cannot evaluate targetCriteria '%s' of " + "endpointType: %s, resourceType: %s, operation: %s, " + "adapterReference: %s. Cause: %s", spec.targetCriteria, spec.endpointType, spec.resourceType, spec.operation, spec.adapterReference, Utils.toString(e));
}
return false;
}
use of com.vmware.photon.controller.model.resources.ResourceState in project photon-model by vmware.
the class AzureTestUtil method assertResourceExists.
/**
* Assert that a resource with the provided name exist in the document store.
*
* @param factoryLink
* Factory link to the stateful service which states to check.
* @param name
* name of the resource to assert if exists.
* @param shouldExists
* whether to assert if a resource exists or not.
*/
public static void assertResourceExists(VerificationHost host, String factoryLink, String name, boolean shouldExists) {
ServiceDocumentQueryResult result = host.getExpandedFactoryState(UriUtils.buildUri(host, factoryLink));
boolean exists = false;
for (Object document : result.documents.values()) {
ResourceState state = Utils.fromJson(document, ResourceState.class);
if (name.equals(state.name)) {
exists = true;
break;
}
}
assertEquals("Expected: " + shouldExists + ", but was: " + exists, shouldExists, exists);
}
use of com.vmware.photon.controller.model.resources.ResourceState in project photon-model by vmware.
the class AzureTestUtil method assertResourceDisassociated.
/**
* Assert that a resource with the provided name exist in the document store.
*
* @param factoryLink
* Factory link to the stateful service which states to check.
* @param name
* name of the resource to assert if exists.
* @param isDisassociated
* whether to assert if a resource exists or not.
*/
public static void assertResourceDisassociated(VerificationHost host, String factoryLink, String name, boolean isDisassociated) {
ServiceDocumentQueryResult result = host.getExpandedFactoryState(UriUtils.buildUri(host, factoryLink));
boolean disassociated = false;
for (Object document : result.documents.values()) {
// Read doc as ServiceDocument to access its 'documentKind'
ServiceDocument serviceDoc = Utils.fromJson(document, ServiceDocument.class);
Class<? extends ResourceState> resourceClass = ENDPOINT_LINK_EXPLICIT_SUPPORT.stream().filter(clazz -> serviceDoc.documentKind.equals(Utils.buildKind(clazz))).findFirst().orElse(null);
if (resourceClass != null) {
// Read doc as ResourceState to access its 'endpointLinks'
ResourceState resource = Utils.fromJson(document, resourceClass);
if (Objects.equals(name, resource.name) && resource.endpointLinks.isEmpty()) {
String endpointLink = PhotonModelUtils.getEndpointLink(resource);
if (endpointLink == null || endpointLink.isEmpty()) {
disassociated = true;
break;
}
}
}
}
assertEquals("isDisassociated", isDisassociated, disassociated);
}
Aggregations