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

(feat) addon list api. create cli #2

Merged
merged 2 commits into from
Apr 2, 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
51 changes: 48 additions & 3 deletions addon.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package pagerduty

import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/google/go-querystring/query"
"io/ioutil"
)

type Addon struct {
APIObject
Name string
Src string
Services []APIObject
Name string `json:"name,omitempty"`
Src string `json:"src,omitempty"`
Services []APIObject `json:"services,omitempty"`
}

type ListAddonOptions struct {
Expand All @@ -34,3 +37,45 @@ func (c *Client) ListAddons(o ListAddonOptions) (*ListAddonResponse, error) {
var result ListAddonResponse
return &result, c.decodeJson(resp, &result)
}

func (c *Client) InstallAddon(a Addon) error {
data := make(map[string]Addon)
data["addon"] = a
resp, err := c.Post("/addons", data)
defer resp.Body.Close()
if err != nil {
ct, rErr := ioutil.ReadAll(resp.Body)
if rErr == nil {
log.Debug(string(ct))
}
}
return err
}

func (c *Client) DeleteAddon(id string) error {
_, err := c.Delete("/addons/" + id)
return err
}

func (c *Client) GetAddon(id string) (*Addon, error) {
resp, err := c.Get("/addons/" + id)
if err != nil {
return nil, err
}
var result map[string]Addon
if err := c.decodeJson(resp, &result); err != nil {
return nil, err
}
a, ok := result["addon"]
if !ok {
return nil, fmt.Errorf("JSON response does not have 'addon' field")
}
return &a, nil
}

func (c *Client) UpdateAddon(id string, a Addon) error {
v := make(map[string]Addon)
v["addon"] = a
_, err := c.Put("/addons/"+id, v)
return err
}
7 changes: 6 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pagerduty
import (
"bytes"
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
"io"
"net/http"
Expand Down Expand Up @@ -56,6 +57,7 @@ func (c *Client) Post(path string, payload interface{}) (*http.Response, error)
if err != nil {
return nil, err
}
log.Debugln(string(data))
return c.Do("POST", path, bytes.NewBuffer(data))
}

Expand All @@ -65,7 +67,7 @@ func (c *Client) Get(path string) (*http.Response, error) {

func (c *Client) Do(method, path string, body io.Reader) (*http.Response, error) {
endpoint := "https://" + c.Subdomain + ".pagerduty.com/api/v1" + path
log.Debugf("Endpoint", endpoint)
log.Debugln("Endpoint:", endpoint)
req, _ := http.NewRequest(method, endpoint, body)
req.Header.Set("Accept", "application/vnd.pagerduty+json;version=2")
req.Header.Set("Content-Type", "application/json")
Expand All @@ -74,6 +76,9 @@ func (c *Client) Do(method, path string, body io.Reader) (*http.Response, error)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return resp, fmt.Errorf("HTTP Status Code: %d", resp.StatusCode)
}
return resp, nil
}

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

import (
"encoding/json"
"fmt"
"github.com/PagerDuty/go-pagerduty"
log "github.com/Sirupsen/logrus"
"github.com/mitchellh/cli"
"os"
"strings"
)

type AddonInstall struct {
Meta
}

func AddonInstallCommand() (cli.Command, error) {
return &AddonInstall{}, nil
}

func (c *AddonInstall) Help() string {
helpText := `
pd addon install <FILE> Install a new addon
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

func (c *AddonInstall) Synopsis() string {
return "Install a new addon"
}

func (c *AddonInstall) Run(args []string) int {
flags := c.Meta.FlagSet("addon install")
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
}
client := c.Meta.Client()
var a pagerduty.Addon
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(&a); err != nil {
log.Errorln("Failed to decode json. Error:", err)
return -1
}
log.Debugf("%#v", a)
if err := client.InstallAddon(a); err != nil {
log.Error(err)
return -1
}
return 0
}
4 changes: 3 additions & 1 deletion command/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const (

func loadCommands() map[string]cli.CommandFactory {
return map[string]cli.CommandFactory{
"addon list": AddonListCommand,
"addon list": AddonListCommand,
"addon install": AddonInstallCommand,

"escalation-policy list": EscalationPolicyListCommand,
"escalation-policy create": EscalationPolicyCreateCommand,
"escalation-policy delete": EscalationPolicyDeleteCommand,
Expand Down
5 changes: 5 additions & 0 deletions examples/addon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "full_page_addon",
"name": "Cha0tic addon",
"src": "https://github.com/PagerDuty/blender"
}
5 changes: 1 addition & 4 deletions schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ func (c *Client) ListSchedules(o ListSchedulesOptions) (*ListSchedulesResponse,
return &result, c.decodeJson(resp, &result)
}

type CreateScheduleOptions struct {
}

func (c *Client) CreateSchedule(s Schedule) error {
data := make(map[string]Schedule)
data["schedule"] = s
Expand Down Expand Up @@ -117,7 +114,7 @@ func (c *Client) GetSchedule(id string, o GetScheduleOptions) (*Schedule, error)
}
s, ok := result["schedule"]
if !ok {
return nil, fmt.Errorf("JSON responsde does not have schedule field")
return nil, fmt.Errorf("JSON response does not have schedule field")
}
return &s, nil
}
Expand Down