preflight_check.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "resourceProviders",
  53. "gpu",
  54. }
  55. func (p *CreatePreflightCheckHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  56. ctx, span := telemetry.NewSpan(r.Context(), "preflight-checks")
  57. defer span.End()
  58. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  59. betaFeaturesEnabled := project.GetFeatureFlag(models.BetaFeaturesEnabled, p.Config().LaunchDarklyClient)
  60. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "beta-features-enabled", Value: betaFeaturesEnabled})
  61. cloudValues := &porterv1.PreflightCheckRequest{}
  62. err := helpers.UnmarshalContractObjectFromReader(r.Body, cloudValues)
  63. if err != nil {
  64. e := telemetry.Error(ctx, span, err, "error unmarshalling preflight check data")
  65. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusPreconditionFailed, err.Error()))
  66. return
  67. }
  68. var resp PreflightCheckResponse
  69. input := porterv1.PreflightCheckRequest{
  70. ProjectId: int64(project.ID),
  71. CloudProvider: cloudValues.CloudProvider,
  72. CloudProviderCredentialsId: cloudValues.CloudProviderCredentialsId,
  73. Contract: cloudValues.Contract,
  74. }
  75. if cloudValues.PreflightValues != nil {
  76. if cloudValues.CloudProvider == porterv1.EnumCloudProvider_ENUM_CLOUD_PROVIDER_GCP || cloudValues.CloudProvider == porterv1.EnumCloudProvider_ENUM_CLOUD_PROVIDER_AWS {
  77. input.PreflightValues = cloudValues.PreflightValues
  78. }
  79. }
  80. checkResp, err := p.Config().ClusterControlPlaneClient.PreflightCheck(ctx, connect.NewRequest(&input))
  81. if err != nil {
  82. err = telemetry.Error(ctx, span, err, "error calling preflight checks")
  83. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  84. return
  85. }
  86. if checkResp.Msg == nil {
  87. err = telemetry.Error(ctx, span, nil, "no message received from preflight checks")
  88. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  89. return
  90. }
  91. if !betaFeaturesEnabled {
  92. p.WriteResult(w, r, checkResp)
  93. return
  94. }
  95. errors := []PreflightCheckError{}
  96. for key, val := range checkResp.Msg.PreflightChecks {
  97. if val.Message == "" || !contains(recognizedPreflightCheckKeys, key) {
  98. continue
  99. }
  100. errors = append(errors, PreflightCheckError{
  101. Name: key,
  102. Error: PorterError{
  103. Code: val.Code,
  104. Message: val.Message,
  105. Metadata: val.Metadata,
  106. },
  107. })
  108. }
  109. resp.Errors = errors
  110. p.WriteResult(w, r, resp)
  111. }
  112. func contains(slice []string, elem string) bool {
  113. for _, item := range slice {
  114. if item == elem {
  115. return true
  116. }
  117. }
  118. return false
  119. }