OpsGenie Go API

The OpsGenie SDK for Go provides a set of Go API for OpsGenie services, making it easier for go developers to build applications that integrate with OpsGenie. Developers can build Go applications on top of APIs that take the complexity out of coding directly against a web service interface. The library provides APIs that hide much of the lower-level plumbing, including authentication, request retries, and error handling.

Before you can begin, you must sign up for OpsGenie service, create an API Integration and get your API key. In order to use the OpsGenie Go SDK, you will need the API key from the API Integration you created. The API key is used to authenticate requests to the service and identify yourself as the sender of a request.

Go SDK Installation

Golang SDK requires Go version 1.4.1 or above. Golang can be downloaded here. One should make sure the GOPATH and GOROOT variables are set accordingly. Following that the API source can be downloaded via command go get github.com/opsgenie/opsgenie-go-sdk/.... The command will automatically download and install necessary package files and dependencies under the src directory of the GOPATH.

OpsGenie Go Sdk source code is available at GitHub OpsGenie Go Sdk repository.

Client Initialization

There are packages required to interact with the API. Client packages is required to use All APIs. Alert, Heartbeat, Integration and Policy packages should be added according to the requirement.

import (
  alerts "github.com/opsgenie/opsgenie-go-sdk/alerts"
  ogcli "github.com/opsgenie/opsgenie-go-sdk/client"
  "github.com/opsgenie/opsgenie-go-sdk/heartbeat"
  "github.com/opsgenie/opsgenie-go-sdk/integration"
  "github.com/opsgenie/opsgenie-go-sdk/policy"
)

Client is the basic data structure on which all operations and actions are taken. A client variable can be created as following.

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key here")

It is required to set the API key before going on subsequent calls. Otherwise the authentication will fail and the API is going to raise an exception.

Import statements

import (
  alerts "github.com/opsgenie/opsgenie-go-sdk/alerts"
  ogcli "github.com/opsgenie/opsgenie-go-sdk/client"
 )
    

Alert API

Create Alert

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}

// create the alert
req := alerts.CreateAlertRequest{ Message: "appserver1 down",
                                  Description: "cpu usage is over 60%",
                                  Source: "nagios",
                                  Entity: "appserver1",
                                  Actions: []string{"ping", "restart"},
                                  Tags: []string{"network", "operations"},
                                  Recipients: []string{"john.smith@acme.com", "admin@acme.com"},
                                }
response, alertErr := alertCli.Create(req)

if alertErr != nil {
  panic(alertErr)
}

fmt.Printf("message: %s\n", response.Message)
fmt.Printf("alert id: %s\n", response.AlertID)
fmt.Printf("status: %s\n", response.Status)
fmt.Printf("code: %d\n", response.Code)
        

Close Alert

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}

// close the alert
cloreq := alerts.CloseAlertRequest{ ID: "alert id here",
                                    Notify: []string{"john.smith@acme.com", "admin@acme.com"},
                                  }
cloresponse, alertErr := alertCli.Close(cloreq)

if alertErr != nil {
  panic(alertErr)
}

fmt.Printf("status: %s\n", cloresponse.Status)
fmt.Printf("code: %d\n", cloresponse.Code)
  

Delete Alert

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}
// delete the alert
delreq := alerts.DeleteAlertRequest{ID: "alert id here", Source: "Go API" }
deleteResponse, alertErr := alertCli.Delete(delreq)

if alertErr != nil {
  panic(alertErr)
}

fmt.Printf("status: %s\n", deleteResponse.Status)
fmt.Printf("code: %d\n", deleteResponse.Code)

Get Alert

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}
// get the alert
getreq := alerts.GetAlertRequest{ID: "alert id here"}
getresponse, alertErr := alertCli.Get(getreq)
if alertErr != nil {
  panic(alertErr)
}

