HQU Groovy API
Groovy APIs live here: View SVN
src/org/hyperic/hq/hqu/rendit_sys
Coding Conventions
Many times, methods take only a single argument; a Map of parameters. In such cases, it's written something like this:
def findSomething(p) {
def name = p['name']
def auto = p.get('auto', true)
}
This implies that the method takes 2 arguments, with 1 of them being optional (auto defaults to true)). Documentation for the method should always
provide examples of usage.
Helper APIs
Helpers in rendit_sys/helpers provide APIs for interacting with the HQ backend.
| AgentHelper.groovy | HQ Agents |
| AlertHelper.groovy | Alerts, Alert Definitions, Group Alerting] |
| AuditHelper.groovy | Auditing] |
| ResourceHelper.groovy | Platforms, Servers, Services, Applications, Resources] |
| BaseHelper.groovy | Base class of all helpers |
To get an instance to a helper in your method, use their getter (made available by BaseController.groovy):
import org.hyperic.hq.hqu.rendit.BaseController
class FreezerController extends BaseController {
def FreezerController() {
}
def index(params) {
def myPlatform = getResourceHelper().find(platform:'My Platform')
// or more succinctly
def yourPlat = resourceHelper.find(platform:10001)
}
}
BaseController and the 'Util's
All controllers extend BaseController and therefore have access to all of its utility methods.
A few important methods coming from BaseController:
def MyController extends BaseController {
MyController() {
// Have a closure execute before every action is invoked.
// Also provides getUser() to fetch the current AuthzSubject
// making the request
addBeforeFilter({ log.info "Current request from ${user}"})
}
def index(params) {
log.info 'Hello world!' // built in logging via log4j.
// InvokeArgs provides both HttpServletRequest / Response
log.info "Request from: ${invokeArgs.request.serverName()}"
}
Also
- urlFor(): Uses HtmlUtil.urlFor to generate URLs
- linkTo(): Generates a link to another action or resource
- now(): System.currentTimeMillis()
- escapeHtml(): Escapes a string of HTML
An addition, BaseController provides wrappers around a few methods from the Util classes found here:
- src/org/hyperic/hq/hqu/rendit_sys/util
- src/org/hyperic/hq/hqu/rendit_sys/render
- src/org/hyperic/hq/hqu/rendit_sys/html
MetaClasses and Categories
Groovy allows us to insert methods into objects dynamically at runtime. HQU takes advantage of this to make methods on objects seem
more meaningful.
private getPlatformToDelete(id) {
AuthzSubject curUser = getUser()
log.info "${curUser.isSuperUser()}"
}
The class AuthzSubject exists within HQ, however does not contain the method 'isSuperUser()'. AuthzSubjectCategory.groovy adds it at runtime.
From AuthzSubjectCategory.groovy:
class AuthzSubjectCategory {
static boolean isSuperUser(AuthzSubject subject) {
PermissionManagerFactory
.getInstance()
.hasAdminPermission(subject.id)
}
}
Static methods in the category class tell Groovy to insert those methods for their associated object class. The first parameter is the recipient of the invocation, and all subsequent parameters are passed in like regular.