Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Insight Cloud API Powershell

Gary Smith June 15, 2020

Hi I am trying to import assets into Insight using there API.

I have worked out how to view objects but I'm a little lost on how to add new objects.

 

I can't work out how to attach the JSON to invoke-restMethod.

I have my sample input JSON like this.

$InputJSON = @"
{
"objectTypeId": $6,
"attributes": [{
"objectTypeAttributeId": 22,
"objectAttributeValues": [{
"Default": "$room_name"
}],
"attributes": [{
"objectTypeAttributeId": 46,
"objectAttributeValues": [{
"Default": "$calendar_name"
}]
}]
}
"@

 

And I'm trying to add it to this call.

$InsightURL = "https://insight-api.riada.io/rest/insight/1.0/object/create"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
$Headers = @{    
Authorization  = "Bearer e6292...597f"    
'Content-Type' = 'application/json'
}
$r = Invoke-RestMethod -Uri $InsightURL -Headers $headers -Method POST

Is it supposed to go in the headers? Is there a particular way to attach it?

 

Any help would be awesome or if anyone has a script example that they could show me that would be even better!

I'm using cloud version if that matters.

 

 

1 answer

0 votes
Tobias Ravenstein June 16, 2020

To append your JSON to the Post request you can use the Body parameter of Invoke-RestMethod like this:

$r = Invoke-RestMethod -Uri $InsightURL -Headers $headers -Method POST -Body $InputJSON
Gary Smith June 16, 2020

I did try that but had an error so I reduced it down to its simplest form and found out that I had

"objectTypeId": $6

instead of

"objectTypeId": 6

But I still get a error

Invoke-RestMethod : {"errorMessages":[],"errors":{"rlabs-insight-attribute-22":"At least one value must be set"}}

So this is what my object looks like. 

Annotation 2020-06-17 112216.png(Removed the link to parent object for now.

As you can see I am just going to import some zoom rooms and link the assets between the rooms, and the hardware in said room.

This is my simplified script, trying to just create a object with a 'test' label and nothing else.

$InputJSON = @"
    {
        "objectTypeId": 6,
        "attributes": [{
            "objectTypeAttributeId": 22,
            "objectAttributeValues": [{
                "Default": "Test"
            }]
        }]
    }
"@

$InsightURL = "https://insight-api.riada.io/rest/insight/1.0/object/create"


[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;


$Headers = @{    
Authorization  = "Bearer e62...597f"
    'Content-Type' = 'application/json'
}


Invoke-RestMethod -Uri $InsightURL -Headers $Headers -Body $InputJSON -Method POST

Tobias Ravenstein June 17, 2020

I got it working on a completly fresh cloud instance, so my id's are 1 fore the objecttype and 2 for the atrribute.

Here is my JSON:

$InputJSON = @"
{
"objectTypeId": 1,
"attributes": [{
"objectTypeAttributeId": 2,
"objectAttributeValues": [{
"value": "Demo"
}]
}]
}
"@

You need to write

"value": "Demo"

"value" instead of "Default" <- this in the documentation is reffering to the type of the attribute which can be a "default" like text, interger, float and so on or some special attribute like a jira user or project.

And here is the call which was successful:

$InsightURL = "https://insight-api.riada.io/rest/insight/1.0/object/create"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
$Headers = @{
Authorization = "Bearer 6ed...0a85f"
'Content-Type' = 'application/json'
}

$r = Invoke-RestMethod -Uri $InsightURL -Headers $headers -Method POST -Body $InputJSON

Hope this will help you.

Regards
Tobias

Like Gary Smith likes this
Gary Smith June 17, 2020

Thank you so much!
Been going crazy trying to work it out.

I looked back at my revisions because I was sure that I used what they had and I had capitalized 'Value'.

This will get me through the eval stage to decide if we want to run with it, but i am not looking forward to writing these scripts where i need to work off the ID instead of the name.

Might have to build a module if we go with this application.

Again, thanks heaps for your help.

Tobias Ravenstein June 18, 2020

I am thinking about to write my own groovy module to get and set values. In groovy (for the server version) it is very hard for beginners to understand the connection between the different "Beans" you have to use.

If you want to stay in the powershell environment, do you know https://atlassianps.org/ ?
Maybe you can start using this and extend this one for insight.

Gary Smith June 30, 2020

Sorry not sure why I have only just seen your reply.
I will stay in PS, I have seen that module but I was thinking of creating my one for insight which someone else could then merge. I'll link here if we get the green light to use jira

Deleted user September 7, 2020

Hey there,

I've started working on the same requirements, but getting a 415 error.

I've repeated your example.

Did you ever come across this?

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events