| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package api
- import (
- "context"
- "encoding/json"
- "fmt"
- "net/http"
- "net/url"
- "strings"
- "github.com/porter-dev/porter/internal/models"
- )
- // GetReleaseLatestRevision gets the latest revision of a Helm release
- type GetReleaseWebhookResponse models.ReleaseExternal
- func (c *Client) GetReleaseWebhook(
- ctx context.Context,
- projID, clusterID uint,
- name, namespace string,
- ) (*GetReleaseWebhookResponse, error) {
- req, err := http.NewRequest(
- "GET",
- fmt.Sprintf("%s/projects/%d/releases/%s/webhook_token?"+url.Values{
- "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
- "namespace": []string{namespace},
- "storage": []string{"secret"},
- }.Encode(), c.BaseURL, projID, name),
- nil,
- )
- if err != nil {
- return nil, err
- }
- req = req.WithContext(ctx)
- bodyResp := &GetReleaseWebhookResponse{}
- if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
- if httpErr != nil {
- return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
- }
- return nil, err
- }
- return bodyResp, nil
- }
- // DeployWithWebhook deploys an application with an image tag using a unique webhook URI
- func (c *Client) DeployWithWebhook(
- ctx context.Context,
- webhook, tag string,
- ) error {
- req, err := http.NewRequest(
- "POST",
- fmt.Sprintf("%s/webhooks/deploy/%s?commit=%s", c.BaseURL, webhook, tag),
- nil,
- )
- if err != nil {
- return err
- }
- req = req.WithContext(ctx)
- if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
- if httpErr != nil {
- return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
- }
- return err
- }
- return nil
- }
- type UpdateBatchImageRequest struct {
- ImageRepoURI string `json:"image_repo_uri"`
- Tag string `json:"tag"`
- }
- // UpdateBatchImage updates all releases that use a certain image with a new tag
- func (c *Client) UpdateBatchImage(
- ctx context.Context,
- projID, clusterID uint,
- namespace string,
- updateImageReq *UpdateBatchImageRequest,
- ) error {
- data, err := json.Marshal(updateImageReq)
- if err != nil {
- return nil
- }
- req, err := http.NewRequest(
- "POST",
- fmt.Sprintf("%s/projects/%d/releases/image/update/batch?"+url.Values{
- "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
- "namespace": []string{namespace},
- "storage": []string{"secret"},
- }.Encode(), c.BaseURL, projID),
- strings.NewReader(string(data)),
- )
- if err != nil {
- return err
- }
- req = req.WithContext(ctx)
- if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
- if httpErr != nil {
- return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
- }
- return err
- }
- return nil
- }
|