infra.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // Status is the status of the infra
  41. Status InfraStatus `json:"status"`
  42. // The AWS integration that was used to create the infra
  43. AWSIntegrationID uint
  44. // The GCP integration that was used to create the infra
  45. GCPIntegrationID uint
  46. // The DO integration that was used to create the infra:
  47. // this points to an OAuthIntegrationID
  48. DOIntegrationID uint
  49. // ------------------------------------------------------------------
  50. // All fields below this line are encrypted before storage
  51. // ------------------------------------------------------------------
  52. // The last-applied input variables to the provisioner
  53. LastApplied []byte
  54. }
  55. // InfraExternal is an external Infra to be shared over REST
  56. type InfraExternal struct {
  57. ID uint `json:"id"`
  58. // The project that this integration belongs to
  59. ProjectID uint `json:"project_id"`
  60. // The type of infra that was provisioned
  61. Kind InfraKind `json:"kind"`
  62. // Status is the status of the infra
  63. Status InfraStatus `json:"status"`
  64. }
  65. // Externalize generates an external Infra to be shared over REST
  66. func (i *Infra) Externalize() *InfraExternal {
  67. return &InfraExternal{
  68. ID: i.ID,
  69. ProjectID: i.ProjectID,
  70. Kind: i.Kind,
  71. Status: i.Status,
  72. }
  73. }
  74. // GetID returns the unique id for this infra
  75. func (i *Infra) GetUniqueName() string {
  76. return fmt.Sprintf("%s-%d-%d-%s", i.Kind, i.ProjectID, i.ID, i.Suffix)
  77. }
  78. // ParseUniqueName returns the (kind, projectID, infraID, suffix)
  79. func ParseUniqueName(workspaceID string) (string, uint, uint, error) {
  80. strArr := strings.Split(workspaceID, "-")
  81. if len(strArr) < 3 {
  82. return "", 0, 0, fmt.Errorf("workspace id improperly formatted")
  83. }
  84. projID, err := strconv.ParseUint(strArr[1], 10, 64)
  85. if err != nil {
  86. return "", 0, 0, err
  87. }
  88. infraID, err := strconv.ParseUint(strArr[2], 10, 64)
  89. if err != nil {
  90. return "", 0, 0, err
  91. }
  92. return strArr[0], uint(projID), uint(infraID), nil
  93. }