registry.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. serv = types.GCR
  37. } else if r.DOIntegrationID != 0 {
  38. serv = types.DOCR
  39. } else if r.AzureIntegrationID != 0 {
  40. serv = types.ACR
  41. } else if strings.Contains(r.URL, "index.docker.io") {
  42. serv = types.DockerHub
  43. }
  44. uri := r.URL
  45. // remove the protocol
  46. if splStr := strings.Split(uri, "://"); len(splStr) > 1 {
  47. uri = splStr[1]
  48. }
  49. return &types.Registry{
  50. ID: r.ID,
  51. ProjectID: r.ProjectID,
  52. Name: r.Name,
  53. URL: uri,
  54. Service: string(serv),
  55. InfraID: r.InfraID,
  56. GCPIntegrationID: r.GCPIntegrationID,
  57. AWSIntegrationID: r.AWSIntegrationID,
  58. AzureIntegrationID: r.AzureIntegrationID,
  59. DOIntegrationID: r.DOIntegrationID,
  60. BasicIntegrationID: r.BasicIntegrationID,
  61. }
  62. }