Permalink
Browse files

Initial implementation of setting criteria for a Group

  • Loading branch information...
1 parent 1b2a1d3 commit 61a1484bfd2ecb8d12b6f1e0d5850bc733911155 Jennifer Hickey committed Jul 29, 2010
@@ -1,6 +1,19 @@
+import org.hyperic.hq.grouping.CritterList;
+import org.hyperic.hq.grouping.CritterType;
+import org.hyperic.hq.grouping.CritterRegistry;
+import org.hyperic.hq.grouping.prop.CritterPropType
+import org.hyperic.hq.grouping.prop.EnumCritterProp
+import org.hyperic.hq.grouping.prop.EnumCritterPropDescription
+import org.hyperic.hq.grouping.prop.GroupCritterProp
+import org.hyperic.hq.grouping.prop.ProtoCritterProp
+import org.hyperic.hq.grouping.prop.ResourceCritterProp
+import org.hyperic.hq.grouping.prop.StringCritterProp
import org.hyperic.hq.hqapi1.ErrorCode
-import org.hyperic.hq.authz.shared.PermissionException
+import org.hyperic.hq.authz.shared.PermissionException;
+import org.hyperic.hq.authz.shared.ResourceGroupManager
import org.hyperic.hq.common.VetoException
+import org.hyperic.hq.context.Bootstrap
+import org.hyperic.util.HypericEnum
class GroupController extends ApiController {
@@ -100,6 +113,96 @@ class GroupController extends ApiController {
}
}
}
+
+ def setCriteria(params) {
+ def xmlIn = new XmlParser().parseText(getPostData())
+ def group = resourceHelper.findGroupByName(xmlIn.'@groupName')
+ //TODO figure out weirdness with isAny being a NodeList?
+ //def isAny = xmlIn.'GroupCriteriaList'.'@isAny'
+ def critters
+ def failureXml = null
+ try {
+ critters = parseCritters(xmlIn.'GroupCriteriaList')
+ CritterList clist = new CritterList(critters, true)
+ Bootstrap.getBean(ResourceGroupManager.class).setCriteria(user, group, clist)
+ } catch(Exception e) {
+ //TODO use proper error code
+ failureXml = getFailureXML(ErrorCode.PERMISSION_DENIED,e.getMessage())
+ log.error("Unable to parse critters", e)
+ }
+ renderXml() {
+ GroupResponse() {
+ if (failureXml) {
+ out << failureXml
+ } else {
+ out << getSuccessXML()
+ out << getGroupXML(group)
+ }
+ }
+ }
+ }
+
+ private CritterType findCritterType(String name) {
+ CritterRegistry.getRegistry().critterTypes.find { t ->
+ t.class.name == name
+ }
+ }
+
+ private List parseCritters(xmlIn) {
+ xmlIn.'GroupCriteria'.collect { critterDef ->
+ CritterType critterType = findCritterType(critterDef.'@class')
+
+ if (critterType == null) {
+ throw new Exception("Unable to find critter class [${critterDef.'@class'}]")
+ }
+
+ def props = [:]
+ for (propDef in critterDef.children()) {
+ String propId = propDef.'@name'
+ String propType = propDef.'@type'
+ //TODO maybe not so case sensitive?
+ if (propType == 'string') {
+ props[propId] = new StringCritterProp(propId, propDef.'@value')
+ } else if (propType == 'resource') {
+ def rsrcId = propDef.'@value'.toInteger()
+ def resource = resourceHelper.findResource(rsrcId)
+ props[propId] = new ResourceCritterProp(propId, resource)
+ } else if (propType == 'group') {
+ def group = resourceHelper.findGroupByName(propDef.'@value')
+ props[propId] = new GroupCritterProp(propId, group)
+ } else if (propType == 'proto') {
+ def proto = resourceHelper.findResourcePrototype(propDef.'@value')
+ props[propId] = new ProtoCritterProp(propId, proto)
+ } else if (propType == 'enum') {
+ def desc = critterType.propDescriptions.find { it.id == propId }
+ if (!desc) {
+ throw new Exception("Unknown prop [${propId}] for " +
+ "critter [${critterDef.class}]")
+ }
+ if (desc.type != CritterPropType.ENUM) {
+ throw new Exception("Property [${propId}] of critter ["+
+ "[${critterDef.class}] should be " +
+ "of type <enum>")
+ }
+
+ EnumCritterPropDescription eDesc = desc
+ def propDesc = propDef.'@value'
+ HypericEnum match = eDesc.possibleValues.find { it.description == propDesc }
+ if (match == null) {
+ throw new Exception("[${propDesc}] is not a valid " +
+ "value for prop [${propId}] for " +
+ "critter [${critterDef.@class}]")
+ }
+ props[propId] = new EnumCritterProp(propId, match)
+ } else {
+ throw new Exception("Unknown prop type [${propDef.@type}] for " +
+ "critter [${critterDef.@class}]")
+ }
+ }
+ critterType.newInstance(props)
+ }
+ }
+
def sync(params) {
def syncRequest = new XmlParser().parseText(getPostData())
@@ -29,6 +29,8 @@
import org.hyperic.hq.hqapi1.types.EscalationResponse;
import org.hyperic.hq.hqapi1.types.Group;
+import org.hyperic.hq.hqapi1.types.GroupCriteriaList;
+import org.hyperic.hq.hqapi1.types.GroupCriteriaRequest;
import org.hyperic.hq.hqapi1.types.GroupResponse;
import org.hyperic.hq.hqapi1.types.GroupsRequest;
import org.hyperic.hq.hqapi1.types.GroupsResponse;
@@ -287,4 +289,13 @@ public GroupsResponse syncGroups(List<Group> groups)
return doPost("group/sync.hqu", groupRequest,
new XmlResponseHandler<GroupsResponse>(GroupsResponse.class));
}
+
+ public GroupResponse setCriteria(String groupName, GroupCriteriaList criteria) throws IOException {
+ GroupCriteriaRequest criteriaRequest = new GroupCriteriaRequest();
+ criteriaRequest.setGroupName(groupName);
+ criteriaRequest.setGroupCriteriaList(criteria);
+ return doPost("group/setCriteria.hqu",criteriaRequest,
+ new XmlResponseHandler<GroupResponse>(GroupResponse.class));
+ }
+
}
@@ -379,6 +379,37 @@
<xs:attribute name="resourceId" type="xs:int" use="optional" />
</xs:complexType>
+ <xs:element name="GroupCriteriaRequest">
+ <xs:complexType>
+ <xs:sequence>
+ <xs:element name="GroupCriteriaList" type="GroupCriteriaList" minOccurs="1" maxOccurs="1" />
+ </xs:sequence>
+ <xs:attribute name="groupName" type="xs:string" use="required"/>
+ </xs:complexType>
+ </xs:element>
+
+ <xs:complexType name="GroupCriteriaList">
+ <xs:sequence>
+ <xs:element name="GroupCriteria" type="GroupCriteria" minOccurs="1" maxOccurs="unbounded" />
+ </xs:sequence>
+ <xs:attribute name="isAny" type="xs:boolean" use="required"/>
+ </xs:complexType>
+
+
+ <xs:complexType name="GroupCriteria">
+ <xs:sequence>
+ <xs:element name="CriteriaProp" type="CriteriaProp" minOccurs="1" maxOccurs="unbounded" />
+ </xs:sequence>
+ <xs:attribute name="class" type="xs:string" use="required"/>
+ </xs:complexType>
+
+ <xs:complexType name="CriteriaProp">
+ <xs:attribute name="name" type="xs:string" use="required"/>
+ <xs:attribute name="type" type="xs:string" use="required"/>
+ <xs:attribute name="value" type="xs:string" use="required"/>
+ </xs:complexType>
+
+
<!-- Group Responses -->
<xs:element name="GroupsRequest">

0 comments on commit 61a1484

Please sign in to comment.