registry.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // CreateGCRRequest represents the accepted fields for creating
  48. // a GCR registry
  49. type CreateGCRRequest struct {
  50. Name string `json:"name"`
  51. GCPIntegrationID uint `json:"gcp_integration_id"`
  52. }
  53. // CreateGCRResponse is the resulting registry after creation
  54. type CreateGCRResponse models.RegistryExternal
  55. // CreateGCR creates an Google Container Registry integration
  56. func (c *Client) CreateGCR(
  57. ctx context.Context,
  58. projectID uint,
  59. createGCR *CreateGCRRequest,
  60. ) (*CreateGCRResponse, error) {
  61. data, err := json.Marshal(createGCR)
  62. if err != nil {
  63. return nil, err
  64. }
  65. req, err := http.NewRequest(
  66. "POST",
  67. fmt.Sprintf("%s/projects/%d/registries", c.BaseURL, projectID),
  68. strings.NewReader(string(data)),
  69. )
  70. if err != nil {
  71. return nil, err
  72. }
  73. req = req.WithContext(ctx)
  74. bodyResp := &CreateGCRResponse{}
  75. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  76. if httpErr != nil {
  77. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  78. }
  79. return nil, err
  80. }
  81. return bodyResp, nil
  82. }
  83. // ListRegistryRepositoryResponse is the list of repositories in a registry
  84. type ListRegistryRepositoryResponse []registry.Repository
  85. // ListRegistryRepositories lists the repositories in a registry
  86. func (c *Client) ListRegistryRepositories(
  87. ctx context.Context,
  88. projectID uint,
  89. registryID uint,
  90. ) (ListRegistryRepositoryResponse, error) {
  91. req, err := http.NewRequest(
  92. "GET",
  93. fmt.Sprintf("%s/projects/%d/registries/%d/repositories", c.BaseURL, projectID, registryID),
  94. nil,
  95. )
  96. if err != nil {
  97. return nil, err
  98. }
  99. req = req.WithContext(ctx)
  100. bodyResp := &ListRegistryRepositoryResponse{}
  101. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  102. if httpErr != nil {
  103. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  104. }
  105. return nil, err
  106. }
  107. return *bodyResp, nil
  108. }
  109. // ListImagesResponse is the list of images in a repository
  110. type ListImagesResponse []registry.Image
  111. // ListImages lists the images (repository+tag) in a repository
  112. func (c *Client) ListImages(
  113. ctx context.Context,
  114. projectID uint,
  115. registryID uint,
  116. repoName string,
  117. ) (ListImagesResponse, error) {
  118. req, err := http.NewRequest(
  119. "GET",
  120. fmt.Sprintf("%s/projects/%d/registries/%d/repositories/%s", c.BaseURL, projectID, registryID, repoName),
  121. nil,
  122. )
  123. if err != nil {
  124. return nil, err
  125. }
  126. req = req.WithContext(ctx)
  127. bodyResp := &ListImagesResponse{}
  128. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  129. if httpErr != nil {
  130. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  131. }
  132. return nil, err
  133. }
  134. return *bodyResp, nil
  135. }