func rbdExists(name string) (bool, error) { conn, err := rados.NewConn() if err != nil { log.Fatal(err) return false, err } conn.ReadDefaultConfigFile() conn.Connect() defer conn.Shutdown() ioContext, err := conn.OpenIOContext("rbd") if err != nil { log.Fatal(err) return false, err } defer ioContext.Destroy() volumes, err := rbd.GetImageNames(ioContext) if err != nil { log.Fatal(err) return false, err } for _, volumeName := range volumes { if name == volumeName { return true, nil } } return false, nil }
// rbdImageExists will check for an existing Ceph RBD Image func (d *cephRBDVolumeDriver) rbdImageExists(pool, findName string) (bool, error) { log.Printf("INFO: rbdImageExists(%s/%s)", pool, findName) if findName != "" { ctx, err := d.openContext(pool) if err != nil { return false, err } defer d.shutdownContext(ctx) rbdImageNames, err := rbd.GetImageNames(ctx) if err != nil { log.Printf("ERROR: Unable to get Ceph RBD Image list: %s", err) return false, err } for _, imageName := range rbdImageNames { if imageName == findName { return true, nil } } } return false, nil }
func TestGetImageNames(t *testing.T) { conn, _ := rados.NewConn() conn.ReadDefaultConfigFile() conn.Connect() poolname := GetUUID() err := conn.MakePool(poolname) assert.NoError(t, err) ioctx, err := conn.OpenIOContext(poolname) assert.NoError(t, err) createdList := []string{} for i := 0; i < 10; i++ { name := GetUUID() _, err := rbd.Create(ioctx, name, 1<<22) assert.NoError(t, err) createdList = append(createdList, name) } imageNames, err := rbd.GetImageNames(ioctx) assert.NoError(t, err) sort.Strings(createdList) sort.Strings(imageNames) assert.Equal(t, createdList, imageNames) for _, name := range createdList { img := rbd.GetImage(ioctx, name) err := img.Remove() assert.NoError(t, err) } ioctx.Destroy() conn.DeletePool(poolname) conn.Shutdown() }