registry.go 2.1 KB

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