registry.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. }
  16. // ToRegistry converts the form to a gorm registry model
  17. func (cr *CreateRegistry) ToRegistry(repo repository.Repository) (*models.Registry, error) {
  18. registry := &models.Registry{
  19. Name: cr.Name,
  20. ProjectID: cr.ProjectID,
  21. URL: cr.URL,
  22. GCPIntegrationID: cr.GCPIntegrationID,
  23. AWSIntegrationID: cr.AWSIntegrationID,
  24. }
  25. if registry.URL == "" && registry.AWSIntegrationID != 0 {
  26. awsInt, err := repo.AWSIntegration.ReadAWSIntegration(registry.AWSIntegrationID)
  27. if err != nil {
  28. return nil, err
  29. }
  30. sess, err := awsInt.GetSession()
  31. if err != nil {
  32. return nil, err
  33. }
  34. ecrSvc := ecr.New(sess)
  35. output, err := ecrSvc.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{})
  36. if err != nil {
  37. return nil, err
  38. }
  39. registry.URL = *output.AuthorizationData[0].ProxyEndpoint
  40. }
  41. return registry, nil
  42. }
  43. // UpdateRegistryForm represents the accepted values for updating a
  44. // registry (only name for now)
  45. type UpdateRegistryForm struct {
  46. ID uint
  47. Name string `json:"name" form:"required"`
  48. }
  49. // ToRegistry converts the form to a cluster
  50. func (urf *UpdateRegistryForm) ToRegistry(repo repository.RegistryRepository) (*models.Registry, error) {
  51. registry, err := repo.ReadRegistry(urf.ID)
  52. if err != nil {
  53. return nil, err
  54. }
  55. registry.Name = urf.Name
  56. return registry, nil
  57. }