github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/client/validate.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2020 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 "fmt" 26 "net/url" 27 28 "golang.org/x/xerrors" 29 ) 30 31 // ValidateApplyOptions carries options for ApplyValidationSet. 32 type ValidateApplyOptions struct { 33 Mode string 34 Sequence int 35 } 36 37 // ValidationSetResult holds information about a single validation set. 38 type ValidationSetResult struct { 39 AccountID string `json:"account-id"` 40 Name string `json:"name"` 41 // set sequence key and optional pinned-at (=) 42 Sequence int `json:"sequence,omitempty"` 43 PinnedAt int `json:"pinned-at,omitempty"` 44 // set current state 45 Mode string `json:"mode"` 46 Valid bool `json:"valid"` 47 // TODO: flags/states for notes column 48 } 49 50 type postValidationSetData struct { 51 Action string `json:"action"` 52 Mode string `json:"mode,omitempty"` 53 Sequence int `json:"sequence,omitempty"` 54 } 55 56 // ForgetValidationSet forgets the given validation set identified by account, 57 // name and optional sequence (if non-zero). 58 func (client *Client) ForgetValidationSet(accountID, name string, sequence int) error { 59 if accountID == "" || name == "" { 60 return xerrors.Errorf("cannot forget validation set without account ID and name") 61 } 62 63 data := &postValidationSetData{ 64 Action: "forget", 65 Sequence: sequence, 66 } 67 68 var body bytes.Buffer 69 if err := json.NewEncoder(&body).Encode(data); err != nil { 70 return err 71 } 72 path := fmt.Sprintf("/v2/validation-sets/%s/%s", accountID, name) 73 if _, err := client.doSync("POST", path, nil, nil, &body, nil); err != nil { 74 fmt := "cannot forget validation set: %w" 75 return xerrors.Errorf(fmt, err) 76 } 77 return nil 78 } 79 80 // ApplyValidationSet applies the given validation set identified by account and name. 81 func (client *Client) ApplyValidationSet(accountID, name string, opts *ValidateApplyOptions) error { 82 if accountID == "" || name == "" { 83 return xerrors.Errorf("cannot apply validation set without account ID and name") 84 } 85 86 data := &postValidationSetData{ 87 Action: "apply", 88 Mode: opts.Mode, 89 Sequence: opts.Sequence, 90 } 91 92 var body bytes.Buffer 93 if err := json.NewEncoder(&body).Encode(data); err != nil { 94 return err 95 } 96 path := fmt.Sprintf("/v2/validation-sets/%s/%s", accountID, name) 97 if _, err := client.doSync("POST", path, nil, nil, &body, nil); err != nil { 98 fmt := "cannot apply validation set: %w" 99 return xerrors.Errorf(fmt, err) 100 } 101 return nil 102 } 103 104 // ListValidationsSets queries all validation sets. 105 func (client *Client) ListValidationsSets() ([]*ValidationSetResult, error) { 106 var res []*ValidationSetResult 107 if _, err := client.doSync("GET", "/v2/validation-sets", nil, nil, nil, &res); err != nil { 108 fmt := "cannot list validation sets: %w" 109 return nil, xerrors.Errorf(fmt, err) 110 } 111 return res, nil 112 } 113 114 // ValidationSet queries the given validation set identified by account/name. 115 func (client *Client) ValidationSet(accountID, name string, sequence int) (*ValidationSetResult, error) { 116 if accountID == "" || name == "" { 117 return nil, xerrors.Errorf("cannot query validation set without account ID and name") 118 } 119 120 q := url.Values{} 121 if sequence != 0 { 122 q.Set("sequence", fmt.Sprintf("%d", sequence)) 123 } 124 125 var res *ValidationSetResult 126 path := fmt.Sprintf("/v2/validation-sets/%s/%s", accountID, name) 127 if _, err := client.doSync("GET", path, q, nil, nil, &res); err != nil { 128 fmt := "cannot query validation set: %w" 129 return nil, xerrors.Errorf(fmt, err) 130 } 131 return res, nil 132 }