rebase: update go-ceph to v0.10.0

This commit updates the go-ceph to latest
release. More details about release at
https://github.com/ceph/go-ceph/releases/tag/v0.10.0

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
Madhu Rajanna
2021-06-09 10:24:52 +05:30
committed by mergify[bot]
parent 17b0091cba
commit 5b7b5f1e3a
27 changed files with 1099 additions and 272 deletions

View File

@ -0,0 +1,53 @@
package commands
import (
"encoding/json"
ccom "github.com/ceph/go-ceph/common/commands"
"github.com/ceph/go-ceph/rados"
)
func validate(m interface{}) error {
if m == nil {
return rados.ErrNotConnected
}
return nil
}
// RawMgrCommand takes a byte buffer and sends it to the MGR as a command.
// The buffer is expected to contain preformatted JSON.
func RawMgrCommand(m ccom.MgrCommander, buf []byte) Response {
if err := validate(m); err != nil {
return Response{err: err}
}
return NewResponse(m.MgrCommand([][]byte{buf}))
}
// MarshalMgrCommand takes an generic interface{} value, converts it to JSON
// and sends the json to the MGR as a command.
func MarshalMgrCommand(m ccom.MgrCommander, v interface{}) Response {
b, err := json.Marshal(v)
if err != nil {
return Response{err: err}
}
return RawMgrCommand(m, b)
}
// RawMonCommand takes a byte buffer and sends it to the MON as a command.
// The buffer is expected to contain preformatted JSON.
func RawMonCommand(m ccom.MonCommander, buf []byte) Response {
if err := validate(m); err != nil {
return Response{err: err}
}
return NewResponse(m.MonCommand(buf))
}
// MarshalMonCommand takes an generic interface{} value, converts it to JSON
// and sends the json to the MGR as a command.
func MarshalMonCommand(m ccom.MonCommander, v interface{}) Response {
b, err := json.Marshal(v)
if err != nil {
return Response{err: err}
}
return RawMonCommand(m, b)
}

View File

@ -0,0 +1,163 @@
package commands
import (
"encoding/json"
"errors"
"fmt"
"strings"
)
var (
// ErrStatusNotEmpty may be returned if a call should not have a status
// string set but one is.
ErrStatusNotEmpty = errors.New("response status not empty")
// ErrBodyNotEmpty may be returned if a call should have an empty body but
// a body value is present.
ErrBodyNotEmpty = errors.New("response body not empty")
)
const (
deprecatedSuffix = "call is deprecated and will be removed in a future release"
missingPrefix = "No handler found"
einval = -22
)
type cephError interface {
ErrorCode() int
}
// NotImplementedError error values will be returned in the case that an API
// call is not available in the version of Ceph that is running in the target
// cluster.
type NotImplementedError struct {
Response
}
// Error implements the error interface.
func (e NotImplementedError) Error() string {
return fmt.Sprintf("API call not implemented server-side: %s", e.status)
}
// Response encapsulates the data returned by ceph and supports easy processing
// pipelines.
type Response struct {
body []byte
status string
err error
}
// Ok returns true if the response contains no error.
func (r Response) Ok() bool {
return r.err == nil
}
// Error implements the error interface.
func (r Response) Error() string {
if r.status == "" {
return r.err.Error()
}
return fmt.Sprintf("%s: %q", r.err, r.status)
}
// Unwrap returns the error this response contains.
func (r Response) Unwrap() error {
return r.err
}
// Status returns the status string value.
func (r Response) Status() string {
return r.status
}
// Body returns the response body as a raw byte-slice.
func (r Response) Body() []byte {
return r.body
}
// End returns an error if the response contains an error or nil, indicating
// that response is no longer needed for processing.
func (r Response) End() error {
if !r.Ok() {
if ce, ok := r.err.(cephError); ok {
if ce.ErrorCode() == einval && strings.HasPrefix(r.status, missingPrefix) {
return NotImplementedError{Response: r}
}
}
return r
}
return nil
}
// NoStatus asserts that the input response has no status value.
func (r Response) NoStatus() Response {
if !r.Ok() {
return r
}
if r.status != "" {
return Response{r.body, r.status, ErrStatusNotEmpty}
}
return r
}
// NoBody asserts that the input response has no body value.
func (r Response) NoBody() Response {
if !r.Ok() {
return r
}
if len(r.body) != 0 {
return Response{r.body, r.status, ErrBodyNotEmpty}
}
return r
}
// NoData asserts that the input response has no status or body values.
func (r Response) NoData() Response {
return r.NoStatus().NoBody()
}
// FilterPrefix sets the status value to an empty string if the status
// value contains the given prefix string.
func (r Response) FilterPrefix(p string) Response {
if !r.Ok() {
return r
}
if strings.HasPrefix(r.status, p) {
return Response{r.body, "", r.err}
}
return r
}
// FilterSuffix sets the status value to an empty string if the status
// value contains the given suffix string.
func (r Response) FilterSuffix(s string) Response {
if !r.Ok() {
return r
}
if strings.HasSuffix(r.status, s) {
return Response{r.body, "", r.err}
}
return r
}
// FilterDeprecated removes deprecation warnings from the response status.
// Use it when checking the response from calls that may be deprecated in ceph
// if you want those calls to continue working if the warning is present.
func (r Response) FilterDeprecated() Response {
return r.FilterSuffix(deprecatedSuffix)
}
// Unmarshal data from the response body into v.
func (r Response) Unmarshal(v interface{}) Response {
if !r.Ok() {
return r
}
if err := json.Unmarshal(r.body, v); err != nil {
return Response{body: r.body, err: err}
}
return r
}
// NewResponse returns a response.
func NewResponse(b []byte, s string, e error) Response {
return Response{b, s, e}
}

View File

@ -0,0 +1,53 @@
package commands
import (
"fmt"
ccom "github.com/ceph/go-ceph/common/commands"
)
// NewTraceCommander is a RadosCommander that wraps a given RadosCommander
// and when commands are executes prints debug level "traces" to the
// standard output.
func NewTraceCommander(c ccom.RadosCommander) ccom.RadosCommander {
return &tracingCommander{c}
}
// tracingCommander serves two purposes: first, it allows one to trace the
// input and output json when running the tests. It can help with actually
// debugging the tests. Second, it demonstrates the rationale for using an
// interface in FSAdmin. You can layer any sort of debugging, error injection,
// or whatnot between the FSAdmin layer and the RADOS layer.
type tracingCommander struct {
conn ccom.RadosCommander
}
func (t *tracingCommander) MgrCommand(buf [][]byte) ([]byte, string, error) {
fmt.Println("(MGR Command)")
for i := range buf {
fmt.Println("IN:", string(buf[i]))
}
r, s, err := t.conn.MgrCommand(buf)
fmt.Println("OUT(result):", string(r))
if s != "" {
fmt.Println("OUT(status):", s)
}
if err != nil {
fmt.Println("OUT(error):", err.Error())
}
return r, s, err
}
func (t *tracingCommander) MonCommand(buf []byte) ([]byte, string, error) {
fmt.Println("(MON Command)")
fmt.Println("IN:", string(buf))
r, s, err := t.conn.MonCommand(buf)
fmt.Println("OUT(result):", string(r))
if s != "" {
fmt.Println("OUT(status):", s)
}
if err != nil {
fmt.Println("OUT(error):", err.Error())
}
return r, s, err
}