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

Commit

Permalink
REST API - adds href attributes for plugin resources
Browse files Browse the repository at this point in the history
  • Loading branch information
lynxbat committed Nov 21, 2015
1 parent 2483d23 commit d51b85b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
13 changes: 10 additions & 3 deletions mgmt/rest/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"compress/gzip"
"crypto/sha256"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
Expand Down Expand Up @@ -179,7 +180,7 @@ func (s *Server) loadPlugin(w http.ResponseWriter, r *http.Request, _ httprouter
respond(ec, rb, w)
return
}
lp.LoadedPlugins = append(lp.LoadedPlugins, *catalogedPluginToLoaded(pl))
lp.LoadedPlugins = append(lp.LoadedPlugins, *catalogedPluginToLoaded(r.Host, pl))
respond(201, lp, w)
}
}
Expand Down Expand Up @@ -270,7 +271,7 @@ func (s *Server) getPlugins(w http.ResponseWriter, r *http.Request, _ httprouter
plCatalog := s.mm.PluginCatalog()
plugins.LoadedPlugins = make([]rbody.LoadedPlugin, len(plCatalog))
for i, p := range s.mm.PluginCatalog() {
plugins.LoadedPlugins[i] = *catalogedPluginToLoaded(p)
plugins.LoadedPlugins[i] = *catalogedPluginToLoaded(r.Host, p)
}

if detail {
Expand All @@ -291,14 +292,15 @@ func (s *Server) getPlugins(w http.ResponseWriter, r *http.Request, _ httprouter
respond(200, plugins, w)
}

func catalogedPluginToLoaded(c core.CatalogedPlugin) *rbody.LoadedPlugin {
func catalogedPluginToLoaded(host string, c core.CatalogedPlugin) *rbody.LoadedPlugin {
return &rbody.LoadedPlugin{
Name: c.Name(),
Version: c.Version(),
Type: c.TypeName(),
Signed: c.IsSigned(),
Status: c.Status(),
LoadedTimestamp: c.LoadedTimestamp().Unix(),
Href: catalogedPluginURI(host, c),
}
}

Expand Down Expand Up @@ -384,7 +386,12 @@ func (s *Server) getPlugin(w http.ResponseWriter, r *http.Request, p httprouter.
Signed: plugin.IsSigned(),
Status: plugin.Status(),
LoadedTimestamp: plugin.LoadedTimestamp().Unix(),
Href: catalogedPluginURI(r.Host, plugin),
}
respond(200, pluginRet, w)
}
}

func catalogedPluginURI(host string, c core.CatalogedPlugin) string {
return fmt.Sprintf("%s://%s/v1/plugins/%s/%s/%d", protocolPrefix, host, c.TypeName(), c.Name(), c.Version())
}
2 changes: 2 additions & 0 deletions mgmt/rest/rbody/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type LoadedPlugin struct {
Signed bool `json:"signed"`
Status string `json:"status"`
LoadedTimestamp int64 `json:"loaded_timestamp"`
Href string `json:"href"`
}

type AvailablePlugin struct {
Expand All @@ -104,4 +105,5 @@ type AvailablePlugin struct {
HitCount int `json:"hitcount"`
LastHitTimestamp int64 `json:"last_hit_timestamp"`
ID uint32 `json:"id"`
Href string `json:"href"`
}
4 changes: 3 additions & 1 deletion mgmt/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ const (
var (
ErrBadCert = errors.New("Invalid certificate given")

restLogger = log.WithField("_module", "_mgmt-rest")
restLogger = log.WithField("_module", "_mgmt-rest")
protocolPrefix = "http"
)

type managesMetrics interface {
Expand Down Expand Up @@ -135,6 +136,7 @@ func New(https bool, cpath, kpath string) (*Server, error) {
if err != nil {
return nil, err
}
protocolPrefix = "https"
}

restLogger.Info(fmt.Sprintf("Configuring REST API with HTTPS set to: %v", https))
Expand Down

0 comments on commit d51b85b

Please sign in to comment.