deploy.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. namespace string,
  71. updateImageReq *UpdateBatchImageRequest,
  72. ) error {
  73. data, err := json.Marshal(updateImageReq)
  74. if err != nil {
  75. return nil
  76. }
  77. req, err := http.NewRequest(
  78. "POST",
  79. fmt.Sprintf("%s/projects/%d/releases/image/update/batch?"+url.Values{
  80. "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
  81. "namespace": []string{namespace},
  82. "storage": []string{"secret"},
  83. }.Encode(), c.BaseURL, projID),
  84. strings.NewReader(string(data)),
  85. )
  86. if err != nil {
  87. return err
  88. }
  89. req = req.WithContext(ctx)
  90. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  91. if httpErr != nil {
  92. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  93. }
  94. return err
  95. }
  96. return nil
  97. }