infra.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package models
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "gorm.io/gorm"
  7. "github.com/porter-dev/porter/api/types"
  8. )
  9. // Infra represents the metadata for an infrastructure type provisioned on
  10. // Porter
  11. type Infra struct {
  12. gorm.Model
  13. // The type of infra that was provisioned
  14. Kind types.InfraKind `json:"kind"`
  15. // A random 6-byte suffix to ensure workspace/stream ids are unique
  16. Suffix string
  17. // The project that this infra belongs to
  18. ProjectID uint `json:"project_id"`
  19. // Status is the status of the infra
  20. Status types.InfraStatus `json:"status"`
  21. // The AWS integration that was used to create the infra
  22. AWSIntegrationID uint
  23. // The GCP integration that was used to create the infra
  24. GCPIntegrationID uint
  25. // The DO integration that was used to create the infra:
  26. // this points to an OAuthIntegrationID
  27. DOIntegrationID uint
  28. // ------------------------------------------------------------------
  29. // All fields below this line are encrypted before storage
  30. // ------------------------------------------------------------------
  31. // The last-applied input variables to the provisioner
  32. LastApplied []byte
  33. }
  34. // ToInfraType generates an external Infra to be shared over REST
  35. func (i *Infra) ToInfraType() *types.Infra {
  36. return &types.Infra{
  37. ID: i.ID,
  38. ProjectID: i.ProjectID,
  39. Kind: i.Kind,
  40. Status: i.Status,
  41. }
  42. }
  43. // GetID returns the unique id for this infra
  44. func (i *Infra) GetUniqueName() string {
  45. return fmt.Sprintf("%s-%d-%d-%s", i.Kind, i.ProjectID, i.ID, i.Suffix)
  46. }
  47. // ParseUniqueName returns the (kind, projectID, infraID, suffix)
  48. func ParseUniqueName(workspaceID string) (string, uint, uint, error) {
  49. strArr := strings.Split(workspaceID, "-")
  50. if len(strArr) < 3 {
  51. return "", 0, 0, fmt.Errorf("workspace id improperly formatted")
  52. }
  53. projID, err := strconv.ParseUint(strArr[1], 10, 64)
  54. if err != nil {
  55. return "", 0, 0, err
  56. }
  57. infraID, err := strconv.ParseUint(strArr[2], 10, 64)
  58. if err != nil {
  59. return "", 0, 0, err
  60. }
  61. return strArr[0], uint(projID), uint(infraID), nil
  62. }