It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Cannot use Responder or Tags in API to Create an ops genie alert

Using powershell to create an alert in OpsGenie via the API, and I keep getting "The remote server returned an error: (422) Unprocessable Entity"

This only happens when I attempt to use the tags or responders fields in the body. If I leave those out, I can successfully create alerts. What am I doing wrong with the formatting??


$body = @{            
message     = "MESSAGE TITLE HERE"            
description = "DESCRIPTION HERE"            
priority    = "P5"            
alias       = "XXXXXXX"            
tags        = "tag"            
responders  = {                
name = 
"TEAMNAMEHERE"                
type = 
"team"            
}
        
} | 
ConvertTo-Json

If I remove the tags and responders fields, this works perfectly.

1 answer

2 votes

Hi good morning,

It would appear that tags and responders are being defined as incorrect data types, "tags" should be an array that accepts a list of values, while responders should be an array that accepts a list of objects.

It also would appear that you are creating a lot of unnecessary work for yourself by first constructing the payload $body as a PsObject then converting it to a JSON. The most efficient method would be to store your JSON (in a JSON format) and store it in $body as a literal string, please see my example below.  Please note that no conversion is needed $body already contains a valid JSON

$body = '{
"message": "MESSAGE TITLE HERE",
"description":"DESCRIPTION HERE",
"alias": "XXXXXXX",
"priority":"P5",
"responders":[{"id":"TEAMNAMEHERE", "type":"team"}],
"tags": ["Tag1","Tag2"]
}'

I've formatted tags as an array with @{"tag1", "tag2"}. I prefer the convert-to method to keep my syntax uniform throughout my scripts.

I'll give the JSON formatting a try and let you know if that works.

This worked! Strange the convertto-json didn't format the request correctly.

One other question...how do I insert a variable? I have a list of servers I want to pass as a note.

adding this works just fine...but looks like it tries to read my variable as a string and just sends the variable's name.

"note": "TEXT"

Hi Dlehr, my apologies for delayed response I have been on annual leave, could you please provide an example of how you are constructing the payload so I can test it in my lab?



$body
 = '{
    "message": "servers",
    "description": "server list",
    "alias": "xxx",
    "priority": "P5",
    "responders": [
        {            
"id": "myteam",
            "type": "team"
        }
    ],
    "tags": [
        "tag1", "tag2"
    ],
    "note": $MyVariable}'
}'



This will only return the actual string saying $MyVariable even though its a list of servers

Hi Dlehr,

Since we are constructing the JSON as a literal string in Powershell, you will need to use string concatenation to input dynamic variables as part of the constructed literal string.

 

This payload works in my lab.

 $body = ('{
"message": "servers",
"description": "server list",
"alias": "xxx",
"priority": "P5",
"responders": [
{
"id": "myteam",
"type": "team"
}
],
"tags": [
"tag1", "tag2"
],
"note": "' + $MyVariable + '"
}')

 

Please note that the literal string is now contained between an opening and a closing )

Please also note how I have encapsulated the variable within double-quotes within the literal string.

"' + $MyVariable + '"
$body = @{
            message     = "servers"
            description = "list of servers"
            priority    = "P5"
            alias       = "XXXX"
            note        = $MyVariable
} | ConvertTo-Json


Previously, I used this format which worked except when I attempted to use the responders and tag fields. I was able to send my variable (list of servers) using this format before and it displayed correctly by displaying the servers in the $myvariable powershell object.

 



Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData

Using the literal JSON format method as you showed up there, it does try to send an object this time instead of just the variable name, but it's more like the object typename and not the object itself. The above is what's shown in the opsgenie notes section using your formatting above.

I'd prefer not to have to use pure JSON syntax and have my original $body formatting work where I convertto-json...but using responder and tag fields i would get an error from opsgenie

Hi Dlehr good morning, 

Can you please provide an example of the $body PsObject with Responders and tags included in the object.  Can you also provide an output of that $body after it has been piped to ConvertTo-Json that way you can view the converted JSON in your shell.  I would like to see the how ConvertTo-Json is constructing the JSON so I can run a validation check to make sure it compliant with RFC 8259 JSON standard.

Please note we only support JSON paylaods that compliant with RFC xxxx JSON standards, please further note that we only support JSON formats we do not support the conversion of JSON from other objects such as PsObject.

Please see the screenshot of a working example of the syntax that I provided my previous message, as you can see the API accepts the request with a HTTP status of 202 and returns a JSON which confirms the request will be processed.

PowershellExample.PNG

And here is the resulting Alert in OpsGenie, with responders assigned and the $MyVariable value string correctly displayed in the notes.

ResultingAlert.PNG

$Compare = Compare-Object -ReferenceObject $AllVMNames -DifferenceObject $TaggedVMsNames |
 Where-Object {$_.SideIndicator -eq "<="} |
 Select-Object -ExpandProperty InputObject |
 Sort-Object |
 Format-Table



#this is my new working syntax

$body
 = @{
            message     = "servers"
            description = "list of servers"
            alias       = "XXXX"
            priority    = "P5"
            responders  = @(
                @{
                    id      = "myteam"
                    type    = "team"
                }
            )
            tags        = @(
                "tag1"
                "tag2"
            )
            note        = $Compare
        } | ConvertTo-Json
#output from $compare

server1
server2
server3
server4
server5


2020-02-27 13_19_20-● nsx_alert_no_tag.ps1 - powershell scripts - Visual Studio Code [Administrator].jpg

so I went back to the drawing board determined to make my powershell syntax work so I didn't have to deal with JSON and figured it out.

However...I'm getting a 422 (unprocessable Entity) error from Opsgenie when I sub in $Compare for any string. It doesn't like the object being given. 

So I made one other change that made this work. I piped $Compare | Out-String. This only allowed it to get input in the Opsgenie notes section as a list of server names.

 

It still does not appear anywhere in the Jira Service Desk ticket despite a rule in the Opsgenie to JSD integration that would lead me to believe it should. 

2020-02-27 16_25_39-Opsgenie - Integration.png

Hi Dlehr,

 

Glad you now have the add note working on Opsgenie, I will need access to your Opsgenie instance and logs.  For security reasons for me to be able to request access to your logs I need you to raise a support ticket.  The most convenient way for you to do this is to log into https://app.opsgenie.com, then once you are logged in use the chat bubble to start a chat this will automatically open a support ticket in our system. If you provide the Chat Support Engineer with a link to this community post you may request the chat be escalated directly to me or another Senior Support Engineer in your region.  

If you prefer a more traditional support method than chat you can log into this page and support a ticket for the Opsgenie product https://support.atlassian.com/contact/#/

 

Thanks and kind regards,

 

Allen

Support has confirmed Opsgenie notes not appearing in Jira service desk ticket is an issue in their end and has elevated my issue

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Opsgenie

A deep dive on Routing Rules

  Hi there! Folks new to Opsgenie sometimes can get a bit perplexed when setting up their Routing Rules and Escalation Policies. Especially if they’re used to using an excel spreadsheet for sc...

183 views 0 2
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you