Skip to content

Commit

Permalink
Implements Event Orchestrations API (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
EronWright authored Oct 7, 2022
1 parent bb8fa27 commit b6aa0d7
Show file tree
Hide file tree
Showing 8 changed files with 1,177 additions and 0 deletions.
69 changes: 69 additions & 0 deletions command/event_orchestration_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

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

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

type EventOrchestrationCreate struct {
Meta
}

func EventOrchestrationCreateCommand() (cli.Command, error) {
return &EventOrchestrationCreate{}, nil
}

func (c *EventOrchestrationCreate) Help() string {
helpText := `
pd event-orchestration create <FILE> Create a new event orchestration
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

func (c *EventOrchestrationCreate) Synopsis() string {
return "Creates a new event orchestration"
}

func (c *EventOrchestrationCreate) Run(args []string) int {
flags := c.Meta.FlagSet("event-orchestration 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 eo pagerduty.Orchestration
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(&eo); err != nil {
log.Errorln("Failed to decode json. Error:", err)
return -1
}
log.Debugf("%#v", eo)
if _, err := client.CreateOrchestrationWithContext(context.Background(), eo); err != nil {
log.Error(err)
return -1
}
return 0
}
62 changes: 62 additions & 0 deletions command/event_orchestration_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"context"
"fmt"
"strings"

"github.com/mitchellh/cli"
log "github.com/sirupsen/logrus"
)

type EventOrchestrationDelete struct {
Meta
}

func EventOrchestrationDeleteCommand() (cli.Command, error) {
return &EventOrchestrationDelete{}, nil
}

func (c *EventOrchestrationDelete) Help() string {
helpText := `
pd event-orchestration delete Delete an event orchestration
Options:
-id The event orchestration ID
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

func (c *EventOrchestrationDelete) Synopsis() string {
return "Delete an existing event orchestration and its rules"
}

func (c *EventOrchestrationDelete) Run(args []string) int {
var eoID string
flags := c.Meta.FlagSet("event-orchestration delete")
flags.Usage = func() { fmt.Println(c.Help()) }
flags.StringVar(&eoID, "id", "", "Event orchestration ID")

if err := flags.Parse(args); err != nil {
log.Error(err)
return -1
}

if eoID == "" {
log.Error("You must provide an event orchestration ID")
return -1
}

if err := c.Meta.Setup(); err != nil {
log.Error(err)
return -1
}

client := c.Meta.Client()
if err := client.DeleteOrchestrationWithContext(context.Background(), eoID); err != nil {
log.Error(err)
return -1
}

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

import (
"context"
"fmt"
"strings"

"github.com/PagerDuty/go-pagerduty"
"github.com/mitchellh/cli"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)

type EventOrchestrationList struct {
Meta
}

func (c *EventOrchestrationList) Help() string {
helpText := `
pd event-orchestration list List all of the existing event orchestrations
Options:
-sort Sort results by property (name:asc, name:desc, routes:asc, routes:desc, created_at:asc, created_at:desc)
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

func (c *EventOrchestrationList) Synopsis() string {
return "List all of the existing event orchestrations"
}

func EventOrchestrationListCommand() (cli.Command, error) {
return &EventOrchestrationList{}, nil
}

func (c *EventOrchestrationList) Run(args []string) int {
var sortBy string

flags := c.Meta.FlagSet("event-orchestration list")
flags.Usage = func() { fmt.Println(c.Help()) }
flags.StringVar(&sortBy, "sort", "", "Sort results by name, routes, or created_at (ascending or descending)")

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.ListOrchestrationsOptions{
SortBy: sortBy,
}
if eps, err := client.ListOrchestrationsWithContext(context.Background(), opts); err != nil {
log.Error(err)
return -1
} else {
for i, p := range eps.Orchestrations {
if i > 0 {
fmt.Println("---")
}
data, err := yaml.Marshal(p)
if err != nil {
log.Error(err)
return -1
}
fmt.Println(string(data))
}
}
return 0
}
84 changes: 84 additions & 0 deletions command/event_orchestration_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"context"
"fmt"
"strings"

"github.com/PagerDuty/go-pagerduty"
"github.com/mitchellh/cli"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)

type EventOrchestrationShow struct {
Meta
}

func (c *EventOrchestrationShow) Help() string {
helpText := `
pd event-orchestration show Show event orchestrations
Options:
-id
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

func (c *EventOrchestrationShow) Synopsis() string {
return "Show information about an existing event orchestration and its rules"
}

func EventOrchestrationShowCommand() (cli.Command, error) {
return &EventOrchestrationShow{}, nil
}

func (c *EventOrchestrationShow) Run(args []string) int {
var eoID string
flags := c.Meta.FlagSet("event-orchestration show")
flags.Usage = func() { fmt.Println(c.Help()) }
flags.StringVar(&eoID, "id", "", "Event orchestration 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 eoID == "" {
log.Error("You must provide event orchestration id")
return -1
}
client := c.Meta.Client()
o := &pagerduty.GetOrchestrationOptions{}
ep, err := client.GetOrchestrationWithContext(context.Background(), eoID, o)
if err != nil {
log.Error(err)
return -1
}
data, err := yaml.Marshal(ep)
if err != nil {
log.Error(err)
return -1
}
fmt.Println(string(data))
fmt.Println("---")

ro := &pagerduty.GetOrchestrationRouterOptions{}
rules, err := client.GetOrchestrationRouterWithContext(context.Background(), eoID, ro)
if err != nil {
log.Error(err)
return -1
}
data, err = yaml.Marshal(rules)
if err != nil {
log.Error(err)
return -1
}
fmt.Println(string(data))

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

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"strings"

"github.com/PagerDuty/go-pagerduty"
log "github.com/sirupsen/logrus"

"github.com/mitchellh/cli"
)

type EventOrchestrationUpdate struct {
Meta
}

func EventOrchestrationUpdateCommand() (cli.Command, error) {
return &EventOrchestrationUpdate{}, nil
}

func (c *EventOrchestrationUpdate) Help() string {
helpText := `
pd event-orchestration update <FILE> Update an event orchestration from json file
Options:
-id
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

func (c *EventOrchestrationUpdate) Synopsis() string {
return "Update an existing event orchestration"
}

func (c *EventOrchestrationUpdate) Run(args []string) int {
var eoID string
flags := c.Meta.FlagSet("event-orchestration update")
flags.Usage = func() { fmt.Println(c.Help()) }
flags.StringVar(&eoID, "id", "", "Event orchestration id")
if err := flags.Parse(args); err != nil {
log.Error(err)
return -1
}
if err := c.Meta.Setup(); err != nil {
log.Error(err)
return -1
}
if eoID == "" {
log.Error("You must provide event orchestration id")
return -1
}

client := c.Meta.Client()
var eo pagerduty.Orchestration
if len(flags.Args()) != 1 {
log.Error("Please specify input json file")
return -1
}

log.Info("Input file is:", flags.Arg(0))
f, err := ioutil.ReadFile(flags.Arg(0))
if err != nil {
log.Error(err)
return -1
}

if err := json.Unmarshal(f, &eo); err != nil {
log.Errorln("Failed to decode json. Error:", err)
return -1
}

log.Debugf("%#v", eo)
if _, err := client.UpdateOrchestrationWithContext(context.Background(), eoID, eo); err != nil {
log.Error(err)
return -1
}

return 0
}
6 changes: 6 additions & 0 deletions command/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func loadCommands() map[string]cli.CommandFactory {
"escalation-policy show": EscalationPolicyShowCommand,
"escalation-policy update": EscalationPolicyUpdateCommand,

"event-orchestration list": EventOrchestrationListCommand,
"event-orchestration create": EventOrchestrationCreateCommand,
"event-orchestration delete": EventOrchestrationDeleteCommand,
"event-orchestration show": EventOrchestrationShowCommand,
"event-orchestration update": EventOrchestrationUpdateCommand,

"event-v2 manage": EventV2ManageCommand,

"incident list": IncidentListCommand,
Expand Down
Loading

0 comments on commit b6aa0d7

Please sign in to comment.