[lxc-devel] [lxd/master] Fix bad instance filtering

stgraber on Github lxc-bot at linuxcontainers.org
Tue Nov 5 18:24:17 UTC 2019


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/20191105/d53fbd17/attachment.bin>
-------------- next part --------------
From fbf60fe7adda57adb3b11619e1613eef03b64fe4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 5 Nov 2019 13:13:12 -0500
Subject: [PATCH 1/2] lxd/db: Cover all combinations of instance filters
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/containers.go | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/lxd/db/containers.go b/lxd/db/containers.go
index 489b97fbef..b2b4cf9d3d 100644
--- a/lxd/db/containers.go
+++ b/lxd/db/containers.go
@@ -24,12 +24,21 @@ import (
 //go:generate mapper reset
 //
 //go:generate mapper stmt -p db -e instance objects
-//go:generate mapper stmt -p db -e instance objects-by-Type
+//go:generate mapper stmt -p db -e instance objects-by-Project
 //go:generate mapper stmt -p db -e instance objects-by-Project-and-Type
-//go:generate mapper stmt -p db -e instance objects-by-Node-and-Type
-//go:generate mapper stmt -p db -e instance objects-by-Project-and-Node-and-Type
+//go:generate mapper stmt -p db -e instance objects-by-Project-and-Type-and-Node
+//go:generate mapper stmt -p db -e instance objects-by-Project-and-Type-and-Node-and-Name
+//go:generate mapper stmt -p db -e instance objects-by-Project-and-Type-and-Name
 //go:generate mapper stmt -p db -e instance objects-by-Project-and-Name
-//go:generate mapper stmt -p db -e instance objects-by-Project-and-Name-and-Type
+//go:generate mapper stmt -p db -e instance objects-by-Project-and-Name-and-Node
+//go:generate mapper stmt -p db -e instance objects-by-Project-and-Node
+//go:generate mapper stmt -p db -e instance objects-by-Type
+//go:generate mapper stmt -p db -e instance objects-by-Type-and-Name
+//go:generate mapper stmt -p db -e instance objects-by-Type-and-Name-and-Node
+//go:generate mapper stmt -p db -e instance objects-by-Type-and-Node
+//go:generate mapper stmt -p db -e instance objects-by-Node
+//go:generate mapper stmt -p db -e instance objects-by-Node-and-Name
+//go:generate mapper stmt -p db -e instance objects-by-Name
 //go:generate mapper stmt -p db -e instance profiles-ref
 //go:generate mapper stmt -p db -e instance profiles-ref-by-Project
 //go:generate mapper stmt -p db -e instance profiles-ref-by-Node

From 93c12f57bbd2f0bc9c0175d22eff8d353304d992 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 5 Nov 2019 13:13:24 -0500
Subject: [PATCH 2/2] lxd/db: Re-generate DB code
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/instances.mapper.go | 177 ++++++++++++++++++++++++++++++-------
 1 file changed, 143 insertions(+), 34 deletions(-)

diff --git a/lxd/db/instances.mapper.go b/lxd/db/instances.mapper.go
index 2d64b0af2a..e379651322 100644
--- a/lxd/db/instances.mapper.go
+++ b/lxd/db/instances.mapper.go
@@ -19,10 +19,10 @@ SELECT instances.id, projects.name AS project, instances.name, nodes.name AS nod
   ORDER BY projects.id, instances.name
 `)
 
-var instanceObjectsByType = cluster.RegisterStmt(`
+var instanceObjectsByProject = cluster.RegisterStmt(`
 SELECT instances.id, projects.name AS project, instances.name, nodes.name AS node, instances.type, instances.architecture, instances.ephemeral, instances.creation_date, instances.stateful, instances.last_use_date, coalesce(instances.description, ''), instances.expiry_date
   FROM instances JOIN projects ON instances.project_id = projects.id JOIN nodes ON instances.node_id = nodes.id
-  WHERE instances.type = ? ORDER BY projects.id, instances.name
+  WHERE project = ? ORDER BY projects.id, instances.name
 `)
 
 var instanceObjectsByProjectAndType = cluster.RegisterStmt(`
@@ -31,16 +31,22 @@ SELECT instances.id, projects.name AS project, instances.name, nodes.name AS nod
   WHERE project = ? AND instances.type = ? ORDER BY projects.id, instances.name
 `)
 
-var instanceObjectsByNodeAndType = cluster.RegisterStmt(`
+var instanceObjectsByProjectAndTypeAndNode = cluster.RegisterStmt(`
+SELECT instances.id, projects.name AS project, instances.name, nodes.name AS node, instances.type, instances.architecture, instances.ephemeral, instances.creation_date, instances.stateful, instances.last_use_date, coalesce(instances.description, ''), instances.expiry_date
+  FROM instances JOIN projects ON instances.project_id = projects.id JOIN nodes ON instances.node_id = nodes.id
+  WHERE project = ? AND instances.type = ? AND node = ? ORDER BY projects.id, instances.name
+`)
+
+var instanceObjectsByProjectAndTypeAndNodeAndName = cluster.RegisterStmt(`
 SELECT instances.id, projects.name AS project, instances.name, nodes.name AS node, instances.type, instances.architecture, instances.ephemeral, instances.creation_date, instances.stateful, instances.last_use_date, coalesce(instances.description, ''), instances.expiry_date
   FROM instances JOIN projects ON instances.project_id = projects.id JOIN nodes ON instances.node_id = nodes.id
-  WHERE node = ? AND instances.type = ? ORDER BY projects.id, instances.name
+  WHERE project = ? AND instances.type = ? AND node = ? AND instances.name = ? ORDER BY projects.id, instances.name
 `)
 
-var instanceObjectsByProjectAndNodeAndType = cluster.RegisterStmt(`
+var instanceObjectsByProjectAndTypeAndName = cluster.RegisterStmt(`
 SELECT instances.id, projects.name AS project, instances.name, nodes.name AS node, instances.type, instances.architecture, instances.ephemeral, instances.creation_date, instances.stateful, instances.last_use_date, coalesce(instances.description, ''), instances.expiry_date
   FROM instances JOIN projects ON instances.project_id = projects.id JOIN nodes ON instances.node_id = nodes.id
-  WHERE project = ? AND node = ? AND instances.type = ? ORDER BY projects.id, instances.name
+  WHERE project = ? AND instances.type = ? AND instances.name = ? ORDER BY projects.id, instances.name
 `)
 
 var instanceObjectsByProjectAndName = cluster.RegisterStmt(`
@@ -49,10 +55,58 @@ SELECT instances.id, projects.name AS project, instances.name, nodes.name AS nod
   WHERE project = ? AND instances.name = ? ORDER BY projects.id, instances.name
 `)
 
-var instanceObjectsByProjectAndNameAndType = cluster.RegisterStmt(`
+var instanceObjectsByProjectAndNameAndNode = cluster.RegisterStmt(`
+SELECT instances.id, projects.name AS project, instances.name, nodes.name AS node, instances.type, instances.architecture, instances.ephemeral, instances.creation_date, instances.stateful, instances.last_use_date, coalesce(instances.description, ''), instances.expiry_date
+  FROM instances JOIN projects ON instances.project_id = projects.id JOIN nodes ON instances.node_id = nodes.id
+  WHERE project = ? AND instances.name = ? AND node = ? ORDER BY projects.id, instances.name
+`)
+
+var instanceObjectsByProjectAndNode = cluster.RegisterStmt(`
+SELECT instances.id, projects.name AS project, instances.name, nodes.name AS node, instances.type, instances.architecture, instances.ephemeral, instances.creation_date, instances.stateful, instances.last_use_date, coalesce(instances.description, ''), instances.expiry_date
+  FROM instances JOIN projects ON instances.project_id = projects.id JOIN nodes ON instances.node_id = nodes.id
+  WHERE project = ? AND node = ? ORDER BY projects.id, instances.name
+`)
+
+var instanceObjectsByType = cluster.RegisterStmt(`
+SELECT instances.id, projects.name AS project, instances.name, nodes.name AS node, instances.type, instances.architecture, instances.ephemeral, instances.creation_date, instances.stateful, instances.last_use_date, coalesce(instances.description, ''), instances.expiry_date
+  FROM instances JOIN projects ON instances.project_id = projects.id JOIN nodes ON instances.node_id = nodes.id
+  WHERE instances.type = ? ORDER BY projects.id, instances.name
+`)
+
+var instanceObjectsByTypeAndName = cluster.RegisterStmt(`
+SELECT instances.id, projects.name AS project, instances.name, nodes.name AS node, instances.type, instances.architecture, instances.ephemeral, instances.creation_date, instances.stateful, instances.last_use_date, coalesce(instances.description, ''), instances.expiry_date
+  FROM instances JOIN projects ON instances.project_id = projects.id JOIN nodes ON instances.node_id = nodes.id
+  WHERE instances.type = ? AND instances.name = ? ORDER BY projects.id, instances.name
+`)
+
+var instanceObjectsByTypeAndNameAndNode = cluster.RegisterStmt(`
+SELECT instances.id, projects.name AS project, instances.name, nodes.name AS node, instances.type, instances.architecture, instances.ephemeral, instances.creation_date, instances.stateful, instances.last_use_date, coalesce(instances.description, ''), instances.expiry_date
+  FROM instances JOIN projects ON instances.project_id = projects.id JOIN nodes ON instances.node_id = nodes.id
+  WHERE instances.type = ? AND instances.name = ? AND node = ? ORDER BY projects.id, instances.name
+`)
+
+var instanceObjectsByTypeAndNode = cluster.RegisterStmt(`
+SELECT instances.id, projects.name AS project, instances.name, nodes.name AS node, instances.type, instances.architecture, instances.ephemeral, instances.creation_date, instances.stateful, instances.last_use_date, coalesce(instances.description, ''), instances.expiry_date
+  FROM instances JOIN projects ON instances.project_id = projects.id JOIN nodes ON instances.node_id = nodes.id
+  WHERE instances.type = ? AND node = ? ORDER BY projects.id, instances.name
+`)
+
+var instanceObjectsByNode = cluster.RegisterStmt(`
+SELECT instances.id, projects.name AS project, instances.name, nodes.name AS node, instances.type, instances.architecture, instances.ephemeral, instances.creation_date, instances.stateful, instances.last_use_date, coalesce(instances.description, ''), instances.expiry_date
+  FROM instances JOIN projects ON instances.project_id = projects.id JOIN nodes ON instances.node_id = nodes.id
+  WHERE node = ? ORDER BY projects.id, instances.name
+`)
+
+var instanceObjectsByNodeAndName = cluster.RegisterStmt(`
 SELECT instances.id, projects.name AS project, instances.name, nodes.name AS node, instances.type, instances.architecture, instances.ephemeral, instances.creation_date, instances.stateful, instances.last_use_date, coalesce(instances.description, ''), instances.expiry_date
   FROM instances JOIN projects ON instances.project_id = projects.id JOIN nodes ON instances.node_id = nodes.id
-  WHERE project = ? AND instances.name = ? AND instances.type = ? ORDER BY projects.id, instances.name
+  WHERE node = ? AND instances.name = ? ORDER BY projects.id, instances.name
+`)
+
+var instanceObjectsByName = cluster.RegisterStmt(`
+SELECT instances.id, projects.name AS project, instances.name, nodes.name AS node, instances.type, instances.architecture, instances.ephemeral, instances.creation_date, instances.stateful, instances.last_use_date, coalesce(instances.description, ''), instances.expiry_date
+  FROM instances JOIN projects ON instances.project_id = projects.id JOIN nodes ON instances.node_id = nodes.id
+  WHERE instances.name = ? ORDER BY projects.id, instances.name
 `)
 
 var instanceProfilesRef = cluster.RegisterStmt(`
@@ -171,19 +225,41 @@ func (c *ClusterTx) InstanceList(filter InstanceFilter) ([]Instance, error) {
 	var stmt *sql.Stmt
 	var args []interface{}
 
-	if criteria["Project"] != nil && criteria["Name"] != nil && criteria["Type"] != nil {
-		stmt = c.stmt(instanceObjectsByProjectAndNameAndType)
+	if criteria["Project"] != nil && criteria["Type"] != nil && criteria["Node"] != nil && criteria["Name"] != nil {
+		stmt = c.stmt(instanceObjectsByProjectAndTypeAndNodeAndName)
 		args = []interface{}{
 			filter.Project,
-			filter.Name,
 			filter.Type,
+			filter.Node,
+			filter.Name,
 		}
-	} else if criteria["Project"] != nil && criteria["Node"] != nil && criteria["Type"] != nil {
-		stmt = c.stmt(instanceObjectsByProjectAndNodeAndType)
+	} else if criteria["Project"] != nil && criteria["Type"] != nil && criteria["Node"] != nil {
+		stmt = c.stmt(instanceObjectsByProjectAndTypeAndNode)
 		args = []interface{}{
 			filter.Project,
+			filter.Type,
 			filter.Node,
+		}
+	} else if criteria["Project"] != nil && criteria["Type"] != nil && criteria["Name"] != nil {
+		stmt = c.stmt(instanceObjectsByProjectAndTypeAndName)
+		args = []interface{}{
+			filter.Project,
+			filter.Type,
+			filter.Name,
+		}
+	} else if criteria["Type"] != nil && criteria["Name"] != nil && criteria["Node"] != nil {
+		stmt = c.stmt(instanceObjectsByTypeAndNameAndNode)
+		args = []interface{}{
 			filter.Type,
+			filter.Name,
+			filter.Node,
+		}
+	} else if criteria["Project"] != nil && criteria["Name"] != nil && criteria["Node"] != nil {
+		stmt = c.stmt(instanceObjectsByProjectAndNameAndNode)
+		args = []interface{}{
+			filter.Project,
+			filter.Name,
+			filter.Node,
 		}
 	} else if criteria["Project"] != nil && criteria["Type"] != nil {
 		stmt = c.stmt(instanceObjectsByProjectAndType)
@@ -191,23 +267,56 @@ func (c *ClusterTx) InstanceList(filter InstanceFilter) ([]Instance, error) {
 			filter.Project,
 			filter.Type,
 		}
+	} else if criteria["Type"] != nil && criteria["Node"] != nil {
+		stmt = c.stmt(instanceObjectsByTypeAndNode)
+		args = []interface{}{
+			filter.Type,
+			filter.Node,
+		}
+	} else if criteria["Project"] != nil && criteria["Node"] != nil {
+		stmt = c.stmt(instanceObjectsByProjectAndNode)
+		args = []interface{}{
+			filter.Project,
+			filter.Node,
+		}
+	} else if criteria["Type"] != nil && criteria["Name"] != nil {
+		stmt = c.stmt(instanceObjectsByTypeAndName)
+		args = []interface{}{
+			filter.Type,
+			filter.Name,
+		}
 	} else if criteria["Project"] != nil && criteria["Name"] != nil {
 		stmt = c.stmt(instanceObjectsByProjectAndName)
 		args = []interface{}{
 			filter.Project,
 			filter.Name,
 		}
-	} else if criteria["Node"] != nil && criteria["Type"] != nil {
-		stmt = c.stmt(instanceObjectsByNodeAndType)
+	} else if criteria["Node"] != nil && criteria["Name"] != nil {
+		stmt = c.stmt(instanceObjectsByNodeAndName)
 		args = []interface{}{
 			filter.Node,
-			filter.Type,
+			filter.Name,
 		}
 	} else if criteria["Type"] != nil {
 		stmt = c.stmt(instanceObjectsByType)
 		args = []interface{}{
 			filter.Type,
 		}
+	} else if criteria["Node"] != nil {
+		stmt = c.stmt(instanceObjectsByNode)
+		args = []interface{}{
+			filter.Node,
+		}
+	} else if criteria["Project"] != nil {
+		stmt = c.stmt(instanceObjectsByProject)
+		args = []interface{}{
+			filter.Project,
+		}
+	} else if criteria["Name"] != nil {
+		stmt = c.stmt(instanceObjectsByName)
+		args = []interface{}{
+			filter.Name,
+		}
 	} else {
 		stmt = c.stmt(instanceObjects)
 		args = []interface{}{}
@@ -563,28 +672,28 @@ func (c *ClusterTx) InstanceConfigRef(filter InstanceFilter) (map[string]map[str
 	var stmt *sql.Stmt
 	var args []interface{}
 
-	if criteria["Project"] != nil && criteria["Name"] != nil {
+	if criteria["Project"] != nil && criteria["Node"] != nil {
+		stmt = c.stmt(instanceConfigRefByProjectAndNode)
+		args = []interface{}{
+			filter.Project,
+			filter.Node,
+		}
+	} else if criteria["Project"] != nil && criteria["Name"] != nil {
 		stmt = c.stmt(instanceConfigRefByProjectAndName)
 		args = []interface{}{
 			filter.Project,
 			filter.Name,
 		}
-	} else if criteria["Project"] != nil && criteria["Node"] != nil {
-		stmt = c.stmt(instanceConfigRefByProjectAndNode)
+	} else if criteria["Project"] != nil {
+		stmt = c.stmt(instanceConfigRefByProject)
 		args = []interface{}{
 			filter.Project,
-			filter.Node,
 		}
 	} else if criteria["Node"] != nil {
 		stmt = c.stmt(instanceConfigRefByNode)
 		args = []interface{}{
 			filter.Node,
 		}
-	} else if criteria["Project"] != nil {
-		stmt = c.stmt(instanceConfigRefByProject)
-		args = []interface{}{
-			filter.Project,
-		}
 	} else {
 		stmt = c.stmt(instanceConfigRef)
 		args = []interface{}{}
@@ -659,28 +768,28 @@ func (c *ClusterTx) InstanceDevicesRef(filter InstanceFilter) (map[string]map[st
 	var stmt *sql.Stmt
 	var args []interface{}
 
-	if criteria["Project"] != nil && criteria["Node"] != nil {
-		stmt = c.stmt(instanceDevicesRefByProjectAndNode)
-		args = []interface{}{
-			filter.Project,
-			filter.Node,
-		}
-	} else if criteria["Project"] != nil && criteria["Name"] != nil {
+	if criteria["Project"] != nil && criteria["Name"] != nil {
 		stmt = c.stmt(instanceDevicesRefByProjectAndName)
 		args = []interface{}{
 			filter.Project,
 			filter.Name,
 		}
-	} else if criteria["Project"] != nil {
-		stmt = c.stmt(instanceDevicesRefByProject)
+	} else if criteria["Project"] != nil && criteria["Node"] != nil {
+		stmt = c.stmt(instanceDevicesRefByProjectAndNode)
 		args = []interface{}{
 			filter.Project,
+			filter.Node,
 		}
 	} else if criteria["Node"] != nil {
 		stmt = c.stmt(instanceDevicesRefByNode)
 		args = []interface{}{
 			filter.Node,
 		}
+	} else if criteria["Project"] != nil {
+		stmt = c.stmt(instanceDevicesRefByProject)
+		args = []interface{}{
+			filter.Project,
+		}
 	} else {
 		stmt = c.stmt(instanceDevicesRef)
 		args = []interface{}{}


More information about the lxc-devel mailing list