Initial api routes
This commit is contained in:
parent
91076dc1d0
commit
00b6d6e436
10 changed files with 357 additions and 1 deletions
|
@ -1,6 +1,6 @@
|
|||
// Code generated by Wire. DO NOT EDIT.
|
||||
|
||||
//go:generate wire
|
||||
//go:generate go run github.com/google/wire/cmd/wire
|
||||
//+build !wireinject
|
||||
|
||||
package main
|
||||
|
|
80
core/template.go
Normal file
80
core/template.go
Normal file
|
@ -0,0 +1,80 @@
|
|||
// Copyright 2019 Drone IO, Inc.
|
||||
//
|
||||
// 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 core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/drone/drone/handler/api/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
errTemplateNameInvalid = errors.New("Invalid Template Name")
|
||||
errTemplateDataInvalid = errors.New("Invalid Template Data")
|
||||
errTemplateCreatedInvalid = errors.New("Invalid Template Created Value")
|
||||
errTemplateUpdatedInvalid = errors.New("Invalid Template Updated Value")
|
||||
)
|
||||
|
||||
type (
|
||||
TemplateArgs struct {
|
||||
Kind string
|
||||
Load string
|
||||
Data map[string]interface{}
|
||||
}
|
||||
|
||||
Template struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Data string `json:"data,omitempty"`
|
||||
Created int64 `json:"created,omitempty"`
|
||||
Updated int64 `json:"updated,omitempty"`
|
||||
}
|
||||
|
||||
// TemplateStore manages repository templates.
|
||||
TemplateStore interface {
|
||||
// ListAll returns templates list from the datastore.
|
||||
ListAll(ctx context.Context) ([]*Template, error)
|
||||
|
||||
// Find returns a template from the datastore.
|
||||
Find(ctx context.Context, id int64) (*Template, error)
|
||||
|
||||
// FindName returns a template from the datastore by name
|
||||
FindName(ctx context.Context, name string) (*Template, error)
|
||||
|
||||
// Create persists a new template to the datastore.
|
||||
Create(ctx context.Context, template *Template) error
|
||||
|
||||
// Update persists an updated template to the datastore.
|
||||
Update(ctx context.Context, template *Template) error
|
||||
|
||||
// Delete deletes a template from the datastore.
|
||||
Delete(ctx context.Context, template *Template) error
|
||||
}
|
||||
)
|
||||
|
||||
// Validate validates the required fields and formats.
|
||||
func (s *Template) Validate() error {
|
||||
switch {
|
||||
case len(s.Name) == 0:
|
||||
return errTemplateNameInvalid
|
||||
case len(s.Data) == 0:
|
||||
return errTemplateDataInvalid
|
||||
case s.Created == 0:
|
||||
return errTemplateCreatedInvalid
|
||||
case s.Updated == 0:
|
||||
return errTemplateUpdatedInvalid
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
|
@ -40,6 +40,7 @@ import (
|
|||
"github.com/drone/drone/handler/api/repos/sign"
|
||||
globalsecrets "github.com/drone/drone/handler/api/secrets"
|
||||
"github.com/drone/drone/handler/api/system"
|
||||
"github.com/drone/drone/handler/api/template"
|
||||
"github.com/drone/drone/handler/api/user"
|
||||
"github.com/drone/drone/handler/api/user/remote"
|
||||
"github.com/drone/drone/handler/api/users"
|
||||
|
@ -82,6 +83,7 @@ func New(
|
|||
stream core.LogStream,
|
||||
syncer core.Syncer,
|
||||
system *core.System,
|
||||
templates core.TemplateStore,
|
||||
transferer core.Transferer,
|
||||
triggerer core.Triggerer,
|
||||
users core.UserStore,
|
||||
|
@ -111,6 +113,7 @@ func New(
|
|||
Stream: stream,
|
||||
Syncer: syncer,
|
||||
System: system,
|
||||
Templates: templates,
|
||||
Transferer: transferer,
|
||||
Triggerer: triggerer,
|
||||
Users: users,
|
||||
|
@ -143,6 +146,7 @@ type Server struct {
|
|||
Stream core.LogStream
|
||||
Syncer core.Syncer
|
||||
System *core.System
|
||||
Templates core.TemplateStore
|
||||
Transferer core.Transferer
|
||||
Triggerer core.Triggerer
|
||||
Users core.UserStore
|
||||
|
@ -253,6 +257,15 @@ func (s Server) Handler() http.Handler {
|
|||
r.Delete("/{secret}", secrets.HandleDelete(s.Repos, s.Secrets))
|
||||
})
|
||||
|
||||
r.Route("/templates", func(r chi.Router) {
|
||||
r.Use(acl.CheckWriteAccess())
|
||||
r.Get("/", template.HandleList(s.Templates))
|
||||
r.Post("/", template.HandleCreate(s.Templates))
|
||||
r.Get("/{name}", template.HandleFind(s.Templates))
|
||||
r.Patch("/{name}", template.HandleUpdate(s.Templates))
|
||||
r.Delete("/{name}", template.HandleDelete(s.Templates))
|
||||
})
|
||||
|
||||
r.Route("/sign", func(r chi.Router) {
|
||||
r.Use(acl.CheckWriteAccess())
|
||||
r.Post("/", sign.HandleSign(s.Repos))
|
||||
|
|
26
handler/api/template/all.go
Normal file
26
handler/api/template/all.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Drone Non-Commercial License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// +build !oss
|
||||
|
||||
package template
|
||||
|
||||
import (
|
||||
"github.com/drone/drone/core"
|
||||
"github.com/drone/drone/handler/api/render"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// HandleAll returns an http.HandlerFunc that writes a json-encoded
|
||||
// list of secrets to the response body.
|
||||
func HandleAll(templates core.TemplateStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
list, err := templates.ListAll(r.Context())
|
||||
if err != nil {
|
||||
render.NotFound(w, err)
|
||||
return
|
||||
}
|
||||
render.JSON(w, list, 200)
|
||||
}
|
||||
}
|
29
handler/api/template/all_test.go
Normal file
29
handler/api/template/all_test.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Drone Non-Commercial License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// +build !oss
|
||||
|
||||
package template
|
||||
|
||||
//func TestHandleAll(t *testing.T) {
|
||||
// controller := gomock.NewController(t)
|
||||
// defer controller.Finish()
|
||||
//
|
||||
// secrets := mock.NewMockGlobalSecretStore(controller)
|
||||
// secrets.EXPECT().ListAll(gomock.Any()).Return(dummySecretList, nil)
|
||||
//
|
||||
// w := httptest.NewRecorder()
|
||||
// r := httptest.NewRequest("GET", "/", nil)
|
||||
//
|
||||
// HandleAll(secrets).ServeHTTP(w, r)
|
||||
// if got, want := w.Code, http.StatusOK; want != got {
|
||||
// t.Errorf("Want response code %d, got %d", want, got)
|
||||
// }
|
||||
//
|
||||
// got, want := []*core.Secret{}, dummySecretListScrubbed
|
||||
// json.NewDecoder(w.Body).Decode(&got)
|
||||
// if diff := cmp.Diff(got, want); len(diff) != 0 {
|
||||
// t.Errorf(diff)
|
||||
// }
|
||||
//}
|
55
handler/api/template/create.go
Normal file
55
handler/api/template/create.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Drone Non-Commercial License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// +build !oss
|
||||
|
||||
package template
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/drone/drone/core"
|
||||
"github.com/drone/drone/handler/api/render"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type templateInput struct {
|
||||
Name string `json:"name"`
|
||||
Data string `json:"data"`
|
||||
Created int64 `json:"created"`
|
||||
Updated int64 `json:"updated"`
|
||||
}
|
||||
|
||||
// HandleCreate returns an http.HandlerFunc that processes http
|
||||
// requests to create a new template.
|
||||
func HandleCreate(secrets core.TemplateStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
in := new(templateInput)
|
||||
err := json.NewDecoder(r.Body).Decode(in)
|
||||
if err != nil {
|
||||
render.BadRequest(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
t := &core.Template{
|
||||
Name: in.Name,
|
||||
Data: in.Data,
|
||||
Created: in.Created,
|
||||
Updated: in.Updated,
|
||||
}
|
||||
|
||||
err = t.Validate()
|
||||
if err != nil {
|
||||
render.BadRequest(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = secrets.Create(r.Context(), t)
|
||||
if err != nil {
|
||||
render.InternalError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
render.JSON(w, t, 200)
|
||||
}
|
||||
}
|
35
handler/api/template/delete.go
Normal file
35
handler/api/template/delete.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Drone Non-Commercial License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// +build !oss
|
||||
|
||||
package template
|
||||
|
||||
import (
|
||||
"github.com/drone/drone/core"
|
||||
"github.com/drone/drone/handler/api/render"
|
||||
"github.com/go-chi/chi"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// HandleDelete returns an http.HandlerFunc that processes http
|
||||
// requests to delete a template.
|
||||
func HandleDelete(template core.TemplateStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
name = chi.URLParam(r, "name")
|
||||
)
|
||||
s, err := template.FindName(r.Context(), name)
|
||||
if err != nil {
|
||||
render.NotFound(w, err)
|
||||
return
|
||||
}
|
||||
err = template.Delete(r.Context(), s)
|
||||
if err != nil {
|
||||
render.InternalError(w, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
}
|
30
handler/api/template/find.go
Normal file
30
handler/api/template/find.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Drone Non-Commercial License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// +build !oss
|
||||
|
||||
package template
|
||||
|
||||
import (
|
||||
"github.com/drone/drone/core"
|
||||
"github.com/drone/drone/handler/api/render"
|
||||
"github.com/go-chi/chi"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// HandleFind returns an http.HandlerFunc that writes json-encoded
|
||||
// template details to the the response body.
|
||||
func HandleFind(templateStore core.TemplateStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
name = chi.URLParam(r, "name")
|
||||
)
|
||||
template, err := templateStore.FindName(r.Context(), name)
|
||||
if err != nil {
|
||||
render.NotFound(w, err)
|
||||
return
|
||||
}
|
||||
render.JSON(w, template, 200)
|
||||
}
|
||||
}
|
26
handler/api/template/list.go
Normal file
26
handler/api/template/list.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Drone Non-Commercial License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// +build !oss
|
||||
|
||||
package template
|
||||
|
||||
import (
|
||||
"github.com/drone/drone/core"
|
||||
"github.com/drone/drone/handler/api/render"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// HandleList returns an http.HandlerFunc that writes a json-encoded
|
||||
// list of templates to the response body.
|
||||
func HandleList(templateStore core.TemplateStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
list, err := templateStore.ListAll(r.Context())
|
||||
if err != nil {
|
||||
render.NotFound(w, err)
|
||||
return
|
||||
}
|
||||
render.JSON(w, list, 200)
|
||||
}
|
||||
}
|
62
handler/api/template/update.go
Normal file
62
handler/api/template/update.go
Normal file
|
@ -0,0 +1,62 @@
|
|||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Drone Non-Commercial License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package template
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/drone/drone/core"
|
||||
"github.com/drone/drone/handler/api/render"
|
||||
"github.com/go-chi/chi"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type templateUpdate struct {
|
||||
Data *string `json:"data"`
|
||||
Updated *int64 `json:"Updated"`
|
||||
}
|
||||
|
||||
// HandleUpdate returns an http.HandlerFunc that processes http
|
||||
// requests to update a template.
|
||||
func HandleUpdate(templateStore core.TemplateStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
name = chi.URLParam(r, "name")
|
||||
)
|
||||
|
||||
in := new(templateUpdate)
|
||||
err := json.NewDecoder(r.Body).Decode(in)
|
||||
if err != nil {
|
||||
render.BadRequest(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
s, err := templateStore.FindName(r.Context(), name)
|
||||
if err != nil {
|
||||
render.NotFound(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if in.Data != nil {
|
||||
s.Data = *in.Data
|
||||
}
|
||||
if in.Updated != nil {
|
||||
s.Updated = *in.Updated
|
||||
}
|
||||
|
||||
err = s.Validate()
|
||||
if err != nil {
|
||||
render.BadRequest(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = templateStore.Update(r.Context(), s)
|
||||
if err != nil {
|
||||
render.InternalError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
render.JSON(w, s, 200)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue