integration.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. ints "github.com/porter-dev/porter/internal/models/integrations"
  10. )
  11. // CreateAWSIntegration creates an AWS integration with the given request options
  12. func (c *Client) CreateAWSIntegration(
  13. ctx context.Context,
  14. projectID uint,
  15. createAWS *types.CreateAWSRequest,
  16. ) (*types.CreateAWSResponse, error) {
  17. data, err := json.Marshal(createAWS)
  18. if err != nil {
  19. return nil, err
  20. }
  21. req, err := http.NewRequest(
  22. "POST",
  23. fmt.Sprintf("%s/projects/%d/integrations/aws", c.BaseURL, projectID),
  24. strings.NewReader(string(data)),
  25. )
  26. if err != nil {
  27. return nil, err
  28. }
  29. req = req.WithContext(ctx)
  30. bodyResp := &types.CreateAWSResponse{}
  31. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  32. if httpErr != nil {
  33. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  34. }
  35. return nil, err
  36. }
  37. return bodyResp, nil
  38. }
  39. // CreateGCPIntegrationRequest represents the accepted fields for creating
  40. // a gcp integration
  41. type CreateGCPIntegrationRequest struct {
  42. GCPKeyData string `json:"gcp_key_data"`
  43. }
  44. // CreateGCPIntegrationResponse is the resulting integration after creation
  45. type CreateGCPIntegrationResponse ints.GCPIntegrationExternal
  46. // CreateGCPIntegration creates a GCP integration with the given request options
  47. func (c *Client) CreateGCPIntegration(
  48. ctx context.Context,
  49. projectID uint,
  50. createGCP *CreateGCPIntegrationRequest,
  51. ) (*CreateGCPIntegrationResponse, error) {
  52. data, err := json.Marshal(createGCP)
  53. if err != nil {
  54. return nil, err
  55. }
  56. req, err := http.NewRequest(
  57. "POST",
  58. fmt.Sprintf("%s/projects/%d/integrations/gcp", c.BaseURL, projectID),
  59. strings.NewReader(string(data)),
  60. )
  61. if err != nil {
  62. return nil, err
  63. }
  64. req = req.WithContext(ctx)
  65. bodyResp := &CreateGCPIntegrationResponse{}
  66. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  67. if httpErr != nil {
  68. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  69. }
  70. return nil, err
  71. }
  72. return bodyResp, nil
  73. }
  74. // CreateBasicAuthIntegration creates a "basic auth" integration
  75. func (c *Client) CreateBasicAuthIntegration(
  76. ctx context.Context,
  77. projectID uint,
  78. createBasic *types.CreateBasicRequest,
  79. ) (*types.CreateBasicResponse, error) {
  80. data, err := json.Marshal(createBasic)
  81. if err != nil {
  82. return nil, err
  83. }
  84. req, err := http.NewRequest(
  85. "POST",
  86. fmt.Sprintf("%s/projects/%d/integrations/basic", c.BaseURL, projectID),
  87. strings.NewReader(string(data)),
  88. )
  89. if err != nil {
  90. return nil, err
  91. }
  92. req = req.WithContext(ctx)
  93. bodyResp := &types.CreateBasicResponse{}
  94. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  95. if httpErr != nil {
  96. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  97. }
  98. return nil, err
  99. }
  100. return bodyResp, nil
  101. }
  102. // ListOAuthIntegrations lists the oauth integrations in a project
  103. func (c *Client) ListOAuthIntegrations(
  104. ctx context.Context,
  105. projectID uint,
  106. ) (types.ListOAuthResponse, error) {
  107. req, err := http.NewRequest(
  108. "GET",
  109. fmt.Sprintf("%s/projects/%d/integrations/oauth", c.BaseURL, projectID),
  110. nil,
  111. )
  112. if err != nil {
  113. return nil, err
  114. }
  115. req = req.WithContext(ctx)
  116. bodyResp := &types.ListOAuthResponse{}
  117. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  118. if httpErr != nil {
  119. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  120. }
  121. return nil, err
  122. }
  123. return *bodyResp, nil
  124. }