registry.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package api
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "github.com/porter-dev/porter/internal/registry"
  10. "github.com/porter-dev/porter/internal/models"
  11. )
  12. // CreateECRRequest represents the accepted fields for creating
  13. // an ECR registry
  14. type CreateECRRequest struct {
  15. Name string `json:"name"`
  16. AWSIntegrationID uint `json:"aws_integration_id"`
  17. }
  18. // CreateECRResponse is the resulting registry after creation
  19. type CreateECRResponse models.RegistryExternal
  20. // CreateECR creates an Elastic Container Registry integration
  21. func (c *Client) CreateECR(
  22. ctx context.Context,
  23. projectID uint,
  24. createECR *CreateECRRequest,
  25. ) (*CreateECRResponse, error) {
  26. data, err := json.Marshal(createECR)
  27. if err != nil {
  28. return nil, err
  29. }
  30. req, err := http.NewRequest(
  31. "POST",
  32. fmt.Sprintf("%s/projects/%d/registries", c.BaseURL, projectID),
  33. strings.NewReader(string(data)),
  34. )
  35. if err != nil {
  36. return nil, err
  37. }
  38. req = req.WithContext(ctx)
  39. bodyResp := &CreateECRResponse{}
  40. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  41. if httpErr != nil {
  42. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  43. }
  44. return nil, err
  45. }
  46. return bodyResp, nil
  47. }
  48. // CreateGCRRequest represents the accepted fields for creating
  49. // a GCR registry
  50. type CreateGCRRequest struct {
  51. Name string `json:"name"`
  52. GCPIntegrationID uint `json:"gcp_integration_id"`
  53. }
  54. // CreateGCRResponse is the resulting registry after creation
  55. type CreateGCRResponse models.RegistryExternal
  56. // CreateGCR creates an Google Container Registry integration
  57. func (c *Client) CreateGCR(
  58. ctx context.Context,
  59. projectID uint,
  60. createGCR *CreateGCRRequest,
  61. ) (*CreateGCRResponse, error) {
  62. data, err := json.Marshal(createGCR)
  63. if err != nil {
  64. return nil, err
  65. }
  66. req, err := http.NewRequest(
  67. "POST",
  68. fmt.Sprintf("%s/projects/%d/registries", c.BaseURL, projectID),
  69. strings.NewReader(string(data)),
  70. )
  71. if err != nil {
  72. return nil, err
  73. }
  74. req = req.WithContext(ctx)
  75. bodyResp := &CreateGCRResponse{}
  76. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  77. if httpErr != nil {
  78. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  79. }
  80. return nil, err
  81. }
  82. return bodyResp, nil
  83. }
  84. // ListRegistryResponse is the list of registries for a project
  85. type ListRegistryResponse []models.RegistryExternal
  86. // ListRegistries returns a list of registries for a project
  87. func (c *Client) ListRegistries(
  88. ctx context.Context,
  89. projectID uint,
  90. ) (ListRegistryResponse, error) {
  91. req, err := http.NewRequest(
  92. "GET",
  93. fmt.Sprintf("%s/projects/%d/registries", c.BaseURL, projectID),
  94. nil,
  95. )
  96. if err != nil {
  97. return nil, err
  98. }
  99. req = req.WithContext(ctx)
  100. bodyResp := &ListRegistryResponse{}
  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. // DeleteProjectRegistry deletes a registry given a project id and registry id
  110. func (c *Client) DeleteProjectRegistry(
  111. ctx context.Context,
  112. projectID uint,
  113. registryID uint,
  114. ) error {
  115. req, err := http.NewRequest(
  116. "DELETE",
  117. fmt.Sprintf("%s/projects/%d/registries/%d", c.BaseURL, projectID, registryID),
  118. nil,
  119. )
  120. if err != nil {
  121. return err
  122. }
  123. req = req.WithContext(ctx)
  124. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  125. if httpErr != nil {
  126. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  127. }
  128. return err
  129. }
  130. return nil
  131. }
  132. // GetECRTokenResponse blah
  133. type GetECRTokenResponse struct {
  134. Token string `json:"token"`
  135. ExpiresAt *time.Time `json:"expires_at"`
  136. }
  137. // GetECRAuthorizationToken gets an ECR authorization token
  138. func (c *Client) GetECRAuthorizationToken(
  139. ctx context.Context,
  140. projectID uint,
  141. region string,
  142. ) (*GetECRTokenResponse, error) {
  143. req, err := http.NewRequest(
  144. "GET",
  145. fmt.Sprintf("%s/projects/%d/registries/ecr/%s/token", c.BaseURL, projectID, region),
  146. nil,
  147. )
  148. if err != nil {
  149. return nil, err
  150. }
  151. bodyResp := &GetECRTokenResponse{}
  152. req = req.WithContext(ctx)
  153. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  154. if httpErr != nil {
  155. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  156. }
  157. return nil, err
  158. }
  159. return bodyResp, nil
  160. }
  161. // ListRegistryRepositoryResponse is the list of repositories in a registry
  162. type ListRegistryRepositoryResponse []registry.Repository
  163. // ListRegistryRepositories lists the repositories in a registry
  164. func (c *Client) ListRegistryRepositories(
  165. ctx context.Context,
  166. projectID uint,
  167. registryID uint,
  168. ) (ListRegistryRepositoryResponse, error) {
  169. req, err := http.NewRequest(
  170. "GET",
  171. fmt.Sprintf("%s/projects/%d/registries/%d/repositories", c.BaseURL, projectID, registryID),
  172. nil,
  173. )
  174. if err != nil {
  175. return nil, err
  176. }
  177. req = req.WithContext(ctx)
  178. bodyResp := &ListRegistryRepositoryResponse{}
  179. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  180. if httpErr != nil {
  181. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  182. }
  183. return nil, err
  184. }
  185. return *bodyResp, nil
  186. }
  187. // ListImagesResponse is the list of images in a repository
  188. type ListImagesResponse []registry.Image
  189. // ListImages lists the images (repository+tag) in a repository
  190. func (c *Client) ListImages(
  191. ctx context.Context,
  192. projectID uint,
  193. registryID uint,
  194. repoName string,
  195. ) (ListImagesResponse, error) {
  196. req, err := http.NewRequest(
  197. "GET",
  198. fmt.Sprintf("%s/projects/%d/registries/%d/repositories/%s", c.BaseURL, projectID, registryID, repoName),
  199. nil,
  200. )
  201. if err != nil {
  202. return nil, err
  203. }
  204. req = req.WithContext(ctx)
  205. bodyResp := &ListImagesResponse{}
  206. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  207. if httpErr != nil {
  208. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  209. }
  210. return nil, err
  211. }
  212. return *bodyResp, nil
  213. }