Forum Discussion
Joe_Tran
7 years agoAdvisor
I chain two propertysources together--one to set an autoProp, and another to write that autoProp to location via REST API. The location property isn't prefaced by system, so it can't be set in just the one propertySource--i don't think.
In the example below, the autoProp is called "auto.loc".
import org.apache.http.HttpEntity import org.apache.http.client.methods.CloseableHttpResponse import org.apache.http.client.methods.HttpPatch import org.apache.http.impl.client.CloseableHttpClient import org.apache.http.impl.client.HttpClients import org.apache.http.util.EntityUtils import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Hex; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import groovy.json.JsonSlurper import groovy.json.JsonOutput import org.xbill.DNS.* accessId = hostProps.get('lm.api.id') accessKey = hostProps.get('lm.api.key') company = 'mycompany' resourcePath = '/device/devices/' + hostProps.get('system.deviceId') url = 'https://' + company + '.logicmonitor.com/santaba/rest' + resourcePath + "?patchFields=customProperties&opType=replace" location = hostProps.get('auto.loc') customProperties = [name: 'location', value: location] data = [customProperties: [customProperties]] jsonData = JsonOutput.toJson(data) StringEntity payloadEntity = new StringEntity(jsonData) epoch = System.currentTimeMillis() requestVars = "PATCH" + epoch + jsonData + resourcePath // construct signature hmac = Mac.getInstance("HmacSHA256"); secret = new SecretKeySpec(accessKey.getBytes(), "HmacSHA256") hmac.init(secret) hmac_signed = Hex.encodeHexString(hmac.doFinal(requestVars.getBytes())) signature = hmac_signed.bytes.encodeBase64() CloseableHttpClient httpclient = HttpClients.createDefault(); http_request = new HttpPatch(url); http_request.addHeader("Authorization" , "LMv1 " + accessId + ":" + signature + ":" + epoch); http_request.setHeader("Accept", "application/json"); http_request.setHeader("Content-type", "application/json"); http_request.setEntity(payloadEntity); response = httpclient.execute(http_request); responseBody = EntityUtils.toString(response.getEntity()); code = response.getStatusLine().getStatusCode(); return(0)
Related Content
- 2 years ago
- 9 months ago