registry.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package forms
  2. import (
  3. "github.com/aws/aws-sdk-go/service/ecr"
  4. "github.com/porter-dev/porter/internal/models"
  5. "github.com/porter-dev/porter/internal/repository"
  6. )
  7. // CreateRegistry represents the accepted values for creating a
  8. // registry
  9. type CreateRegistry struct {
  10. Name string `json:"name" form:"required"`
  11. ProjectID uint `json:"project_id" form:"required"`
  12. URL string `json:"url"`
  13. GCPIntegrationID uint `json:"gcp_integration_id"`
  14. AWSIntegrationID uint `json:"aws_integration_id"`
  15. DOIntegrationID uint `json:"do_integration_id"`
  16. BasicIntegrationID uint `json:"basic_integration_id"`
  17. }
  18. // ToRegistry converts the form to a gorm registry model
  19. func (cr *CreateRegistry) ToRegistry(repo repository.Repository) (*models.Registry, error) {
  20. registry := &models.Registry{
  21. Name: cr.Name,
  22. ProjectID: cr.ProjectID,
  23. URL: cr.URL,
  24. GCPIntegrationID: cr.GCPIntegrationID,
  25. AWSIntegrationID: cr.AWSIntegrationID,
  26. DOIntegrationID: cr.DOIntegrationID,
  27. BasicIntegrationID: cr.BasicIntegrationID,
  28. }
  29. if registry.URL == "" && registry.AWSIntegrationID != 0 {
  30. awsInt, err := repo.AWSIntegration.ReadAWSIntegration(registry.AWSIntegrationID)
  31. if err != nil {
  32. return nil, err
  33. }
  34. sess, err := awsInt.GetSession()
  35. if err != nil {
  36. return nil, err
  37. }
  38. ecrSvc := ecr.New(sess)
  39. output, err := ecrSvc.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{})
  40. if err != nil {
  41. return nil, err
  42. }
  43. registry.URL = *output.AuthorizationData[0].ProxyEndpoint
  44. }
  45. return registry, nil
  46. }
  47. // UpdateRegistryForm represents the accepted values for updating a
  48. // registry (only name for now)
  49. type UpdateRegistryForm struct {
  50. ID uint
  51. Name string `json:"name" form:"required"`
  52. }
  53. // ToRegistry converts the form to a cluster
  54. func (urf *UpdateRegistryForm) ToRegistry(repo repository.RegistryRepository) (*models.Registry, error) {
  55. registry, err := repo.ReadRegistry(urf.ID)
  56. if err != nil {
  57. return nil, err
  58. }
  59. registry.Name = urf.Name
  60. return registry, nil
  61. }
  62. // CreateRepository represents the accepted values for creating an image repository
  63. // within a registry
  64. type CreateRepository struct {
  65. ImageRepoURI string `json:"image_repo_uri" form:"required"`
  66. }