infra.go 2.2 KB

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