project.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. // GetProjectServiceAccountResponse is the response returned after querying for a
  34. // given project's service account
  35. type GetProjectServiceAccountResponse models.ServiceAccountExternal
  36. // GetProjectServiceAccount retrieves a project's service account by id
  37. func (c *Client) GetProjectServiceAccount(
  38. ctx context.Context,
  39. projectID uint,
  40. saID uint,
  41. ) (*GetProjectServiceAccountResponse, error) {
  42. req, err := http.NewRequest(
  43. "GET",
  44. fmt.Sprintf("%s/projects/%d/serviceAccounts/%d", c.BaseURL, projectID, saID),
  45. nil,
  46. )
  47. if err != nil {
  48. return nil, err
  49. }
  50. req = req.WithContext(ctx)
  51. bodyResp := &GetProjectServiceAccountResponse{}
  52. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  53. if httpErr != nil {
  54. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  55. }
  56. return nil, err
  57. }
  58. return bodyResp, nil
  59. }
  60. // ListProjectClustersResponse lists the linked clusters for a project
  61. type ListProjectClustersResponse []models.ClusterExternal
  62. // ListProjectClusters creates a list of clusters for a given project
  63. func (c *Client) ListProjectClusters(
  64. ctx context.Context,
  65. projectID uint,
  66. ) (ListProjectClustersResponse, error) {
  67. req, err := http.NewRequest(
  68. "GET",
  69. fmt.Sprintf("%s/projects/%d/clusters", c.BaseURL, projectID),
  70. nil,
  71. )
  72. if err != nil {
  73. return nil, err
  74. }
  75. req = req.WithContext(ctx)
  76. bodyResp := ListProjectClustersResponse{}
  77. if httpErr, err := c.sendRequest(req, &bodyResp, true); httpErr != nil || err != nil {
  78. if httpErr != nil {
  79. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  80. }
  81. return nil, err
  82. }
  83. return bodyResp, nil
  84. }
  85. // CreateProjectRequest represents the accepted fields for creating a project
  86. type CreateProjectRequest struct {
  87. Name string `json:"name" form:"required"`
  88. }
  89. // CreateProjectResponse is the resulting project after creation
  90. type CreateProjectResponse models.ProjectExternal
  91. // CreateProject creates a project with the given request options
  92. func (c *Client) CreateProject(
  93. ctx context.Context,
  94. createProjectRequest *CreateProjectRequest,
  95. ) (*CreateProjectResponse, error) {
  96. data, err := json.Marshal(createProjectRequest)
  97. if err != nil {
  98. return nil, err
  99. }
  100. req, err := http.NewRequest(
  101. "POST",
  102. fmt.Sprintf("%s/projects", c.BaseURL),
  103. strings.NewReader(string(data)),
  104. )
  105. if err != nil {
  106. return nil, err
  107. }
  108. req = req.WithContext(ctx)
  109. bodyResp := &CreateProjectResponse{}
  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. // CreateProjectCandidatesRequest creates a project service account candidate,
  119. // which can be resolved to create a service account
  120. type CreateProjectCandidatesRequest struct {
  121. Kubeconfig string `json:"kubeconfig"`
  122. }
  123. // CreateProjectCandidatesResponse is the list of candidates returned after
  124. // creating the candidates
  125. type CreateProjectCandidatesResponse []*models.ServiceAccountCandidateExternal
  126. // CreateProjectCandidates creates a service account candidate for a given project,
  127. // accepting a kubeconfig that gets parsed into a candidate
  128. func (c *Client) CreateProjectCandidates(
  129. ctx context.Context,
  130. projectID uint,
  131. createCandidatesRequest *CreateProjectCandidatesRequest,
  132. ) (CreateProjectCandidatesResponse, error) {
  133. data, err := json.Marshal(createCandidatesRequest)
  134. if err != nil {
  135. return nil, err
  136. }
  137. req, err := http.NewRequest(
  138. "POST",
  139. fmt.Sprintf("%s/projects/%d/candidates", c.BaseURL, projectID),
  140. strings.NewReader(string(data)),
  141. )
  142. if err != nil {
  143. return nil, err
  144. }
  145. req = req.WithContext(ctx)
  146. bodyResp := CreateProjectCandidatesResponse{}
  147. if httpErr, err := c.sendRequest(req, &bodyResp, true); httpErr != nil || err != nil {
  148. if httpErr != nil {
  149. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  150. }
  151. return nil, err
  152. }
  153. return bodyResp, nil
  154. }
  155. // GetProjectCandidatesResponse is the list of service account candidates
  156. type GetProjectCandidatesResponse []*models.ServiceAccountCandidateExternal
  157. // GetProjectCandidates returns the service account candidates for a given
  158. // project id
  159. func (c *Client) GetProjectCandidates(
  160. ctx context.Context,
  161. projectID uint,
  162. ) (GetProjectCandidatesResponse, error) {
  163. req, err := http.NewRequest(
  164. "GET",
  165. fmt.Sprintf("%s/projects/%d/candidates", c.BaseURL, projectID),
  166. nil,
  167. )
  168. if err != nil {
  169. return nil, err
  170. }
  171. req = req.WithContext(ctx)
  172. bodyResp := GetProjectCandidatesResponse{}
  173. if httpErr, err := c.sendRequest(req, &bodyResp, true); httpErr != nil || err != nil {
  174. if httpErr != nil {
  175. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  176. }
  177. return nil, err
  178. }
  179. return bodyResp, nil
  180. }
  181. // CreateProjectServiceAccountRequest is a list of service account actions,
  182. // which resolve a given service account
  183. type CreateProjectServiceAccountRequest []*models.ServiceAccountAllActions
  184. // CreateProjectServiceAccountResponse is the service account that gets
  185. // returned after the actions have been resolved
  186. type CreateProjectServiceAccountResponse models.ServiceAccountExternal
  187. // CreateProjectServiceAccount creates a service account given a project id
  188. // and a candidate id, which gets resolved using the list of actions
  189. func (c *Client) CreateProjectServiceAccount(
  190. ctx context.Context,
  191. projectID uint,
  192. candidateID uint,
  193. createSARequest CreateProjectServiceAccountRequest,
  194. ) (*CreateProjectServiceAccountResponse, error) {
  195. data, err := json.Marshal(&createSARequest)
  196. if err != nil {
  197. return nil, err
  198. }
  199. req, err := http.NewRequest(
  200. "POST",
  201. fmt.Sprintf("%s/projects/%d/candidates/%d/resolve", c.BaseURL, projectID, candidateID),
  202. strings.NewReader(string(data)),
  203. )
  204. if err != nil {
  205. return nil, err
  206. }
  207. req = req.WithContext(ctx)
  208. bodyResp := &CreateProjectServiceAccountResponse{}
  209. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  210. if httpErr != nil {
  211. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  212. }
  213. return nil, err
  214. }
  215. return bodyResp, nil
  216. }
  217. // DeleteProjectResponse is the object returned after project deletion
  218. type DeleteProjectResponse models.ProjectExternal
  219. // DeleteProject deletes a project by id
  220. func (c *Client) DeleteProject(ctx context.Context, projectID uint) (*DeleteProjectResponse, error) {
  221. req, err := http.NewRequest(
  222. "DELETE",
  223. fmt.Sprintf("%s/projects/%d", c.BaseURL, projectID),
  224. nil,
  225. )
  226. if err != nil {
  227. return nil, err
  228. }
  229. req = req.WithContext(ctx)
  230. bodyResp := &DeleteProjectResponse{}
  231. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  232. if httpErr != nil {
  233. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  234. }
  235. return nil, err
  236. }
  237. return bodyResp, nil
  238. }