Skip to content

Commit

Permalink
Enabled repositories API endpoint (helm#203)
Browse files Browse the repository at this point in the history
* Expose repors

* Repository API endpoint
  • Loading branch information
migmartri authored Mar 2, 2017
1 parent c32becd commit 437497d
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/api/data/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Masterminds/semver"
"github.com/helm/monocular/src/api/config"
"github.com/helm/monocular/src/api/config/repos"
"github.com/helm/monocular/src/api/swagger/models"
"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -65,6 +66,29 @@ func MakeChartResource(chart *models.ChartPackage) *models.Resource {
return &ret
}

// MakeRepoResource composes a Resource type that represents a repository
func MakeRepoResource(repo repos.Repo) *models.Resource {
var ret models.Resource
ret.Type = StrToPtr("repository")
ret.ID = StrToPtr(repo.Name)
ret.Attributes = &models.Repo{
Name: &repo.Name,
URL: &repo.URL,
Source: repo.Source,
}
return &ret
}

// MakeRepoResources returns an array of RepoResources
func MakeRepoResources(repos []repos.Repo) []*models.Resource {
var reposResource []*models.Resource
for _, repo := range repos {
resource := MakeRepoResource(repo)
reposResource = append(reposResource, resource)
}
return reposResource
}

// MakeChartResources accepts a slice of repo+chart data, converts each to a Resource type
// and then returns the slice of the converted Resource types (throwing away version information,
// and collapsing all chart+version records into a single resource representation for each chart)
Expand Down
25 changes: 25 additions & 0 deletions src/api/data/helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/arschles/assert"
"github.com/helm/monocular/src/api/config"
"github.com/helm/monocular/src/api/data/cache/charthelper"
"github.com/helm/monocular/src/api/swagger/models"
)
Expand Down Expand Up @@ -78,6 +79,30 @@ func TestMakeChartResources(t *testing.T) {
assert.Equal(t, *chartsResource[0].Attributes.(*models.Chart).Home, chartHome, "chart resource Attributes.Home field value")
}

func TestMakeRepoResource(t *testing.T) {
config, err := config.GetConfig()
assert.NoErr(t, err)
repo := config.Repos[0]
repoResource := MakeRepoResource(repo)
assert.Equal(t, *repoResource.Type, "repository", "repo resource type field value")
assert.Equal(t, *repoResource.ID, repo.Name, "repo resource ID field value")
assert.Equal(t, *repoResource.Attributes.(*models.Repo).Name, repo.Name, "repo name")
assert.Equal(t, *repoResource.Attributes.(*models.Repo).URL, repo.URL, "repo URL")
assert.Equal(t, repoResource.Attributes.(*models.Repo).Source, repo.Source, "chart resource Attributes.URL field value")
}

func TestMakeRepoResources(t *testing.T) {
config, err := config.GetConfig()
assert.NoErr(t, err)
repos := config.Repos
repoResource := MakeRepoResources(repos)[0]
assert.Equal(t, *repoResource.Type, "repository", "repo resource type field value")
assert.Equal(t, *repoResource.ID, repos[0].Name, "repo resource ID field value")
assert.Equal(t, *repoResource.Attributes.(*models.Repo).Name, repos[0].Name, "repo name")
assert.Equal(t, *repoResource.Attributes.(*models.Repo).URL, repos[0].URL, "repo URL")
assert.Equal(t, repoResource.Attributes.(*models.Repo).Source, repos[0].Source, "chart resource Attributes.URL field value")
}

func TestMakeChartVersionResource(t *testing.T) {
charts, err := ParseYAMLRepo(getTestRepoYAML(), repoName)
assert.NoErr(t, err)
Expand Down
23 changes: 23 additions & 0 deletions src/api/handlers/repos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package handlers

import (
middleware "github.com/go-openapi/runtime/middleware"
"github.com/helm/monocular/src/api/config"
"github.com/helm/monocular/src/api/data/helpers"
"github.com/helm/monocular/src/api/swagger/models"
"github.com/helm/monocular/src/api/swagger/restapi/operations"
)

// GetRepos returns all the enabled repositories
func GetRepos(params operations.GetAllReposParams) middleware.Responder {
config, _ := config.GetConfig()
resources := helpers.MakeRepoResources(config.Repos)
return reposHTTPBody(resources)
}

func reposHTTPBody(repos []*models.Resource) middleware.Responder {
resourceArrayData := models.ResourceArrayData{
Data: repos,
}
return operations.NewGetAllReposOK().WithPayload(&resourceArrayData)
}
28 changes: 28 additions & 0 deletions src/api/handlers/repos_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package handlers

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/arschles/assert"
"github.com/go-openapi/runtime"
"github.com/helm/monocular/src/api/config"
"github.com/helm/monocular/src/api/swagger/models"
"github.com/helm/monocular/src/api/swagger/restapi/operations"
"github.com/helm/monocular/src/api/testutil"
)

