2
0

integration.go 4.5 KB

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