registry.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. AzureIntegrationID uint
  26. DOIntegrationID uint
  27. BasicIntegrationID uint
  28. // A token cache that can be used by an auth mechanism (integration), if desired
  29. TokenCache integrations.RegTokenCache
  30. }
  31. func (r *Registry) ToRegistryType() *types.Registry {
  32. var serv types.RegistryService
  33. if r.AWSIntegrationID != 0 {
  34. serv = types.ECR
  35. } else if r.GCPIntegrationID != 0 {
  36. if strings.Contains(r.URL, "pkg.dev") {
  37. serv = types.GAR
  38. } else {
  39. serv = types.GCR
  40. }
  41. } else if r.DOIntegrationID != 0 {
  42. serv = types.DOCR
  43. } else if r.AzureIntegrationID != 0 {
  44. serv = types.ACR
  45. } else if strings.Contains(r.URL, "index.docker.io") {
  46. serv = types.DockerHub
  47. }
  48. uri := r.URL
  49. // remove the protocol
  50. if splStr := strings.Split(uri, "://"); len(splStr) > 1 {
  51. uri = splStr[1]
  52. }
  53. return &types.Registry{
  54. ID: r.ID,
  55. ProjectID: r.ProjectID,
  56. Name: r.Name,
  57. URL: uri,
  58. Service: string(serv),
  59. InfraID: r.InfraID,
  60. GCPIntegrationID: r.GCPIntegrationID,
  61. AWSIntegrationID: r.AWSIntegrationID,
  62. AzureIntegrationID: r.AzureIntegrationID,
  63. DOIntegrationID: r.DOIntegrationID,
  64. BasicIntegrationID: r.BasicIntegrationID,
  65. }
  66. }