Skip to content

Commit

Permalink
Merge pull request #1 from dobs/spring-cleaning
Browse files Browse the repository at this point in the history
This branch incorporates a bunch of old un-merged PRs from upstream, specifically:

- PagerDuty#72
- PagerDuty#73
- PagerDuty#74
- PagerDuty#75
- PagerDuty#78
- PagerDuty#91
- PagerDuty#93
- PagerDuty#97
- PagerDuty#104
- PagerDuty#106
- PagerDuty#108
- PagerDuty#113
- PagerDuty#114
- PagerDuty#126
- PagerDuty#127
- PagerDuty#130
- PagerDuty#132
- PagerDuty#137
- PagerDuty#138
- PagerDuty#140
- PagerDuty#143

It doesn't attempt to merge, due to potential complexity:

- PagerDuty#48
- PagerDuty#99
- PagerDuty#134

And skips the following as it was picked up as part of pulling in the above merged PRs:

- PagerDuty#133

Will have subsequent PRs for cleanup, specifically:
- Reducing duplicate code.
- Standardizing response and error handling.

Followed by more janitorial and refactor-oriented changes.
  • Loading branch information
dobs authored Mar 19, 2019
2 parents 3cbcad3 + 850eb75 commit 7aa5782
Show file tree
Hide file tree
Showing 13 changed files with 442 additions and 62 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ API represented by their own sub commands. For an exhaustive list of sub-command

__Install:__
```
cd $GOPATH/github.com/PagerDuty/go-pagerduty
cd $GOPATH/src/github.com/PagerDuty/go-pagerduty
go build -o $GOPATH/bin/pd command/*
```
Expand Down
67 changes: 67 additions & 0 deletions command/event_v2_manage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"encoding/json"
"fmt"
"os"
"strings"

pagerduty "github.com/PagerDuty/go-pagerduty"
log "github.com/Sirupsen/logrus"
"github.com/mitchellh/cli"
)

type EventV2Manage struct {
Meta
}

func EventV2ManageCommand() (cli.Command, error) {
return &EventV2Manage{}, nil
}

