Skip to content

Instantly share code, notes, and snippets.

@michaelrice
Last active August 29, 2015 14:01
Show Gist options
  • Save michaelrice/d54a237295e017b032a5 to your computer and use it in GitHub Desktop.
Save michaelrice/d54a237295e017b032a5 to your computer and use it in GitHub Desktop.
Resets an alarm from red to green using the unexposed soap api in vsphere.
import com.budjb.requestbuilder.RequestBuilder
import com.rackspace.vmaintenance.hba.exception.RescanHbaException
import com.vmware.vim25.mo.HostSystem
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
import org.apache.log4j.Logger
/**
* Ported Michael Rice code from perl to Groovy
* for rescanHba.
* Released under the terms of the Apache-2.0 License
*/
class ResetAlarm {
/**
* Setting up logger
*/
Logger log = Logger.getLogger(RescanHbaClient)
boolean success
/**
* Resets the alarm status to green.
*
* @param alarmVal
* @param hostSystem
*/
ResetAlarm(String alarmVal, HostSystem hostSystem) throws RescanHbaException {
String entity = hostSystem.triggeredAlarmState[0].entity.val
Closure soapPayload = buildSoapPayload(alarmVal, entity)
success = makeRequest(soapPayload, hostSystem.serverConnection.url.toString(), hostSystem.serverConnection.sessionStr)
}
/**
* Builds the soap payload needed to set alarm to green.
*
* @param alarmVal
* @param entityVal
* @return
*/
Closure buildSoapPayload(String alarmVal, String entityVal) {
Closure soapPayload = {
'soap:Envelope'('xmlns:xsd': "http://www.w3.org/2001/XMLSchema", 'xmlns:xsi': "http://www.w3.org/2001/XMLSchema-instance", 'xmlns:soap': "http://schemas.xmlsoap.org/soap/envelope/") {
'soap:Body' {
vim25:SetAlarmStatus ('xmlns': "urn:vim25") {
vim25:_this ('xsi:type': "ManagedObjectReference", type: "AlarmManager", "AlarmManager")
vim25:alarm(type: "Alarm", alarmVal)
vim25:entity('xsi:type': "ManagedObjectReference", type: "HostSystem", entityVal)
vim25:status('' + "green")
}
}
}
}
return soapPayload
}
/**
* Builds and sends the request to make the soap call.
*
* @param soapPayload
* @param url
* @param cookie
* @return
*/
boolean makeRequest(Closure soapPayload, String url, String cookie) throws RescanHbaException {
StreamingMarkupBuilder builder = new StreamingMarkupBuilder()
builder.encoding = "UTF-8"
String xml = XmlUtil.serialize(builder.bind(soapPayload))
log.debug(xml)
try {
def response = new RequestBuilder().post {
ignoreInvalidSSL = true
uri = url
contentType = 'application/xml'
headers = [Cookie: cookie, SOAPAction: "urn:vim25"]
body = xml
debug = true
}
log.debug("response : ${response}")
success = true
}
catch(Exception e) {
log.error("Error occured: ${e.message}")
throw new RescanHbaException("Error occured while resetting alarm.", e)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment