integration.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. }