preflight_check.go 4.3 KB

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