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

Update maintenance_window.go to accept a context.Context #298

Merged
merged 1 commit into from
Mar 16, 2021
Merged
Changes from all commits
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
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
}