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)implement integration creation #43

Merged
merged 1 commit into from
Oct 5, 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
45 changes: 43 additions & 2 deletions command/service_integration_create.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package main

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

type ServiceIntegrationCreate struct {
Meta
}

func ServiceIntegrationCreateCommand() (cli.Command, error) {
Expand All @@ -14,14 +20,49 @@ func ServiceIntegrationCreateCommand() (cli.Command, error) {

func (c *ServiceIntegrationCreate) Help() string {
helpText := `
`
pd service integration create <SERVICE_ID> <FILE> Create a new integration from json file
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

func (c *ServiceIntegrationCreate) Synopsis() string {
return "Create a new integration belonging to a service"
return "Create a new integration within service"
}

func (c *ServiceIntegrationCreate) Run(args []string) int {
flags := c.Meta.FlagSet("service integration create")
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 i pagerduty.Integration
if len(flags.Args()) != 2 {
log.Error("Please specify service id and input json file")
return -1
}
log.Info("Service id is:", flags.Arg(0))
log.Info("Input file is:", flags.Arg(1))
f, err := os.Open(flags.Arg(1))
if err != nil {
log.Error(err)
return -1
}
defer f.Close()
decoder := json.NewDecoder(f)
if err := decoder.Decode(&i); err != nil {
log.Errorln("Failed to decode json. Error:", err)
return -1
}
log.Debugf("%#v", i)
if _, err := client.CreateIntegration(flags.Arg(0), i); err != nil {
log.Error(err)
return -1
}
return 0
}
4 changes: 3 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func (c *Client) DeleteService(id string) error {

// CreateIntegration creates a new integration belonging to a service.
func (c *Client) CreateIntegration(id string, i Integration) (*Integration, error) {
resp, err := c.post("/services/"+id+"/integrations", i)
data := make(map[string]Integration)
data["integration"] = i
resp, err := c.post("/services/"+id+"/integrations", data)
return getIntegrationFromResponse(c, resp, err)
}

Expand Down