infra.go 6.1 KB

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