From 7524b39272f95634a4c61813638e846b557d54ec Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Mon, 30 Nov 2020 15:51:14 -0500 Subject: [PATCH] cephfs admin: add flagSet interface and mergeFlags function To prepare for multiple flags types, we create a flagSet interface and break out the functionality that was in Update into a per-type method and then the common "merger" function. Signed-off-by: John Mulligan --- cephfs/admin/flags.go | 30 +++++++++++++++++++++++++----- cephfs/admin/subvolume.go | 4 ++-- cephfs/admin/subvolumegroup.go | 4 ++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/cephfs/admin/flags.go b/cephfs/admin/flags.go index db1f9f9..1030000 100644 --- a/cephfs/admin/flags.go +++ b/cephfs/admin/flags.go @@ -2,17 +2,37 @@ package admin +// For APIs that accept extra sets of "boolean" flags we may end up wanting +// multiple different sets of supported flags. Example: most rm functions +// accept a force flag, but only subvolume delete has retain snapshots. +// To make this somewhat uniform in the admin package we define a utility +// interface and helper function to merge flags with naming options. + +type flagSet interface { + flags() map[string]bool +} + type commonRmFlags struct { force bool } -func (f commonRmFlags) Update(m map[string]string) map[string]interface{} { - o := make(map[string]interface{}) - for k, v := range m { - o[k] = v - } +func (f commonRmFlags) flags() map[string]bool { + o := make(map[string]bool) if f.force { o["force"] = true } return o } + +// mergeFlags combines a set of key-value settings with any type implementing +// the flagSet interface. +func mergeFlags(m map[string]string, f flagSet) map[string]interface{} { + o := make(map[string]interface{}) + for k, v := range m { + o[k] = v + } + for k, v := range f.flags() { + o[k] = v + } + return o +} diff --git a/cephfs/admin/subvolume.go b/cephfs/admin/subvolume.go index 176a90b..5d564c3 100644 --- a/cephfs/admin/subvolume.go +++ b/cephfs/admin/subvolume.go @@ -109,7 +109,7 @@ func (fsa *FSAdmin) rmSubVolume(volume, group, name string, o commonRmFlags) err if group != NoGroup { m["group_name"] = group } - return fsa.marshalMgrCommand(o.Update(m)).noData().End() + return fsa.marshalMgrCommand(mergeFlags(m, o)).noData().End() } type subVolumeResizeFields struct { @@ -287,7 +287,7 @@ func (fsa *FSAdmin) rmSubVolumeSnapshot(volume, group, subvolume, name string, o if group != NoGroup { m["group_name"] = group } - return fsa.marshalMgrCommand(o.Update(m)).noData().End() + return fsa.marshalMgrCommand(mergeFlags(m, o)).noData().End() } // ListSubVolumeSnapshots returns a listing of snapshots for a given subvolume. diff --git a/cephfs/admin/subvolumegroup.go b/cephfs/admin/subvolumegroup.go index d1209cf..573c8c1 100644 --- a/cephfs/admin/subvolumegroup.go +++ b/cephfs/admin/subvolumegroup.go @@ -80,12 +80,12 @@ func (fsa *FSAdmin) ForceRemoveSubVolumeGroup(volume, name string) error { } func (fsa *FSAdmin) rmSubVolumeGroup(volume, name string, o commonRmFlags) error { - res := fsa.marshalMgrCommand(o.Update(map[string]string{ + res := fsa.marshalMgrCommand(mergeFlags(map[string]string{ "prefix": "fs subvolumegroup rm", "vol_name": volume, "group_name": name, "format": "json", - })) + }, o)) return res.noData().End() }