fmt.Printf("tags: %v\n", getresponse.Tags)
fmt.Printf("count: %d\n", getresponse.Count)
fmt.Printf("teams: %v\n", getresponse.Teams)
fmt.Printf("recipients: %v\n", getresponse.Recipients)
fmt.Printf("tiny id: %s\n", getresponse.TinyID)
fmt.Printf("alias: %s\n", getresponse.Alias)
fmt.Printf("entity: %s\n", getresponse.Entity)
fmt.Printf("id: %s\n", getresponse.ID)
fmt.Printf("updated at: %d\n", getresponse.UpdatedAt)
fmt.Printf("message: %s\n", getresponse.Message)
fmt.Printf("details: %v\n", getresponse.Details)
fmt.Printf("source: %s\n", getresponse.Source)
fmt.Printf("description: %s\n", getresponse.Description)
fmt.Printf("created at: %d\n", getresponse.CreatedAt)
fmt.Printf("is seen?: %t\n", getresponse.IsSeen)
fmt.Printf("acknowledged?: %t\n", getresponse.Acknowledged)
fmt.Printf("owner: %s\n", getresponse.Owner)
fmt.Printf("actions: %s\n", getresponse.Actions)
fmt.Printf("system data: %v\n", getresponse.SystemData)

List Alerts

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}
// list the alerts
listreq := alerts.ListAlertsRequest{  CreatedAfter: 1351634560000000,
                                      Status: "acked",
                                      Teams: []string{"team1", "team2"},
                                      Tags: []string{"tag1", "tag2"},
                                      TagsOperator: "or"},
                                    }
listresp, listErr := alertCli.List(listreq)

if listErr != nil {
  panic(listErr)
}

for _, alert := range listresp.Alerts {
    fmt.Printf("Id: %s\n", alert.ID)
    fmt.Printf("Alias: %s\n", alert.Alias)
    fmt.Printf("Message: %s\n", alert.Message)
    fmt.Printf("Status: %s\n", alert.Status)
    fmt.Printf("IsSeen?: %t\n", alert.IsSeen)
    fmt.Printf("Acknowledged?: %t\n", alert.Acknowledged)
    fmt.Printf("Created at: %d\n", alert.CreatedAt)
    fmt.Printf("Updated at: %d\n", alert.UpdatedAt)
    fmt.Printf("Tiny id: %s\n", alert.TinyID)
    fmt.Printf("Owner: %s\n", alert.Owner)
}

Count Alerts

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")

alertCli, cliErr := cli.Alert()

if cliErr != nil {
  panic(cliErr)
}
// count the alerts
countreq := alerts.CountAlertsRequest{  CreatedAfter: 1351634560000000,
                                        Status: "acked",
                                        Tags: []string{"tag1", "tag2"},
                                        TagsOperator: "or"},
                                     }
countresp, countErr := alertCli.Count(countreq)

if countErr != nil {
  panic(countErr)
}
fmt.Printf("Count: %d\n", countresp.Count)


List Alert Notes

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}
// list the alert notes
listNotesReq := alerts.ListAlertNotesRequest{ID: "alert id"}
listNotesResponse, alertErr := alertCli.ListNotes(listNotesReq)

if alertErr != nil {
  panic(alertErr)
}

alertNotes := listNotesResponse.Notes

fmt.Printf("Last key: %s\n", listNotesResponse.LastKey)

for _, note := range alertNotes {
    fmt.Printf("Note: %s\n", note.Note)
    fmt.Printf("Owner: %s\n", note.Owner)
    fmt.Printf("Created at: %d\n", note.CreatedAt)
}  

List Alert Logs

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}
// list alert activity notes
getLogsReq := alerts.ListAlertLogsRequest{ID: "alert id"}
getLogsResponse, alertErr := alertCli.ListLogs(getLogsReq)
if alertErr != nil {
  panic(alertErr)
}

logs := getLogsResponse.Logs
for _, log := range logs {
    fmt.Printf("Owner: %s\n", log.Owner)
    fmt.Printf("Log: %s\n", log.Log)
    fmt.Printf("Log type: %s\n", log.LogType)
    fmt.Printf("Created at: %d\n", log.CreatedAt)
}  

List Alert Recipients

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}
// list alert recipients
getRecipientsReq := alerts.ListAlertRecipientsRequest{ID: "alert id"}
getRecipientsResponse, alertErr := alertCli.ListRecipients(getRecipientsReq)

if alertErr != nil {
  panic(alertErr)
}

fmt.Printf("Users:  %v\n", getRecipientsResponse.Users)
fmt.Printf("Groups:  %v\n", getRecipientsResponse.Groups)

Acknowledge

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")

alertCli, cliErr := cli.Alert()

if cliErr != nil {
  panic(cliErr)
}

