provision_handler.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/go-chi/chi"
  7. "github.com/porter-dev/porter/internal/forms"
  8. "github.com/porter-dev/porter/internal/kubernetes"
  9. )
  10. // HandleProvisionTest will create a test resource by deploying a provisioner
  11. // container pod
  12. func (app *App) HandleProvisionTest(w http.ResponseWriter, r *http.Request) {
  13. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  14. if err != nil || projID == 0 {
  15. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  16. return
  17. }
  18. // create a new agent
  19. agent, err := kubernetes.GetAgentInClusterConfig()
  20. if err != nil {
  21. app.handleErrorDataRead(err, w)
  22. return
  23. }
  24. _, err = agent.ProvisionTest(uint(projID))
  25. if err != nil {
  26. app.handleErrorInternal(err, w)
  27. return
  28. }
  29. w.WriteHeader(http.StatusOK)
  30. }
  31. // HandleProvisionAWSECRInfra provisions a new aws ECR instance for a project
  32. func (app *App) HandleProvisionAWSECRInfra(w http.ResponseWriter, r *http.Request) {
  33. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  34. if err != nil || projID == 0 {
  35. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  36. return
  37. }
  38. form := &forms.CreateECRInfra{
  39. ProjectID: uint(projID),
  40. }
  41. // decode from JSON to form value
  42. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  43. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  44. return
  45. }
  46. // validate the form
  47. if err := app.validator.Struct(form); err != nil {
  48. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  49. return
  50. }
  51. // convert the form to an aws infra instance
  52. infra, err := form.ToAWSInfra()
  53. if err != nil {
  54. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  55. return
  56. }
  57. // handle write to the database
  58. infra, err = app.Repo.AWSInfra.CreateAWSInfra(infra)
  59. if err != nil {
  60. app.handleErrorDataWrite(err, w)
  61. return
  62. }
  63. awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(infra.AWSIntegrationID)
  64. if err != nil {
  65. app.handleErrorDataRead(err, w)
  66. return
  67. }
  68. // launch provisioning pod
  69. agent, err := kubernetes.GetAgentInClusterConfig()
  70. if err != nil {
  71. app.handleErrorDataRead(err, w)
  72. return
  73. }
  74. _, err = agent.ProvisionECR(
  75. uint(projID),
  76. awsInt,
  77. form.ECRName,
  78. )
  79. if err != nil {
  80. app.handleErrorInternal(err, w)
  81. return
  82. }
  83. app.Logger.Info().Msgf("New aws ecr infra created: %d", infra.ID)
  84. w.WriteHeader(http.StatusCreated)
  85. infraExt := infra.Externalize()
  86. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  87. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  88. return
  89. }
  90. }