[lxc-devel] [lxd/master] Add support for non-JSON endpoints in lxc/query
stgraber on Github
lxc-bot at linuxcontainers.org
Mon Apr 16 17:46:22 UTC 2018
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180416/fd13d613/attachment.bin>
-------------- next part --------------
From b122c7aa1c5085cfc3d3300e559cf9af782437dd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 16 Apr 2018 13:43:59 -0400
Subject: [PATCH 1/2] client: Expose http URL in ConnectionInfo
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
client/interfaces.go | 1 +
client/lxd.go | 1 +
client/simplestreams.go | 1 +
3 files changed, 3 insertions(+)
diff --git a/client/interfaces.go b/client/interfaces.go
index 97e884f44..7a13841f2 100644
--- a/client/interfaces.go
+++ b/client/interfaces.go
@@ -206,6 +206,7 @@ type ConnectionInfo struct {
Addresses []string
Certificate string
Protocol string
+ URL string
}
// The ImageCreateArgs struct is used for direct image upload
diff --git a/client/lxd.go b/client/lxd.go
index 96458efed..593cf3c24 100644
--- a/client/lxd.go
+++ b/client/lxd.go
@@ -44,6 +44,7 @@ func (r *ProtocolLXD) GetConnectionInfo() (*ConnectionInfo, error) {
info := ConnectionInfo{}
info.Certificate = r.httpCertificate
info.Protocol = "lxd"
+ info.URL = r.httpHost
urls := []string{}
if r.httpProtocol == "https" {
diff --git a/client/simplestreams.go b/client/simplestreams.go
index 68fa55b8b..471cf54b4 100644
--- a/client/simplestreams.go
+++ b/client/simplestreams.go
@@ -23,6 +23,7 @@ func (r *ProtocolSimpleStreams) GetConnectionInfo() (*ConnectionInfo, error) {
info.Addresses = []string{r.httpHost}
info.Certificate = r.httpCertificate
info.Protocol = "simplestreams"
+ info.URL = r.httpHost
return &info, nil
}
From 55230a54dfbfc169d5c5d544b6b9a8242d600822 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 16 Apr 2018 13:44:45 -0400
Subject: [PATCH 2/2] 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 at ubuntu.com>
---
lxc/query.go | 45 ++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 44 insertions(+), 1 deletion(-)
diff --git a/lxc/query.go b/lxc/query.go
index 1c8efc755..5e8f948a5 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 != "" {
More information about the lxc-devel
mailing list