infra.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // The ID of the user that created this infra
  20. CreatedByUserID uint
  21. // Status is the status of the infra
  22. Status types.InfraStatus `json:"status"`
  23. // The AWS integration that was used to create the infra
  24. AWSIntegrationID uint
  25. // The GCP integration that was used to create the infra
  26. GCPIntegrationID uint
  27. // The DO integration that was used to create the infra:
  28. // this points to an OAuthIntegrationID
  29. DOIntegrationID uint
  30. // ------------------------------------------------------------------
  31. // All fields below this line are encrypted before storage
  32. // ------------------------------------------------------------------
  33. // The last-applied input variables to the provisioner
  34. LastApplied []byte
  35. }
  36. // ToInfraType generates an external Infra to be shared over REST
  37. func (i *Infra) ToInfraType() *types.Infra {
  38. return &types.Infra{
  39. ID: i.ID,
  40. ProjectID: i.ProjectID,
  41. Kind: i.Kind,
  42. Status: i.Status,
  43. }
  44. }
  45. // GetID returns the unique id for this infra
  46. func (i *Infra) GetUniqueName() string {
  47. return fmt.Sprintf("%s-%d-%d-%s", i.Kind, i.ProjectID, i.ID, i.Suffix)
  48. }
  49. // ParseUniqueName returns the (kind, projectID, infraID, suffix)
  50. func ParseUniqueName(workspaceID string) (string, uint, uint, error) {
  51. strArr := strings.Split(workspaceID, "-")
  52. if len(strArr) < 3 {
  53. return "", 0, 0, fmt.Errorf("workspace id improperly formatted")
  54. }
  55. projID, err := strconv.ParseUint(strArr[1], 10, 64)
  56. if err != nil {
  57. return "", 0, 0, err
  58. }
  59. infraID, err := strconv.ParseUint(strArr[2], 10, 64)
  60. if err != nil {
  61. return "", 0, 0, err
  62. }
  63. return strArr[0], uint(projID), uint(infraID), nil
  64. }