integration.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package client
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "github.com/porter-dev/porter/api/types"
  9. )
  10. // CreateAWSIntegration creates an AWS integration with the given request options
  11. func (c *Client) CreateAWSIntegration(
  12. ctx context.Context,
  13. projectID uint,
  14. createAWS *types.CreateAWSRequest,
  15. ) (*types.CreateAWSResponse, error) {
  16. data, err := json.Marshal(createAWS)
  17. if err != nil {
  18. return nil, err
  19. }
  20. req, err := http.NewRequest(
  21. "POST",
  22. fmt.Sprintf("%s/projects/%d/integrations/aws", c.BaseURL, projectID),
  23. strings.NewReader(string(data)),
  24. )
  25. if err != nil {
  26. return nil, err
  27. }
  28. req = req.WithContext(ctx)
  29. bodyResp := &types.CreateAWSResponse{}
  30. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  31. if httpErr != nil {
  32. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  33. }
  34. return nil, err
  35. }
  36. return bodyResp, nil
  37. }
  38. // CreateGCPIntegration creates a GCP integration with the given request options
  39. func (c *Client) CreateGCPIntegration(
  40. ctx context.Context,
  41. projectID uint,
  42. createGCP *types.CreateGCPRequest,
  43. ) (*types.CreateGCPResponse, error) {
  44. data, err := json.Marshal(createGCP)
  45. if err != nil {
  46. return nil, err
  47. }
  48. req, err := http.NewRequest(
  49. "POST",
  50. fmt.Sprintf("%s/projects/%d/integrations/gcp", c.BaseURL, projectID),
  51. strings.NewReader(string(data)),
  52. )
  53. if err != nil {
  54. return nil, err
  55. }
  56. req = req.WithContext(ctx)
  57. bodyResp := &types.CreateGCPResponse{}
  58. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  59. if httpErr != nil {
  60. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  61. }
  62. return nil, err
  63. }
  64. return bodyResp, nil
  65. }
  66. // CreateBasicAuthIntegration creates a "basic auth" integration
  67. func (c *Client) CreateBasicAuthIntegration(
  68. ctx context.Context,
  69. projectID uint,
  70. createBasic *types.CreateBasicRequest,
  71. ) (*types.CreateBasicResponse, error) {
  72. data, err := json.Marshal(createBasic)
  73. if err != nil {
  74. return nil, err
  75. }
  76. req, err := http.NewRequest(
  77. "POST",
  78. fmt.Sprintf("%s/projects/%d/integrations/basic", c.BaseURL, projectID),
  79. strings.NewReader(string(data)),
  80. )
  81. if err != nil {
  82. return nil, err
  83. }
  84. req = req.WithContext(ctx)
  85. bodyResp := &types.CreateBasicResponse{}
  86. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  87. if httpErr != nil {
  88. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  89. }
  90. return nil, err
  91. }
  92. return bodyResp, nil
  93. }
  94. // ListOAuthIntegrations lists the oauth integrations in a project
  95. func (c *Client) ListOAuthIntegrations(
  96. ctx context.Context,
  97. projectID uint,
  98. ) (types.ListOAuthResponse, error) {
  99. req, err := http.NewRequest(
  100. "GET",
  101. fmt.Sprintf("%s/projects/%d/integrations/oauth", c.BaseURL, projectID),
  102. nil,
  103. )
  104. if err != nil {
  105. return nil, err
  106. }
  107. req = req.WithContext(ctx)
  108. bodyResp := &types.ListOAuthResponse{}
  109. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  110. if httpErr != nil {
  111. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  112. }
  113. return nil, err
  114. }
  115. return *bodyResp, nil
  116. }