| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- package client
- 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
- }
- type DeployTemplateGitAction struct {
- GitRepo string `json:"git_repo"`
- GitBranch string `json:"git_branch"`
- ImageRepoURI string `json:"image_repo_uri"`
- DockerfilePath string `json:"dockerfile_path"`
- FolderPath string `json:"folder_path"`
- GitRepoID uint `json:"git_repo_id"`
- BuildEnv map[string]string `json:"env"`
- RegistryID uint `json:"registry_id"`
- }
- type DeployTemplateRequest struct {
- TemplateName string `json:"templateName"`
- ImageURL string `json:"imageURL"`
- FormValues map[string]interface{} `json:"formValues"`
- Namespace string `json:"namespace"`
- Name string `json:"name"`
- GitAction *DeployTemplateGitAction `json:"github_action"`
- }
- func (c *Client) DeployTemplate(
- ctx context.Context,
- projID, clusterID uint,
- templateName string,
- templateVersion string,
- deployReq *DeployTemplateRequest,
- ) error {
- data, err := json.Marshal(deployReq)
- if err != nil {
- return err
- }
- req, err := http.NewRequest(
- "POST",
- fmt.Sprintf("%s/projects/%d/deploy/%s/%s?"+url.Values{
- "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
- "storage": []string{"secret"},
- }.Encode(), c.BaseURL, projID, templateName, templateVersion),
- 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
- }
- type UpgradeReleaseRequest struct {
- Values string `json:"values"`
- Namespace string `json:"namespace"`
- }
- func (c *Client) UpgradeRelease(
- ctx context.Context,
- projID, clusterID uint,
- name string,
- upgradeReq *UpgradeReleaseRequest,
- ) error {
- data, err := json.Marshal(upgradeReq)
- if err != nil {
- return err
- }
- req, err := http.NewRequest(
- "POST",
- fmt.Sprintf("%s/projects/%d/releases/%s/upgrade?"+url.Values{
- "namespace": []string{upgradeReq.Namespace},
- "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
- "storage": []string{"secret"},
- }.Encode(), c.BaseURL, projID, name),
- 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
- }
|