package main

import (
	"fmt"

	"github.com/ceph/go-ceph/rados"
)

func connect() (_ CephConnection, err error) {
	var cephConn CephConnection = CephConnection{}

	defer func() {
		if err != nil {
			err = fmt.Errorf("Error in func connect() %w", err)
		}
	}()

	cephConn.conn, err = rados.NewConnWithClusterAndUser("ceph", "client.admin")
	if err != nil {
		return CephConnection{}, err
	}

	err = cephConn.conn.ReadConfigFile(params.Config)
	if err != nil {
		return CephConnection{}, err
	}

	err = cephConn.conn.SetConfigOption("keyring", params.Keyring)
	if err != nil {
		return CephConnection{}, err
	}

	err = cephConn.conn.Connect()
	if err != nil {
		return CephConnection{}, err
	}

	return cephConn, nil
}
