-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements Event Orchestrations API (#450)
- Loading branch information
1 parent
bb8fa27
commit b6aa0d7
Showing
8 changed files
with
1,177 additions
and
0 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
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 | ||
} |
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 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 | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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.