Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Remove duplicate function for getPluginType:
Browse files Browse the repository at this point in the history
- remove from config.go  v1 and v2 (duplicated)
- moved to plugin.go
- simplified function to reduce duplicate error handling
  • Loading branch information
kjlyon committed Apr 12, 2017
1 parent c068511 commit 8f59234
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 32 deletions.
11 changes: 11 additions & 0 deletions core/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"time"

"github.com/intelsdi-x/snap/control/plugin/cpolicy"
Expand Down Expand Up @@ -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",
Expand Down
21 changes: 3 additions & 18 deletions mgmt/rest/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ limitations under the License.
package v1

import (
"fmt"
"net/http"
"strconv"

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
17 changes: 3 additions & 14 deletions mgmt/rest/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 8f59234

Please sign in to comment.