From b943a99d87b48da97eb5b1590eae88d0d2a738c6 Mon Sep 17 00:00:00 2001 From: Tim Heckman Date: Sat, 27 Feb 2021 11:44:53 -0800 Subject: [PATCH] Update maintenance_window.go to accept a context.Context Updates #267 --- maintenance_window.go | 88 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 18 deletions(-) diff --git a/maintenance_window.go b/maintenance_window.go index 262c90ad..545791cf 100644 --- a/maintenance_window.go +++ b/maintenance_window.go @@ -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 } @@ -80,19 +115,32 @@ 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) } @@ -100,14 +148,18 @@ func getMaintenanceWindowFromResponse(c *Client, resp *http.Response, err 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 }