infra.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package forms
  2. import (
  3. "math/rand"
  4. "time"
  5. "github.com/porter-dev/porter/internal/models"
  6. )
  7. const randCharset string = "abcdefghijklmnopqrstuvwxyz1234567890"
  8. // CreateECRInfra represents the accepted values for creating an
  9. // ECR infra via the provisioning container
  10. type CreateECRInfra struct {
  11. ECRName string `json:"ecr_name" form:"required"`
  12. ProjectID uint `json:"project_id" form:"required"`
  13. AWSIntegrationID uint `json:"aws_integration_id" form:"required"`
  14. }
  15. // ToInfra converts the form to a gorm aws infra model
  16. func (ce *CreateECRInfra) ToInfra() (*models.Infra, error) {
  17. return &models.Infra{
  18. Kind: models.InfraECR,
  19. ProjectID: ce.ProjectID,
  20. Suffix: stringWithCharset(6, randCharset),
  21. Status: models.StatusCreating,
  22. AWSIntegrationID: ce.AWSIntegrationID,
  23. }, nil
  24. }
  25. // CreateEKSInfra represents the accepted values for creating an
  26. // EKS infra via the provisioning container
  27. type CreateEKSInfra struct {
  28. EKSName string `json:"eks_name" form:"required"`
  29. ProjectID uint `json:"project_id" form:"required"`
  30. AWSIntegrationID uint `json:"aws_integration_id" form:"required"`
  31. }
  32. // ToInfra converts the form to a gorm aws infra model
  33. func (ce *CreateEKSInfra) ToInfra() (*models.Infra, error) {
  34. return &models.Infra{
  35. Kind: models.InfraEKS,
  36. ProjectID: ce.ProjectID,
  37. Suffix: stringWithCharset(6, randCharset),
  38. Status: models.StatusCreating,
  39. AWSIntegrationID: ce.AWSIntegrationID,
  40. }, nil
  41. }
  42. // CreateGCRInfra represents the accepted values for creating an
  43. // GCR infra via the provisioning container
  44. type CreateGCRInfra struct {
  45. ProjectID uint `json:"project_id" form:"required"`
  46. GCPIntegrationID uint `json:"gcp_integration_id" form:"required"`
  47. }
  48. // ToInfra converts the form to a gorm aws infra model
  49. func (ce *CreateGCRInfra) ToInfra() (*models.Infra, error) {
  50. return &models.Infra{
  51. Kind: models.InfraGCR,
  52. ProjectID: ce.ProjectID,
  53. Suffix: stringWithCharset(6, randCharset),
  54. Status: models.StatusCreating,
  55. GCPIntegrationID: ce.GCPIntegrationID,
  56. }, nil
  57. }
  58. // CreateGKEInfra represents the accepted values for creating a
  59. // GKE infra via the provisioning container
  60. type CreateGKEInfra struct {
  61. GKEName string `json:"gke_name" form:"required"`
  62. ProjectID uint `json:"project_id" form:"required"`
  63. GCPIntegrationID uint `json:"gcp_integration_id" form:"required"`
  64. }
  65. // ToInfra converts the form to a gorm aws infra model
  66. func (ce *CreateGKEInfra) ToInfra() (*models.Infra, error) {
  67. return &models.Infra{
  68. Kind: models.InfraGKE,
  69. ProjectID: ce.ProjectID,
  70. Suffix: stringWithCharset(6, randCharset),
  71. Status: models.StatusCreating,
  72. GCPIntegrationID: ce.GCPIntegrationID,
  73. }, nil
  74. }
  75. // CreateDOCRInfra represents the accepted values for creating an
  76. // DOCR infra via the provisioning container
  77. type CreateDOCRInfra struct {
  78. DOCRName string `json:"docr_name" form:"required"`
  79. DOCRSubscriptionTier string `json:"docr_subscription_tier" form:"required"`
  80. ProjectID uint `json:"project_id" form:"required"`
  81. DOIntegrationID uint `json:"do_integration_id" form:"required"`
  82. }
  83. // ToInfra converts the form to a gorm infra model
  84. func (de *CreateDOCRInfra) ToInfra() (*models.Infra, error) {
  85. return &models.Infra{
  86. Kind: models.InfraDOCR,
  87. ProjectID: de.ProjectID,
  88. Suffix: stringWithCharset(6, randCharset),
  89. Status: models.StatusCreating,
  90. DOIntegrationID: de.DOIntegrationID,
  91. }, nil
  92. }
  93. // CreateDOKSInfra represents the accepted values for creating a
  94. // DOKS infra via the provisioning container
  95. type CreateDOKSInfra struct {
  96. DORegion string `json:"do_region" form:"required"`
  97. DOKSName string `json:"doks_name" form:"required"`
  98. ProjectID uint `json:"project_id" form:"required"`
  99. DOIntegrationID uint `json:"do_integration_id" form:"required"`
  100. }
  101. // ToInfra converts the form to a gorm infra model
  102. func (de *CreateDOKSInfra) ToInfra() (*models.Infra, error) {
  103. return &models.Infra{
  104. Kind: models.InfraDOKS,
  105. ProjectID: de.ProjectID,
  106. Suffix: stringWithCharset(6, randCharset),
  107. Status: models.StatusCreating,
  108. DOIntegrationID: de.DOIntegrationID,
  109. }, nil
  110. }
  111. // DestroyECRInfra represents the accepted values for destroying an
  112. // ECR infra via the provisioning container
  113. type DestroyECRInfra struct {
  114. ECRName string `json:"ecr_name" form:"required"`
  115. }
  116. // DestroyEKSInfra represents the accepted values for destroying an
  117. // EKS infra via the provisioning container
  118. type DestroyEKSInfra struct {
  119. EKSName string `json:"eks_name" form:"required"`
  120. }
  121. // DestroyGKEInfra represents the accepted values for destroying an
  122. // GKE infra via the provisioning container
  123. type DestroyGKEInfra struct {
  124. GKEName string `json:"gke_name" form:"required"`
  125. }
  126. // DestroyDOCRInfra represents the accepted values for destroying an
  127. // DOCR infra via the provisioning container
  128. type DestroyDOCRInfra struct {
  129. DOCRName string `json:"docr_name" form:"required"`
  130. }
  131. // DestroyDOKSInfra represents the accepted values for destroying an
  132. // DOKS infra via the provisioning container
  133. type DestroyDOKSInfra struct {
  134. DOKSName string `json:"doks_name" form:"required"`
  135. }
  136. // helpers for random string
  137. var seededRand *rand.Rand = rand.New(
  138. rand.NewSource(time.Now().UnixNano()))
  139. // stringWithCharset returns a random string by pulling from a given charset
  140. func stringWithCharset(length int, charset string) string {
  141. b := make([]byte, length)
  142. for i := range b {
  143. b[i] = charset[seededRand.Intn(len(charset))]
  144. }
  145. return string(b)
  146. }