2019-02-21 16:58:25 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// 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 api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2019-03-14 13:36:18 +00:00
|
|
|
"net/url"
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-02-21 16:58:25 +00:00
|
|
|
csapi "github.com/sorintlab/agola/internal/services/configstore/api"
|
|
|
|
"github.com/sorintlab/agola/internal/services/gateway/command"
|
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
2019-04-09 12:53:00 +00:00
|
|
|
"github.com/sorintlab/agola/internal/util"
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CreateProjectRequest struct {
|
2019-03-14 13:36:18 +00:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
ParentID string `json:"parent_id,omitempty"`
|
2019-04-03 09:07:54 +00:00
|
|
|
RepoPath string `json:"repo_path,omitempty"`
|
2019-03-14 13:36:18 +00:00
|
|
|
RemoteSourceName string `json:"remote_source_name,omitempty"`
|
|
|
|
SkipSSHHostKeyCheck bool `json:"skip_ssh_host_key_check,omitempty"`
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CreateProjectHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
ch *command.CommandHandler
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
exposedURL string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCreateProjectHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client, exposedURL string) *CreateProjectHandler {
|
|
|
|
return &CreateProjectHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient, exposedURL: exposedURL}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
var req CreateProjectRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
userIDVal := ctx.Value("userid")
|
|
|
|
if userIDVal == nil {
|
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("user not authenticated")))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
userID := userIDVal.(string)
|
2019-02-21 16:58:25 +00:00
|
|
|
h.log.Infof("userID: %q", userID)
|
|
|
|
|
2019-02-28 14:52:35 +00:00
|
|
|
creq := &command.CreateProjectRequest{
|
2019-02-21 16:58:25 +00:00
|
|
|
Name: req.Name,
|
2019-03-14 13:36:18 +00:00
|
|
|
ParentID: req.ParentID,
|
2019-04-03 09:07:54 +00:00
|
|
|
RepoPath: req.RepoPath,
|
2019-02-21 16:58:25 +00:00
|
|
|
RemoteSourceName: req.RemoteSourceName,
|
2019-03-14 13:36:18 +00:00
|
|
|
CurrentUserID: userID,
|
2019-02-21 16:58:25 +00:00
|
|
|
SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck,
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
project, err := h.ch.CreateProject(ctx, creq)
|
2019-04-08 09:35:45 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-21 16:58:25 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:08:33 +00:00
|
|
|
res := createProjectResponse(project)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusCreated, res); err != nil {
|
2019-02-21 16:58:25 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectReconfigHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
ch *command.CommandHandler
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
exposedURL string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectReconfigHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client, exposedURL string) *ProjectReconfigHandler {
|
|
|
|
return &ProjectReconfigHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient, exposedURL: exposedURL}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectReconfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-04-08 07:04:55 +00:00
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
2019-02-28 14:52:35 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-04-08 07:04:55 +00:00
|
|
|
if err := h.ch.ReconfigProject(ctx, projectRef); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
httpError(w, err)
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteProjectHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDeleteProjectHandler(logger *zap.Logger, configstoreClient *csapi.Client) *DeleteProjectHandler {
|
|
|
|
return &DeleteProjectHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-04-08 07:04:55 +00:00
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
2019-02-28 14:52:35 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-04-08 07:04:55 +00:00
|
|
|
project, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-02-28 14:52:35 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = h.configstoreClient.DeleteProject(ctx, project.ID)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-02-21 16:58:25 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-04-08 09:35:45 +00:00
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectHandler(logger *zap.Logger, configstoreClient *csapi.Client) *ProjectHandler {
|
|
|
|
return &ProjectHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-04-08 07:04:55 +00:00
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:04:55 +00:00
|
|
|
project, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-02-21 16:58:25 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createProjectResponse(project)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, res); err != nil {
|
2019-02-21 16:58:25 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectResponse struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func createProjectResponse(r *types.Project) *ProjectResponse {
|
2019-03-14 13:36:18 +00:00
|
|
|
res := &ProjectResponse{
|
2019-02-21 16:58:25 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Name: r.Name,
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
return res
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|