infra.go 2.4 KB

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