func TestGetAllRepos200(t *testing.T) {
w := httptest.NewRecorder()
params := operations.GetAllReposParams{}
resp := GetRepos(params)
assert.NotNil(t, resp, "GetRepos response")
resp.WriteResponse(w, runtime.JSONProducer())
assert.Equal(t, w.Code, http.StatusOK, "expect a 200 response code")
var httpBody models.ResourceArrayData
assert.NoErr(t, testutil.ResourceArrayDataFromJSON(w.Body, &httpBody))
config, err := config.GetConfig()
assert.NoErr(t, err)
assert.Equal(t, len(config.Repos), len(httpBody.Data), "Returns the enabled repos")
}
13 changes: 13 additions & 0 deletions src/api/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ paths:
description: unexpected error
schema:
$ref: "#/definitions/error"
/v1/repos:
get:
operationId: getAllRepos
summary: "get all repositories enabled in the backend"
responses:
200:
description: repositories response
schema:
$ref: "#/definitions/resourceArrayData"
default:
description: unexpected error
schema:
$ref: "#/definitions/error"
parameters:
chartNameParam:
name: name
Expand Down
4 changes: 4 additions & 0 deletions src/api/swagger/restapi/configure_monocular.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func configureAPI(api *operations.MonocularAPI) http.Handler {
api.JSONConsumer = runtime.JSONConsumer()
api.JSONProducer = runtime.JSONProducer()

api.GetAllReposHandler = operations.GetAllReposHandlerFunc(func(params operations.GetAllReposParams) middleware.Responder {
return handlers.GetRepos(params)
})

api.GetChartHandler = operations.GetChartHandlerFunc(func(params operations.GetChartParams) middleware.Responder {
return handlers.GetChart(params, chartsImplementation)
})
Expand Down
2 changes: 1 addition & 1 deletion src/api/swagger/restapi/embedded_spec.go

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions src/api/swagger/restapi/operations/get_all_repos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package operations

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the generate command

import (
"net/http"

middleware "github.com/go-openapi/runtime/middleware"
)

// GetAllReposHandlerFunc turns a function with the right signature into a get all repos handler
type GetAllReposHandlerFunc func(GetAllReposParams) middleware.Responder

// Handle executing the request and returning a response
func (fn GetAllReposHandlerFunc) Handle(params GetAllReposParams) middleware.Responder {
return fn(params)
}

// GetAllReposHandler interface for that can handle valid get all repos params
type GetAllReposHandler interface {
Handle(GetAllReposParams) middleware.Responder
}

// NewGetAllRepos creates a new http.Handler for the get all repos operation
func NewGetAllRepos(ctx *middleware.Context, handler GetAllReposHandler) *GetAllRepos {
return &GetAllRepos{Context: ctx, Handler: handler}
}

/*GetAllRepos swagger:route GET /v1/repos getAllRepos
get all repositories enabled in the backend
*/
type GetAllRepos struct {
Context *middleware.Context
Handler GetAllReposHandler
}

func (o *GetAllRepos) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
route, _ := o.Context.RouteInfo(r)
var Params = NewGetAllReposParams()

if err := o.Context.BindValidRequest(r, route, &Params); err != nil { // bind params
o.Context.Respond(rw, r, route.Produces, route, err)
return
}

res := o.Handler.Handle(Params) // actually handle the request

o.Context.Respond(rw, r, route.Produces, route, res)

}
40 changes: 40 additions & 0 deletions src/api/swagger/restapi/operations/get_all_repos_parameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package operations

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

import (
"net/http"

"github.com/go-openapi/errors"
"github.com/go-openapi/runtime/middleware"
)

// NewGetAllReposParams creates a new GetAllReposParams object
// with the default values initialized.
func NewGetAllReposParams() GetAllReposParams {
var ()
return GetAllReposParams{}
}

// GetAllReposParams contains all the bound params for the get all repos operation
// typically these are obtained from a http.Request
//
// swagger:parameters getAllRepos
type GetAllReposParams struct {

// HTTP Request Object
HTTPRequest *http.Request
}

// BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface
// for simple values it will use straight method calls
func (o *GetAllReposParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error {
var res []error
o.HTTPRequest = r

if len(res) > 0 {
return errors.CompositeValidationError(res...)
}
return nil
}
104 changes: 104 additions & 0 deletions src/api/swagger/restapi/operations/get_all_repos_responses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package operations

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

import (
"net/http"

"github.com/go-openapi/runtime"

"github.com/helm/monocular/src/api/swagger/models"
)

/*GetAllReposOK repositories response
swagger:response getAllReposOK
*/
type GetAllReposOK struct {

// In: body
Payload *models.ResourceArrayData `json:"body,omitempty"`
}

// NewGetAllReposOK creates GetAllReposOK with default headers values
func NewGetAllReposOK() *GetAllReposOK {
return &GetAllReposOK{}
}

// WithPayload adds the payload to the get all repos o k response
func (o *GetAllReposOK) WithPayload(payload *models.ResourceArrayData) *GetAllReposOK {
o.Payload = payload
return o
}

// SetPayload sets the payload to the get all repos o k response
func (o *GetAllReposOK) SetPayload(payload *models.ResourceArrayData) {
o.Payload = payload
}

// WriteResponse to the client
func (o *GetAllReposOK) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) {

rw.WriteHeader(200)
if o.Payload != nil {
if err := producer.Produce(rw, o.Payload); err != nil {
panic(err) // let the recovery middleware deal with this
}
}
}

/*GetAllReposDefault unexpected error
swagger:response getAllReposDefault
*/
type GetAllReposDefault struct {
_statusCode int

// In: body
Payload *models.Error `json:"body,omitempty"`
}

// NewGetAllReposDefault creates GetAllReposDefault with default headers values
func NewGetAllReposDefault(code int) *GetAllReposDefault {
if code <= 0 {
code = 500
}

return &GetAllReposDefault{
_statusCode: code,
}
}

// WithStatusCode adds the status to the get all repos default response
func (o *GetAllReposDefault) WithStatusCode(code int) *GetAllReposDefault {
o._statusCode = code
return o
}

// SetStatusCode sets the status to the get all repos default response
func (o *GetAllReposDefault) SetStatusCode(code int) {
o._statusCode = code
}

// WithPayload adds the payload to the get all repos default response
func (o *GetAllReposDefault) WithPayload(payload *models.Error) *GetAllReposDefault {
o.Payload = payload
return o
}

// SetPayload sets the payload to the get all repos default response
func (o *GetAllReposDefault) SetPayload(payload *models.Error) {
o.Payload = payload
}

// WriteResponse to the client
func (o *GetAllReposDefault) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) {

rw.WriteHeader(o._statusCode)
if o.Payload != nil {
if err := producer.Produce(rw, o.Payload); err != nil {
panic(err) // let the recovery middleware deal with this
}
}
}
11 changes: 11 additions & 0 deletions src/api/swagger/restapi/operations/monocular_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type MonocularAPI struct {

// GetAllChartsHandler sets the operation handler for the get all charts operation
GetAllChartsHandler GetAllChartsHandler
// GetAllReposHandler sets the operation handler for the get all repos operation
GetAllReposHandler GetAllReposHandler
// GetChartHandler sets the operation handler for the get chart operation
GetChartHandler GetChartHandler
// GetChartVersionHandler sets the operation handler for the get chart version operation
Expand Down Expand Up @@ -123,6 +125,10 @@ func (o *MonocularAPI) Validate() error {
unregistered = append(unregistered, "GetAllChartsHandler")
}

if o.GetAllReposHandler == nil {
unregistered = append(unregistered, "GetAllReposHandler")
}

if o.GetChartHandler == nil {
unregistered = append(unregistered, "GetChartHandler")
}
Expand Down Expand Up @@ -225,6 +231,11 @@ func (o *MonocularAPI) initHandlerCache() {
}
o.handlers["GET"]["/v1/charts"] = NewGetAllCharts(o.context, o.GetAllChartsHandler)

if o.handlers["GET"] == nil {
o.handlers[strings.ToUpper("GET")] = make(map[string]http.Handler)
}
o.handlers["GET"]["/v1/repos"] = NewGetAllRepos(o.context, o.GetAllReposHandler)

if o.handlers["GET"] == nil {
o.handlers[strings.ToUpper("GET")] = make(map[string]http.Handler)
}
Expand Down

0 comments on commit 437497d

Please sign in to comment.