func (c *EventV2Manage) Help() string {
helpText := `
pd event-v2 manage <FILE> Manage Events from json file
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

func (c *EventV2Manage) Synopsis() string {
return "Create a New V2 Event"
}

func (c *EventV2Manage) Run(args []string) int {
flags := c.Meta.FlagSet("event-v2 manage")
flags.Usage = func() { fmt.Println(c.Help()) }
if err := flags.Parse(args); err != nil {
log.Error(err)
return -1
}
if err := c.Meta.Setup(); err != nil {
log.Error(err)
return -1
}
var e pagerduty.V2Event
if len(flags.Args()) != 1 {
log.Error("Please specify input json file")
return -1
}
log.Info("Input file is: ", flags.Arg(0))
f, err := os.Open(flags.Arg(0))
if err != nil {
log.Error(err)
return -1
}
defer f.Close()
decoder := json.NewDecoder(f)
if err := decoder.Decode(&e); err != nil {
log.Errorln("Failed to decode json. Error:", err)
return -1
}
log.Debugf("%#v", e)
if _, err := pagerduty.ManageEvent(e); err != nil {
log.Error(err)
return -1
}
return 0
}
5 changes: 5 additions & 0 deletions command/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func loadCommands() map[string]cli.CommandFactory {
"escalation-policy show": EscalationPolicyShowCommand,
"escalation-policy update": EscalationPolicyUpdateCommand,

"event-v2 manage": EventV2ManageCommand,

"incident list": IncidentListCommand,
"incident manage": IncidentManageCommand,
"incident show": IncidentShowCommand,
Expand Down Expand Up @@ -92,6 +94,9 @@ func loadCommands() map[string]cli.CommandFactory {
"user notification-rule delete": UserNotificationRuleDeleteCommand,
"user notification-rule show": UserNotificationRuleShowCommand,
"user notification-rule update": UserNotificationRuleUpdateCommand,

"vendor list": VendorListCommand,
"vendor show": VendorShowCommand,
}
}

Expand Down
71 changes: 71 additions & 0 deletions command/vendor_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

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

type VendorList struct {
Meta
}

func VendorListCommand() (cli.Command, error) {
return &VendorList{}, nil
}

func (c *VendorList) Help() string {
helpText := `
pd vendor list List vendors
Options:
-query Filter vendors with certain name
`
return strings.TrimSpace(helpText)
}

func (c *VendorList) Synopsis() string {
return "List vendors within PagerDuty, optionally filtered by a search query"
}

func (c *VendorList) Run(args []string) int {
var query string

flags := c.Meta.FlagSet("user list")
flags.Usage = func() { fmt.Println(c.Help()) }
flags.StringVar(&query, "query", "", "Show vendors whose names contain the query")

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.ListVendorOptions{
Query: query,
}
if resp, err := client.ListVendors(opts); err != nil {
log.Error(err)
return -1
} else {
for i, p := range resp.Vendors {
fmt.Println("Entry: ", i)
data, err := yaml.Marshal(p)
if err != nil {
log.Error(err)
return -1
}
fmt.Println(string(data))
}
}

return 0
}
63 changes: 63 additions & 0 deletions command/vendor_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

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

type VendorShow struct {
Meta
}

func VendorShowCommand() (cli.Command, error) {
return &VendorShow{}, nil
}

func (c *VendorShow) Help() string {
helpText := `
vendor show Show details of an individual vendor
Options:
-id Vendor ID
`
return strings.TrimSpace(helpText)
}

func (c *VendorShow) Synopsis() string { return "Get details about a vendor" }

func (c *VendorShow) Run(args []string) int {
log.Println(args)
var id string
flags := c.Meta.FlagSet("vendor show")
flags.Usage = func() { fmt.Println(c.Help()) }
flags.StringVar(&id, "id", "", "Vendor ID")
if err := flags.Parse(args); err != nil {
log.Errorln(err)
return -1
}
if err := c.Meta.Setup(); err != nil {
log.Error(err)
return -1
}

if id == "" {
log.Error("You must specify the vendor id using -id flag")
return -1
}
client := c.Meta.Client()
result, err := client.GetVendor(id)
if err != nil {
log.Error(err)
return -1
}
data, err := yaml.Marshal(result)
if err != nil {
log.Error(err)
return -1
}
fmt.Println(string(data))
return 0
}
62 changes: 62 additions & 0 deletions contact_method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package pagerduty

import (
"fmt"
"net/http"
)

// ContactMethod is a way of contacting the user.
type ContactMethod struct {
ID string `json:"id"`
Type string `json:"type"`
Summary string `json:"summary"`
Self string `json:"self"`
Label string `json:"label"`
Address string `json:"address"`
SendShortEmail bool `json:"send_short_email,omitempty"`
SendHTMLEmail bool `json:"send_html_email,omitempty"`
Blacklisted bool `json:"blacklisted,omitempty"`
CountryCode int `json:"country_code,omitempty"`
Enabled bool `json:"enabled,omitempty"`
HTMLUrl string `json:"html_url"`
}


// ListContactMethodsResponse is the data structure returned from calling the ListContactMethods API endpoint.
type ListContactMethodsResponse struct {
APIListObject
ContactMethods []ContactMethod
}

// ListContactMethods lists all contact methods for a user.
// TODO: Unify with `ListUserContactMethods`.
func (c *Client) ListContactMethods(userID string) (*ListContactMethodsResponse, error) {
resp, err := c.get("/users/" + userID + "/contact_methods")
if err != nil {
return nil, err
}
var result ListContactMethodsResponse
return &result, c.decodeJSON(resp, &result)
}

// GetContactMethod gets details about a contact method.
func (c *Client) GetContactMethod(userID, id string) (*ContactMethod, error) {
resp, err := c.get("/users/" + userID + "/contact_methods/" + id)
return getContactMethodFromResponse(c, resp, err)
}

func getContactMethodFromResponse(c *Client, resp *http.Response, err error) (*ContactMethod, error) {
if err != nil {
return nil, err
}
var target map[string]ContactMethod
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
rootNode := "contact_method"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}
2 changes: 1 addition & 1 deletion escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type EscalationPolicy struct {
APIObject
Name string `json:"name,omitempty"`
EscalationRules []EscalationRule `json:"escalation_rules,omitempty"`
Services []APIReference `json:"services,omitempty"`
Services []APIObject `json:"services,omitempty"`
NumLoops uint `json:"num_loops,omitempty"`
Teams []APIReference `json:"teams,omitempty"`
Description string `json:"description,omitempty"`
Expand Down
Loading

0 comments on commit 7aa5782

Please sign in to comment.