deploy.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package api
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "github.com/porter-dev/porter/internal/models"
  10. )
  11. // GetReleaseLatestRevision gets the latest revision of a Helm release
  12. type GetReleaseWebhookResponse models.ReleaseExternal
  13. func (c *Client) GetReleaseWebhook(
  14. ctx context.Context,
  15. projID, clusterID uint,
  16. name, namespace string,
  17. ) (*GetReleaseWebhookResponse, error) {
  18. req, err := http.NewRequest(
  19. "GET",
  20. fmt.Sprintf("%s/projects/%d/releases/%s/webhook_token?"+url.Values{
  21. "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
  22. "namespace": []string{namespace},
  23. "storage": []string{"secret"},
  24. }.Encode(), c.BaseURL, projID, name),
  25. nil,
  26. )
  27. if err != nil {
  28. return nil, err
  29. }
  30. req = req.WithContext(ctx)
  31. bodyResp := &GetReleaseWebhookResponse{}
  32. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  33. if httpErr != nil {
  34. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  35. }
  36. return nil, err
  37. }
  38. return bodyResp, nil
  39. }
  40. // DeployWithWebhook deploys an application with an image tag using a unique webhook URI
  41. func (c *Client) DeployWithWebhook(
  42. ctx context.Context,
  43. webhook, tag string,
  44. ) error {
  45. req, err := http.NewRequest(
  46. "POST",
  47. fmt.Sprintf("%s/webhooks/deploy/%s?commit=%s", c.BaseURL, webhook, tag),
  48. nil,
  49. )
  50. if err != nil {
  51. return err
  52. }
  53. req = req.WithContext(ctx)
  54. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  55. if httpErr != nil {
  56. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  57. }
  58. return err
  59. }
  60. return nil
  61. }
  62. type UpdateBatchImageRequest struct {
  63. ImageRepoURI string `json:"image_repo_uri"`
  64. Tag string `json:"tag"`
  65. }
  66. // UpdateBatchImage updates all releases that use a certain image with a new tag
  67. func (c *Client) UpdateBatchImage(
  68. ctx context.Context,
  69. projID, clusterID uint,
  70. updateImageReq *UpdateBatchImageRequest,
  71. ) error {
  72. data, err := json.Marshal(updateImageReq)
  73. if err != nil {
  74. return nil
  75. }
  76. req, err := http.NewRequest(
  77. "POST",
  78. fmt.Sprintf("%s/projects/%d/releases/image/update/batch?"+url.Values{
  79. "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
  80. "namespace": []string{"default"},
  81. "storage": []string{"secret"},
  82. }.Encode(), c.BaseURL, projID),
  83. strings.NewReader(string(data)),
  84. )
  85. if err != nil {
  86. return err
  87. }
  88. req = req.WithContext(ctx)
  89. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  90. if httpErr != nil {
  91. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  92. }
  93. return err
  94. }
  95. return nil
  96. }