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

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) ===
Signed-off-by: Free Ekanayaka <free.ekanay...@canonical.com>
From 129eb84c6a678ab5aea81b84fa59373e9afc701b Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanay...@canonical.com>
Date: Thu, 12 Apr 2018 10:50:10 +0000
Subject: [PATCH] Add db.ContainersNodeList

Signed-off-by: Free Ekanayaka <free.ekanay...@canonical.com>
---
 lxd/db/containers.go      | 20 ++++++++++++++++++++
 lxd/db/containers_test.go | 24 ++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/lxd/db/containers.go b/lxd/db/containers.go
index 867f5971c9..3b93a0d51a 100644
--- a/lxd/db/containers.go
+++ b/lxd/db/containers.go
@@ -663,6 +663,26 @@ func (c *Cluster) ContainersList(cType ContainerType) 
([]string, error) {
        return ret, nil
 }
 
+// ContainersNodeList returns the names of all the containers of the given type
+// running on the local node.
+func (c *Cluster) ContainersNodeList(cType ContainerType) ([]string, error) {
+       q := fmt.Sprintf("SELECT name FROM containers WHERE type=? AND 
node_id=? ORDER BY name")
+       inargs := []interface{}{cType, c.nodeID}
+       var container string
+       outfmt := []interface{}{container}
+       result, err := queryScan(c.db, q, inargs, outfmt)
+       if err != nil {
+               return nil, err
+       }
+
+       var ret []string
+       for _, container := range result {
+               ret = append(ret, container[0].(string))
+       }
+
+       return ret, nil
+}
+
 // ContainersResetState resets the power state of all containers.
 func (c *Cluster) ContainersResetState() error {
        // Reset all container states
diff --git a/lxd/db/containers_test.go b/lxd/db/containers_test.go
index 291a58992e..f82d079c82 100644
--- a/lxd/db/containers_test.go
+++ b/lxd/db/containers_test.go
@@ -89,6 +89,30 @@ func TestContainerPool(t *testing.T) {
        assert.Equal(t, "default", poolName)
 }
 
+// Only containers running on the local node are returned.
+func TestContainersNodeList(t *testing.T) {
+       cluster, cleanup := db.NewTestCluster(t)
+       defer cleanup()
+
+       nodeID1 := int64(1) // This is the default local node
+
+       // Add another node
+       var nodeID2 int64
+       err := cluster.Transaction(func(tx *db.ClusterTx) error {
+               var err error
+               nodeID2, err = tx.NodeAdd("node2", "1.2.3.4:666")
+               require.NoError(t, err)
+               addContainer(t, tx, nodeID1, "c1")
+               addContainer(t, tx, nodeID2, "c2")
+               return nil
+       })
+       require.NoError(t, err)
+
+       names, err := cluster.ContainersNodeList(db.CTypeRegular)
+       require.NoError(t, err)
+       assert.Equal(t, names, []string{"c1"})
+}
+
 func addContainer(t *testing.T, tx *db.ClusterTx, nodeID int64, name string) {
        stmt := `
 INSERT INTO containers(node_id, name, architecture, type) VALUES (?, ?, 1, ?)
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to