2021-05-19 13:18:24 +00:00
|
|
|
// 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"
|
2021-06-01 09:29:58 +00:00
|
|
|
"net/http"
|
|
|
|
|
2021-05-19 13:18:24 +00:00
|
|
|
"github.com/drone/drone/core"
|
|
|
|
"github.com/drone/drone/handler/api/render"
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
)
|
|
|
|
|
|
|
|
type templateUpdate struct {
|
2021-05-28 15:59:00 +00:00
|
|
|
Data *string `json:"data"`
|
|
|
|
Namespace *string `json:"namespace"`
|
2021-05-19 13:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (
|
2021-05-28 15:59:00 +00:00
|
|
|
name = chi.URLParam(r, "name")
|
|
|
|
namespace = chi.URLParam(r, "namespace")
|
2021-05-19 13:18:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
in := new(templateUpdate)
|
|
|
|
err := json.NewDecoder(r.Body).Decode(in)
|
|
|
|
if err != nil {
|
|
|
|
render.BadRequest(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-28 15:59:00 +00:00
|
|
|
s, err := templateStore.FindName(r.Context(), name, namespace)
|
2021-05-19 13:18:24 +00:00
|
|
|
if err != nil {
|
|
|
|
render.NotFound(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if in.Data != nil {
|
|
|
|
s.Data = *in.Data
|
|
|
|
}
|
2021-05-28 15:59:00 +00:00
|
|
|
if in.Namespace != nil {
|
|
|
|
s.Namespace = *in.Namespace
|
2021-05-19 13:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|