Skip to content

Commit

Permalink
Added service checks and inline comments.
Browse files Browse the repository at this point in the history
Signed-off-by: utkarshbhatthere <[email protected]>
  • Loading branch information
UtkarshBhatthere committed May 5, 2023
1 parent e10f972 commit 9810c34
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
7 changes: 5 additions & 2 deletions microceph/api/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions microceph/api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"encoding/json"
"fmt"
"net/http"

"github.com/canonical/microceph/microceph/api/types"
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 17 additions & 5 deletions microceph/ceph/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -54,7 +66,7 @@ type ConfigDumpItem struct{
type ConfigDump []ConfigDumpItem

func SetConfigItem(c types.Config) error {
configTable := GetConfigTable()
configTable := GetConstConfigTable()

args := []string{
"config",
Expand All @@ -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"

Expand All @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion microceph/cmd/microceph/cluster_config_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion microceph/cmd/microceph/cluster_config_reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion microceph/cmd/microceph/cluster_config_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 9810c34

Please sign in to comment.