delete.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package infra
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/handlers"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/kubernetes"
  10. "github.com/porter-dev/porter/internal/kubernetes/provisioner"
  11. "github.com/porter-dev/porter/internal/models"
  12. "github.com/porter-dev/porter/internal/repository"
  13. )
  14. type InfraDeleteHandler struct {
  15. handlers.PorterHandlerReadWriter
  16. }
  17. func NewInfraDeleteHandler(
  18. config *config.Config,
  19. decoderValidator shared.RequestDecoderValidator,
  20. writer shared.ResultWriter,
  21. ) *InfraDeleteHandler {
  22. return &InfraDeleteHandler{
  23. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  24. }
  25. }
  26. func (c *InfraDeleteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  27. infra, _ := r.Context().Value(types.InfraScope).(*models.Infra)
  28. request := &types.DeleteInfraRequest{}
  29. if ok := c.DecodeAndValidate(w, r, request); !ok {
  30. return
  31. }
  32. infra.Status = types.StatusDestroying
  33. infra, err := c.Repo().Infra().UpdateInfra(infra)
  34. if err != nil {
  35. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  36. return
  37. }
  38. switch infra.Kind {
  39. case types.InfraECR:
  40. err = destroyECR(c.Repo(), c.Config(), infra, request.Name)
  41. case types.InfraEKS:
  42. err = destroyEKS(c.Repo(), c.Config(), infra, request.Name)
  43. case types.InfraDOCR:
  44. err = destroyDOCR(c.Repo(), c.Config(), infra, request.Name)
  45. case types.InfraDOKS:
  46. err = destroyDOKS(c.Repo(), c.Config(), infra, request.Name)
  47. case types.InfraGKE:
  48. err = destroyGKE(c.Repo(), c.Config(), infra, request.Name)
  49. }
  50. if err != nil {
  51. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  52. return
  53. }
  54. }
  55. func destroyECR(repo repository.Repository, conf *config.Config, infra *models.Infra, name string) error {
  56. awsInt, err := repo.AWSIntegration().ReadAWSIntegration(infra.ProjectID, infra.AWSIntegrationID)
  57. if err != nil {
  58. return err
  59. }
  60. _, err = conf.ProvisionerAgent.ProvisionECR(
  61. &kubernetes.SharedProvisionOpts{
  62. ProjectID: infra.ProjectID,
  63. Repo: repo,
  64. Infra: infra,
  65. Operation: provisioner.Destroy,
  66. PGConf: conf.DBConf,
  67. RedisConf: conf.RedisConf,
  68. ProvImageTag: conf.ServerConf.ProvisionerImageTag,
  69. ProvImagePullSecret: conf.ServerConf.ProvisionerImagePullSecret,
  70. },
  71. awsInt,
  72. name,
  73. )
  74. return err
  75. }
  76. func destroyEKS(repo repository.Repository, conf *config.Config, infra *models.Infra, name string) error {
  77. awsInt, err := repo.AWSIntegration().ReadAWSIntegration(infra.ProjectID, infra.AWSIntegrationID)
  78. if err != nil {
  79. return err
  80. }
  81. _, err = conf.ProvisionerAgent.ProvisionEKS(
  82. &kubernetes.SharedProvisionOpts{
  83. ProjectID: infra.ProjectID,
  84. Repo: repo,
  85. Infra: infra,
  86. Operation: provisioner.Destroy,
  87. PGConf: conf.DBConf,
  88. RedisConf: conf.RedisConf,
  89. ProvImageTag: conf.ServerConf.ProvisionerImageTag,
  90. ProvImagePullSecret: conf.ServerConf.ProvisionerImagePullSecret,
  91. },
  92. awsInt,
  93. name,
  94. "",
  95. )
  96. return err
  97. }
  98. func destroyDOCR(repo repository.Repository, conf *config.Config, infra *models.Infra, name string) error {
  99. doInt, err := repo.OAuthIntegration().ReadOAuthIntegration(infra.ProjectID, infra.DOIntegrationID)
  100. if err != nil {
  101. return err
  102. }
  103. _, err = conf.ProvisionerAgent.ProvisionDOCR(
  104. &kubernetes.SharedProvisionOpts{
  105. ProjectID: infra.ProjectID,
  106. Repo: repo,
  107. Infra: infra,
  108. Operation: provisioner.Destroy,
  109. PGConf: conf.DBConf,
  110. RedisConf: conf.RedisConf,
  111. ProvImageTag: conf.ServerConf.ProvisionerImageTag,
  112. ProvImagePullSecret: conf.ServerConf.ProvisionerImagePullSecret,
  113. },
  114. doInt,
  115. conf.DOConf,
  116. name,
  117. "",
  118. )
  119. return err
  120. }
  121. func destroyDOKS(repo repository.Repository, conf *config.Config, infra *models.Infra, name string) error {
  122. doInt, err := repo.OAuthIntegration().ReadOAuthIntegration(infra.ProjectID, infra.DOIntegrationID)
  123. if err != nil {
  124. return err
  125. }
  126. _, err = conf.ProvisionerAgent.ProvisionDOKS(
  127. &kubernetes.SharedProvisionOpts{
  128. ProjectID: infra.ProjectID,
  129. Repo: repo,
  130. Infra: infra,
  131. Operation: provisioner.Destroy,
  132. PGConf: conf.DBConf,
  133. RedisConf: conf.RedisConf,
  134. ProvImageTag: conf.ServerConf.ProvisionerImageTag,
  135. ProvImagePullSecret: conf.ServerConf.ProvisionerImagePullSecret,
  136. },
  137. doInt,
  138. conf.DOConf,
  139. "",
  140. name,
  141. )
  142. return err
  143. }
  144. func destroyGKE(repo repository.Repository, conf *config.Config, infra *models.Infra, name string) error {
  145. gcpInt, err := repo.GCPIntegration().ReadGCPIntegration(infra.ProjectID, infra.GCPIntegrationID)
  146. if err != nil {
  147. return err
  148. }
  149. _, err = conf.ProvisionerAgent.ProvisionGKE(
  150. &kubernetes.SharedProvisionOpts{
  151. ProjectID: infra.ProjectID,
  152. Repo: repo,
  153. Infra: infra,
  154. Operation: provisioner.Destroy,
  155. PGConf: conf.DBConf,
  156. RedisConf: conf.RedisConf,
  157. ProvImageTag: conf.ServerConf.ProvisionerImageTag,
  158. ProvImagePullSecret: conf.ServerConf.ProvisionerImagePullSecret,
  159. },
  160. gcpInt,
  161. name,
  162. )
  163. return err
  164. }