create_basic.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package project_integration
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/handlers"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/models"
  10. ints "github.com/porter-dev/porter/internal/models/integrations"
  11. )
  12. type CreateBasicHandler struct {
  13. handlers.PorterHandlerReadWriter
  14. }
  15. func NewCreateBasicHandler(
  16. config *config.Config,
  17. decoderValidator shared.RequestDecoderValidator,
  18. writer shared.ResultWriter,
  19. ) *CreateBasicHandler {
  20. return &CreateBasicHandler{
  21. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  22. }
  23. }
  24. func (p *CreateBasicHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  25. user, _ := r.Context().Value(types.UserScope).(*models.User)
  26. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  27. request := &types.CreateBasicRequest{}
  28. if ok := p.DecodeAndValidate(w, r, request); !ok {
  29. return
  30. }
  31. basic := CreateBasicIntegration(request.Username, request.Password, project.ID, user.ID)
  32. basic, err := p.Repo().BasicIntegration().CreateBasicIntegration(basic)
  33. if err != nil {
  34. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  35. return
  36. }
  37. res := types.CreateBasicResponse{
  38. BasicIntegration: basic.ToBasicIntegrationType(),
  39. }
  40. p.WriteResult(w, r, res)
  41. }
  42. func CreateBasicIntegration(username, password string, projectID, userID uint) *ints.BasicIntegration {
  43. return &ints.BasicIntegration{
  44. UserID: userID,
  45. ProjectID: projectID,
  46. Username: []byte(username),
  47. Password: []byte(password),
  48. }
  49. }