// acknowledge the alert
ackReq := alerts.AcknowledgeAlertRequest{ ID: "alert id" }
ackResponse, alertErr := alertCli.Acknowledge(ackReq)
if alertErr != nil {
  panic(alertErr)
}

fmt.Printf("status: %s\n", ackResponse.Status)
fmt.Printf("code: %d\n", ackResponse.Code)

Unacknowledge

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}

//unacknowledge the alert

unAckReq := alerts.UnAcknowledgeAlertRequest{ ID: "alert id" }
unAckResponse, unAckErr := alertCli.UnAcknowledge(unAckReq)
if unAckErr != nil {
    panic(unAckErr)
}

fmt.Printf("status: %s\n", unAckResponse.Status)
fmt.Printf("code: %d\n", unAckResponse.Code)
fmt.Printf("took: %d\n", unAckResponse.Took)

Snooze

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")

alertCli, cliErr := cli.Alert()

if cliErr != nil {
  panic(cliErr)
}

//snooze the alert

snoozeReq := alerts.SnoozeAlertRequest{ID: "alert id", EndDate: "end date"}
snoozeResp, snoozeErr := alertCli.Snooze(snoozeReq)
if snoozeErr != nil{
    panic(snoozeErr)
}

fmt.Printf("status: %s\n", snoozeResp.Status)
fmt.Printf("code: %s\n", snoozeResp.Code)

Renotify

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}
// renotify
renReq := alerts.RenotifyAlertRequest{ ID: "alert id" }
renResponse, alertErr := alertCli.Renotify(renReq)
if alertErr != nil {
  panic(alertErr)
}

fmt.Printf("status: %s\n", renResponse.Status)
fmt.Printf("code: %d\n", renResponse.Code)

Take Ownership

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}
// take ownership of the alert
takeOwnershipReq := alerts.TakeOwnershipAlertRequest{ ID: "alert id" }
takeOwnershipResponse, alertErr := alertCli.TakeOwnership(takeOwnershipReq)

if alertErr != nil {
  panic(alertErr)
}

fmt.Printf("status: %s\n", takeOwnershipResponse.Status)
fmt.Printf("code: %d\n", takeOwnershipResponse.Code)

Assign Owner

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}
// assign the owner for the alert
assignOwnerReq := alerts.AssignOwnerAlertRequest{ ID: "alert id", Owner: "john.smith@acme.com", }
assignOwnerResponse, alertErr := alertCli.AssignOwner(assignOwnerReq)

if alertErr != nil {
  panic(alertErr)
}

fmt.Printf("status: %s\n",  assignOwnerResponse.Status)
fmt.Printf("code: %d\n",  assignOwnerResponse.Code)

Add Team

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}
// add team
addTeamReq := alerts.AddTeamAlertRequest{ ID: "alert id", Team: TEAM_NAME, }
addTeamResponse, alertErr := alertCli.AddTeam(addTeamReq)

if alertErr != nil {
  panic(alertErr)
}

fmt.Printf("status: %s\n",  addTeamResponse.Status)
fmt.Printf("code: %d\n",  addTeamResponse.Code)

Add Recipient

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}
// add recipient
addRecipientReq := alerts.AddRecipientAlertRequest{ ID: "alert id", Recipient: "recipient", }
addRecipientResponse, alertErr := alertCli.AddRecipient(addRecipientReq)

if alertErr != nil {
  panic(alertErr)
}

fmt.Printf("status: %s\n",  addRecipientResponse.Status)
fmt.Printf("code: %d\n",  addRecipientResponse.Code)

Add Note

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")

alertCli, cliErr := cli.Alert()

if cliErr != nil {
  panic(cliErr)
}

// add a note
addnotereq := alerts.AddNoteAlertRequest{ID: "alert id", Note: "some note"}
addnoteresp, alertErr := alertCli.AddNote(addnotereq)
if alertErr != nil {
  panic(alertErr)
}
fmt.Printf("[Add note] %s %d\n", addnoteresp.Status, addnoteresp.Code)

Add Tags

//initialize the client
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey(constants.APIKey)

alertCli, cliErr := cli.Alert()

if cliErr != nil {
    panic(cliErr)
}

