project.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. IsLocal bool `json:"is_local"`
  123. }
  124. // CreateProjectCandidatesResponse is the list of candidates returned after
  125. // creating the candidates
  126. type CreateProjectCandidatesResponse []*models.ServiceAccountCandidateExternal
  127. // CreateProjectCandidates creates a service account candidate for a given project,
  128. // accepting a kubeconfig that gets parsed into a candidate
  129. func (c *Client) CreateProjectCandidates(
  130. ctx context.Context,
  131. projectID uint,
  132. createCandidatesRequest *CreateProjectCandidatesRequest,
  133. ) (CreateProjectCandidatesResponse, error) {
  134. data, err := json.Marshal(createCandidatesRequest)
  135. if err != nil {
  136. return nil, err
  137. }
  138. req, err := http.NewRequest(
  139. "POST",
  140. fmt.Sprintf("%s/projects/%d/candidates", c.BaseURL, projectID),
  141. strings.NewReader(string(data)),
  142. )
  143. if err != nil {
  144. return nil, err
  145. }
  146. req = req.WithContext(ctx)
  147. bodyResp := CreateProjectCandidatesResponse{}
  148. if httpErr, err := c.sendRequest(req, &bodyResp, true); httpErr != nil || err != nil {
  149. if httpErr != nil {
  150. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  151. }
  152. return nil, err
  153. }
  154. return bodyResp, nil
  155. }
  156. // GetProjectCandidatesResponse is the list of service account candidates
  157. type GetProjectCandidatesResponse []*models.ServiceAccountCandidateExternal
  158. // GetProjectCandidates returns the service account candidates for a given
  159. // project id
  160. func (c *Client) GetProjectCandidates(
  161. ctx context.Context,
  162. projectID uint,
  163. ) (GetProjectCandidatesResponse, error) {
  164. req, err := http.NewRequest(
  165. "GET",
  166. fmt.Sprintf("%s/projects/%d/candidates", c.BaseURL, projectID),
  167. nil,
  168. )
  169. if err != nil {
  170. return nil, err
  171. }
  172. req = req.WithContext(ctx)
  173. bodyResp := GetProjectCandidatesResponse{}
  174. if httpErr, err := c.sendRequest(req, &bodyResp, true); httpErr != nil || err != nil {
  175. if httpErr != nil {
  176. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  177. }
  178. return nil, err
  179. }
  180. return bodyResp, nil
  181. }
  182. // CreateProjectServiceAccountRequest is a list of service account actions,
  183. // which resolve a given service account
  184. type CreateProjectServiceAccountRequest []*models.ServiceAccountAllActions
  185. // CreateProjectServiceAccountResponse is the service account that gets
  186. // returned after the actions have been resolved
  187. type CreateProjectServiceAccountResponse models.ServiceAccountExternal
  188. // CreateProjectServiceAccount creates a service account given a project id
  189. // and a candidate id, which gets resolved using the list of actions
  190. func (c *Client) CreateProjectServiceAccount(
  191. ctx context.Context,
  192. projectID uint,
  193. candidateID uint,
  194. createSARequest CreateProjectServiceAccountRequest,
  195. ) (*CreateProjectServiceAccountResponse, error) {
  196. data, err := json.Marshal(&createSARequest)
  197. if err != nil {
  198. return nil, err
  199. }
  200. req, err := http.NewRequest(
  201. "POST",
  202. fmt.Sprintf("%s/projects/%d/candidates/%d/resolve", c.BaseURL, projectID, candidateID),
  203. strings.NewReader(string(data)),
  204. )
  205. if err != nil {
  206. return nil, err
  207. }
  208. req = req.WithContext(ctx)
  209. bodyResp := &CreateProjectServiceAccountResponse{}
  210. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  211. if httpErr != nil {
  212. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  213. }
  214. return nil, err
  215. }
  216. return bodyResp, nil
  217. }
  218. // DeleteProjectResponse is the object returned after project deletion
  219. type DeleteProjectResponse models.ProjectExternal
  220. // DeleteProject deletes a project by id
  221. func (c *Client) DeleteProject(ctx context.Context, projectID uint) (*DeleteProjectResponse, error) {
  222. req, err := http.NewRequest(
  223. "DELETE",
  224. fmt.Sprintf("%s/projects/%d", c.BaseURL, projectID),
  225. nil,
  226. )
  227. if err != nil {
  228. return nil, err
  229. }
  230. req = req.WithContext(ctx)
  231. bodyResp := &DeleteProjectResponse{}
  232. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  233. if httpErr != nil {
  234. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  235. }
  236. return nil, err
  237. }
  238. return bodyResp, nil
  239. }