[lxc-devel] [lxd/master] lxd/db: Automatically strip ?project=default

stgraber on Github lxc-bot at linuxcontainers.org
Sat Jun 13 03:03:07 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200612/df773b0d/attachment.bin>
-------------- next part --------------
From 7881e3348ed6be1edb53e93a1636a92b6c0ffe3d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 12 Jun 2020 18:42:25 -0400
Subject: [PATCH] lxd/db: Automatically strip ?project=default
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>
---
 lxd/db/profiles.mapper.go    | 5 +++++
 lxd/db/projects.mapper.go    | 5 +++++
 lxd/profiles.go              | 9 ---------
 shared/generate/db/method.go | 7 +++++++
 4 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/lxd/db/profiles.mapper.go b/lxd/db/profiles.mapper.go
index 71f7a2d0c0..256733abfe 100644
--- a/lxd/db/profiles.mapper.go
+++ b/lxd/db/profiles.mapper.go
@@ -278,6 +278,11 @@ func (c *ClusterTx) GetProfiles(filter ProfileFilter) ([]Profile, error) {
 		if value == nil {
 			value = []string{}
 		}
+		for j, entry := range value {
+			if len(entry) > 16 && entry[len(entry)-16:] == "?project=default" {
+				value[j] = entry[0 : len(entry)-16]
+			}
+		}
 		objects[i].UsedBy = value
 	}
 
diff --git a/lxd/db/projects.mapper.go b/lxd/db/projects.mapper.go
index 3b763721f3..01b9ef3cf5 100644
--- a/lxd/db/projects.mapper.go
+++ b/lxd/db/projects.mapper.go
@@ -177,6 +177,11 @@ func (c *ClusterTx) GetProjects(filter ProjectFilter) ([]api.Project, error) {
 		if value == nil {
 			value = []string{}
 		}
+		for j, entry := range value {
+			if len(entry) > 16 && entry[len(entry)-16:] == "?project=default" {
+				value[j] = entry[0 : len(entry)-16]
+			}
+		}
 		objects[i].UsedBy = value
 	}
 
diff --git a/lxd/profiles.go b/lxd/profiles.go
index ac449cf157..7783ecb5d2 100644
--- a/lxd/profiles.go
+++ b/lxd/profiles.go
@@ -179,15 +179,6 @@ func profileGet(d *Daemon, r *http.Request) response.Response {
 		return response.SmartError(err)
 	}
 
-	// For backward-compatibility, we strip the "?project" query parameter
-	// in case the project is the default one.
-	for i, uri := range resp.UsedBy {
-		suffix := "?project=default"
-		if strings.HasSuffix(uri, suffix) {
-			resp.UsedBy[i] = uri[:len(uri)-len(suffix)]
-		}
-	}
-
 	etag := []interface{}{resp.Config, resp.Description, resp.Devices}
 	return response.SyncResponseETag(true, resp, etag)
 }
diff --git a/shared/generate/db/method.go b/shared/generate/db/method.go
index c8667a110c..acb6048121 100644
--- a/shared/generate/db/method.go
+++ b/shared/generate/db/method.go
@@ -531,6 +531,13 @@ func (m *Method) fillSliceReferenceField(buf *file.Buffer, nk []*Field, field *F
 	buf.L("        if value == nil {")
 	buf.L("                value = %s{}", field.Type.Name)
 	buf.L("        }")
+	if field.Name == "UsedBy" {
+		buf.L("        for j, entry := range value {")
+		buf.L("                if len(entry) > 16 && entry[len(entry)-16:] == \"?project=default\" {")
+		buf.L("                         value[j] = entry[0:len(entry)-16]")
+		buf.L("                }")
+		buf.L("        }")
+	}
 	buf.L("        objects[i].%s = value", field.Name)
 	buf.L("}")
 	buf.N()


More information about the lxc-devel mailing list