The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/4462

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===

From b122c7aa1c5085cfc3d3300e559cf9af782437dd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgra...@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 <stgra...@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?= <stgra...@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 <stgra...@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 != "" {
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to