helm_repo.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package api
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "github.com/porter-dev/porter/internal/models"
  9. )
  10. // CreateHelmRepoRequest represents the accepted fields for creating
  11. // a Helm repository with basic authentication
  12. type CreateHelmRepoRequest struct {
  13. Name string `json:"name"`
  14. RepoURL string `json:"repo_url"`
  15. BasicIntegrationID uint `json:"basic_integration_id"`
  16. }
  17. // CreateHelmRepoResponse is the resulting helm repo after creation
  18. type CreateHelmRepoResponse models.HelmRepoExternal
  19. // CreateHelmRepo creates an Helm repository integration with basic authentication
  20. func (c *Client) CreateHelmRepo(
  21. ctx context.Context,
  22. projectID uint,
  23. createHR *CreateHelmRepoRequest,
  24. ) (*CreateHelmRepoResponse, error) {
  25. data, err := json.Marshal(createHR)
  26. if err != nil {
  27. return nil, err
  28. }
  29. req, err := http.NewRequest(
  30. "POST",
  31. fmt.Sprintf("%s/projects/%d/helmrepos", c.BaseURL, projectID),
  32. strings.NewReader(string(data)),
  33. )
  34. if err != nil {
  35. return nil, err
  36. }
  37. req = req.WithContext(ctx)
  38. bodyResp := &CreateHelmRepoResponse{}
  39. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  40. if httpErr != nil {
  41. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  42. }
  43. return nil, err
  44. }
  45. return bodyResp, nil
  46. }
  47. // ListHelmRepoResponse is the list of Helm repos for a project
  48. type ListHelmRepoResponse []models.HelmRepoExternal
  49. // ListHelmRepos returns a list of Helm repos for a project
  50. func (c *Client) ListHelmRepos(
  51. ctx context.Context,
  52. projectID uint,
  53. ) (ListHelmRepoResponse, error) {
  54. req, err := http.NewRequest(
  55. "GET",
  56. fmt.Sprintf("%s/projects/%d/helmrepos", c.BaseURL, projectID),
  57. nil,
  58. )
  59. if err != nil {
  60. return nil, err
  61. }
  62. req = req.WithContext(ctx)
  63. bodyResp := &ListHelmRepoResponse{}
  64. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  65. if httpErr != nil {
  66. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  67. }
  68. return nil, err
  69. }
  70. return *bodyResp, nil
  71. }
  72. // ListChartsResponse is the list of charts in a Helm repository
  73. type ListChartsResponse []models.PorterChartList
  74. // ListCharts lists the charts in a Helm repository
  75. func (c *Client) ListCharts(
  76. ctx context.Context,
  77. projectID uint,
  78. helmID uint,
  79. ) (ListChartsResponse, error) {
  80. req, err := http.NewRequest(
  81. "GET",
  82. fmt.Sprintf("%s/projects/%d/helmrepos/%d/charts", c.BaseURL, projectID, helmID),
  83. nil,
  84. )
  85. if err != nil {
  86. return nil, err
  87. }
  88. req = req.WithContext(ctx)
  89. bodyResp := &ListChartsResponse{}
  90. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  91. if httpErr != nil {
  92. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  93. }
  94. return nil, err
  95. }
  96. return *bodyResp, nil
  97. }