app/vmbackup/snapshot: add missing status code check for the returned response when working with snapshot API

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/929
This commit is contained in:
Aliaksandr Valialkin 2020-11-30 14:49:07 +02:00
parent d5ba66c303
commit 1dce37b2fa

View file

@ -25,16 +25,17 @@ func Create(createSnapshotURL string) (string, error) {
if err != nil {
return "", err
}
resp, err := http.Get(u.String())
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected status code returned from %q; expecting %d; got %d; response body: %q", createSnapshotURL, resp.StatusCode, http.StatusOK, body)
}
snap := snapshot{}
err = json.Unmarshal(body, &snap)
@ -58,21 +59,21 @@ func Delete(deleteSnapshotURL string, snapshotName string) error {
formData := url.Values{
"snapshot": {snapshotName},
}
u, err := url.Parse(deleteSnapshotURL)
if err != nil {
return err
}
resp, err := http.PostForm(u.String(), formData)
if err != nil {
return err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code returned from %q; expecting %d; got %d; response body: %q", deleteSnapshotURL, resp.StatusCode, http.StatusOK, body)
}
snap := snapshot{}
err = json.Unmarshal(body, &snap)