integration.go 1.4 KB

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