infra.go 6.1 KB

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