preflight_check.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package project_integration
  2. import (
  3. "net/http"
  4. "connectrpc.com/connect"
  5. "github.com/porter-dev/api-contracts/generated/go/helpers"
  6. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  7. "github.com/porter-dev/porter/api/server/handlers"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "github.com/porter-dev/porter/api/types"
  12. "github.com/porter-dev/porter/internal/models"
  13. "github.com/porter-dev/porter/internal/telemetry"
  14. )
  15. // CreatePreflightCheckHandler Create Preflight Checks
  16. type CreatePreflightCheckHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. // NewCreatePreflightCheckHandler Create Preflight Checks with /integrations/preflightcheck
  20. func NewCreatePreflightCheckHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *CreatePreflightCheckHandler {
  25. return &CreatePreflightCheckHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. // PorterError is the error response for the preflight check endpoint
  30. type PorterError struct {
  31. Code string `json:"code"`
  32. Message string `json:"message"`
  33. Metadata map[string]string `json:"metadata,omitempty"`
  34. }
  35. // PreflightCheckError is the error response for the preflight check endpoint
  36. type PreflightCheckError struct {
  37. Name string `json:"name"`
  38. Error PorterError `json:"error"`
  39. }
  40. // PreflightCheckResponse is the response to the preflight check endpoint
  41. type PreflightCheckResponse struct {
  42. Errors []PreflightCheckError `json:"errors"`
  43. }
  44. var recognizedPreflightCheckKeys = []string{
  45. "eip",
  46. "vcpu",
  47. "vpc",
  48. "natGateway",
  49. "apiEnabled",
  50. "cidrAvailability",
  51. "iamPermissions",
  52. "authz",
  53. }
  54. func (p *CreatePreflightCheckHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  55. ctx, span := telemetry.NewSpan(r.Context(), "preflight-checks")
  56. defer span.End()
  57. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  58. cloudValues := &porterv1.PreflightCheckRequest{}
  59. err := helpers.UnmarshalContractObjectFromReader(r.Body, cloudValues)
  60. if err != nil {
  61. e := telemetry.Error(ctx, span, err, "error unmarshalling preflight check data")
  62. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusPreconditionFailed, err.Error()))
  63. return
  64. }
  65. var resp PreflightCheckResponse
  66. input := porterv1.PreflightCheckRequest{
  67. ProjectId: int64(project.ID),
  68. CloudProvider: cloudValues.CloudProvider,
  69. CloudProviderCredentialsId: cloudValues.CloudProviderCredentialsId,
  70. Contract: cloudValues.Contract,
  71. }
  72. if cloudValues.PreflightValues != nil {
  73. if cloudValues.CloudProvider == porterv1.EnumCloudProvider_ENUM_CLOUD_PROVIDER_GCP || cloudValues.CloudProvider == porterv1.EnumCloudProvider_ENUM_CLOUD_PROVIDER_AWS {
  74. input.PreflightValues = cloudValues.PreflightValues
  75. }
  76. }
  77. checkResp, err := p.Config().ClusterControlPlaneClient.PreflightCheck(ctx, connect.NewRequest(&input))
  78. if err != nil {
  79. err = telemetry.Error(ctx, span, err, "error calling preflight checks")
  80. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  81. return
  82. }
  83. if checkResp.Msg == nil {
  84. err = telemetry.Error(ctx, span, nil, "no message received from preflight checks")
  85. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  86. return
  87. }
  88. errors := []PreflightCheckError{}
  89. for key, val := range checkResp.Msg.PreflightChecks {
  90. if val.Message == "" || !contains(recognizedPreflightCheckKeys, key) {
  91. continue
  92. }
  93. errors = append(errors, PreflightCheckError{
  94. Name: key,
  95. Error: PorterError{
  96. Code: val.Code,
  97. Message: val.Message,
  98. Metadata: val.Metadata,
  99. },
  100. })
  101. }
  102. resp.Errors = errors
  103. p.WriteResult(w, r, resp)
  104. }
  105. func contains(slice []string, elem string) bool {
  106. for _, item := range slice {
  107. if item == elem {
  108. return true
  109. }
  110. }
  111. return false
  112. }