Forum Discussion
Chengjie_Zhu
7 years agoFormer Employee
Hi Sarah,
Thanks a lot for sharing it.
I suppose I have made it work.
import org.apache.http.client.methods.HttpGet import org.apache.http.client.methods.HttpPut 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 groovy.json.*; import org.apache.http.entity.StringEntity; import org.apache.http.entity.ContentType; //define credentials and url def accessId = 'xxx'; def accessKey = 'xxx'; def account = 'xxx';
// Change GroupID // Please note: It does not work on the group's child groups. // Only works on devices in the parent group. def GroupID = 'xxx'
def resourcePath = "/device/groups/" + GroupID+ "/devices" def queryParams ='' def url = "https://" + account + ".logicmonitor.com" + "/santaba/rest" + resourcePath + queryParams def deviceID = [] //get current time epoch = System.currentTimeMillis(); //calculate signature requestVars = "GET" + epoch + resourcePath; 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(); // HTTP Get CloseableHttpClient httpclient = HttpClients.createDefault(); httpGet = new HttpGet(url); httpGet.addHeader("Authorization" , "LMv1 " + accessId + ":" + signature + ":" + epoch); response = httpclient.execute(httpGet); responseBody = EntityUtils.toString(response.getEntity()); JsonSlurper slurper = new JsonSlurper() def json = slurper.parseText(responseBody) def items = json.data.items // Iterate all of devices in the parent group // Please note: It does not work on the group's child groups. for (i=0; i<items.size(); i++){ deviceID.add(items[i].id) // You can customize following line whatever you like. At this example, I made Displayname to Displayname + "@logicmontior.com": items[i].displayName = items[i].displayName + "@logicmonitor.com"; items[i] = JsonOutput.toJson(items[i]) StringEntity params = new StringEntity(items[i],ContentType.APPLICATION_JSON); epoch = System.currentTimeMillis(); //get current time resourcePath = "/device/devices/" + deviceID[i] requestVars = "PUT" + epoch + items[i] + 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(); // HTTP PUT url = "https://" + account + ".logicmonitor.com" + "/santaba/rest" + resourcePath; httpPut = new HttpPut(url); httpPut.addHeader("Authorization" , "LMv1 " + accessId + ":" + signature + ":" + epoch); httpPut.setHeader("Accept", "application/json"); httpPut.setHeader("Content-type", "application/json"); httpPut.setEntity(params); responsePut = httpclient.execute(httpPut); responseBodyPut = EntityUtils.toString(responsePut.getEntity()); codePut = responsePut.getStatusLine().getStatusCode(); // Print Response println "Status:" + codePut; println "Response body:" + responseBodyPut; } httpclient.close();
Related Content
- 7 months ago
- 6 months ago
- 11 months ago