2016-05-02 19:21:25 +00:00
|
|
|
package server
|
2016-03-31 19:01:32 +00:00
|
|
|
|
|
|
|
import (
|
2016-06-27 21:06:02 +00:00
|
|
|
"net/http"
|
|
|
|
|
2016-03-31 19:01:32 +00:00
|
|
|
"github.com/drone/drone/model"
|
|
|
|
"github.com/drone/drone/router/middleware/session"
|
|
|
|
"github.com/drone/drone/store"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2016-06-27 21:06:02 +00:00
|
|
|
func GetSecrets(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
secrets, err := store.GetSecretList(c, repo)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-31 20:58:29 +00:00
|
|
|
var list []*model.RepoSecret
|
2016-06-27 21:06:02 +00:00
|
|
|
|
|
|
|
for _, s := range secrets {
|
|
|
|
list = append(list, s.Clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, list)
|
|
|
|
}
|
|
|
|
|
2016-03-31 19:01:32 +00:00
|
|
|
func PostSecret(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
|
2016-07-31 20:58:29 +00:00
|
|
|
in := &model.RepoSecret{}
|
2016-03-31 19:01:32 +00:00
|
|
|
err := c.Bind(in)
|
|
|
|
if err != nil {
|
2016-06-27 21:06:02 +00:00
|
|
|
c.String(http.StatusBadRequest, "Invalid JSON input. %s", err.Error())
|
2016-03-31 19:01:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
in.ID = 0
|
|
|
|
in.RepoID = repo.ID
|
|
|
|
|
|
|
|
err = store.SetSecret(c, in)
|
|
|
|
if err != nil {
|
2016-06-27 21:06:02 +00:00
|
|
|
c.String(http.StatusInternalServerError, "Unable to persist secret. %s", err.Error())
|
2016-03-31 19:01:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:06:02 +00:00
|
|
|
c.String(http.StatusOK, "")
|
2016-03-31 19:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteSecret(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
name := c.Param("secret")
|
|
|
|
|
|
|
|
secret, err := store.GetSecret(c, repo, name)
|
|
|
|
if err != nil {
|
2016-06-27 21:06:02 +00:00
|
|
|
c.String(http.StatusNotFound, "Cannot find secret %s.", name)
|
2016-03-31 19:01:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = store.DeleteSecret(c, secret)
|
|
|
|
if err != nil {
|
2016-06-27 21:06:02 +00:00
|
|
|
c.String(http.StatusInternalServerError, "Unable to delete secret. %s", err.Error())
|
2016-03-31 19:01:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:06:02 +00:00
|
|
|
c.String(http.StatusOK, "")
|
2016-03-31 19:01:32 +00:00
|
|
|
}
|