Skip to content

Commit

Permalink
Merge pull request #298 from PagerDuty/ctx_maintenance_window
Browse files Browse the repository at this point in the history
Update maintenance_window.go to accept a context.Context
  • Loading branch information
Scott McAllister committed Mar 16, 2021
2 parents fdccb8d + b943a99 commit a03562d
Showing 1 changed file with 70 additions and 18 deletions.
88 changes: 70 additions & 18 deletions maintenance_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,77 @@ type ListMaintenanceWindowsOptions struct {
Filter string `url:"filter,omitempty,brackets"`
}

// ListMaintenanceWindows lists existing maintenance windows, optionally filtered by service and/or team, or whether they are from the past, present or future.
// ListMaintenanceWindows lists existing maintenance windows, optionally
// filtered by service and/or team, or whether they are from the past, present
// or future. It's recommended to use ListMaintenanceWindowsWithContext instead.
func (c *Client) ListMaintenanceWindows(o ListMaintenanceWindowsOptions) (*ListMaintenanceWindowsResponse, error) {
return c.ListMaintenanceWindowsWithContext(context.Background(), o)
}

// ListMaintenanceWindowsWithContext lists existing maintenance windows,
// optionally filtered by service and/or team, or whether they are from the
// past, present or future.
func (c *Client) ListMaintenanceWindowsWithContext(ctx context.Context, o ListMaintenanceWindowsOptions) (*ListMaintenanceWindowsResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(context.TODO(), "/maintenance_windows?"+v.Encode())

resp, err := c.get(ctx, "/maintenance_windows?"+v.Encode())
if err != nil {
return nil, err
}

var result ListMaintenanceWindowsResponse
return &result, c.decodeJSON(resp, &result)
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}

return &result, nil
}

// CreateMaintenanceWindow creates a new maintenance window for the specified services.
// CreateMaintenanceWindow creates a new maintenance window for the specified
// services. It's recommended to use CreateMaintenanceWindowWithContext instead.
func (c *Client) CreateMaintenanceWindow(from string, o MaintenanceWindow) (*MaintenanceWindow, error) {
data := make(map[string]MaintenanceWindow)
return c.CreateMaintenanceWindowWithContext(context.Background(), from, o)
}

// CreateMaintenanceWindowWithContext creates a new maintenance window for the specified services.
func (c *Client) CreateMaintenanceWindowWithContext(ctx context.Context, from string, o MaintenanceWindow) (*MaintenanceWindow, error) {
o.Type = "maintenance_window"
data["maintenance_window"] = o
headers := make(map[string]string)

d := map[string]MaintenanceWindow{
"maintenance_window": o,
}

var h map[string]string
if from != "" {
headers["From"] = from
h = map[string]string{
"From": from,
}
}
resp, err := c.post(context.TODO(), "/maintenance_windows", data, headers)

resp, err := c.post(ctx, "/maintenance_windows", d, h)
return getMaintenanceWindowFromResponse(c, resp, err)
}

// CreateMaintenanceWindows creates a new maintenance window for the specified services.
// Deprecated: Use `CreateMaintenanceWindow` instead.
// Deprecated: Use `CreateMaintenanceWindowWithContext` instead.
func (c *Client) CreateMaintenanceWindows(o MaintenanceWindow) (*MaintenanceWindow, error) {
return c.CreateMaintenanceWindow("", o)
return c.CreateMaintenanceWindowWithContext(context.Background(), "", o)
}

// DeleteMaintenanceWindow deletes an existing maintenance window if it's in the future, or ends it if it's currently on-going.
// DeleteMaintenanceWindow deletes an existing maintenance window if it's in the
// future, or ends it if it's currently on-going. It's recommended to use
// DeleteMaintenanceWindowWithContext instead.
func (c *Client) DeleteMaintenanceWindow(id string) error {
_, err := c.delete(context.TODO(), "/maintenance_windows/"+id)
return c.DeleteMaintenanceWindowWithContext(context.Background(), id)
}

// DeleteMaintenanceWindowWithContext deletes an existing maintenance window if it's in the
// future, or ends it if it's currently on-going.
func (c *Client) DeleteMaintenanceWindowWithContext(ctx context.Context, id string) error {
_, err := c.delete(ctx, "/maintenance_windows/"+id)
return err
}

Expand All @@ -80,34 +115,51 @@ type GetMaintenanceWindowOptions struct {
Includes []string `url:"include,omitempty,brackets"`
}

// GetMaintenanceWindow gets an existing maintenance window.
// GetMaintenanceWindow gets an existing maintenance window. It's recommended to
// use GetMaintenanceWindowWithContext instead.
func (c *Client) GetMaintenanceWindow(id string, o GetMaintenanceWindowOptions) (*MaintenanceWindow, error) {
return c.GetMaintenanceWindowWithContext(context.Background(), id, o)
}

// GetMaintenanceWindowWithContext gets an existing maintenance window.
func (c *Client) GetMaintenanceWindowWithContext(ctx context.Context, id string, o GetMaintenanceWindowOptions) (*MaintenanceWindow, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(context.TODO(), "/maintenance_windows/"+id+"?"+v.Encode())

resp, err := c.get(ctx, "/maintenance_windows/"+id+"?"+v.Encode())
return getMaintenanceWindowFromResponse(c, resp, err)
}

// UpdateMaintenanceWindow updates an existing maintenance window.
// UpdateMaintenanceWindow updates an existing maintenance window. It's
// recommended to use UpdateMaintenanceWindowWithContext instead.
func (c *Client) UpdateMaintenanceWindow(m MaintenanceWindow) (*MaintenanceWindow, error) {
resp, err := c.put(context.TODO(), "/maintenance_windows/"+m.ID, m, nil)
return c.UpdateMaintenanceWindowWithContext(context.Background(), m)
}

// UpdateMaintenanceWindowWithContext updates an existing maintenance window.
func (c *Client) UpdateMaintenanceWindowWithContext(ctx context.Context, m MaintenanceWindow) (*MaintenanceWindow, error) {
resp, err := c.put(ctx, "/maintenance_windows/"+m.ID, m, nil)
return getMaintenanceWindowFromResponse(c, resp, err)
}

func getMaintenanceWindowFromResponse(c *Client, resp *http.Response, err error) (*MaintenanceWindow, error) {
if err != nil {
return nil, err
}

var target map[string]MaintenanceWindow
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
rootNode := "maintenance_window"

const rootNode = "maintenance_window"

t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}

return &t, nil
}

0 comments on commit a03562d

Please sign in to comment.