[lxc-devel] [lxd/master] Fix missing project in some URLs

stgraber on Github lxc-bot at linuxcontainers.org
Thu Oct 11 04:24:55 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/20181011/a0e75698/attachment.bin>
-------------- next part --------------
From 20aa1bc8b30aa9d4344fe3b66577acecbb83aa79 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 10 Oct 2018 23:52:15 -0400
Subject: [PATCH 1/2] client: Always use the "do()" wrapper
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/lxd_containers.go | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/client/lxd_containers.go b/client/lxd_containers.go
index cf48cf97c5..285956878e 100644
--- a/client/lxd_containers.go
+++ b/client/lxd_containers.go
@@ -1388,7 +1388,7 @@ func (r *ProtocolLXD) GetContainerTemplateFile(containerName string, templateNam
 	}
 
 	// Send the request
-	resp, err := r.http.Do(req)
+	resp, err := r.do(req)
 	if err != nil {
 		return nil, err
 	}
@@ -1432,7 +1432,7 @@ func (r *ProtocolLXD) setContainerTemplateFile(containerName string, templateNam
 	}
 
 	// Send the request
-	resp, err := r.http.Do(req)
+	resp, err := r.do(req)
 	// Check the return value for a cleaner error
 	if resp.StatusCode != http.StatusOK {
 		_, _, err := lxdParseResponse(resp)

From ef17383e01c7cf4783e4a46b70b4b50ce431deb1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 11 Oct 2018 00:02:13 -0400
Subject: [PATCH 2/2] client: Fix URLs with missing project/target
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/lxd.go            | 25 ++++++++++++++++++-------
 client/lxd_containers.go | 38 +++++++++++++++++++++++++++++++++++++-
 client/lxd_images.go     |  6 +++++-
 3 files changed, 60 insertions(+), 9 deletions(-)

diff --git a/client/lxd.go b/client/lxd.go
index 86759792da..cca5d242bb 100644
--- a/client/lxd.go
+++ b/client/lxd.go
@@ -77,6 +77,7 @@ func (r *ProtocolLXD) GetHTTPClient() (*http.Client, error) {
 
 // Do performs a Request, using macaroon authentication if set.
 func (r *ProtocolLXD) do(req *http.Request) (*http.Response, error) {
+
 	if r.bakeryClient != nil {
 		r.addMacaroonHeaders(req)
 		return r.bakeryClient.Do(req)
@@ -225,14 +226,11 @@ func (r *ProtocolLXD) rawQuery(method string, url string, data interface{}, ETag
 	return lxdParseResponse(resp)
 }
 
-func (r *ProtocolLXD) query(method string, path string, data interface{}, ETag string) (*api.Response, string, error) {
-	// Generate the URL
-	url := fmt.Sprintf("%s/1.0%s", r.httpHost, path)
-
+func (r *ProtocolLXD) setQueryAttributes(uri string) (string, error) {
 	// Parse the full URI
-	fields, err := neturl.Parse(url)
+	fields, err := neturl.Parse(uri)
 	if err != nil {
-		return nil, "", err
+		return "", err
 	}
 
 	// Extract query fields and update for cluster targeting or project
@@ -250,8 +248,21 @@ func (r *ProtocolLXD) query(method string, path string, data interface{}, ETag s
 	}
 	fields.RawQuery = values.Encode()
 
+	return fields.String(), nil
+}
+
+func (r *ProtocolLXD) query(method string, path string, data interface{}, ETag string) (*api.Response, string, error) {
+	// Generate the URL
+	url := fmt.Sprintf("%s/1.0%s", r.httpHost, path)
+
+	// Add project/target
+	url, err := r.setQueryAttributes(url)
+	if err != nil {
+		return nil, "", err
+	}
+
 	// Run the actual query
-	return r.rawQuery(method, fields.String(), data, ETag)
+	return r.rawQuery(method, url, data, ETag)
 }
 
 func (r *ProtocolLXD) queryStruct(method string, path string, data interface{}, ETag string, target interface{}) (string, error) {
diff --git a/client/lxd_containers.go b/client/lxd_containers.go
index 285956878e..3b4fccf05f 100644
--- a/client/lxd_containers.go
+++ b/client/lxd_containers.go
@@ -757,6 +757,11 @@ func (r *ProtocolLXD) GetContainerFile(containerName string, path string) (io.Re
 		return nil, nil, err
 	}
 
+	requestURL, err = r.setQueryAttributes(requestURL)
+	if err != nil {
+		return nil, nil, err
+	}
+
 	req, err := http.NewRequest("GET", requestURL, nil)
 	if err != nil {
 		return nil, nil, err
@@ -836,7 +841,14 @@ func (r *ProtocolLXD) CreateContainerFile(containerName string, path string, arg
 	}
 
 	// Prepare the HTTP request
-	req, err := http.NewRequest("POST", fmt.Sprintf("%s/1.0/containers/%s/files?path=%s", r.httpHost, url.QueryEscape(containerName), url.QueryEscape(path)), args.Content)
+	requestURL := fmt.Sprintf("%s/1.0/containers/%s/files?path=%s", r.httpHost, url.QueryEscape(containerName), url.QueryEscape(path))
+
+	requestURL, err := r.setQueryAttributes(requestURL)
+	if err != nil {
+		return err
+	}
+
+	req, err := http.NewRequest("POST", requestURL, args.Content)
 	if err != nil {
 		return err
 	}
@@ -1283,6 +1295,12 @@ func (r *ProtocolLXD) GetContainerLogfiles(name string) ([]string, error) {
 func (r *ProtocolLXD) GetContainerLogfile(name string, filename string) (io.ReadCloser, error) {
 	// Prepare the HTTP request
 	url := fmt.Sprintf("%s/1.0/containers/%s/logs/%s", r.httpHost, url.QueryEscape(name), url.QueryEscape(filename))
+
+	url, err := r.setQueryAttributes(url)
+	if err != nil {
+		return nil, err
+	}
+
 	req, err := http.NewRequest("GET", url, nil)
 	if err != nil {
 		return nil, err
@@ -1377,6 +1395,12 @@ func (r *ProtocolLXD) GetContainerTemplateFile(containerName string, templateNam
 	}
 
 	url := fmt.Sprintf("%s/1.0/containers/%s/metadata/templates?path=%s", r.httpHost, url.QueryEscape(containerName), url.QueryEscape(templateName))
+
+	url, err := r.setQueryAttributes(url)
+	if err != nil {
+		return nil, err
+	}
+
 	req, err := http.NewRequest("GET", url, nil)
 	if err != nil {
 		return nil, err
@@ -1420,6 +1444,12 @@ func (r *ProtocolLXD) setContainerTemplateFile(containerName string, templateNam
 	}
 
 	url := fmt.Sprintf("%s/1.0/containers/%s/metadata/templates?path=%s", r.httpHost, url.QueryEscape(containerName), url.QueryEscape(templateName))
+
+	url, err := r.setQueryAttributes(url)
+	if err != nil {
+		return err
+	}
+
 	req, err := http.NewRequest(httpMethod, url, content)
 	if err != nil {
 		return err
@@ -1532,6 +1562,12 @@ func (r *ProtocolLXD) GetContainerConsoleLog(containerName string, args *Contain
 
 	// Prepare the HTTP request
 	url := fmt.Sprintf("%s/1.0/containers/%s/console", r.httpHost, url.QueryEscape(containerName))
+
+	url, err := r.setQueryAttributes(url)
+	if err != nil {
+		return nil, err
+	}
+
 	req, err := http.NewRequest("GET", url, nil)
 	if err != nil {
 		return nil, err
diff --git a/client/lxd_images.go b/client/lxd_images.go
index 2c5064ff53..43b8237c9c 100644
--- a/client/lxd_images.go
+++ b/client/lxd_images.go
@@ -391,7 +391,11 @@ func (r *ProtocolLXD) CreateImage(image api.ImagesPost, args *ImageCreateArgs) (
 	}
 
 	// Prepare the HTTP request
-	reqURL := fmt.Sprintf("%s/1.0/images", r.httpHost)
+	reqURL, err := r.setQueryAttributes(fmt.Sprintf("%s/1.0/images", r.httpHost))
+	if err != nil {
+		return nil, err
+	}
+
 	req, err := http.NewRequest("POST", reqURL, body)
 	if err != nil {
 		return nil, err


More information about the lxc-devel mailing list