util: implement CreateObject() with go-ceph

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2020-06-02 10:03:32 +02:00 committed by mergify[bot]
parent 6e24b10364
commit be703d1e42

View File

@ -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 // CreateObject creates the object name passed in and returns ErrObjectExists if the provided object
// is already present in rados // is already present in rados
func CreateObject(ctx context.Context, monitors string, cr *Credentials, poolName, namespace, objectName string) error { func CreateObject(ctx context.Context, monitors string, cr *Credentials, poolName, namespace, objectName string) error {
// Command: "rados <options> create objectName" conn := ClusterConnection{}
args := []string{ err := conn.Connect(monitors, cr)
"-m", monitors, if err != nil {
"--id", cr.ID, return err
"--keyfile=" + cr.KeyFile,
"-c", CephConfigPath,
"-p", poolName,
"create", objectName,
} }
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 != "" { if namespace != "" {
args = append(args, "--namespace="+namespace) ioctx.SetNamespace(namespace)
} }
_, stderr, err := ExecCommand("rados", args[:]...) err = ioctx.Create(objectName, rados.CreateExclusive)
if err != nil { 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) 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 return err
} }