From 42e9904d0b17f39ed58d06810d316ab9f95a4a3c Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Wed, 3 Mar 2021 14:45:21 -0500 Subject: [PATCH] commands: start a new internal sub-package for working with json commands This code was part of cephfs/admin but will be shared with a soon-to-be rbd/admin package. Signed-off-by: John Mulligan --- internal/commands/commands.go | 71 +++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 internal/commands/commands.go diff --git a/internal/commands/commands.go b/internal/commands/commands.go new file mode 100644 index 0000000..095140f --- /dev/null +++ b/internal/commands/commands.go @@ -0,0 +1,71 @@ +package commands + +import ( + "encoding/json" + + "github.com/ceph/go-ceph/rados" +) + +// MgrCommander in an interface for the API needed to execute JSON formatted +// commands on the ceph mgr. +type MgrCommander interface { + MgrCommand(buf [][]byte) ([]byte, string, error) +} + +// MonCommander is an interface for the API needed to execute JSON formatted +// commands on the ceph mon(s). +type MonCommander interface { + MonCommand(buf []byte) ([]byte, string, error) +} + +// RadosCommander provides an interface for APIs needed to execute JSON +// formatted commands on the Ceph cluster. +type RadosCommander interface { + MgrCommander + MonCommander +} + +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 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 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 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 MonCommander, v interface{}) Response { + b, err := json.Marshal(v) + if err != nil { + return Response{err: err} + } + return RawMonCommand(m, b) +}