// add tags to the alert
addTagsReq := alerts.AddTagsAlertRequest{ID: "alert id", Tags: []string{"tag1","tag2"}}
addTagsResponse, alertErr := alertCli.AddTags(addTagsReq)

if alertErr != nil {
    panic(alertErr)
}

fmt.Printf("status: %s\n", addTagsResponse.Status)
fmt.Printf("code: %d\n", addTagsResponse.Code)

Remove Tags

//initialize the client
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey(constants.APIKey)

alertCli, cliErr := cli.Alert()

if cliErr != nil {
    panic(cliErr)
}

// remove tags from the alert
removeTagsReq := alerts.RemoveTagsAlertRequest{ID: "alert id", Tags: []string{"tag1","tag2"}}
removeTagsResponse, removeErr := alertCli.RemoveTags(removeTagsReq)

if removeErr != nil {
    panic(removeErr)
}

fmt.Printf("status: %s\n", removeTagsResponse.Status)
fmt.Printf("code: %d\n", removeTagsResponse.Code)

Add Details

//initialize the client
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey(constants.APIKey)

alertCli, cliErr := cli.Alert()

if cliErr != nil {
    panic(cliErr)
}

// add details to the alert
addDetailsReq := alerts.AddDetailsAlertRequest{ID: "alert id", Details:	map[string]string{"detail1" : "detail1", "detail2" : "detail2"}}
addDetailsResp, addDetailsErr := alertCli.AddDetails(addDetailsReq)
if addDetailsErr != nil{
    panic(addDetailsErr)
}

fmt.Printf("status: %s\n", addDetailsResp.Status)
fmt.Printf("code: %d\n", addDetailsResp.Code)

Remove Details

//initialize the client
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey(constants.APIKey)

alertCli, cliErr := cli.Alert()

if cliErr != nil {
    panic(cliErr)
}

// remove details from the alert
removeDetailsReq := alerts.RemoveDetailsAlertRequest{ID: "alert id", Keys: []string{"detail1", "detail2"}}
removeDetailsResp, removeDetailsErr := alertCli.RemoveDetails(removeDetailsReq)
if removeDetailsErr != nil{
    panic(removeDetailsErr)
}

fmt.Printf("status: %s\n", removeDetailsResp.Status)
fmt.Printf("code: %d\n", removeDetailsResp.Code)

Execute Action

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}
// execute sample 'ping' action for the alert
execActionReq := alerts.ExecuteActionAlertRequest{  ID: "alert id",
                                                    Action: "ping", 
                                                    Note: "Action 'ping' was executed by the Go API" },
execActionResponse, alertErr := alertCli.ExecuteAction(execActionReq)

if alertErr != nil {
  panic(alertErr)
}

fmt.Printf("status: %s\n",  execActionResponse.Result)
fmt.Printf("code: %d\n",  execActionResponse.Code)

Attach File

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
  
alertCli, cliErr := cli.Alert()
  
if cliErr != nil {
  panic(cliErr)
}
// attach the file
attachFileReq := alerts.AttachFileAlertRequest{ ID: "alert id",
                                                Attachment: "/path/to/file", }
attachFileResp, attachFileErr := alertCli.AttachFile( attachFileReq )

if attachFileErr != nil {
  panic(attachFileErr)
}

fmt.Printf("Status: %s\n",  attachFileResp.Status)
fmt.Printf("Code: %d\n",    attachFileResp.Code)

Escalate To Next

// initialize the client
cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")

alertCli, cliErr := cli.Alert()

if cliErr != nil {
  panic(cliErr)
}
// escalate to next
escalateToNextReq := alerts.EscalateToNextAlertRequest{ID: createResp.AlertID, EscalationName: constants.EscalationName}
escalateToNextResp, escalateToNextErr := alertCli.EscalateToNext(escalateToNextReq)
if escalateToNextErr != nil{
    panic(escalateToNextErr)
}

fmt.Printf("status: %s\n", escalateToNextResp.Status)
fmt.Printf("code: %d\n", escalateToNextResp.Code)

Heartbeat API

Import statements

import (
  hb "github.com/opsgenie/opsgenie-go-sdk/heartbeat"
  ogcli "github.com/opsgenie/opsgenie-go-sdk/client"
 )
 

Add heartbeat

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")

hbCli, cliErr := cli.Heartbeat()

