Forum Discussion

Ian_Fraser's avatar
2 years ago

API Patch for CustomProperties

Hi Guys

I'm trying to write in customproperties to a device as follows:

I can correctly patch the displayname, but the customproperties hastable is not being written. I get a success 200 response back from my invoke-restmethod however.

Ive tried with the customproperties hash table seperate to displayname but i cant get this working.

Does anyone have any ideas on this?

NOTE: xxx is the correct ID

Thanks

 
$company = 'xxx'
$accessId = 'xxx'
$accessKey = 'xxx
$computername = "$env:COMPUTERNAME"
$httpVerb_04 = 'PATCH'
$resourcePath_04 = "/device/devices/xxx"
$patchfields = '?patchFields=displayName,customproperties&opType=add'
 
$body = @{
    'displayName' = "$computername"
        customproperties = @{
        name='srv.location'
        value='test'
        }
    }
 
$body = $body | convertto-json
 
<# Construct URL #>
$url_04 = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath_04 + $patchfields
 
<# Get current time in milliseconds #>
$epoch_04 = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end (Get-Date).ToUniversalTime()).TotalMilliseconds)
 
<# Concatenate Request Details #>
$requestVars_04 = $httpVerb_04 + $epoch_04 + $body + $resourcePath_04
 
<# Construct Signature #>
$hmac_04 = New-Object System.Security.Cryptography.HMACSHA256
$hmac_04.Key = [Text.Encoding]::UTF8.GetBytes($accessKey)
$signatureBytes_04 = $hmac_04.ComputeHash([Text.Encoding]::UTF8.GetBytes($requestVars_04))
$signatureHex_04 = [System.BitConverter]::ToString($signatureBytes_04) -replace '-'
$signature_04 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($signatureHex_04.ToLower()))
 
<# Construct Headers #>
$auth_04 = 'LMv1 ' + $accessId + ':' + $signature_04 + ':' + $epoch_04
$headers_04 = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers_04.Add("Authorization",$auth_04)
$headers_04.Add("Content-Type",'application/json')
 
<# Make Request #>
$response_04 = Invoke-RestMethod -Uri $url_04 -Method patch -body $body -Header $headers_04
 
  • Anonymous's avatar
    Anonymous

    Custom properties needs to be a list of hash tables. Not a single hash table. Be aware, if you do this, you are potentially erasing all other custom properties on the device and replacing with this one. I would fetch the current custom properties, add my property in, then patch the whole thing. Alternatively, you could do a PUT on /device/devices/{deviceId}/properties/{name}, which will ensure you're only touching that one property. PUT will create it if it doesn't exist, update it if it does exist.

    Also, I've had inconsistent behavior around the opType query parameter (I also use the SDK a lot and that may be where the inconsistency lies).