registry.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. func (r *Registry) ToRegistryType() *types.Registry {
  31. var serv types.RegistryService
  32. if r.AWSIntegrationID != 0 {
  33. serv = types.ECR
  34. } else if r.GCPIntegrationID != 0 {
  35. serv = types.GCR
  36. } else if r.DOIntegrationID != 0 {
  37. serv = types.DOCR
  38. } else if strings.Contains(r.URL, "index.docker.io") {
  39. serv = types.DockerHub
  40. }
  41. uri := r.URL
  42. // remove the protocol
  43. if splStr := strings.Split(uri, "://"); len(splStr) > 1 {
  44. uri = splStr[1]
  45. }
  46. return &types.Registry{
  47. ID: r.ID,
  48. ProjectID: r.ProjectID,
  49. Name: r.Name,
  50. URL: uri,
  51. Service: serv,
  52. InfraID: r.InfraID,
  53. GCPIntegrationID: r.GCPIntegrationID,
  54. AWSIntegrationID: r.AWSIntegrationID,
  55. DOIntegrationID: r.DOIntegrationID,
  56. BasicIntegrationID: r.BasicIntegrationID,
  57. }
  58. }