integration.go 4.4 KB

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