infra.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. )
  27. // Infra represents the metadata for an infrastructure type provisioned on
  28. // Porter
  29. type Infra struct {
  30. gorm.Model
  31. // The type of infra that was provisioned
  32. Kind InfraKind `json:"kind"`
  33. // A random 6-byte suffix to ensure workspace/stream ids are unique
  34. Suffix string
  35. // The project that this infra belongs to
  36. ProjectID uint `json:"project_id"`
  37. // Status is the status of the infra
  38. Status InfraStatus `json:"status"`
  39. // The AWS integration that was used to create the infra
  40. AWSIntegrationID uint
  41. // The GCP integration that was used to create the infra
  42. GCPIntegrationID uint
  43. }
  44. // InfraExternal is an external Infra to be shared over REST
  45. type InfraExternal struct {
  46. ID uint `json:"id"`
  47. // The project that this integration belongs to
  48. ProjectID uint `json:"project_id"`
  49. // The type of infra that was provisioned
  50. Kind InfraKind `json:"kind"`
  51. // Status is the status of the infra
  52. Status InfraStatus `json:"status"`
  53. }
  54. // Externalize generates an external Infra to be shared over REST
  55. func (i *Infra) Externalize() *InfraExternal {
  56. return &InfraExternal{
  57. ID: i.ID,
  58. ProjectID: i.ProjectID,
  59. Kind: i.Kind,
  60. Status: i.Status,
  61. }
  62. }
  63. // GetID returns the unique id for this infra
  64. func (i *Infra) GetID() string {
  65. return fmt.Sprintf("%s-%d-%d-%s", i.Kind, i.ProjectID, i.ID, i.Suffix)
  66. }
  67. // ParseWorkspaceID returns the (kind, projectID, infraID)
  68. func ParseWorkspaceID(workspaceID string) (string, uint, uint, error) {
  69. strArr := strings.Split(workspaceID, "-")
  70. if len(strArr) < 3 {
  71. return "", 0, 0, fmt.Errorf("workspace id improperly formatted")
  72. }
  73. projID, err := strconv.ParseUint(strArr[1], 10, 64)
  74. if err != nil {
  75. return "", 0, 0, err
  76. }
  77. infraID, err := strconv.ParseUint(strArr[2], 10, 64)
  78. if err != nil {
  79. return "", 0, 0, err
  80. }
  81. return strArr[0], uint(projID), uint(infraID), nil
  82. }