Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement incident list cli. event posting api #8

Merged
merged 1 commit into from
Apr 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions command/incident_list.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package main

import (
"fmt"
"github.com/PagerDuty/go-pagerduty"
log "github.com/Sirupsen/logrus"
"github.com/mitchellh/cli"
"gopkg.in/yaml.v2"
"strings"
)

type IncidentList struct {
Meta
}

func IncidentListCommand() (cli.Command, error) {
Expand All @@ -14,6 +19,8 @@ func IncidentListCommand() (cli.Command, error) {

func (c *IncidentList) Help() string {
helpText := `
pd incident list List incidents

`
return strings.TrimSpace(helpText)
}
Expand All @@ -23,5 +30,45 @@ func (c *IncidentList) Synopsis() string {
}

func (c *IncidentList) Run(args []string) int {
var teamIDs []string
var timeZone string
var sortBy string
var includes []string
flags := c.Meta.FlagSet("service list")
flags.Usage = func() { fmt.Println(c.Help()) }
flags.Var((*ArrayFlags)(&includes), "include", "Additional details to include (can be specified multiple times)")
flags.Var((*ArrayFlags)(&teamIDs), "team-id", "Only show for team ID (can be specified multiple times)")
flags.StringVar(&timeZone, "time-zone", "", "Time Zone")
flags.StringVar(&sortBy, "sort-by", "", "sort by")

if err := flags.Parse(args); err != nil {
log.Error(err)
return -1
}
if err := c.Meta.Setup(); err != nil {
log.Error(err)
return -1
}
client := c.Meta.Client()
opts := pagerduty.ListIncidentsOptions{
TeamIDs: teamIDs,
TimeZone: timeZone,
SortBy: sortBy,
Includes: includes,
}
if incidentList, err := client.ListIncidents(opts); err != nil {
log.Error(err)
return -1
} else {
for i, incident := range incidentList.Incidents {
fmt.Println("Entry: ", i+1)
data, err := yaml.Marshal(incident)
if err != nil {
log.Error(err)
return -1
}
fmt.Println(string(data))
}
}
return 0
}
46 changes: 46 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package pagerduty

import (
"bytes"
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
"net/http"
)

const EventEndPoint = "https://events.pagerduty.com/generic/2010-04-15/create_event.json"

type Event struct {
Type string `json:"event_type"`
ServiceKey string `json:"service_key"`
Description string `json:"description,omitempty"`
Client string `json:"client,omitempty"`
ClientURL string `json:"client_url,omitempty"`
Details *json.RawMessage `json:"details,omitempty"`
Contexts []*json.RawMessage `json:"contexts,omitempty"`
}

type EventResponse struct {
Status string
Message string
IncidentKey string
}

func CreateEvent(e Event) (*http.Response, error) {
log.Debugln("Endpoint:", EventEndPoint)
data, err := json.Marshal(e)
if err != nil {
return nil, err
}
log.Debugln(string(data))
req, _ := http.NewRequest("POST", EventEndPoint, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return resp, fmt.Errorf("HTTP Status Code: %d", resp.StatusCode)
}
return resp, nil
}