Skip to content

Commit

Permalink
Fixes #36 Support for creating/deleting/listing/enabling/disabling he… (
Browse files Browse the repository at this point in the history
#43)

* Fixes #36 Support for creating/deleting/listing/enabling/disabling heartbeats

This PR adds support for creating/deleting/listing/enabling/disabling heartbeats
and changed name of ping function

adding Changelog.md

updating go-sdk version

* Updating ChangeLog.md

Co-authored-by: pmudaiya <[email protected]>
  • Loading branch information
djmgit and prakhar-mudaiya authored Jul 1, 2021
1 parent aad7b28 commit 9e14a61
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 14 deletions.
23 changes: 23 additions & 0 deletions CHANEGLOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## 3.1.4 (July 1, 2021)
* **Heartbeat:** Added create/delete/list/enable/diable functionality for heatbeart
* **Go-SDK:** Updated Go-SDK version

## 3.1.3 (June 21, 2021)
* **Github Actions** Adding github Actions for deploying all binaries in github.
* **Logs:** Added Default Limit to Logs Download
* **Config:** Updated Default Config File Location
* **File Logging:** Added functionality to allow logging of Lamp into file
* **Service:** Added Service Commands functionality
* **Incident:** Added Incident Commands functionality
* **Schedule:** Added Schedule Commands functionality
* **Esclation:** Added Escalation Commands functionality

## 3.1.2 (February 27, 2020)
* **Dependencies:** Updating the used Dependencies

## 3.1.1 (February 26, 2020)
BUGFIX:
* **Logs:** Stop comparing endDate if it is bigger than compared log file while downloading Logs

## 3.1.0 (December 18, 2010)
* **GO SDK Version:** Updated Go-SDK Version
171 changes: 170 additions & 1 deletion command/heartbeat_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package command
import (
"errors"
"github.com/opsgenie/opsgenie-go-sdk-v2/heartbeat"
"github.com/opsgenie/opsgenie-go-sdk-v2/og"
gcli "github.com/urfave/cli"
"os"
"strconv"
"strings"
)

