registry.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. // CreateDOCRRequest represents the accepted fields for creating
  86. // a DOCR registry
  87. type CreateDOCRRequest struct {
  88. Name string `json:"name"`
  89. DOIntegrationID uint `json:"do_integration_id"`
  90. URL string `json:"url"`
  91. }
  92. // CreateDOCRResponse is the resulting registry after creation
  93. type CreateDOCRResponse models.RegistryExternal
  94. // CreateDOCR creates an Digital Ocean Container Registry integration
  95. func (c *Client) CreateDOCR(
  96. ctx context.Context,
  97. projectID uint,
  98. createDOCR *CreateDOCRRequest,
  99. ) (*CreateDOCRResponse, error) {
  100. data, err := json.Marshal(createDOCR)
  101. if err != nil {
  102. return nil, err
  103. }
  104. req, err := http.NewRequest(
  105. "POST",
  106. fmt.Sprintf("%s/projects/%d/registries", c.BaseURL, projectID),
  107. strings.NewReader(string(data)),
  108. )
  109. if err != nil {
  110. return nil, err
  111. }
  112. req = req.WithContext(ctx)
  113. bodyResp := &CreateDOCRResponse{}
  114. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  115. if httpErr != nil {
  116. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  117. }
  118. return nil, err
  119. }
  120. return bodyResp, nil
  121. }
  122. // ListRegistryResponse is the list of registries for a project
  123. type ListRegistryResponse []models.RegistryExternal
  124. // ListRegistries returns a list of registries for a project
  125. func (c *Client) ListRegistries(
  126. ctx context.Context,
  127. projectID uint,
  128. ) (ListRegistryResponse, error) {
  129. req, err := http.NewRequest(
  130. "GET",
  131. fmt.Sprintf("%s/projects/%d/registries", c.BaseURL, projectID),
  132. nil,
  133. )
  134. if err != nil {
  135. return nil, err
  136. }
  137. req = req.WithContext(ctx)
  138. bodyResp := &ListRegistryResponse{}
  139. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  140. if httpErr != nil {
  141. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  142. }
  143. return nil, err
  144. }
  145. return *bodyResp, nil
  146. }
  147. // DeleteProjectRegistry deletes a registry given a project id and registry id
  148. func (c *Client) DeleteProjectRegistry(
  149. ctx context.Context,
  150. projectID uint,
  151. registryID uint,
  152. ) error {
  153. req, err := http.NewRequest(
  154. "DELETE",
  155. fmt.Sprintf("%s/projects/%d/registries/%d", c.BaseURL, projectID, registryID),
  156. nil,
  157. )
  158. if err != nil {
  159. return err
  160. }
  161. req = req.WithContext(ctx)
  162. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  163. if httpErr != nil {
  164. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  165. }
  166. return err
  167. }
  168. return nil
  169. }
  170. // GetTokenResponse blah
  171. type GetTokenResponse struct {
  172. Token string `json:"token"`
  173. ExpiresAt *time.Time `json:"expires_at"`
  174. }
  175. // GetECRAuthorizationToken gets an ECR authorization token
  176. func (c *Client) GetECRAuthorizationToken(
  177. ctx context.Context,
  178. projectID uint,
  179. region string,
  180. ) (*GetTokenResponse, error) {
  181. req, err := http.NewRequest(
  182. "GET",
  183. fmt.Sprintf("%s/projects/%d/registries/ecr/%s/token", c.BaseURL, projectID, region),
  184. nil,
  185. )
  186. if err != nil {
  187. return nil, err
  188. }
  189. bodyResp := &GetTokenResponse{}
  190. req = req.WithContext(ctx)
  191. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  192. if httpErr != nil {
  193. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  194. }
  195. return nil, err
  196. }
  197. return bodyResp, nil
  198. }
  199. type GetGCRTokenRequest struct {
  200. ServerURL string `json:"server_url"`
  201. }
  202. // GetGCRAuthorizationToken gets a GCR authorization token
  203. func (c *Client) GetGCRAuthorizationToken(
  204. ctx context.Context,
  205. projectID uint,
  206. gcrRequest *GetGCRTokenRequest,
  207. ) (*GetTokenResponse, error) {
  208. data, err := json.Marshal(gcrRequest)
  209. if err != nil {
  210. return nil, err
  211. }
  212. req, err := http.NewRequest(
  213. "GET",
  214. fmt.Sprintf("%s/projects/%d/registries/gcr/token", c.BaseURL, projectID),
  215. strings.NewReader(string(data)),
  216. )
  217. if err != nil {
  218. return nil, err
  219. }
  220. bodyResp := &GetTokenResponse{}
  221. req = req.WithContext(ctx)
  222. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  223. if httpErr != nil {
  224. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  225. }
  226. return nil, err
  227. }
  228. return bodyResp, nil
  229. }
  230. type GetDOCRTokenRequest struct {
  231. ServerURL string `json:"server_url"`
  232. }
  233. // GetDOCRAuthorizationToken gets a DOCR authorization token
  234. func (c *Client) GetDOCRAuthorizationToken(
  235. ctx context.Context,
  236. projectID uint,
  237. docrRequest *GetDOCRTokenRequest,
  238. ) (*GetTokenResponse, error) {
  239. data, err := json.Marshal(docrRequest)
  240. if err != nil {
  241. return nil, err
  242. }
  243. req, err := http.NewRequest(
  244. "GET",
  245. fmt.Sprintf("%s/projects/%d/registries/docr/token", c.BaseURL, projectID),
  246. strings.NewReader(string(data)),
  247. )
  248. if err != nil {
  249. return nil, err
  250. }
  251. bodyResp := &GetTokenResponse{}
  252. req = req.WithContext(ctx)
  253. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  254. if httpErr != nil {
  255. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  256. }
  257. return nil, err
  258. }
  259. return bodyResp, nil
  260. }
  261. // ListRegistryRepositoryResponse is the list of repositories in a registry
  262. type ListRegistryRepositoryResponse []registry.Repository
  263. // ListRegistryRepositories lists the repositories in a registry
  264. func (c *Client) ListRegistryRepositories(
  265. ctx context.Context,
  266. projectID uint,
  267. registryID uint,
  268. ) (ListRegistryRepositoryResponse, error) {
  269. req, err := http.NewRequest(
  270. "GET",
  271. fmt.Sprintf("%s/projects/%d/registries/%d/repositories", c.BaseURL, projectID, registryID),
  272. nil,
  273. )
  274. if err != nil {
  275. return nil, err
  276. }
  277. req = req.WithContext(ctx)
  278. bodyResp := &ListRegistryRepositoryResponse{}
  279. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  280. if httpErr != nil {
  281. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  282. }
  283. return nil, err
  284. }
  285. return *bodyResp, nil
  286. }
  287. // ListImagesResponse is the list of images in a repository
  288. type ListImagesResponse []registry.Image
  289. // ListImages lists the images (repository+tag) in a repository
  290. func (c *Client) ListImages(
  291. ctx context.Context,
  292. projectID uint,
  293. registryID uint,
  294. repoName string,
  295. ) (ListImagesResponse, error) {
  296. req, err := http.NewRequest(
  297. "GET",
  298. fmt.Sprintf("%s/projects/%d/registries/%d/repositories/%s", c.BaseURL, projectID, registryID, repoName),
  299. nil,
  300. )
  301. if err != nil {
  302. return nil, err
  303. }
  304. req = req.WithContext(ctx)
  305. bodyResp := &ListImagesResponse{}
  306. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  307. if httpErr != nil {
  308. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  309. }
  310. return nil, err
  311. }
  312. return *bodyResp, nil
  313. }