infra.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package models
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "gorm.io/gorm"
  8. "github.com/porter-dev/porter/api/types"
  9. )
  10. // Infra represents the metadata for an infrastructure type provisioned on
  11. // Porter
  12. type Infra struct {
  13. gorm.Model
  14. // The type of infra that was provisioned
  15. Kind types.InfraKind `json:"kind"`
  16. // A random 6-byte suffix to ensure workspace/stream ids are unique
  17. Suffix string
  18. // The project that this infra belongs to
  19. ProjectID uint `json:"project_id"`
  20. // The ID of the user that created this infra
  21. CreatedByUserID uint
  22. // Status is the status of the infra
  23. Status types.InfraStatus `json:"status"`
  24. // The AWS integration that was used to create the infra
  25. AWSIntegrationID uint
  26. // The GCP integration that was used to create the infra
  27. GCPIntegrationID uint
  28. // The DO integration that was used to create the infra:
  29. // this points to an OAuthIntegrationID
  30. DOIntegrationID uint
  31. // ------------------------------------------------------------------
  32. // All fields below this line are encrypted before storage
  33. // ------------------------------------------------------------------
  34. // The last-applied input variables to the provisioner
  35. LastApplied []byte
  36. }
  37. // ToInfraType generates an external Infra to be shared over REST
  38. func (i *Infra) ToInfraType() *types.Infra {
  39. return &types.Infra{
  40. ID: i.ID,
  41. CreatedAt: i.CreatedAt,
  42. UpdatedAt: i.UpdatedAt,
  43. ProjectID: i.ProjectID,
  44. Kind: i.Kind,
  45. Status: i.Status,
  46. AWSIntegrationID: i.AWSIntegrationID,
  47. DOIntegrationID: i.DOIntegrationID,
  48. GCPIntegrationID: i.GCPIntegrationID,
  49. LastApplied: i.SafelyGetLastApplied(),
  50. }
  51. }
  52. // SafeGetLastApplied gets non-sensitive values for the last applied configuration
  53. func (i *Infra) SafelyGetLastApplied() map[string]string {
  54. resp := make(map[string]string)
  55. switch i.Kind {
  56. case types.InfraECR:
  57. lastApplied := &types.CreateECRInfraRequest{}
  58. if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
  59. return resp
  60. }
  61. resp["ecr_name"] = lastApplied.ECRName
  62. return resp
  63. case types.InfraEKS:
  64. lastApplied := &types.CreateEKSInfraRequest{}
  65. if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
  66. return resp
  67. }
  68. resp["eks_name"] = lastApplied.EKSName
  69. resp["machine_type"] = lastApplied.MachineType
  70. return resp
  71. case types.InfraGCR:
  72. return resp
  73. case types.InfraGKE:
  74. lastApplied := &types.CreateGKEInfraRequest{}
  75. if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
  76. return resp
  77. }
  78. resp["gke_name"] = lastApplied.GKEName
  79. return resp
  80. case types.InfraDOCR:
  81. lastApplied := &types.CreateDOCRInfraRequest{}
  82. if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
  83. return resp
  84. }
  85. resp["docr_name"] = lastApplied.DOCRName
  86. resp["docr_subscription_tier"] = lastApplied.DOCRSubscriptionTier
  87. return resp
  88. case types.InfraDOKS:
  89. lastApplied := &types.CreateDOKSInfraRequest{}
  90. if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
  91. return resp
  92. }
  93. resp["cluster_name"] = lastApplied.DOKSName
  94. resp["do_region"] = lastApplied.DORegion
  95. return resp
  96. }
  97. return resp
  98. }
  99. // GetID returns the unique id for this infra
  100. func (i *Infra) GetUniqueName() string {
  101. return fmt.Sprintf("%s-%d-%d-%s", i.Kind, i.ProjectID, i.ID, i.Suffix)
  102. }
  103. // ParseUniqueName returns the (kind, projectID, infraID, suffix)
  104. func ParseUniqueName(workspaceID string) (string, uint, uint, error) {
  105. strArr := strings.Split(workspaceID, "-")
  106. if len(strArr) < 3 {
  107. return "", 0, 0, fmt.Errorf("workspace id improperly formatted")
  108. }
  109. projID, err := strconv.ParseUint(strArr[1], 10, 64)
  110. if err != nil {
  111. return "", 0, 0, err
  112. }
  113. infraID, err := strconv.ParseUint(strArr[2], 10, 64)
  114. if err != nil {
  115. return "", 0, 0, err
  116. }
  117. return strArr[0], uint(projID), uint(infraID), nil
  118. }