From be703d1e4295c8e328ef479b57e4e97cc056dc54 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Tue, 2 Jun 2020 10:03:32 +0200 Subject: [PATCH] util: implement CreateObject() with go-ceph Signed-off-by: Niels de Vos --- internal/util/cephcmds.go | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/internal/util/cephcmds.go b/internal/util/cephcmds.go index 0e9790994..7d921df0d 100644 --- a/internal/util/cephcmds.go +++ b/internal/util/cephcmds.go @@ -219,27 +219,31 @@ func RemoveOMapKey(ctx context.Context, monitors string, cr *Credentials, poolNa // CreateObject creates the object name passed in and returns ErrObjectExists if the provided object // is already present in rados func CreateObject(ctx context.Context, monitors string, cr *Credentials, poolName, namespace, objectName string) error { - // Command: "rados create objectName" - args := []string{ - "-m", monitors, - "--id", cr.ID, - "--keyfile=" + cr.KeyFile, - "-c", CephConfigPath, - "-p", poolName, - "create", objectName, + conn := ClusterConnection{} + err := conn.Connect(monitors, cr) + if err != nil { + return err } + defer conn.Destroy() + + ioctx, err := conn.GetIoctx(poolName) + if err != nil { + if _, ok := err.(ErrPoolNotFound); ok { + err = ErrObjectNotFound{poolName, err} + } + return err + } + defer ioctx.Destroy() if namespace != "" { - args = append(args, "--namespace="+namespace) + ioctx.SetNamespace(namespace) } - _, stderr, err := ExecCommand("rados", args[:]...) - if err != nil { + err = ioctx.Create(objectName, rados.CreateExclusive) + if err == rados.ErrObjectExists { + return ErrObjectExists{objectName, err} + } else if err != nil { klog.Errorf(Log(ctx, "failed creating omap (%s) in pool (%s): (%v)"), objectName, poolName, err) - if strings.Contains(string(stderr), "error creating "+poolName+"/"+objectName+ - ": (17) File exists") { - return ErrObjectExists{objectName, err} - } return err }