ITOM Custom Pattern Operation -> Rest w/ Authorization token for Proxmox - need help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2023 01:36 PM - edited 11-08-2023 02:26 PM
I'm stumped.
I'm trying to create a pattern for proxmox similar to how RHV and other cloud discovery patterns run. Should be pretty simple, but I'm getting stuck w/ the Authorization.
To authenticate, i'm using a Proxmox token.
Everything works great when i'm doing curl at a shell/command prompt. So I know my token works.
curl -H "Athorization: PVEAPIToken=XYZ@ABC=1234567" https://my_rul/api2/json/cluster/resources?type=vm
yay, i get all my pve.
NOTE: I know I can do this with a Parse Command Output w/ curl but I'd rather not since I'd have to fiddle with a lot of mid servers and such.
But when i try to make this into a custom operation, that's where I get stuck.
1) I'm trying to use RestMessageV2() to get the job done, but I'm stuck with sn_ws being undefined. Is this because this is a "MID" script included and so I have to use HttpCallHandler?
2) If the above is not true, can someone put up a code snip to help me out? My code is more or less copied/googled from elsewhere is below. the $url gets treated like a custom operation parameter and will get manipulated later on in the pattern.
3) maybe I should just give up and make a custom operation that does curl instead?
In the end, i've seen a bunch of ways to get similar authorized http get, but this is my first crack at it for a custom operation. I've seen all sorts of ways to do this in the out of the box operations. Full blown classes like RHVApiUtil() and AVIApiUtil(), odd ball scenarios like KubernetesCurlCommand(), and then there's HttpCallHandler which wouldn't let me set the authorization header or at least I couldn't get it to work.
var proxmox_request = new sn_ws.RESTMessageV2("my_proxmox_test");
proxmox_request.setHttpMethod('get');
proxmox_request.setEndpoint($url);
proxmox_request.setRequestHeader("Authentication", "PVEAPIToken=XYZ@ABC=123456");
var response = proxmox_request.execute();
var httpResponseStatus = response.getStatusCode();
var httpResponseContentType = response.getHeader('Content-Type');
var parser = new global.JSONParser();
var parsed = {};
var httpResponseBody;
if (httpResponseStatus == 200 && httpResponseContentType == 'application/json') {
httpResponseBody = response.getBody();
parsed = parser.parse(httpResponseBody);
for (var i = 0; i < parsed.results.length; i++) { gs.print('id: ' + parsed.results[i].id) }
}
rtrn = httpResponseBody;