if cliErr != nil {
panic(cliErr)
}

// create the hb
req := hb.AddHeartbeatRequest{Name: "name of the heartbeat" }
response, hbErr := hbCli.Add(req)

if hbErr != nil {
panic(hbErr)
}

fmt.Printf("name: %s\n",    response.Name)
fmt.Printf("status: %s\n",  response.Status)
fmt.Printf("code: %d\n",  response.Code)
 

Update heartbeat

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")

hbCli, cliErr := cli.Heartbeat()

if cliErr != nil {
  panic(cliErr)
}

// update 
updateReq := hb.UpdateHeartbeatRequest{
                                        Name: "heartbeat name",
                                        Description: "updated description",
                                      }
updateResp, updateErr := hbCli.Update(updateReq)

if updateErr != nil {
  panic(updateErr)
}
fmt.Printf("name: %s\n",    updateResp.Name)
fmt.Printf("status: %s\n",  updateResp.Status)
fmt.Printf("code: %d\n",  updateResp.Code)

Enable heartbeat

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")

hbCli, cliErr := cli.Heartbeat()

if cliErr != nil {
  panic(cliErr)
}
// enable the hb
enableReq := hb.EnableHeartbeatRequest{Name: "heartbeat name"}
enableResp, enableErr := hbCli.Enable( enableReq )

if enableErr != nil {
  panic(enableErr)
}

fmt.Printf("Status: %s\n",  enableResp.Status)
fmt.Printf("Code: %d\n",    enableResp.Code)

Disable heartbeat

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")

hbCli, cliErr := cli.Heartbeat()

if cliErr != nil {
  panic(cliErr)
}
// disable
disableReq := hb.DisableHeartbeatRequest{Name: "heartbeat name"}
disableResp, disableErr := hbCli.Disable( disableReq )

if disableErr != nil {
  panic(disableErr)
}

fmt.Printf("Status: %s\n",  disableResp.Status)
fmt.Printf("Code: %d\n",    disableResp.Code)

Delete heartbeat

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")

hbCli, cliErr := cli.Heartbeat()

