registry.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package api
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "github.com/porter-dev/porter/internal/registry"
  9. "github.com/porter-dev/porter/internal/models"
  10. )
  11. // CreateECRRequest represents the accepted fields for creating
  12. // an ECR registry
  13. type CreateECRRequest struct {
  14. Name string `json:"name"`
  15. AWSIntegrationID uint `json:"aws_integration_id"`
  16. }
  17. // CreateECRResponse is the resulting registry after creation
  18. type CreateECRResponse models.RegistryExternal
  19. // CreateECR creates an Elastic Container Registry integration
  20. func (c *Client) CreateECR(
  21. ctx context.Context,
  22. projectID uint,
  23. createECR *CreateECRRequest,
  24. ) (*CreateECRResponse, error) {
  25. data, err := json.Marshal(createECR)
  26. if err != nil {
  27. return nil, err
  28. }
  29. req, err := http.NewRequest(
  30. "POST",
  31. fmt.Sprintf("%s/projects/%d/registries", c.BaseURL, projectID),
  32. strings.NewReader(string(data)),
  33. )
  34. if err != nil {
  35. return nil, err
  36. }
  37. req = req.WithContext(ctx)
  38. bodyResp := &CreateECRResponse{}
  39. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  40. if httpErr != nil {
  41. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  42. }
  43. return nil, err
  44. }
  45. return bodyResp, nil
  46. }
  47. // ListRegistryRepositoryResponse is the list of repositories in a registry
  48. type ListRegistryRepositoryResponse []registry.Repository
  49. // ListRegistryRepositories lists the repositories in a registry
  50. func (c *Client) ListRegistryRepositories(
  51. ctx context.Context,
  52. projectID uint,
  53. registryID uint,
  54. ) (ListRegistryRepositoryResponse, error) {
  55. req, err := http.NewRequest(
  56. "GET",
  57. fmt.Sprintf("%s/projects/%d/registries/%d/repositories", c.BaseURL, projectID, registryID),
  58. nil,
  59. )
  60. if err != nil {
  61. return nil, err
  62. }
  63. req = req.WithContext(ctx)
  64. bodyResp := &ListRegistryRepositoryResponse{}
  65. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  66. if httpErr != nil {
  67. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  68. }
  69. return nil, err
  70. }
  71. return *bodyResp, nil
  72. }