From 9810c34c1278b6a37745ec3e7e5c132934b959ee Mon Sep 17 00:00:00 2001 From: utkarshbhatthere Date: Fri, 5 May 2023 18:02:36 +0530 Subject: [PATCH] Added service checks and inline comments. Signed-off-by: utkarshbhatthere --- microceph/api/configs.go | 7 ++++-- microceph/api/services.go | 11 ++++++++++ microceph/ceph/config.go | 22 ++++++++++++++----- microceph/cmd/microceph/cluster_config_get.go | 2 +- .../cmd/microceph/cluster_config_reset.go | 2 +- microceph/cmd/microceph/cluster_config_set.go | 2 +- 6 files changed, 36 insertions(+), 10 deletions(-) diff --git a/microceph/api/configs.go b/microceph/api/configs.go index ffc32ed5..c46b1144 100644 --- a/microceph/api/configs.go +++ b/microceph/api/configs.go @@ -39,13 +39,16 @@ func cmdConfigsGet(s *state.State, r *http.Request) response.Response { // Fetch all configs. configs, err = ceph.ListConfigs() } + if err != nil { + return response.SmartError(err) + } return response.SyncResponse(true, configs) } func cmdConfigsPut(s *state.State, r *http.Request) response.Response { var req types.Config - configTable := ceph.GetConfigTable() + configTable := ceph.GetConstConfigTable() err := json.NewDecoder(r.Body).Decode(&req) if err != nil { @@ -66,7 +69,7 @@ func cmdConfigsPut(s *state.State, r *http.Request) response.Response { func cmdConfigsDelete(s *state.State, r *http.Request) response.Response { var req types.Config - configTable := ceph.GetConfigTable() + configTable := ceph.GetConstConfigTable() err := json.NewDecoder(r.Body).Decode(&req) if err != nil { diff --git a/microceph/api/services.go b/microceph/api/services.go index 04399600..c98f67f6 100644 --- a/microceph/api/services.go +++ b/microceph/api/services.go @@ -2,6 +2,7 @@ package api import ( "encoding/json" + "fmt" "net/http" "github.com/canonical/microceph/microceph/api/types" @@ -46,6 +47,16 @@ func cmdRestartServicePost(s *state.State, r *http.Request) response.Response { return response.InternalError(err) } + // Check if provided services are valid and available in microceph + for _, service := range(services) { + valid_services := ceph.GetConfigTableServiceSet() + if _, ok := valid_services[service.Service]; !ok { + err := fmt.Errorf("%s is not a valid ceph service", service.Service) + logger.Errorf("%v", err) + return response.InternalError(err) + } + } + for _, service := range services { err = ceph.RestartCephService(service.Service) if err != nil { diff --git a/microceph/ceph/config.go b/microceph/ceph/config.go index 751808ad..0303b940 100644 --- a/microceph/ceph/config.go +++ b/microceph/ceph/config.go @@ -38,13 +38,25 @@ func (c ConfigTable) Keys() (keys []string) { return keys } -func GetConfigTable() ConfigTable { +// Since we can't have const maps, we encapsulate the map into a func +// so that each request for the map gaurantees consistent definition. +func GetConstConfigTable() ConfigTable { return ConfigTable{ "public_network": {"global", []string{"mon", "osd"}}, "cluster_network": {"global", []string{"osd"}}, } } +func GetConfigTableServiceSet() Set { + return Set{ + "mon": struct{}{}, + "mgr": struct{}{}, + "osd": struct{}{}, + "mds": struct{}{}, + "rgw": struct{}{}, + } +} + // Struct to get Config Items from config dump json output. type ConfigDumpItem struct{ Section string @@ -54,7 +66,7 @@ type ConfigDumpItem struct{ type ConfigDump []ConfigDumpItem func SetConfigItem(c types.Config) error { - configTable := GetConfigTable() + configTable := GetConstConfigTable() args := []string{ "config", @@ -76,7 +88,7 @@ func SetConfigItem(c types.Config) error { func GetConfigItem(c types.Config) (types.Configs, error) { var err error - configTable := GetConfigTable() + configTable := GetConstConfigTable() ret := make(types.Configs, 1) who := "mon" @@ -102,7 +114,7 @@ func GetConfigItem(c types.Config) (types.Configs, error) { } func RemoveConfigItem(c types.Config) error { - configTable := GetConfigTable() + configTable := GetConstConfigTable() args := []string{ "config", "rm", @@ -121,7 +133,7 @@ func RemoveConfigItem(c types.Config) error { func ListConfigs() (types.Configs, error) { var dump ConfigDump var configs types.Configs - configTable := GetConfigTable() + configTable := GetConstConfigTable() args := []string{ "config", "dump", diff --git a/microceph/cmd/microceph/cluster_config_get.go b/microceph/cmd/microceph/cluster_config_get.go index 53912293..1c3ef9fe 100644 --- a/microceph/cmd/microceph/cluster_config_get.go +++ b/microceph/cmd/microceph/cluster_config_get.go @@ -29,7 +29,7 @@ func (c *cmdClusterConfigGet) Command() *cobra.Command { } func (c *cmdClusterConfigGet) Run(cmd *cobra.Command, args []string) error { - allowList := ceph.GetConfigTable() + allowList := ceph.GetConstConfigTable() // Get can be called with a single key. if len(args) != 1 { diff --git a/microceph/cmd/microceph/cluster_config_reset.go b/microceph/cmd/microceph/cluster_config_reset.go index 5b7e855b..0c1c5ee7 100644 --- a/microceph/cmd/microceph/cluster_config_reset.go +++ b/microceph/cmd/microceph/cluster_config_reset.go @@ -31,7 +31,7 @@ func (c *cmdClusterConfigReset) Command() *cobra.Command { } func (c *cmdClusterConfigReset) Run(cmd *cobra.Command, args []string) error { - allowList := ceph.GetConfigTable() + allowList := ceph.GetConstConfigTable() if len(args) != 1 { return cmd.Help() } diff --git a/microceph/cmd/microceph/cluster_config_set.go b/microceph/cmd/microceph/cluster_config_set.go index 3022d9ed..4894b527 100644 --- a/microceph/cmd/microceph/cluster_config_set.go +++ b/microceph/cmd/microceph/cluster_config_set.go @@ -31,7 +31,7 @@ func (c *cmdClusterConfigSet) Command() *cobra.Command { } func (c *cmdClusterConfigSet) Run(cmd *cobra.Command, args []string) error { - allowList := ceph.GetConfigTable() + allowList := ceph.GetConstConfigTable() if len(args) != 2 { return cmd.Help() }