From 80c3790a2cb18f21f53e59d1f3580aca460eaad2 Mon Sep 17 00:00:00 2001 From: Lichao Xue <68891670+xuelichao@users.noreply.github.com> Date: Sun, 7 Feb 2021 10:04:40 +0800 Subject: [PATCH] VIC UI is blocked on VC that including new refactoring H5 client API (#698) 1. In VIC UI java layer, we import QueryUtil class which will be moved into a new package: com.vmware.vise.data.query.QueryUtil -> com.vmware.vise.data.query.util.QueryUtil 2. we can not simply fix it by update the import path to com.vmware.vise.data.query.util.QueryUtil Because it will block VIC UI running on lower version before H5 client API changing. 3. Solution: Remove the code dependency on QueryUtil class. Add and implements three Functions to replace the original Functions the new three funcs will provide the same function. --- .../vic/services/ResourcePoolServiceImpl.java | 106 +++++++++++++++--- 1 file changed, 91 insertions(+), 15 deletions(-) diff --git a/h5c/vic-service/src/main/java/com/vmware/vic/services/ResourcePoolServiceImpl.java b/h5c/vic-service/src/main/java/com/vmware/vic/services/ResourcePoolServiceImpl.java index f305d12f1..0cda19c1e 100644 --- a/h5c/vic-service/src/main/java/com/vmware/vic/services/ResourcePoolServiceImpl.java +++ b/h5c/vic-service/src/main/java/com/vmware/vic/services/ResourcePoolServiceImpl.java @@ -22,14 +22,17 @@ import com.vmware.vic.model.constants.VsphereObjects; import com.vmware.vic.mvc.ServicesController; +import com.vmware.vise.data.Constraint; import com.vmware.vise.data.query.CompositeConstraint; +import com.vmware.vise.data.query.Comparator; import com.vmware.vise.data.query.Conjoiner; import com.vmware.vise.data.query.DataService; import com.vmware.vise.data.query.PropertyConstraint; import com.vmware.vise.data.query.QuerySpec; -import com.vmware.vise.data.query.QueryUtil; import com.vmware.vise.data.query.RequestSpec; import com.vmware.vise.data.query.Response; +import com.vmware.vise.data.ResourceSpec; +import com.vmware.vise.data.PropertySpec; /** * Resource Pool Service implementation for checking the uniqueness of @@ -53,28 +56,31 @@ public ResourcePoolServiceImpl(DataService dataService) * @throws Exception */ public boolean isNameUnique(String name) throws Exception { - PropertyConstraint sameVappNameConstraint = - QueryUtil.createPropertyConstraint( - VsphereObjects.VirtualApp, - VsphereObjects.NamePropertyKey, - com.vmware.vise.data.query.Comparator.EQUALS, - name); - PropertyConstraint sameRpNameConstraint = - QueryUtil.createPropertyConstraint( - VsphereObjects.ResourcePool, - VsphereObjects.NamePropertyKey, - com.vmware.vise.data.query.Comparator.EQUALS, - name); + PropertyConstraint sameVappNameConstraint = createPropertyConstraint( + VsphereObjects.VirtualApp, + VsphereObjects.NamePropertyKey, + Comparator.EQUALS, + name); + + PropertyConstraint sameRpNameConstraint = createPropertyConstraint( + VsphereObjects.ResourcePool, + VsphereObjects.NamePropertyKey, + Comparator.EQUALS, + name); + PropertyConstraint[] propConstraints = new PropertyConstraint[]{ sameVappNameConstraint, sameRpNameConstraint }; CompositeConstraint compConstraint = - QueryUtil.createCompositeConstraint(propConstraints, Conjoiner.OR); + createCompositeConstraint(propConstraints, Conjoiner.OR); + QuerySpec querySpec = new QuerySpec(); querySpec.name = "check-name-uniqueness"; - querySpec.resourceSpec = QueryUtil.createEmptyResourceSpec(); + + querySpec.resourceSpec = createEmptyResourceSpec(); + querySpec.resourceSpec.constraint = compConstraint; RequestSpec requestSpec = new RequestSpec(); @@ -90,4 +96,74 @@ public boolean isNameUnique(String name) throws Exception { } return true; } + + /** + * Creates a PropertyConstraint + * + * @param objectType + * Type of object to retrieve. + * @param propertyName + * Name of the property to be matched. + * @param comparator + * The operator to use for comparison + * ('equals', 'unequals', etc.) + * @param comparableValue + * The value to compare. + * + * @return + * A PropertyConstraint. + */ + private PropertyConstraint createPropertyConstraint( + String objectType, + String propertyName, + Comparator comparator, + Object comparableValue) { + + PropertyConstraint constraint = new PropertyConstraint(); + constraint.targetType = objectType; + constraint.propertyName = propertyName; + constraint.comparator = comparator; + constraint.comparableValue = comparableValue; + + return constraint; + } + + /** + * Creates a CompositeConstraint by given nested Constraints and + * conjoiner between them. + * + * @param constraints + * Array of nested Constraints to add into the composite constraint. + * @param conjoiner + * Conjoiner defining how to combine the nested Constraints. + * Can be "AND" or "OR". + * + * @return + * A CompositeConstraint. + */ + private CompositeConstraint createCompositeConstraint( + Constraint[] nestedConstraints, + Conjoiner conjoiner) { + + // create the result constraint and initialize it + CompositeConstraint compositeConstraint = new CompositeConstraint(); + compositeConstraint.nestedConstraints = nestedConstraints; + compositeConstraint.conjoiner = conjoiner; + + return compositeConstraint; + } + + /** + * Creates an empty ResourceSpec without any additional + * properties of that object. + * + * @return an empty ResourceSpec + */ + private ResourceSpec createEmptyResourceSpec() { + PropertySpec propSpec = new PropertySpec(); + propSpec.propertyNames = new String[] {}; + ResourceSpec resourceSpec = new ResourceSpec(); + resourceSpec.propertySpecs = new PropertySpec[] { propSpec }; + return resourceSpec; + } }