[lxc-devel] [lxd/stable-2.0] client: fix profiles list

brauner on Github lxc-bot at linuxcontainers.org
Tue May 9 20:58:26 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 381 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170509/f52b1747/attachment.bin>
-------------- next part --------------
From 0525b3e99027e6c725bef9630be90feaf3fb48cb Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 9 May 2017 22:56:22 +0200
Subject: [PATCH] client: fix profiles list

Closes #3304.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 client.go | 33 ++++++++++-----------------------
 1 file changed, 10 insertions(+), 23 deletions(-)

diff --git a/client.go b/client.go
index d8cacf0..f087d42 100644
--- a/client.go
+++ b/client.go
@@ -2157,37 +2157,24 @@ func (c *Client) ListProfiles() ([]string, error) {
 		return nil, fmt.Errorf("This function isn't supported by public remotes.")
 	}
 
-	resp, err := c.get("profiles")
+	resp, err := c.get("profiles?recursion=1")
 	if err != nil {
 		return nil, err
 	}
 
-	var result []string
-
-	if err := resp.MetadataAsStruct(&result); err != nil {
+	profiles := []api.Profile{}
+	if err := resp.MetadataAsStruct(&profiles); err != nil {
 		return nil, err
 	}
 
-	names := []string{}
-
-	for _, url := range result {
-		toScan := strings.Replace(url, "/", " ", -1)
-		urlVersion := ""
-		name := ""
-		count, err := fmt.Sscanf(toScan, " %s profiles %s", &urlVersion, &name)
-		if err != nil {
-			return nil, err
-		}
-
-		if count != 2 {
-			return nil, fmt.Errorf("bad profile url %s", url)
-		}
-
-		if urlVersion != version.APIVersion {
-			return nil, fmt.Errorf("bad version in profile url")
-		}
+	if len(profiles) == 0 {
+		return nil, nil
+	}
 
-		names = append(names, name)
+	// spare a few allocation cycles
+	names := make([]string, len(profiles))
+	for i := 0; i < len(profiles); i++ {
+		names[i] = profiles[i].Name
 	}
 
 	return names, nil


More information about the lxc-devel mailing list