infra.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // The database id for the infra, if this infra provisioned a database
  32. DatabaseID uint
  33. // ------------------------------------------------------------------
  34. // All fields below this line are encrypted before storage
  35. // ------------------------------------------------------------------
  36. // The last-applied input variables to the provisioner
  37. LastApplied []byte
  38. }
  39. // ToInfraType generates an external Infra to be shared over REST
  40. func (i *Infra) ToInfraType() *types.Infra {
  41. return &types.Infra{
  42. ID: i.ID,
  43. CreatedAt: i.CreatedAt,
  44. UpdatedAt: i.UpdatedAt,
  45. ProjectID: i.ProjectID,
  46. Kind: i.Kind,
  47. Status: i.Status,
  48. AWSIntegrationID: i.AWSIntegrationID,
  49. DOIntegrationID: i.DOIntegrationID,
  50. GCPIntegrationID: i.GCPIntegrationID,
  51. LastApplied: i.SafelyGetLastApplied(),
  52. }
  53. }
  54. // SafeGetLastApplied gets non-sensitive values for the last applied configuration
  55. func (i *Infra) SafelyGetLastApplied() map[string]string {
  56. resp := make(map[string]string)
  57. switch i.Kind {
  58. case types.InfraECR:
  59. lastApplied := &types.CreateECRInfraRequest{}
  60. if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
  61. return resp
  62. }
  63. resp["ecr_name"] = lastApplied.ECRName
  64. return resp
  65. case types.InfraEKS:
  66. lastApplied := &types.CreateEKSInfraRequest{}
  67. if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
  68. return resp
  69. }
  70. resp["eks_name"] = lastApplied.EKSName
  71. resp["machine_type"] = lastApplied.MachineType
  72. return resp
  73. case types.InfraGCR:
  74. return resp
  75. case types.InfraGKE:
  76. lastApplied := &types.CreateGKEInfraRequest{}
  77. if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
  78. return resp
  79. }
  80. resp["gke_name"] = lastApplied.GKEName
  81. return resp
  82. case types.InfraDOCR:
  83. lastApplied := &types.CreateDOCRInfraRequest{}
  84. if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
  85. return resp
  86. }
  87. resp["docr_name"] = lastApplied.DOCRName
  88. resp["docr_subscription_tier"] = lastApplied.DOCRSubscriptionTier
  89. return resp
  90. case types.InfraDOKS:
  91. lastApplied := &types.CreateDOKSInfraRequest{}
  92. if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
  93. return resp
  94. }
  95. resp["cluster_name"] = lastApplied.DOKSName
  96. resp["do_region"] = lastApplied.DORegion
  97. return resp
  98. case types.InfraRDS:
  99. lastApplied := &types.RDSInfraLastApplied{}
  100. if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
  101. return resp
  102. }
  103. resp["cluster_id"] = fmt.Sprintf("%d", lastApplied.ClusterID)
  104. resp["aws_region"] = lastApplied.AWSRegion
  105. resp["db_name"] = lastApplied.DBName
  106. return resp
  107. }
  108. return resp
  109. }
  110. // GetID returns the unique id for this infra
  111. func (i *Infra) GetUniqueName() string {
  112. return fmt.Sprintf("%s-%d-%d-%s", i.Kind, i.ProjectID, i.ID, i.Suffix)
  113. }
  114. // ParseUniqueName returns the (kind, projectID, infraID, suffix)
  115. func ParseUniqueName(workspaceID string) (string, uint, uint, error) {
  116. strArr := strings.Split(workspaceID, "-")
  117. if len(strArr) < 3 {
  118. return "", 0, 0, fmt.Errorf("workspace id improperly formatted")
  119. }
  120. projID, err := strconv.ParseUint(strArr[1], 10, 64)
  121. if err != nil {
  122. return "", 0, 0, err
  123. }
  124. infraID, err := strconv.ParseUint(strArr[2], 10, 64)
  125. if err != nil {
  126. return "", 0, 0, err
  127. }
  128. return strArr[0], uint(projID), uint(infraID), nil
  129. }