if cliErr != nil {
  panic(cliErr)
}
// delete the hb
deleteReq := hb.DeleteHeartbeatRequest{Name: "heartbeat name}
deleteResp, deleteErr := hbCli.Delete( deleteReq )

if deleteErr != nil {
  panic(deleteErr)
}

fmt.Printf("Status: %s\n",  deleteResp.Status)
fmt.Printf("Code: %d\n",    deleteResp.Code)

Get heartbeat

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")

hbCli, cliErr := cli.Heartbeat()

if cliErr != nil {
  panic(cliErr)
}

// get the heartbeat
getReq := hb.GetHeartbeatRequest{Name: "heartbeat name"}
getResp, getErr := hbCli.Get( getReq )

if getErr != nil {
  panic(getErr)
}

fmt.Printf("Name: %s\n", getResp.Name)
fmt.Printf("Status: %s\n", getResp.Status)
fmt.Printf("Description: %s\n", getResp.Description)
fmt.Printf("Enabled?: %t\n", getResp.Enabled)
fmt.Printf("Last Heartbeat: %d\n", getResp.LastHeartbeat)
fmt.Printf("Interval: %d\n", getResp.Interval)
fmt.Printf("Interval Unit: %s\n", getResp.IntervalUnit)
fmt.Printf("Expired?: %t\n", getResp.Expired)

List heartbeats

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")

hbCli, cliErr := cli.Heartbeat()

if cliErr != nil {
  panic(cliErr)
}
// list the HBs
listReq := hb.ListHeartbeatsRequest{}
listResp, listErr := hbCli.List(listReq)
if listErr != nil {
  panic(listErr)
}

beats := listResp.Heartbeats
for _, beat := range beats {
    fmt.Printf("Name: %s\n", beat.Name)
    fmt.Printf("Status %s\n", beat.Status)
    fmt.Printf("Description: %s\n", beat.Description)
    fmt.Printf("Enabled?: %t\n", beat.Enabled)
    fmt.Printf("Last Heartbeat: %d\n", beat.LastHeartbeat)
    fmt.Printf("Interval: %d\n", beat.Interval)
    fmt.Printf("Interval Unit: %s\n", beat.IntervalUnit)
    fmt.Printf("Expired?: %t\n", beat.Expired)
}  

Send heartbeat

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")

hbCli, cliErr := cli.Heartbeat()

if cliErr != nil {
  panic(cliErr)
}
// send heart beat request
sendReq := hb.SendHeartbeatRequest{Name: hbName}
sendResp, sendErr := hbCli.Send(sendReq)

if sendErr != nil {
  panic(sendErr)
}

fmt.Printf("Heartbeat: %d\n", sendResp.Heartbeat)
fmt.Printf("Will expire at: %d\n", sendResp.WillExpireAt)
fmt.Printf("Status: %s\n", sendResp.Status)
fmt.Printf("Code: %d\n", sendResp.Code)

Policy API

Import statements
import(
  policy "github.com/opsgenie/opsgenie-go-sdk/policy"
  ogcli "github.com/opsgenie/opsgenie-go-sdk/client"
)  

Enable policy

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
polCli, cliErr := cli.Policy()

if cliErr != nil {
  panic(cliErr)
}
// enable the policy
req := policy.EnablePolicyRequest{Name: "policy name"}

_, err = polCli.Enable(req)

if err != nil {
  panic(err)
} else { 
  fmt.Printf("Policy enabled successfuly")
}

Disable policy

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
polCli, cliErr := cli.Policy()

if cliErr != nil {
  panic(cliErr)
}
// enable the policy
req := policy.DisablePolicyRequest{Name: "policy name"}

_, err = polCli.Disable(req)

if err != nil {
  panic(err)
} else { 
  fmt.Printf("Policy disabled successfuly")
}

Integration API

Import statements

import(
  integration "github.com/opsgenie/opsgenie-go-sdk/integration"
  ogcli "github.com/opsgenie/opsgenie-go-sdk/client"
)  

Enable integration

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey(apiKey)
intCli, cliErr := cli.Integration()

if cliErr != nil {
  panic(cliErr)
} 

// enable the integration
req := integration.EnableIntegrationRequest{Name: "integration name"}

_, err = intCli.Enable(req)

if err != nil {
  panic(err)
} else {
  fmt.Printf("Integration enabled successfuly")
}

Disable integration

cli := new (ogcli.OpsGenieClient)
cli.SetAPIKey(apiKey)
intCli, cliErr := cli.Integration()

if cliErr != nil {
  panic(cliErr)
} 

// enable the integration
req := integration.DisableIntegrationRequest{Name: "integration name"}

_, err = intCli.Disable(req)

if err != nil {
  panic(err)
} else {
  fmt.Printf("Integration disabled successfuly")
}

Connection Configuration

Proxy configuration

Users can set the enlisted parameters if they would like to connect through a proxy server.
  • Username
  • Password
  • Host IP/name
  • Port number
  • Protocol i.e. https or http.
The configuration is achieved via setting a proxy data structure with the above given parameters and client. Here is an example of how to configure the OpsGenieClient.
  cli := new (ogcli.OpsGenieClient)
  // OpsGenie client proxy configuration
  pc := new (ogcli.ClientProxyConfiguration)
  pc.Host = "proxy.example.com"
  pc.Port = 8080
  pc.Username = "user"
  pc.Password = "secret"
  pc.Protocol = "http"
  // set the proxy configuration
  cli.SetClientProxyConfiguration( pc )

Http transport layer configuration

It is also possible to modify a few Http transport layer settings. They include:
  • Connection timeout
  • Request timeout
  • Maximum number of retries when the requests fallback

Connection timeout is the value in seconds that the client tries to connect to the server. Request timeout is total time of the request, including connection time. Hence, it should be configured longer than connection timeout.

  cli := new (ogcli.OpsGenieClient)
  // HttpTransportSettings 
  cfg := new (ogcli.HttpTransportSettings)
  cfg.ConnectionTimeout = 2   // in seconds
  cfg.RequestTimeout = 5   // in seconds
  cfg.MaxRetryAttempts = 5

  cli.SetHttpTransportSettings( cfg )

Exception Handling

OpsGenie Go SDK, has many functions usually return two values. One of the values is a pointer to an error value. Errors are the way to check if the performing operation is completed successfuly.
  resp, err := alertClient.Get( alertRequest )
  // check if error
  if err != nil {
    // you have now an error message accessible via err.Error()
  }