registry.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package models
  2. import (
  3. "strings"
  4. "github.com/porter-dev/porter/api/types"
  5. "github.com/porter-dev/porter/internal/models/integrations"
  6. "gorm.io/gorm"
  7. )
  8. // Registry is an integration that can connect to a Docker image registry via
  9. // a specific auth mechanism
  10. type Registry struct {
  11. gorm.Model
  12. // Name of the registry
  13. Name string `json:"name"`
  14. // URL of the registry
  15. URL string `json:"url"`
  16. // The project that this integration belongs to
  17. ProjectID uint `json:"project_id"`
  18. // The infra id, if registry was provisioned with Porter
  19. InfraID uint `json:"infra_id"`
  20. // ------------------------------------------------------------------
  21. // All fields below this line are encrypted before storage
  22. // ------------------------------------------------------------------
  23. GCPIntegrationID uint
  24. AWSIntegrationID uint
  25. DOIntegrationID uint
  26. BasicIntegrationID uint
  27. // A token cache that can be used by an auth mechanism (integration), if desired
  28. TokenCache integrations.RegTokenCache
  29. }
  30. // RegistryExternal is an external Registry to be shared over REST
  31. type RegistryExternal struct {
  32. ID uint `json:"id"`
  33. // The project that this integration belongs to
  34. ProjectID uint `json:"project_id"`
  35. // Name of the registry
  36. Name string `json:"name"`
  37. // URL of the registry
  38. URL string `json:"url"`
  39. // The integration service for this registry
  40. Service integrations.IntegrationService `json:"service"`
  41. // The infra id, if registry was provisioned with Porter
  42. InfraID uint `json:"infra_id"`
  43. }
  44. // Externalize generates an external Registry to be shared over REST
  45. func (r *Registry) Externalize() *RegistryExternal {
  46. var serv integrations.IntegrationService
  47. if r.AWSIntegrationID != 0 {
  48. serv = integrations.ECR
  49. } else if r.GCPIntegrationID != 0 {
  50. serv = integrations.GCR
  51. } else if r.DOIntegrationID != 0 {
  52. serv = integrations.DOCR
  53. } else if strings.Contains(r.URL, "index.docker.io") {
  54. serv = integrations.DockerHub
  55. }
  56. uri := r.URL
  57. // remove the protocol
  58. if splStr := strings.Split(uri, "://"); len(splStr) > 1 {
  59. uri = splStr[1]
  60. }
  61. return &RegistryExternal{
  62. ID: r.ID,
  63. ProjectID: r.ProjectID,
  64. Name: r.Name,
  65. URL: uri,
  66. Service: serv,
  67. InfraID: r.InfraID,
  68. }
  69. }
  70. func (r *Registry) ToRegistryType() *types.Registry {
  71. var serv integrations.IntegrationService
  72. if r.AWSIntegrationID != 0 {
  73. serv = integrations.ECR
  74. } else if r.GCPIntegrationID != 0 {
  75. serv = integrations.GCR
  76. } else if r.DOIntegrationID != 0 {
  77. serv = integrations.DOCR
  78. } else if strings.Contains(r.URL, "index.docker.io") {
  79. serv = integrations.DockerHub
  80. }
  81. uri := r.URL
  82. // remove the protocol
  83. if splStr := strings.Split(uri, "://"); len(splStr) > 1 {
  84. uri = splStr[1]
  85. }
  86. return &types.Registry{
  87. ID: r.ID,
  88. ProjectID: r.ProjectID,
  89. Name: r.Name,
  90. URL: uri,
  91. Service: serv,
  92. InfraID: r.InfraID,
  93. }
  94. }