infra.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // ToAWSInfra converts the form to a gorm aws infra model
  16. func (ce *CreateECRInfra) ToAWSInfra() (*models.AWSInfra, error) {
  17. return &models.AWSInfra{
  18. Kind: models.AWSInfraECR,
  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. // ToAWSInfra converts the form to a gorm aws infra model
  33. func (ce *CreateEKSInfra) ToAWSInfra() (*models.AWSInfra, error) {
  34. return &models.AWSInfra{
  35. Kind: models.AWSInfraEKS,
  36. ProjectID: ce.ProjectID,
  37. Suffix: stringWithCharset(6, randCharset),
  38. Status: models.StatusCreating,
  39. AWSIntegrationID: ce.AWSIntegrationID,
  40. }, nil
  41. }
  42. // DestroyECRInfra represents the accepted values for destroying an
  43. // ECR infra via the provisioning container
  44. type DestroyECRInfra struct {
  45. ECRName string `json:"ecr_name" form:"required"`
  46. }
  47. // DestroyEKSInfra represents the accepted values for destroying an
  48. // EKS infra via the provisioning container
  49. type DestroyEKSInfra struct {
  50. EKSName string `json:"eks_name" form:"required"`
  51. }
  52. // helpers for random string
  53. var seededRand *rand.Rand = rand.New(
  54. rand.NewSource(time.Now().UnixNano()))
  55. // stringWithCharset returns a random string by pulling from a given charset
  56. func stringWithCharset(length int, charset string) string {
  57. b := make([]byte, length)
  58. for i := range b {
  59. b[i] = charset[seededRand.Intn(len(charset))]
  60. }
  61. return string(b)
  62. }