registry.go 1.9 KB

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