registry.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. // ListRegistryResponse is the list of registries for a project
  84. type ListRegistryResponse []models.RegistryExternal
  85. // ListRegistries returns a list of registries for a project
  86. func (c *Client) ListRegistries(
  87. ctx context.Context,
  88. projectID uint,
  89. ) (ListRegistryResponse, error) {
  90. req, err := http.NewRequest(
  91. "GET",
  92. fmt.Sprintf("%s/projects/%d/registries", c.BaseURL, projectID),
  93. nil,
  94. )
  95. if err != nil {
  96. return nil, err
  97. }
  98. req = req.WithContext(ctx)
  99. bodyResp := &ListRegistryResponse{}
  100. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  101. if httpErr != nil {
  102. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  103. }
  104. return nil, err
  105. }
  106. return *bodyResp, nil
  107. }
  108. // DeleteProjectRegistry deletes a registry given a project id and registry id
  109. func (c *Client) DeleteProjectRegistry(
  110. ctx context.Context,
  111. projectID uint,
  112. registryID uint,
  113. ) error {
  114. req, err := http.NewRequest(
  115. "DELETE",
  116. fmt.Sprintf("%s/projects/%d/registries/%d", c.BaseURL, projectID, registryID),
  117. nil,
  118. )
  119. if err != nil {
  120. return err
  121. }
  122. req = req.WithContext(ctx)
  123. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  124. if httpErr != nil {
  125. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  126. }
  127. return err
  128. }
  129. return nil
  130. }
  131. // ListRegistryRepositoryResponse is the list of repositories in a registry
  132. type ListRegistryRepositoryResponse []registry.Repository
  133. // ListRegistryRepositories lists the repositories in a registry
  134. func (c *Client) ListRegistryRepositories(
  135. ctx context.Context,
  136. projectID uint,
  137. registryID uint,
  138. ) (ListRegistryRepositoryResponse, error) {
  139. req, err := http.NewRequest(
  140. "GET",
  141. fmt.Sprintf("%s/projects/%d/registries/%d/repositories", c.BaseURL, projectID, registryID),
  142. nil,
  143. )
  144. if err != nil {
  145. return nil, err
  146. }
  147. req = req.WithContext(ctx)
  148. bodyResp := &ListRegistryRepositoryResponse{}
  149. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  150. if httpErr != nil {
  151. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  152. }
  153. return nil, err
  154. }
  155. return *bodyResp, nil
  156. }
  157. // ListImagesResponse is the list of images in a repository
  158. type ListImagesResponse []registry.Image
  159. // ListImages lists the images (repository+tag) in a repository
  160. func (c *Client) ListImages(
  161. ctx context.Context,
  162. projectID uint,
  163. registryID uint,
  164. repoName string,
  165. ) (ListImagesResponse, error) {
  166. req, err := http.NewRequest(
  167. "GET",
  168. fmt.Sprintf("%s/projects/%d/registries/%d/repositories/%s", c.BaseURL, projectID, registryID, repoName),
  169. nil,
  170. )
  171. if err != nil {
  172. return nil, err
  173. }
  174. req = req.WithContext(ctx)
  175. bodyResp := &ListImagesResponse{}
  176. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  177. if httpErr != nil {
  178. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  179. }
  180. return nil, err
  181. }
  182. return *bodyResp, nil
  183. }