Opsgenie Go API (deprecated)
Opsgenie GO API v1 is deprecated.
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/alertsv2"
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.
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 successfully.
resp, err := alertClient.Get( alertRequest )
// check if error
if err != nil {
// you have now an error message accessible via err.Error()
}
Alert API Import Statements
import (
alerts "github.com/opsgenie/opsgenie-go-sdk/alertsv2"
ogcli "github.com/opsgenie/opsgenie-go-sdk/client"
)
Create Alert
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
teams := []alerts.TeamRecipient{
&alerts.Team{Name: "teamId"},
&alerts.Team{ID: "teamId"},
}
visibleTo := [] alerts.Recipient{
&alerts.Team{ID: "teamId"},
&alerts.Team{Name: "teamName"},
&alerts.User{ID: "userId"},
&alerts.User{Username: "[email protected]"},
}
request := alerts.CreateAlertRequest{
Message: "message",
Alias: "alias",
Description: "alert description",
Teams: teams,
VisibleTo: visibleTo,
Actions: []string{"action1", "action2"},
Tags: []string{"tag1", "tag2"},
Details: map[string]string{
"key": "value",
"key2": "value2",
},
Entity: "entity",
Source: "source",
Priority: alerts.P1,
User: "[email protected]",
Note: "alert note",
}
response, err := alertCli.Create(request)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Create request ID: " + response.RequestID)
}
Close Alert
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
identifier := alerts.Identifier{
TinyID: "2",
};
closeRequest := alerts.CloseRequest{
Identifier: &identifier,
User: "test",
Source: "Source",
Note: "Note",
}
response, err := alertCli.Close(closeRequest)
if err != nil {
panic(err)
} else {
fmt.Println("RequestID: " + response.RequestID)
}
Delete Alert
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alerts.DeleteAlertRequest{
Identifier: &alerts.Identifier{
TinyID: "2",
},
Source: "source",
User: "[email protected]",
}
response, err := alertCli.Delete(request)
if err != nil {
panic(err)
} else {
fmt.Println("RequestID:" + response.RequestID)
}
Get Alert
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
response, err := alertCli.Get(alerts.GetAlertRequest{
Identifier: &alerts.Identifier{
TinyID: "2",
},
})
if err != nil {
panic(err)
} else {
alert := response.Alert
fmt.Printf("ID: %s\n", alert.ID)
fmt.Printf("Message: %s\n", alert.Message)
fmt.Printf("Tags: %v\n", alert.Tags)
fmt.Printf("Count: %d\n", alert.Count)
fmt.Printf("Tiny: %s\n", alert.TinyID)
}
List Alerts
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
response, err := alertCli.List(alerts.ListAlertRequest{
Limit: 5,
Offset: 0,
SearchIdentifierType: alerts.Name,
})
if err != nil {
panic(err)
} else {
for i, alert := range response.Alerts {
fmt.Println(strconv.Itoa(i) + ". " + alert.Message)
}
}
List Alert Notes
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alerts.ListAlertNotesRequest{
Identifier: &alerts.Identifier{
TinyID: "2",
},
Order: "asc",
Direction: alerts.Next,
Offset: "1",
Limit: 2,
}
response, err := alertCli.ListAlertNotes(request)
if err != nil {
fmt.Println(err.Error())
} else {
for _, note := range response.AlertNotes {
fmt.Println(note.Note)
}
}
List Alert Logs
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alerts.ListAlertLogsRequest{
Identifier: &alerts.Identifier{
TinyID: "2",
},
Order: "asc",
Direction: alerts.Next,
Offset: "0",
Limit: 5,
}
response, err := alertCli.ListAlertLogs(request)
if err != nil {
fmt.Println(err.Error())
} else {
for _, log := range response.AlertLogs {
fmt.Println(log.Log + " [" + log.Type + "]")
}
}
List Alert Recipients
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alerts.ListAlertRecipientsRequest{
Identifier: &alerts.Identifier{
TinyID: "2",
},
}
response, err := alertCli.ListAlertRecipients(request)
if err != nil {
panic(err)
} else {
for i, recipient := range response.Recipients {
fmt.Println(strconv.Itoa(i) + ". " + recipient.User.Username + " : " + recipient.State)
}
}
Acknowledge
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
identifier := alerts.Identifier{
TinyID: "2",
};
ackRequest := alerts.AcknowledgeRequest{
Identifier: &identifier,
User: "test",
Source: "Source",
Note: "Note",
}
response, err := alertCli.Acknowledge(ackRequest)
if err != nil {
fmt.Print(err.Error())
} else {
fmt.Println("RequestId: " + response.RequestID)
}
Unacknowledge
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
identifier := alerts.Identifier{
TinyID: "2",
};
unackRequest := alerts.UnacknowledgeRequest{
Identifier: &identifier,
User: "test",
Source: "Source",
Note: "Note",
}
response, _ := alertCli.Unacknowledge(unackRequest)
fmt.Println("RequestID: " + response.RequestID)
Snooze
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
identifier := alerts.Identifier{
TinyID: "2",
};
actionRequest := alerts.AlertActionRequest{
Identifier: &identifier,
User: "test",
Source: "Source",
Note: "Note",
}
ackRequest := alerts.SnoozeRequest{
AlertActionRequest: actionRequest,
EndTime: time.Now().AddDate(0, 0, 1)
}
response, err := alertCli.Snooze(ackRequest)
if err != nil {
panic(err)
} else {
fmt.Println("RequestId: " + response.RequestID)
}
Assign Owner
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alerts.AssignAlertRequest{
Identifier: &alerts.Identifier{
TinyID: "2",
},
User: "test",
Source: "Source",
Note: "Note",
Owner: alerts.User{
Username: "[email protected]",
},
}
response, err := alertCli.Assign(request)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("RequestID: " + response.RequestID)
}
Add Team
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alerts.AddTeamToAlertRequest{
Identifier: &alerts.Identifier{
TinyID: "2",
},
User: "test",
Source: "Source",
Note: "Note",
Team: alerts.Team{
Name: "team_name",
},
}
response, err := alertCli.AddTeamToAlert(request)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("RequestID: " + response.RequestID)
}
Add Note
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alerts.AddNoteRequest{
Identifier: &alerts.Identifier{
TinyID: "2",
},
User: "test",
Source: "Source",
Note: "Note",
}
response, err := alertCli.AddNote(request)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("RequestID: " + response.RequestID)
}
Add Tags
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alerts.AddTagsToAlertRequest{
Identifier: &alerts.Identifier{
TinyID: "2",
},
User: "test",
Source: "Source",
Note: "Note",
Tags: []string{"tag1", "tag2"},
}
response, err := alertCli.AddTags(request)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("RequestID: " + response.RequestID)
}
Remove Tags
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alerts.RemoveTagsRequest{
Identifier: &alerts.Identifier{
TinyID: "2",
},
User: "test",
Source: "Source",
Note: "Note",
Tags: []string{"tag1"},
}
response, err := alertCli.RemoveTags(request)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("RequestID: " + response.RequestID)
}
Add Details
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alerts.AddDetailsRequest{
Identifier: &alerts.Identifier{
TinyID: "2",
},
User: "test",
Source: "Source",
Note: "Note",
Details: map[string]string{
"key1": "value1",
"key2": "value2",
},
}
response, err := alertCli.AddDetails(request)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("RequestID: " + response.RequestID)
}
Remove Details
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alerts.RemoveDetailsRequest{
Identifier: &alerts.Identifier{
TinyID: "2",
},
User: "test",
Source: "Source",
Note: "Note",
Keys: [] string{"key2"},
}
response, err := alertCli.RemoveDetails(request)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("RequestID: " + response.RequestID)
}
Execute Action
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
identifier := alerts.Identifier{
TinyID: "2",
};
response, err := alertCli.ExecuteCustomAction(alerts.ExecuteCustomActionRequest{
Identifier: &identifier,
User: "test",
Source: "Source",
Note: "Note",
ActionName: "customActionNew",
})
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("RequestID: " + response.RequestID)
}
Escalate To Next
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alerts.EscalateToNextRequest{
Identifier: &alerts.Identifier{
TinyID: "2",
},
User: "test",
Source: "Source",
Note: "Note",
Escalation: alerts.Escalation{
Name: "escalationName",
},
}
response, err := alertCli.EscalateToNext(request)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("RequestId: " + response.RequestID)
}
Add Attachment File
Add Attachment With Bytes
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
// This is for test purpose only. You can provide your file as byte array however you want.
fileInBytes := getBytesFromFile("filepath")
request := alertsv2.AddAlertAttachmentRequest{
AttachmentAlertIdentifier: &alertsv2.AttachmentAlertIdentifier{
TinyID: "5",
},
AttachmentFileContent: fileInBytes,
AttachmentFileName: "test.jpg",
User: "[email protected]",
}
response, err := alertCli.AttachFile(request)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Result: " + response.Result)
}
Add Attachment With File Path
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alertsv2.AddAlertAttachmentRequest{
AttachmentAlertIdentifier: &alertsv2.AttachmentAlertIdentifier{
TinyID: "5",
},
AttachmentFilePath: constants.PathToFile,
User: "[email protected]",
}
response, err := alertCli.AttachFile(request)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Result: " + response.Result)
}
Get Alert Attachment
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alertsv2.GetAlertAttachmentRequest{
AttachmentAlertIdentifier: &alertsv2.AttachmentAlertIdentifier{
TinyID: "5",
},
AttachmentId:"1500294613021000158",
}
response, err := alertCli.GetAttachmentFile(request)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Attachment Name : " + response.Attachment.Name)
fmt.Println("Link : " + response.Attachment.DownloadLink)
}
List Alert Attachments
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alertsv2.ListAlertAttachmentRequest{
AttachmentAlertIdentifier: &alertsv2.AttachmentAlertIdentifier{
TinyID: "5",
},
}
response, err := alertCli.ListAlertAttachments(request)
if err != nil {
fmt.Println(err.Error())
} else {
for _, attachment := range response.AlertAttachments {
fmt.Printf("AttachmentName : %s\n" , attachment.Name)
fmt.Printf("AttachmentID : %d\n" , attachment.Id)
}
}
Delete Alert Attachment
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
alertCli, _ := cli.AlertV2()
request := alertsv2.DeleteAlertAttachmentRequest{
AttachmentAlertIdentifier: &alertsv2.AttachmentAlertIdentifier{
TinyID: "5",
},
AttachmentId:"1500294613021000158",
}
response, err := alertCli.DeleteAttachment(request)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Result : " + response.Result)
}
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 (Ping)
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
hbCli, cliErr := cli.Heartbeat()
if cliErr != nil {
panic(cliErr)
}
// send heart beat request
pingRequest := hb.PingHeartbeatRequest{Name: "heeartbeat name"}
pingResponse, sendErr := hbCli.Ping(pingRequest)
if sendErr != nil {
panic(sendErr)
}
fmt.Println()
fmt.Printf("Heartbeat request sent\n")
fmt.Printf("----------------------\n")
fmt.Printf("RequestId: %s\n", pingResponse.RequestID)
fmt.Printf("Response Time: %f\n", pingResponse.ResponseTime)
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 successfully")
}
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 successfully")
}
Contact API Import Statements
import (
hb "github.com/opsgenie/opsgenie-go-sdk/contact"
ogcli "github.com/opsgenie/opsgenie-go-sdk/client"
)
Create Contact
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
contactCli, cliErr := cli.Contact()
if cliErr != nil {
panic(cliErr)
}
contactReq := contacts.CreateContactRequest{ Method: "method name", To: "to", Username: "username"}
contactResp, contactErr := contactCli.Create(contactReq)
if contactErr != nil {
panic(contactErr)
}
fmt.Printf("id: %s\n", contactResp.Id)
fmt.Printf("status: %s\n", contactResp.Status)
fmt.Printf("code: %d\n", contactResp.Code)
Delete Contact
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
contactCli, cliErr := cli.Contact()
if cliErr != nil {
panic(cliErr)
}
contactReq := contacts.CreateContactRequest{ Method: "method name", To: "to", Username: "username"}
contactResp, contactErr := contactCli.Create(contactReq)
if contactErr != nil {
panic(contactErr)
}
contactDeleteReq := contacts.DeleteContactRequest{ Id: contactResp.Id, Username: "username"}
contactDeleteResp, contactDeleteErr := contactCli.Delete(contactDeleteReq)
if contactDeleteErr != nil {
panic(contactDeleteErr)
}
fmt.Printf("status: %s\n", contactDeleteResp.Status)
fmt.Printf("code: %d\n", contactDeleteResp.Code)
Get Contact
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
contactCli, cliErr := cli.Contact()
if cliErr != nil {
panic(cliErr)
}
contactReq := contacts.CreateContactRequest{ Method: "method name", To: "to", Username: "username"}
contactResp, contactErr := contactCli.Create(contactReq)
if contactErr != nil {
panic(contactErr)
}
contactGetReq := contacts.GetContactRequest{ Id: contactResp.Id, Username: "username"}
contactGetResp, contactGetErr := contactCli.Get(contactGetReq)
if contactGetErr != nil {
panic(contactGetErr)
}
fmt.Printf("disabledReason: %s\n", contactGetResp.DisabledReason)
fmt.Printf("method: %s\n", contactGetResp.Method)
fmt.Printf("to: %s\n", contactGetResp.To)
fmt.Printf("id: %s\n", contactGetResp.Id)
fmt.Printf("enabled: %t\n", contactGetResp.Enabled)
Enable Contact
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
contactCli, cliErr := cli.Contact()
if cliErr != nil {
panic(cliErr)
}
contactReq := contacts.CreateContactRequest{ Method: "method name", To: "to", Username: "username"}
contactResp, contactErr := contactCli.Create(contactReq)
if contactErr != nil {
panic(contactErr)
}
contactEnableReq := contacts.EnableContactRequest{ Id: contactResp.Id, Username: "username"}
contactEnableResp, contactEnableErr := contactCli.Enable(contactEnableReq)
if contactEnableErr != nil {
panic(contactEnableErr)
}
fmt.Printf("status: %s\n", contactEnableResp.Status)
Disable Contact
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
contactCli, cliErr := cli.Contact()
if cliErr != nil {
panic(cliErr)
}
contactReq := contacts.CreateContactRequest{ Method: "method name", To: "to", Username: "username"}
contactResp, contactErr := contactCli.Create(contactReq)
if contactErr != nil {
panic(contactErr)
}
contactDisableReq := contacts.DisableContactRequest{ Id: contactResp.Id, Username: "username"}
contactDisableResp, contactDisableErr := contactCli.Disable(contactDisableReq)
if contactDisableErr != nil {
panic(contactDisableErr)
}
fmt.Printf("status: %s\n", contactDisableResp.Status)
Update Contact
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey("your OpsGenie api key")
contactCli, cliErr := cli.Contact()
if cliErr != nil {
panic(cliErr)
}
contactReq := contacts.CreateContactRequest{ Method: "method name", To: "to", Username: "username"}
contactResp, contactErr := contactCli.Create(contactReq)
if contactErr != nil {
panic(contactErr)
}
contactUpdateReq := contacts.UpdateContactRequest{ Username: "username", Id: contactResp.Id, To: "to"}
contactUpdateResp, contactUpdateErr := contactCli.Update(contactUpdateReq)
if contactUpdateErr != nil {
panic(contactUpdateErr)
}
fmt.Printf("id: %s\n", contactUpdateResp.Id)
fmt.Printf("status: %s\n", contactUpdateResp.Status)
fmt.Printf("code: %d\n", contactUpdateResp.Code)
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 successfully")
}
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 successfully")
}
Updated over 5 years ago