diff --git a/rados/conn.go b/rados/conn.go index 1b12642..971e8bc 100644 --- a/rados/conn.go +++ b/rados/conn.go @@ -296,6 +296,22 @@ func (c *Conn) GetPoolByName(name string) (int64, error) { } } +// GetPoolByID returns the name of a pool by a given ID. +func (c *Conn) GetPoolByID(id int64) (string, error) { + buf := make([]byte, 4096) + if err := c.ensure_connected(); err != nil { + fmt.Println("NOT CONNECTED WHOOPS") + return "", err + } + c_id := C.int64_t(id) + ret := int(C.rados_pool_reverse_lookup(c.cluster, c_id, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))) + if ret < 0 { + return "", RadosError(ret) + } else { + return C.GoString((*C.char)(unsafe.Pointer(&buf[0]))), nil + } +} + // MonCommand sends a command to one of the monitors func (c *Conn) MonCommand(args []byte) (buffer []byte, info string, err error) { return c.monCommand(args, nil) diff --git a/rados/rados_test.go b/rados/rados_test.go index 26c1c9d..86b8605 100644 --- a/rados/rados_test.go +++ b/rados/rados_test.go @@ -351,6 +351,48 @@ func (suite *RadosTestSuite) TestGetPoolByName() { } } +func (suite *RadosTestSuite) TestGetPoolByID() { + suite.SetupConnection() + + // check that new pool name is unique + new_name := uuid.Must(uuid.NewV4()).String() + pool, err := suite.conn.GetPoolByName(new_name) + if pool != 0 || err == nil { + suite.T().Error("Random pool name exists!") + return + } + + // create pool + err = suite.conn.MakePool(new_name) + assert.NoError(suite.T(), err) + + // verify that the new pool name exists + id, err := suite.conn.GetPoolByName(new_name) + assert.NoError(suite.T(), err) + + if id == 0 { + suite.T().Error("Cannot find newly created pool") + } + + // get the name of the pool + name, err := suite.conn.GetPoolByID(id) + assert.NoError(suite.T(), err) + + if name == "" { + suite.T().Error("Cannot find name of newly created pool") + } + + // delete the pool + err = suite.conn.DeletePool(new_name) + assert.NoError(suite.T(), err) + + // verify that it is gone + name, err = suite.conn.GetPoolByID(id) + if name != "" || err == nil { + suite.T().Error("Deleted pool still exists") + } +} + func (suite *RadosTestSuite) TestPingMonitor() { suite.SetupConnection()