From 8f592349f13bf1244b56372cd0415efe54131d4c Mon Sep 17 00:00:00 2001 From: Kelly Lyon Date: Wed, 12 Apr 2017 16:11:10 -0700 Subject: [PATCH] Remove duplicate function for getPluginType: - remove from config.go v1 and v2 (duplicated) - moved to plugin.go - simplified function to reduce duplicate error handling --- core/plugin.go | 11 +++++++++++ mgmt/rest/v1/config.go | 21 +++------------------ mgmt/rest/v2/config.go | 17 +++-------------- 3 files changed, 17 insertions(+), 32 deletions(-) diff --git a/core/plugin.go b/core/plugin.go index ea8c53f83..a1637d756 100644 --- a/core/plugin.go +++ b/core/plugin.go @@ -26,6 +26,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strconv" "time" "github.com/intelsdi-x/snap/control/plugin/cpolicy" @@ -66,6 +67,16 @@ func CheckPluginType(id PluginType) bool { return ok } +func GetPluginType(t string) (PluginType, error) { + if ityp, err := strconv.Atoi(t); err == nil { + if !CheckPluginType(PluginType(ityp)) { + return PluginType(-1), fmt.Errorf("invalid plugin type id given %d", ityp) + } + return PluginType(ityp), nil + } + return ToPluginType(t) +} + func (pt PluginType) String() string { return []string{ "collector", diff --git a/mgmt/rest/v1/config.go b/mgmt/rest/v1/config.go index 8ee1d8aa2..ad3c506e4 100644 --- a/mgmt/rest/v1/config.go +++ b/mgmt/rest/v1/config.go @@ -20,7 +20,6 @@ limitations under the License. package v1 import ( - "fmt" "net/http" "strconv" @@ -40,7 +39,7 @@ func (s *apiV1) getPluginConfigItem(w http.ResponseWriter, r *http.Request, p ht return } - typ, err := getPluginType(styp) + typ, err := core.GetPluginType(styp) if err != nil { rbody.Write(400, rbody.FromError(err), w) return @@ -68,7 +67,7 @@ func (s *apiV1) deletePluginConfigItem(w http.ResponseWriter, r *http.Request, p var typ core.PluginType styp := p.ByName("type") if styp != "" { - typ, err = getPluginType(styp) + typ, err = core.GetPluginType(styp) if err != nil { rbody.Write(400, rbody.FromError(err), w) return @@ -110,7 +109,7 @@ func (s *apiV1) setPluginConfigItem(w http.ResponseWriter, r *http.Request, p ht var typ core.PluginType styp := p.ByName("type") if styp != "" { - typ, err = getPluginType(styp) + typ, err = core.GetPluginType(styp) if err != nil { rbody.Write(400, rbody.FromError(err), w) return @@ -146,17 +145,3 @@ func (s *apiV1) setPluginConfigItem(w http.ResponseWriter, r *http.Request, p ht item := &rbody.SetPluginConfigItem{ConfigDataNode: res} rbody.Write(200, item, w) } - -func getPluginType(t string) (core.PluginType, error) { - if ityp, err := strconv.Atoi(t); err == nil { - if !core.CheckPluginType(core.PluginType(ityp)) { - return core.PluginType(-1), fmt.Errorf("invalid plugin type id given %d", ityp) - } - return core.PluginType(ityp), nil - } - ityp, err := core.ToPluginType(t) - if err != nil { - return core.PluginType(-1), err - } - return ityp, nil -} diff --git a/mgmt/rest/v2/config.go b/mgmt/rest/v2/config.go index 134d6f5b1..efa27b580 100644 --- a/mgmt/rest/v2/config.go +++ b/mgmt/rest/v2/config.go @@ -48,7 +48,7 @@ func (s *apiV2) getPluginConfigItem(w http.ResponseWriter, r *http.Request, p ht return } - typ, err := getPluginType(styp) + typ, err := core.GetPluginType(styp) if err != nil { Write(400, FromError(err), w) return @@ -74,7 +74,7 @@ func (s *apiV2) deletePluginConfigItem(w http.ResponseWriter, r *http.Request, p var typ core.PluginType styp := p.ByName("type") if styp != "" { - typ, err = getPluginType(styp) + typ, err = core.GetPluginType(styp) if err != nil { Write(400, FromError(err), w) return @@ -114,7 +114,7 @@ func (s *apiV2) setPluginConfigItem(w http.ResponseWriter, r *http.Request, p ht var typ core.PluginType styp := p.ByName("type") if styp != "" { - typ, err = getPluginType(styp) + typ, err = core.GetPluginType(styp) if err != nil { Write(400, FromError(err), w) return @@ -148,14 +148,3 @@ func (s *apiV2) setPluginConfigItem(w http.ResponseWriter, r *http.Request, p ht item := &PluginConfigItem{ConfigDataNode: res} Write(200, item, w) } - -func getPluginType(t string) (core.PluginType, error) { - if ityp, err := strconv.Atoi(t); err == nil { - return core.PluginType(ityp), nil - } - ityp, err := core.ToPluginType(t) - if err != nil { - return core.PluginType(-1), err - } - return ityp, nil -}