diff --git a/command/service_integration_create.go b/command/service_integration_create.go index 6451f30f..c84987ed 100644 --- a/command/service_integration_create.go +++ b/command/service_integration_create.go @@ -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) { @@ -14,14 +20,49 @@ func ServiceIntegrationCreateCommand() (cli.Command, error) { func (c *ServiceIntegrationCreate) Help() string { helpText := ` - ` + pd service integration create 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 } diff --git a/service.go b/service.go index 453797f6..4525bfd3 100644 --- a/service.go +++ b/service.go @@ -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) }