registry.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. URL string `json:"url"`
  54. }
  55. // CreateGCRResponse is the resulting registry after creation
  56. type CreateGCRResponse models.RegistryExternal
  57. // CreateGCR creates an Google Container Registry integration
  58. func (c *Client) CreateGCR(
  59. ctx context.Context,
  60. projectID uint,
  61. createGCR *CreateGCRRequest,
  62. ) (*CreateGCRResponse, error) {
  63. data, err := json.Marshal(createGCR)
  64. if err != nil {
  65. return nil, err
  66. }
  67. req, err := http.NewRequest(
  68. "POST",
  69. fmt.Sprintf("%s/projects/%d/registries", c.BaseURL, projectID),
  70. strings.NewReader(string(data)),
  71. )
  72. if err != nil {
  73. return nil, err
  74. }
  75. req = req.WithContext(ctx)
  76. bodyResp := &CreateGCRResponse{}
  77. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  78. if httpErr != nil {
  79. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  80. }
  81. return nil, err
  82. }
  83. return bodyResp, nil
  84. }
  85. // ListRegistryResponse is the list of registries for a project
  86. type ListRegistryResponse []models.RegistryExternal
  87. // ListRegistries returns a list of registries for a project
  88. func (c *Client) ListRegistries(
  89. ctx context.Context,
  90. projectID uint,
  91. ) (ListRegistryResponse, error) {
  92. req, err := http.NewRequest(
  93. "GET",
  94. fmt.Sprintf("%s/projects/%d/registries", c.BaseURL, projectID),
  95. nil,
  96. )
  97. if err != nil {
  98. return nil, err
  99. }
  100. req = req.WithContext(ctx)
  101. bodyResp := &ListRegistryResponse{}
  102. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  103. if httpErr != nil {
  104. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  105. }
  106. return nil, err
  107. }
  108. return *bodyResp, nil
  109. }
  110. // DeleteProjectRegistry deletes a registry given a project id and registry id
  111. func (c *Client) DeleteProjectRegistry(
  112. ctx context.Context,
  113. projectID uint,
  114. registryID uint,
  115. ) error {
  116. req, err := http.NewRequest(
  117. "DELETE",
  118. fmt.Sprintf("%s/projects/%d/registries/%d", c.BaseURL, projectID, registryID),
  119. nil,
  120. )
  121. if err != nil {
  122. return err
  123. }
  124. req = req.WithContext(ctx)
  125. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  126. if httpErr != nil {
  127. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  128. }
  129. return err
  130. }
  131. return nil
  132. }
  133. // GetTokenResponse blah
  134. type GetTokenResponse struct {
  135. Token string `json:"token"`
  136. ExpiresAt *time.Time `json:"expires_at"`
  137. }
  138. // GetECRAuthorizationToken gets an ECR authorization token
  139. func (c *Client) GetECRAuthorizationToken(
  140. ctx context.Context,
  141. projectID uint,
  142. region string,
  143. ) (*GetTokenResponse, error) {
  144. req, err := http.NewRequest(
  145. "GET",
  146. fmt.Sprintf("%s/projects/%d/registries/ecr/%s/token", c.BaseURL, projectID, region),
  147. nil,
  148. )
  149. if err != nil {
  150. return nil, err
  151. }
  152. bodyResp := &GetTokenResponse{}
  153. req = req.WithContext(ctx)
  154. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  155. if httpErr != nil {
  156. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  157. }
  158. return nil, err
  159. }
  160. return bodyResp, nil
  161. }
  162. type GetGCRTokenRequest struct {
  163. ServerURL string `json:"server_url"`
  164. }
  165. // GetGCRAuthorizationToken gets a GCR authorization token
  166. func (c *Client) GetGCRAuthorizationToken(
  167. ctx context.Context,
  168. projectID uint,
  169. gcrRequest *GetGCRTokenRequest,
  170. ) (*GetTokenResponse, error) {
  171. data, err := json.Marshal(gcrRequest)
  172. if err != nil {
  173. return nil, err
  174. }
  175. req, err := http.NewRequest(
  176. "GET",
  177. fmt.Sprintf("%s/projects/%d/registries/gcr/token", c.BaseURL, projectID),
  178. strings.NewReader(string(data)),
  179. )
  180. if err != nil {
  181. return nil, err
  182. }
  183. bodyResp := &GetTokenResponse{}
  184. req = req.WithContext(ctx)
  185. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  186. if httpErr != nil {
  187. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  188. }
  189. return nil, err
  190. }
  191. return bodyResp, nil
  192. }
  193. // ListRegistryRepositoryResponse is the list of repositories in a registry
  194. type ListRegistryRepositoryResponse []registry.Repository
  195. // ListRegistryRepositories lists the repositories in a registry
  196. func (c *Client) ListRegistryRepositories(
  197. ctx context.Context,
  198. projectID uint,
  199. registryID uint,
  200. ) (ListRegistryRepositoryResponse, error) {
  201. req, err := http.NewRequest(
  202. "GET",
  203. fmt.Sprintf("%s/projects/%d/registries/%d/repositories", c.BaseURL, projectID, registryID),
  204. nil,
  205. )
  206. if err != nil {
  207. return nil, err
  208. }
  209. req = req.WithContext(ctx)
  210. bodyResp := &ListRegistryRepositoryResponse{}
  211. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  212. if httpErr != nil {
  213. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  214. }
  215. return nil, err
  216. }
  217. return *bodyResp, nil
  218. }
  219. // ListImagesResponse is the list of images in a repository
  220. type ListImagesResponse []registry.Image
  221. // ListImages lists the images (repository+tag) in a repository
  222. func (c *Client) ListImages(
  223. ctx context.Context,
  224. projectID uint,
  225. registryID uint,
  226. repoName string,
  227. ) (ListImagesResponse, error) {
  228. req, err := http.NewRequest(
  229. "GET",
  230. fmt.Sprintf("%s/projects/%d/registries/%d/repositories/%s", c.BaseURL, projectID, registryID, repoName),
  231. nil,
  232. )
  233. if err != nil {
  234. return nil, err
  235. }
  236. req = req.WithContext(ctx)
  237. bodyResp := &ListImagesResponse{}
  238. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  239. if httpErr != nil {
  240. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  241. }
  242. return nil, err
  243. }
  244. return *bodyResp, nil
  245. }