github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/client/warnings.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package client
    21  
    22  import (
    23  	"bytes"
    24  	"encoding/json"
    25  	"net/url"
    26  	"time"
    27  )
    28  
    29  // A Warning is a short messages that's meant to alert about system events.
    30  // There'll only ever be one Warning with the same message, and it can be
    31  // silenced for a while before repeating. After a (supposedly longer) while
    32  // it'll go away on its own (unless it recurrs).
    33  type Warning struct {
    34  	Message     string        `json:"message"`
    35  	FirstAdded  time.Time     `json:"first-added"`
    36  	LastAdded   time.Time     `json:"last-added"`
    37  	LastShown   time.Time     `json:"last-shown,omitempty"`
    38  	ExpireAfter time.Duration `json:"expire-after,omitempty"`
    39  	RepeatAfter time.Duration `json:"repeat-after,omitempty"`
    40  }
    41  
    42  type jsonWarning struct {
    43  	Warning
    44  	ExpireAfter string `json:"expire-after,omitempty"`
    45  	RepeatAfter string `json:"repeat-after,omitempty"`
    46  }
    47  
    48  // WarningsOptions contains options for querying snapd for warnings
    49  // supported options:
    50  // - All: return all warnings, instead of only the un-okayed ones.
    51  type WarningsOptions struct {
    52  	All bool
    53  }
    54  
    55  // Warnings returns the list of un-okayed warnings.
    56  func (client *Client) Warnings(opts WarningsOptions) ([]*Warning, error) {
    57  	var jws []*jsonWarning
    58  	q := make(url.Values)
    59  	if opts.All {
    60  		q.Add("select", "all")
    61  	}
    62  	_, err := client.doSync("GET", "/v2/warnings", q, nil, nil, &jws)
    63  
    64  	ws := make([]*Warning, len(jws))
    65  	for i, jw := range jws {
    66  		ws[i] = &jw.Warning
    67  		ws[i].ExpireAfter, _ = time.ParseDuration(jw.ExpireAfter)
    68  		ws[i].RepeatAfter, _ = time.ParseDuration(jw.RepeatAfter)
    69  	}
    70  
    71  	return ws, err
    72  }
    73  
    74  type warningsAction struct {
    75  	Action    string    `json:"action"`
    76  	Timestamp time.Time `json:"timestamp"`
    77  }
    78  
    79  // Okay asks snapd to chill about the warnings that would have been returned by
    80  // Warnings at the given time.
    81  func (client *Client) Okay(t time.Time) error {
    82  	var body bytes.Buffer
    83  	var op = warningsAction{Action: "okay", Timestamp: t}
    84  	if err := json.NewEncoder(&body).Encode(op); err != nil {
    85  		return err
    86  	}
    87  	_, err := client.doSync("POST", "/v2/warnings", nil, nil, &body, nil)
    88  	return err
    89  }