infra.go 6.1 KB

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