project.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package api
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "github.com/porter-dev/porter/internal/models"
  9. )
  10. // GetProjectResponse is the response returned after querying for a
  11. // given project
  12. type GetProjectResponse models.ProjectExternal
  13. // GetProject retrieves a project by id
  14. func (c *Client) GetProject(ctx context.Context, projectID uint) (*GetProjectResponse, error) {
  15. req, err := http.NewRequest(
  16. "GET",
  17. fmt.Sprintf("%s/projects/%d", c.BaseURL, projectID),
  18. nil,
  19. )
  20. if err != nil {
  21. return nil, err
  22. }
  23. req = req.WithContext(ctx)
  24. bodyResp := &GetProjectResponse{}
  25. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  26. if httpErr != nil {
  27. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  28. }
  29. return nil, err
  30. }
  31. return bodyResp, nil
  32. }
  33. // CreateProjectRequest represents the accepted fields for creating a project
  34. type CreateProjectRequest struct {
  35. Name string `json:"name" form:"required"`
  36. }
  37. // CreateProjectResponse is the resulting project after creation
  38. type CreateProjectResponse models.ProjectExternal
  39. // CreateProject creates a project with the given request options
  40. func (c *Client) CreateProject(
  41. ctx context.Context,
  42. createProjectRequest *CreateProjectRequest,
  43. ) (*CreateProjectResponse, error) {
  44. data, err := json.Marshal(createProjectRequest)
  45. if err != nil {
  46. return nil, err
  47. }
  48. req, err := http.NewRequest(
  49. "POST",
  50. fmt.Sprintf("%s/projects", c.BaseURL),
  51. strings.NewReader(string(data)),
  52. )
  53. if err != nil {
  54. return nil, err
  55. }
  56. req = req.WithContext(ctx)
  57. bodyResp := &CreateProjectResponse{}
  58. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  59. if httpErr != nil {
  60. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  61. }
  62. return nil, err
  63. }
  64. return bodyResp, nil
  65. }
  66. // CreateProjectCandidatesRequest creates a project service account candidate,
  67. // which can be resolved to create a service account
  68. type CreateProjectCandidatesRequest struct {
  69. Kubeconfig string `json:"kubeconfig"`
  70. }
  71. // CreateProjectCandidatesResponse is the list of candidates returned after
  72. // creating the candidates
  73. type CreateProjectCandidatesResponse []*models.ServiceAccountCandidateExternal
  74. // CreateProjectCandidates creates a service account candidate for a given project,
  75. // accepting a kubeconfig that gets parsed into a candidate
  76. func (c *Client) CreateProjectCandidates(
  77. ctx context.Context,
  78. projectID uint,
  79. createCandidatesRequest *CreateProjectCandidatesRequest,
  80. ) (CreateProjectCandidatesResponse, error) {
  81. data, err := json.Marshal(createCandidatesRequest)
  82. if err != nil {
  83. return nil, err
  84. }
  85. req, err := http.NewRequest(
  86. "POST",
  87. fmt.Sprintf("%s/projects/%d/candidates", c.BaseURL, projectID),
  88. strings.NewReader(string(data)),
  89. )
  90. if err != nil {
  91. return nil, err
  92. }
  93. req = req.WithContext(ctx)
  94. bodyResp := CreateProjectCandidatesResponse{}
  95. if httpErr, err := c.sendRequest(req, &bodyResp, true); httpErr != nil || err != nil {
  96. if httpErr != nil {
  97. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  98. }
  99. return nil, err
  100. }
  101. return bodyResp, nil
  102. }
  103. // GetProjectCandidatesResponse is the list of service account candidates
  104. type GetProjectCandidatesResponse []*models.ServiceAccountCandidateExternal
  105. // GetProjectCandidates returns the service account candidates for a given
  106. // project id
  107. func (c *Client) GetProjectCandidates(
  108. ctx context.Context,
  109. projectID uint,
  110. ) (GetProjectCandidatesResponse, error) {
  111. req, err := http.NewRequest(
  112. "GET",
  113. fmt.Sprintf("%s/projects/%d/candidates", c.BaseURL, projectID),
  114. nil,
  115. )
  116. if err != nil {
  117. return nil, err
  118. }
  119. req = req.WithContext(ctx)
  120. bodyResp := GetProjectCandidatesResponse{}
  121. if httpErr, err := c.sendRequest(req, &bodyResp, true); httpErr != nil || err != nil {
  122. if httpErr != nil {
  123. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  124. }
  125. return nil, err
  126. }
  127. return bodyResp, nil
  128. }
  129. // CreateProjectServiceAccountRequest is a list of service account actions,
  130. // which resolve a given service account
  131. type CreateProjectServiceAccountRequest []*models.ServiceAccountAllActions
  132. // CreateProjectServiceAccountResponse is the service account that gets
  133. // returned after the actions have been resolved
  134. type CreateProjectServiceAccountResponse models.ServiceAccountExternal
  135. // CreateProjectServiceAccount creates a service account given a project id
  136. // and a candidate id, which gets resolved using the list of actions
  137. func (c *Client) CreateProjectServiceAccount(
  138. ctx context.Context,
  139. projectID uint,
  140. candidateID uint,
  141. createSARequest CreateProjectServiceAccountRequest,
  142. ) (*CreateProjectServiceAccountResponse, error) {
  143. data, err := json.Marshal(&createSARequest)
  144. if err != nil {
  145. return nil, err
  146. }
  147. req, err := http.NewRequest(
  148. "POST",
  149. fmt.Sprintf("%s/projects/%d/candidates/%d/resolve", c.BaseURL, projectID, candidateID),
  150. strings.NewReader(string(data)),
  151. )
  152. if err != nil {
  153. return nil, err
  154. }
  155. req = req.WithContext(ctx)
  156. bodyResp := &CreateProjectServiceAccountResponse{}
  157. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  158. if httpErr != nil {
  159. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  160. }
  161. return nil, err
  162. }
  163. return bodyResp, nil
  164. }