forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dobs/spring-cleaning
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
Showing
13 changed files
with
442 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.