func NewHeartbeatClient(c *gcli.Context) (*heartbeat.Client, error) {
Expand All @@ -19,7 +22,7 @@ func NewHeartbeatClient(c *gcli.Context) (*heartbeat.Client, error) {
}

// HeartbeatAction sends an Heartbeat signal to Opsgenie.
func HeartbeatAction(c *gcli.Context) {
func PingHeartbeatAction(c *gcli.Context) {
cli, err := NewHeartbeatClient(c)
if err != nil {
os.Exit(1)
Expand All @@ -39,3 +42,169 @@ func HeartbeatAction(c *gcli.Context) {
}
printMessage(DEBUG,"Ping request has received. RequestID: " + response.RequestId)
}

func CreateHeartbeatAction(c *gcli.Context) {
cli, err := NewHeartbeatClient(c)
if err != nil {
os.Exit(1)
}

addRequest := heartbeat.AddRequest{}

if val, success := getVal("name", c); success {
addRequest.Name = val
}

if val, success := getVal("description", c); success {
addRequest.Description = val
}

if val, success := getVal("alertMessage", c); success {
addRequest.AlertMessage = val
}

if val, success := getVal("alertTag", c); success {
addRequest.AlertTag = strings.Split(val, ",")
}

if val, success := getVal("alertPriority", c); success {
addRequest.AlertPriority = val
}

if val, success := getVal("ownerTeam", c); success {
addRequest.OwnerTeam = og.OwnerTeam{
Name: val,
}
}

if val, success := getVal("interval", c); success {
addRequest.Interval, err = strconv.Atoi(val)

if err != nil {
printMessage(ERROR, "Please provide a valid integer for interval.")
os.Exit(1)
}
}

if val, success := getVal("intervalType", c); success {
if val == "m" {
addRequest.IntervalUnit = heartbeat.Minutes
} else if val == "h" {
addRequest.IntervalUnit = heartbeat.Hours
} else if val == "d" {
addRequest.IntervalUnit = heartbeat.Days
} else {
printMessage(ERROR, "Please provide a valid interval unit.")
os.Exit(1)
}
}

enabled := c.IsSet("enabled")
addRequest.Enabled = &enabled

printMessage(DEBUG, "Heartbeat create request created from flags. Sedning to Opsgenie...")

response, err := cli.Add(nil, &addRequest)
if err != nil {
printMessage(ERROR,err.Error())
os.Exit(1)
}
printMessage(DEBUG,"Heartbeat will be created " + response.RequestId)
}

func DeleteHeartbeatAction(c *gcli.Context) {
cli, err := NewHeartbeatClient(c)
if err != nil {
os.Exit(1)
}

var name string
if val, success := getVal("name", c); success {
name = val
}

printMessage(DEBUG, "Heartbeat delete request created from flags. Sending to Opsgenie...")

response, err := cli.Delete(nil, name)
if err != nil {
printMessage(ERROR,err.Error())
os.Exit(1)
}
printMessage(DEBUG,"Heartbeat will be deleted.")
printMessage(INFO, response.RequestId)
}

func DisableHeartbeatAction(c *gcli.Context) {
cli, err := NewHeartbeatClient(c)
if err != nil {
os.Exit(1)
}

var name string
if val, success := getVal("name", c); success {
name = val
}

printMessage(DEBUG, "Heartbeat disable request created from flags. Sending to Opsgenie...")

response, err := cli.Disable(nil, name)
if err != nil {
printMessage(ERROR,err.Error())
os.Exit(1)
}
printMessage(DEBUG,"Heartbeat will be disabled.")
printMessage(INFO, response.RequestId)
}

func EnableHeartbeatAction(c *gcli.Context) {
cli, err := NewHeartbeatClient(c)
if err != nil {
os.Exit(1)
}

var name string
if val, success := getVal("name", c); success {
name = val
}

printMessage(DEBUG, "Heartbeat enable request created from flags. Sending to Opsgenie...")

response, err := cli.Enable(nil, name)
if err != nil {
printMessage(ERROR,err.Error())
}
printMessage(DEBUG, "Heartbeat will be enabled")
printMessage(INFO, response.RequestId)
}

func ListHeartbeatAction(c *gcli.Context) {
cli, err := NewHeartbeatClient(c)
if err != nil {
os.Exit(1)
}

response, err := cli.List(nil)
if err != nil {
printMessage(ERROR,err.Error())
}

outputFormat := strings.ToLower(c.String("output-format"))
printMessage(DEBUG,"Heartbeats listed successfully, and will print as " + outputFormat)
switch outputFormat {
case "yaml":
output, err := resultToYAML(response.Heartbeats)
if err != nil {
printMessage(ERROR,err.Error())
os.Exit(1)
}
printMessage(INFO, output)
default:
isPretty := c.IsSet("pretty")
output, err := resultToJSON(response.Heartbeats, isPretty)
if err != nil {
printMessage(ERROR,err.Error())
os.Exit(1)
}
printMessage(INFO, output)
}
}
6 changes: 5 additions & 1 deletion conf/lamp.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ apiKey=your_api_key

############## Use following settings options for connection to OpsGenie server############
##connectionTimeout=50
##requestTimeout=100
##requestTimeout=100

############## Use alternative urls for connection to EU / Sandbox server############
## opsgenie.api.url=https://api.eu.opsgenie.com
## opsgenie.api.url=https://api.sandbox.opsgenie.com
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-retryablehttp v0.5.4 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.6
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.8
github.com/urfave/cli v1.21.0
gopkg.in/yaml.v2 v2.2.2
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ github.com/hashicorp/go-retryablehttp v0.5.4/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.6 h1:C3p45LH9szkt8/pbI4CXwBPkLj3pFLlCzbzm6FgG57c=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.6/go.mod h1:4OjcxgwdXzezqytxN534MooNmrxRD50geWZxTD7845s=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.8 h1:qF/rRi8GSU2mjBXfJIyMj9GGmjedsV3Gm1uYbiGlCRk=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.8/go.mod h1:4OjcxgwdXzezqytxN534MooNmrxRD50geWZxTD7845s=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Loading

0 comments on commit 9e14a61

Please sign in to comment.