mcst-linux-kernel/patches-2024.06.26/lxd-3.0.0/0055-lxc-query-Add-support-...

84 lines
1.9 KiB
Diff

From e1a244c2c6b0b88cb1e78d8a2e60f03800739b6c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber@ubuntu.com>
Date: Mon, 16 Apr 2018 13:44:45 -0400
Subject: lxc/query: Add support for non-JSON endpoints
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This should handle the file and image endpoints that don't return JSON
by attempting to perform a direct http query when a LXD API query fails.
Closes: #4452
Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
---
lxc/query.go | 45 ++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 44 insertions(+), 1 deletion(-)
diff --git a/lxc/query.go b/lxc/query.go
index 1c8efc75..5e8f948a 100644
--- a/lxc/query.go
+++ b/lxc/query.go
@@ -1,8 +1,11 @@
package main
import (
+ "bytes"
"encoding/json"
"fmt"
+ "io/ioutil"
+ "net/http"
"github.com/spf13/cobra"
@@ -80,7 +83,47 @@ func (c *cmdQuery) Run(cmd *cobra.Command, args []string) error {
// Perform the query
resp, _, err := d.RawQuery(c.flagAction, path, data, "")
if err != nil {
- return err
+ cleanErr := err
+
+ // Lets assume the endpoint is raw output
+ // Get a raw http client
+ httpClient, err := d.GetHTTPClient()
+ if err != nil {
+ return err
+ }
+
+ // Get the URL prefix
+ httpInfo, err := d.GetConnectionInfo()
+ if err != nil {
+ return err
+ }
+
+ // Setup the request
+ req, err := http.NewRequest(c.flagAction, fmt.Sprintf("%s%s", httpInfo.URL, path), bytes.NewReader([]byte(c.flagData)))
+ if err != nil {
+ return err
+ }
+
+ // Set the encoding accordingly
+ req.Header.Set("Content-Type", "plain/text")
+
+ resp, err := httpClient.Do(req)
+ if err != nil {
+ return err
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ return cleanErr
+ }
+
+ content, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return err
+ }
+
+ fmt.Print(string(content))
+
+ return nil
}
if c.flagRespWait && resp.Operation != "" {