preflight_check.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package project_integration
  2. import (
  3. "fmt"
  4. "net/http"
  5. "connectrpc.com/connect"
  6. "github.com/porter-dev/api-contracts/generated/go/helpers"
  7. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  8. "github.com/porter-dev/porter/api/server/handlers"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/apierrors"
  11. "github.com/porter-dev/porter/api/server/shared/config"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/telemetry"
  15. )
  16. // CreatePreflightCheckHandler Create Preflight Checks
  17. type CreatePreflightCheckHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. }
  20. // NewCreatePreflightCheckHandler Create Preflight Checks with /integrations/preflightcheck
  21. func NewCreatePreflightCheckHandler(
  22. config *config.Config,
  23. decoderValidator shared.RequestDecoderValidator,
  24. writer shared.ResultWriter,
  25. ) *CreatePreflightCheckHandler {
  26. return &CreatePreflightCheckHandler{
  27. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  28. }
  29. }
  30. // PorterError is the error response for the preflight check endpoint
  31. type PorterError struct {
  32. Code string `json:"code"`
  33. Message string `json:"message"`
  34. Metadata map[string]string `json:"metadata,omitempty"`
  35. }
  36. // PreflightCheckError is the error response for the preflight check endpoint
  37. type PreflightCheckError struct {
  38. Name string `json:"name"`
  39. Error PorterError `json:"error"`
  40. }
  41. // PreflightCheckResponse is the response to the preflight check endpoint
  42. type PreflightCheckResponse struct {
  43. Errors []PreflightCheckError `json:"errors"`
  44. }
  45. var recognizedPreflightCheckKeys = []string{
  46. "eip",
  47. "vcpu",
  48. "vpc",
  49. "natGateway",
  50. "apiEnabled",
  51. "cidrAvailability",
  52. "iamPermissions",
  53. "authz",
  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. if cloudValues.Contract != nil && cloudValues.Contract.Cluster != nil && cloudValues.Contract.Cluster.CloudProvider == porterv1.EnumCloudProvider_ENUM_CLOUD_PROVIDER_AZURE {
  81. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "new-endpoint", Value: true})
  82. checkResp, err := p.Config().ClusterControlPlaneClient.CloudContractPreflightCheck(ctx,
  83. connect.NewRequest(
  84. &porterv1.CloudContractPreflightCheckRequest{
  85. Contract: cloudValues.Contract,
  86. },
  87. ),
  88. )
  89. if err != nil {
  90. err = telemetry.Error(ctx, span, err, "error calling preflight checks")
  91. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  92. return
  93. }
  94. if checkResp.Msg == nil {
  95. err = telemetry.Error(ctx, span, nil, "no message received from preflight checks")
  96. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  97. return
  98. }
  99. errors := []PreflightCheckError{}
  100. for _, val := range checkResp.Msg.FailingPreflightChecks {
  101. if val.Message == "" || !contains(recognizedPreflightCheckKeys, val.Type) {
  102. continue
  103. }
  104. fmt.Printf("val: %+v\n", val.Metadata)
  105. errors = append(errors, PreflightCheckError{
  106. Name: val.Type,
  107. Error: PorterError{
  108. Message: val.Message,
  109. Metadata: val.Metadata,
  110. },
  111. })
  112. }
  113. resp.Errors = errors
  114. p.WriteResult(w, r, resp)
  115. return
  116. }
  117. checkResp, err := p.Config().ClusterControlPlaneClient.PreflightCheck(ctx, connect.NewRequest(&input))
  118. if err != nil {
  119. err = telemetry.Error(ctx, span, err, "error calling preflight checks")
  120. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  121. return
  122. }
  123. if checkResp.Msg == nil {
  124. err = telemetry.Error(ctx, span, nil, "no message received from preflight checks")
  125. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  126. return
  127. }
  128. if !betaFeaturesEnabled {
  129. p.WriteResult(w, r, checkResp)
  130. return
  131. }
  132. errors := []PreflightCheckError{}
  133. for key, val := range checkResp.Msg.PreflightChecks {
  134. if val.Message == "" || !contains(recognizedPreflightCheckKeys, key) {
  135. continue
  136. }
  137. errors = append(errors, PreflightCheckError{
  138. Name: key,
  139. Error: PorterError{
  140. Code: val.Code,
  141. Message: val.Message,
  142. Metadata: val.Metadata,
  143. },
  144. })
  145. }
  146. resp.Errors = errors
  147. p.WriteResult(w, r, resp)
  148. }
  149. func contains(slice []string, elem string) bool {
  150. for _, item := range slice {
  151. if item == elem {
  152. return true
  153. }
  154. }
  155. return false
  156. }