infra.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package models
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "gorm.io/gorm"
  7. )
  8. // InfraStatus is the status that an infrastructure can take
  9. type InfraStatus string
  10. // The allowed statuses
  11. const (
  12. StatusCreating InfraStatus = "creating"
  13. StatusCreated InfraStatus = "created"
  14. StatusError InfraStatus = "error"
  15. StatusDestroying InfraStatus = "destroying"
  16. StatusDestroyed InfraStatus = "destroyed"
  17. )
  18. // InfraKind is the kind that infra can be
  19. type InfraKind string
  20. // The supported infra kinds
  21. const (
  22. InfraTest InfraKind = "test"
  23. InfraECR InfraKind = "ecr"
  24. InfraEKS InfraKind = "eks"
  25. InfraGCR InfraKind = "gcr"
  26. InfraGKE InfraKind = "gke"
  27. InfraDOCR InfraKind = "docr"
  28. InfraDOKS InfraKind = "doks"
  29. )
  30. // Infra represents the metadata for an infrastructure type provisioned on
  31. // Porter
  32. type Infra struct {
  33. gorm.Model
  34. // The type of infra that was provisioned
  35. Kind InfraKind `json:"kind"`
  36. // A random 6-byte suffix to ensure workspace/stream ids are unique
  37. Suffix string
  38. // The project that this infra belongs to
  39. ProjectID uint `json:"project_id"`
  40. // The ID of the user that created this infra
  41. CreatedByUserID uint
  42. // Status is the status of the infra
  43. Status InfraStatus `json:"status"`
  44. // The AWS integration that was used to create the infra
  45. AWSIntegrationID uint
  46. // The GCP integration that was used to create the infra
  47. GCPIntegrationID uint
  48. // The DO integration that was used to create the infra:
  49. // this points to an OAuthIntegrationID
  50. DOIntegrationID uint
  51. // ------------------------------------------------------------------
  52. // All fields below this line are encrypted before storage
  53. // ------------------------------------------------------------------
  54. // The last-applied input variables to the provisioner
  55. LastApplied []byte
  56. }
  57. // InfraExternal is an external Infra to be shared over REST
  58. type InfraExternal struct {
  59. ID uint `json:"id"`
  60. // The project that this integration belongs to
  61. ProjectID uint `json:"project_id"`
  62. // The type of infra that was provisioned
  63. Kind InfraKind `json:"kind"`
  64. // Status is the status of the infra
  65. Status InfraStatus `json:"status"`
  66. }
  67. // Externalize generates an external Infra to be shared over REST
  68. func (i *Infra) Externalize() *InfraExternal {
  69. return &InfraExternal{
  70. ID: i.ID,
  71. ProjectID: i.ProjectID,
  72. Kind: i.Kind,
  73. Status: i.Status,
  74. }
  75. }
  76. // GetID returns the unique id for this infra
  77. func (i *Infra) GetUniqueName() string {
  78. return fmt.Sprintf("%s-%d-%d-%s", i.Kind, i.ProjectID, i.ID, i.Suffix)
  79. }
  80. // ParseUniqueName returns the (kind, projectID, infraID, suffix)
  81. func ParseUniqueName(workspaceID string) (string, uint, uint, error) {
  82. strArr := strings.Split(workspaceID, "-")
  83. if len(strArr) < 3 {
  84. return "", 0, 0, fmt.Errorf("workspace id improperly formatted")
  85. }
  86. projID, err := strconv.ParseUint(strArr[1], 10, 64)
  87. if err != nil {
  88. return "", 0, 0, err
  89. }
  90. infraID, err := strconv.ParseUint(strArr[2], 10, 64)
  91. if err != nil {
  92. return "", 0, 0, err
  93. }
  94. return strArr[0], uint(projID), uint(infraID), nil
  95. }