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

implement missing maintenance-window subcommands #363

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 6 additions & 4 deletions command/maintenance_window_create.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

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

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

type MaintenanceWindowCreate struct {
Expand Down Expand Up @@ -59,7 +61,7 @@ func (c *MaintenanceWindowCreate) Run(args []string) int {
return -1
}
log.Debugf("%#v", m)
if _, err := client.CreateMaintenanceWindows(m); err != nil {
if _, err := client.CreateMaintenanceWindowWithContext(context.Background(), "", m); err != nil {
Hsn723 marked this conversation as resolved.
Show resolved Hide resolved
log.Error(err)
return -1
}
Expand Down
35 changes: 33 additions & 2 deletions command/maintenance_window_delete.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package main

import (
"github.com/mitchellh/cli"
"context"
"fmt"
"strings"

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

type MaintenanceWindowDelete struct {
Meta
}

func MaintenanceWindowDeleteCommand() (cli.Command, error) {
Expand All @@ -14,7 +19,11 @@ func MaintenanceWindowDeleteCommand() (cli.Command, error) {

func (c *MaintenanceWindowDelete) Help() string {
helpText := `
`
maintenance-window delete Delete or end a maintenance window

Options:
-id The maintenance window ID
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

Expand All @@ -23,5 +32,27 @@ func (c *MaintenanceWindowDelete) Synopsis() string {
}

func (c *MaintenanceWindowDelete) Run(args []string) int {
var mwID string
flags := c.Meta.FlagSet("maintenance-window delete")
flags.Usage = func() { fmt.Println(c.Help())}
Hsn723 marked this conversation as resolved.
Show resolved Hide resolved
flags.StringVar(&mwID, "id", "", "Maintenance window 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 mwID == "" {
Hsn723 marked this conversation as resolved.
Show resolved Hide resolved
log.Error("You must provide a maintenance window ID")
return -1
}
client := c.Meta.Client()
if err := client.DeleteMaintenanceWindowWithContext(context.Background(), mwID); err != nil {
log.Error(err)
return -1
}
Hsn723 marked this conversation as resolved.
Show resolved Hide resolved
return 0
}
60 changes: 58 additions & 2 deletions command/maintenance_window_list.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package main

import (
"github.com/mitchellh/cli"
"context"
"fmt"
"strings"

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

type MaintenanceWindowList struct {
Meta
}

func MaintenanceWindowListCommand() (cli.Command, error) {
Expand All @@ -14,7 +21,15 @@ func MaintenanceWindowListCommand() (cli.Command, error) {

func (c *MaintenanceWindowList) Help() string {
helpText := `
`
pd maintenance-window list List maintenance windows

Options:
-filter Filter result by state
-include Additional details to include
-query Filter results by query
-service-id Filter result by service ids
-team-id Filter results by team ids
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

Expand All @@ -23,5 +38,46 @@ func (c *MaintenanceWindowList) Synopsis() string {
}

func (c *MaintenanceWindowList) Run(args []string) int {
var includes, serviceIDs, teamIDs []string
var query, filter string
flags := c.Meta.FlagSet("maintenance-window list")
flags.Usage = func() { fmt.Println(c.Help())}
Hsn723 marked this conversation as resolved.
Show resolved Hide resolved
flags.Var((*ArrayFlags)(&includes), "includes", "Additional details to include (can be specified multiple times)")
flags.Var((*ArrayFlags)(&serviceIDs), "service-id", "Show maintenance windows for the specified services only (can be specified multiple times)")
flags.Var((*ArrayFlags)(&teamIDs), "team-id", "Show maintenance windows for the specified teams only (can be specified multiple times)")
flags.StringVar(&filter, "filter", "all", "Filter results by maintenance window state (past, future, ongoing, open, all)")
flags.StringVar(&query, "query", "", "Filter results showing only tags whose labels match the query")

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.ListMaintenanceWindowsOptions{
Query: query,
Includes: includes,
TeamIDs: teamIDs,
ServiceIDs: serviceIDs,
Filter: filter,
}
mws, err := client.ListMaintenanceWindowsWithContext(context.Background(), opts)
if err != nil {
log.Error(err)
return -1
}
for i, mw := range mws.MaintenanceWindows {
fmt.Println("Entry: ", i)
data, err := yaml.Marshal(mw)
if err != nil {
log.Error(err)
return -1
}
fmt.Println(string(data))
}
Hsn723 marked this conversation as resolved.
Show resolved Hide resolved

return 0
}
50 changes: 48 additions & 2 deletions command/maintenance_window_show.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package main

import (
"github.com/mitchellh/cli"
"context"
"fmt"
"strings"

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

type MaintenanceWindowShow struct {
Meta
}

func MaintenanceWindowShowCommand() (cli.Command, error) {
Expand All @@ -14,7 +21,12 @@ func MaintenanceWindowShowCommand() (cli.Command, error) {

func (c *MaintenanceWindowShow) Help() string {
helpText := `
`
maintenance-window show Show a maintenance window

Options:
-id The maintenance window ID
-include Additional details to include
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

Expand All @@ -23,5 +35,39 @@ func (c *MaintenanceWindowShow) Synopsis() string {
}

func (c *MaintenanceWindowShow) Run(args []string) int {
var includes []string
var mwID string
flags := c.Meta.FlagSet("maintenance-window show")
flags.Usage = func() { fmt.Println(c.Help())}
flags.Var((*ArrayFlags)(&includes), "includes", "Additional details to include (can be specified multiple times)")
flags.StringVar(&mwID, "id", "", "Maintenance window 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 mwID == "" {
log.Error("You must provide a maintenance window ID")
return -1
}
client := c.Meta.Client()
opts := pagerduty.GetMaintenanceWindowOptions{
Includes: includes,
}
mw, err := client.GetMaintenanceWindowWithContext(context.Background(), mwID, opts)
if err != nil {
log.Error(err)
return -1
}
data, err := yaml.Marshal(mw)
if err != nil {
log.Error(err)
return -1
}
fmt.Println(string(data))
return 0
}
47 changes: 45 additions & 2 deletions command/maintenance_window_update.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package main

import (
"github.com/mitchellh/cli"
"context"
"encoding/json"
"fmt"
"os"
"strings"

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

"github.com/mitchellh/cli"
)

type MaintenanceWindowUpdate struct {
Meta
}

func MaintenanceWindowUpdateCommand() (cli.Command, error) {
Expand All @@ -14,7 +23,8 @@ func MaintenanceWindowUpdateCommand() (cli.Command, error) {

func (c *MaintenanceWindowUpdate) Help() string {
helpText := `
`
maintenance-window update <FILE> Update a maintenance window from json file
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

Expand All @@ -23,5 +33,38 @@ func (c *MaintenanceWindowUpdate) Synopsis() string {
}

func (c *MaintenanceWindowUpdate) Run(args []string) int {
flags := c.Meta.FlagSet("maintenance-window update")
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 mw pagerduty.MaintenanceWindow
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(&mw); err != nil {
Hsn723 marked this conversation as resolved.
Show resolved Hide resolved
log.Errorln("Failed to decode json. Error:", err)
return -1
}
log.Debugf("%#v", mw)
if _, err := client.UpdateMaintenanceWindowWithContext(context.Background(), mw); err != nil {
log.Error(err)
return -1
}
return 0
}