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

Commit

Permalink
Move v1 under mgmt/rest/v1
Browse files Browse the repository at this point in the history
Implements the API interface to v1.
  • Loading branch information
kindermoumoute committed Jan 24, 2017
1 parent 16da9b2 commit da7e778
Show file tree
Hide file tree
Showing 41 changed files with 1,334 additions and 1,598 deletions.
2 changes: 1 addition & 1 deletion cmd/snaptel/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"time"

"github.com/intelsdi-x/snap/mgmt/rest/client"
"github.com/intelsdi-x/snap/mgmt/rest/rbody"
"github.com/intelsdi-x/snap/mgmt/rest/v1/rbody"
"github.com/urfave/cli"

"github.com/intelsdi-x/snap/pkg/stringutils"
Expand Down
2 changes: 1 addition & 1 deletion mgmt/rest/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (

"github.com/asaskevich/govalidator"

"github.com/intelsdi-x/snap/mgmt/rest/rbody"
"github.com/intelsdi-x/snap/mgmt/rest/v1/rbody"
)

var (
Expand Down
7 changes: 4 additions & 3 deletions mgmt/rest/client/client_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

"github.com/intelsdi-x/snap/control"
"github.com/intelsdi-x/snap/mgmt/rest"
"github.com/intelsdi-x/snap/mgmt/rest/v1"
"github.com/intelsdi-x/snap/plugin/helper"
"github.com/intelsdi-x/snap/scheduler"
"github.com/intelsdi-x/snap/scheduler/wmap"
Expand Down Expand Up @@ -74,7 +75,7 @@ func getWMFromSample(sample string) *wmap.WorkflowMap {
// When we eventually have a REST API Stop command this can be killed.
func startAPI() string {
// Start a REST API to talk to
rest.StreamingBufferWindow = 0.01
v1.StreamingBufferWindow = 0.01
log.SetLevel(LOG_LEVEL)
r, _ := rest.New(rest.GetDefaultConfig())
c := control.New(control.GetDefaultConfig())
Expand Down Expand Up @@ -489,7 +490,7 @@ func TestSnapClient(t *testing.T) {
})
Convey("WatchTasks", func() {
Convey("invalid task ID", func() {
rest.StreamingBufferWindow = 0.01
v1.StreamingBufferWindow = 0.01

type ea struct {
events []string
Expand Down Expand Up @@ -520,7 +521,7 @@ func TestSnapClient(t *testing.T) {
So(r.Err.Error(), ShouldEqual, "Task not found: ID(1)")
})
Convey("event stream", func() {
rest.StreamingBufferWindow = 0.01
v1.StreamingBufferWindow = 0.01
sch := &Schedule{Type: "simple", Interval: "100ms"}
tf := c.CreateTask(sch, wf, "baron", "", false, 0)

Expand Down
2 changes: 1 addition & 1 deletion mgmt/rest/client/client_tribe_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"github.com/intelsdi-x/snap/control"
"github.com/intelsdi-x/snap/mgmt/rest"
"github.com/intelsdi-x/snap/mgmt/rest/client"
"github.com/intelsdi-x/snap/mgmt/rest/rbody"
"github.com/intelsdi-x/snap/mgmt/rest/v1/rbody"
"github.com/intelsdi-x/snap/mgmt/tribe"
"github.com/intelsdi-x/snap/scheduler"
)
Expand Down
2 changes: 1 addition & 1 deletion mgmt/rest/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"net/url"

"github.com/intelsdi-x/snap/core/ctypes"
"github.com/intelsdi-x/snap/mgmt/rest/rbody"
"github.com/intelsdi-x/snap/mgmt/rest/v1/rbody"
)

// GetPluginConfig retrieves the merged plugin config given the type of plugin,
Expand Down
2 changes: 1 addition & 1 deletion mgmt/rest/client/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"errors"
"fmt"

"github.com/intelsdi-x/snap/mgmt/rest/rbody"
"github.com/intelsdi-x/snap/mgmt/rest/v1/rbody"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion mgmt/rest/client/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"time"

"github.com/intelsdi-x/snap/core/serror"
"github.com/intelsdi-x/snap/mgmt/rest/rbody"
"github.com/intelsdi-x/snap/mgmt/rest/v1/rbody"
)

// LoadPlugin loads plugins for the given plugin names.
Expand Down
2 changes: 1 addition & 1 deletion mgmt/rest/client/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"time"

"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/mgmt/rest/rbody"
"github.com/intelsdi-x/snap/mgmt/rest/v1/rbody"
"github.com/intelsdi-x/snap/scheduler/wmap"
)

Expand Down
2 changes: 1 addition & 1 deletion mgmt/rest/client/tribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"encoding/json"
"fmt"

"github.com/intelsdi-x/snap/mgmt/rest/rbody"
"github.com/intelsdi-x/snap/mgmt/rest/v1/rbody"
)

// ListMembers retrieves a list of tribe members through an HTTP GET call.
Expand Down
230 changes: 83 additions & 147 deletions mgmt/rest/config.go
Original file line number Diff line number Diff line change
@@ -1,158 +1,94 @@
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rest

import (
"net/http"
"strconv"

"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/core/cdata"
"github.com/intelsdi-x/snap/mgmt/rest/rbody"
"github.com/julienschmidt/httprouter"
// default configuration values
const (
defaultEnable bool = true
defaultPort int = 8181
defaultAddress string = ""
defaultHTTPS bool = false
defaultRestCertificate string = ""
defaultRestKey string = ""
defaultAuth bool = false
defaultAuthPassword string = ""
defaultPortSetByConfig bool = false
defaultPprof bool = false
)

func (s *Server) getPluginConfigItem(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var err error
styp := p.ByName("type")
if styp == "" {
cdn := s.mc.GetPluginConfigDataNodeAll()
item := &rbody.PluginConfigItem{ConfigDataNode: cdn}
respond(200, item, w)
return
}

typ, err := getPluginType(styp)
if err != nil {
respond(400, rbody.FromError(err), w)
return
}

name := p.ByName("name")
sver := p.ByName("version")
var iver int
if sver != "" {
if iver, err = strconv.Atoi(sver); err != nil {
respond(400, rbody.FromError(err), w)
return
}
} else {
iver = -2
}

cdn := s.mc.GetPluginConfigDataNode(typ, name, iver)
item := &rbody.PluginConfigItem{ConfigDataNode: cdn}
respond(200, item, w)
}

func (s *Server) deletePluginConfigItem(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var err error
var typ core.PluginType
styp := p.ByName("type")
if styp != "" {
typ, err = getPluginType(styp)
if err != nil {
respond(400, rbody.FromError(err), w)
return
}
}

name := p.ByName("name")
sver := p.ByName("version")
var iver int
if sver != "" {
if iver, err = strconv.Atoi(sver); err != nil {
respond(400, rbody.FromError(err), w)
return
}
} else {
iver = -2
}

src := []string{}
errCode, err := core.UnmarshalBody(&src, r.Body)
if errCode != 0 && err != nil {
respond(400, rbody.FromError(err), w)
return
}

var res cdata.ConfigDataNode
if styp == "" {
res = s.mc.DeletePluginConfigDataNodeFieldAll(src...)
} else {
res = s.mc.DeletePluginConfigDataNodeField(typ, name, iver, src...)
}

item := &rbody.DeletePluginConfigItem{ConfigDataNode: res}
respond(200, item, w)
// holds the configuration passed in through the SNAP config file
// Note: if this struct is modified, then the switch statement in the
// UnmarshalJSON method in this same file needs to be modified to
// match the field mapping that is defined here
type Config struct {
Enable bool `json:"enable"yaml:"enable"`
Port int `json:"port"yaml:"port"`
Address string `json:"addr"yaml:"addr"`
HTTPS bool `json:"https"yaml:"https"`
RestCertificate string `json:"rest_certificate"yaml:"rest_certificate"`
RestKey string `json:"rest_key"yaml:"rest_key"`
RestAuth bool `json:"rest_auth"yaml:"rest_auth"`
RestAuthPassword string `json:"rest_auth_password"yaml:"rest_auth_password"`
portSetByConfig bool ``
Pprof bool `json:"pprof"yaml:"pprof"`
}

func (s *Server) setPluginConfigItem(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var err error
var typ core.PluginType
styp := p.ByName("type")
if styp != "" {
typ, err = getPluginType(styp)
if err != nil {
respond(400, rbody.FromError(err), w)
return
}
}

name := p.ByName("name")
sver := p.ByName("version")
var iver int
if sver != "" {
if iver, err = strconv.Atoi(sver); err != nil {
respond(400, rbody.FromError(err), w)
return
}
} else {
iver = -2
}

src := cdata.NewNode()
errCode, err := core.UnmarshalBody(src, r.Body)
if errCode != 0 && err != nil {
respond(400, rbody.FromError(err), w)
return
}
const (
CONFIG_CONSTRAINTS = `
"restapi" : {
"type": ["object", "null"],
"properties" : {
"enable": {
"type": "boolean"
},
"https" : {
"type": "boolean"
},
"rest_auth": {
"type": "boolean"
},
"rest_auth_password": {
"type": "string"
},
"rest_certificate": {
"type": "string"
},
"rest_key" : {
"type": "string"
},
"port" : {
"type": "integer",
"minimum": 1,
"maximum": 65535
},
"addr" : {
"type": "string"
},
"pprof": {
"type": "boolean"
}
},
"additionalProperties": false
}
`
)

var res cdata.ConfigDataNode
if styp == "" {
res = s.mc.MergePluginConfigDataNodeAll(src)
} else {
res = s.mc.MergePluginConfigDataNode(typ, name, iver, src)
// GetDefaultConfig gets the default snapteld configuration
func GetDefaultConfig() *Config {
return &Config{
Enable: defaultEnable,
Port: defaultPort,
Address: defaultAddress,
HTTPS: defaultHTTPS,
RestCertificate: defaultRestCertificate,
RestKey: defaultRestKey,
RestAuth: defaultAuth,
RestAuthPassword: defaultAuthPassword,
portSetByConfig: defaultPortSetByConfig,
Pprof: defaultPprof,
}

item := &rbody.SetPluginConfigItem{ConfigDataNode: res}
respond(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
// define a method that can be used to determine if the port the RESTful
// API is listening on was set in the configuration file
func (c *Config) PortSetByConfigFile() bool {
return c.portSetByConfig
}
44 changes: 44 additions & 0 deletions mgmt/rest/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package rest

import (
"net/http"
"net/http/pprof"

"github.com/julienschmidt/httprouter"
)

func (s *Server) addPprofRoutes() {
if s.pprof {
s.r.GET("/debug/pprof/", s.index)
s.r.GET("/debug/pprof/block", s.index)
s.r.GET("/debug/pprof/goroutine", s.index)
s.r.GET("/debug/pprof/heap", s.index)
s.r.GET("/debug/pprof/threadcreate", s.index)
s.r.GET("/debug/pprof/cmdline", s.cmdline)
s.r.GET("/debug/pprof/profile", s.profile)
s.r.GET("/debug/pprof/symbol", s.symbol)
s.r.GET("/debug/pprof/trace", s.trace)
}
}

// profiling tools handlers

func (s *Server) index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
pprof.Index(w, r)
}

func (s *Server) cmdline(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
pprof.Cmdline(w, r)
}

func (s *Server) profile(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
pprof.Profile(w, r)
}

func (s *Server) symbol(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
pprof.Symbol(w, r)
}

func (s *Server) trace(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
pprof.Trace(w, r)
}
Loading

0 comments on commit da7e778

Please sign in to comment.