infra.go 2.9 KB

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