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.
|
|
|
|
|
|
|
|
// +build !oss
|
|
|
|
|
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/drone/drone/core"
|
|
|
|
"github.com/drone/drone/handler/api/render"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type templateInput struct {
|
2021-05-28 15:59:00 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
Namespace string `json:"namespace"`
|
2021-05-19 13:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleCreate returns an http.HandlerFunc that processes http
|
|
|
|
// requests to create a new template.
|
2021-05-27 13:58:23 +00:00
|
|
|
func HandleCreate(templateStore core.TemplateStore) http.HandlerFunc {
|
2021-05-19 13:18:24 +00:00
|
|
|
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{
|
2021-05-28 15:59:00 +00:00
|
|
|
Name: in.Name,
|
|
|
|
Data: in.Data,
|
|
|
|
Namespace: in.Namespace,
|
2021-05-19 13:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = t.Validate()
|
|
|
|
if err != nil {
|
|
|
|
render.BadRequest(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-27 13:58:23 +00:00
|
|
|
err = templateStore.Create(r.Context(), t)
|
2021-05-19 13:18:24 +00:00
|
|
|
if err != nil {
|
|
|
|
render.InternalError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, t, 200)
|
|
|
|
}
|
|
|
|
}
|