porter_app.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package client
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/porter-dev/porter/api/server/handlers/porter_app"
  6. "github.com/porter-dev/porter/api/types"
  7. )
  8. func (c *Client) NewGetPorterApp(
  9. ctx context.Context,
  10. projectID, clusterID uint,
  11. appName string,
  12. ) (*types.PorterApp, error) {
  13. resp := &types.PorterApp{}
  14. err := c.getRequest(
  15. fmt.Sprintf(
  16. "/projects/%d/clusters/%d/applications/%s",
  17. projectID, clusterID, appName,
  18. ),
  19. nil,
  20. resp,
  21. )
  22. return resp, err
  23. }
  24. func (c *Client) NewCreatePorterApp(
  25. ctx context.Context,
  26. projectID, clusterID uint,
  27. appName string,
  28. req *types.CreatePorterAppRequest,
  29. ) (*types.PorterApp, error) {
  30. resp := &types.PorterApp{}
  31. err := c.postRequest(
  32. fmt.Sprintf(
  33. "/projects/%d/clusters/%d/applications/%s",
  34. projectID, clusterID, appName,
  35. ),
  36. req,
  37. resp,
  38. )
  39. return resp, err
  40. }
  41. // NewCreateOrUpdatePorterAppEvent will create a porter app event if one does not exist, or else it will update the existing one if an ID is passed in the object
  42. func (c *Client) NewCreateOrUpdatePorterAppEvent(
  43. ctx context.Context,
  44. projectID, clusterID uint,
  45. appName string,
  46. req *types.CreateOrUpdatePorterAppEventRequest,
  47. ) (types.PorterAppEvent, error) {
  48. resp := &types.PorterAppEvent{}
  49. err := c.postRequest(
  50. fmt.Sprintf(
  51. "/projects/%d/clusters/%d/applications/%s/events",
  52. projectID, clusterID, appName,
  53. ),
  54. req,
  55. resp,
  56. )
  57. return *resp, err
  58. }
  59. // TODO: remove these functions once they are no longer called (check telemetry)
  60. func (c *Client) GetPorterApp(
  61. ctx context.Context,
  62. projectID, clusterID uint,
  63. stackName string,
  64. ) (*types.PorterApp, error) {
  65. resp := &types.PorterApp{}
  66. err := c.getRequest(
  67. fmt.Sprintf(
  68. "/projects/%d/clusters/%d/stacks/%s",
  69. projectID, clusterID, stackName,
  70. ),
  71. nil,
  72. resp,
  73. )
  74. return resp, err
  75. }
  76. func (c *Client) CreatePorterApp(
  77. ctx context.Context,
  78. projectID, clusterID uint,
  79. name string,
  80. req *types.CreatePorterAppRequest,
  81. ) (*types.PorterApp, error) {
  82. resp := &types.PorterApp{}
  83. err := c.postRequest(
  84. fmt.Sprintf(
  85. "/projects/%d/clusters/%d/stacks/%s",
  86. projectID, clusterID, name,
  87. ),
  88. req,
  89. resp,
  90. )
  91. return resp, err
  92. }
  93. // CreateOrUpdatePorterAppEvent will create a porter app event if one does not exist, or else it will update the existing one if an ID is passed in the object
  94. func (c *Client) CreateOrUpdatePorterAppEvent(
  95. ctx context.Context,
  96. projectID, clusterID uint,
  97. name string,
  98. req *types.CreateOrUpdatePorterAppEventRequest,
  99. ) (types.PorterAppEvent, error) {
  100. resp := &types.PorterAppEvent{}
  101. err := c.postRequest(
  102. fmt.Sprintf(
  103. "/projects/%d/clusters/%d/stacks/%s/events",
  104. projectID, clusterID, name,
  105. ),
  106. req,
  107. resp,
  108. )
  109. return *resp, err
  110. }
  111. // ListEnvGroups (List all Env Groups for a given cluster)
  112. func (c *Client) ListEnvGroups(
  113. ctx context.Context,
  114. projectID, clusterID uint,
  115. ) (types.ListEnvironmentGroupsResponse, error) {
  116. resp := &types.ListEnvironmentGroupsResponse{}
  117. err := c.getRequest(
  118. fmt.Sprintf(
  119. "/projects/%d/clusters/%d/environment-groups",
  120. projectID, clusterID,
  121. ),
  122. nil,
  123. resp,
  124. )
  125. return *resp, err
  126. }
  127. // ParseYAML takes in a base64 encoded porter yaml and returns an app proto
  128. func (c *Client) ParseYAML(
  129. ctx context.Context,
  130. projectID, clusterID uint,
  131. b64Yaml string,
  132. ) (*porter_app.ParsePorterYAMLToProtoResponse, error) {
  133. resp := &porter_app.ParsePorterYAMLToProtoResponse{}
  134. req := &porter_app.ParsePorterYAMLToProtoRequest{
  135. B64Yaml: b64Yaml,
  136. }
  137. err := c.postRequest(
  138. fmt.Sprintf(
  139. "/projects/%d/clusters/%d/apps/parse",
  140. projectID, clusterID,
  141. ),
  142. req,
  143. resp,
  144. )
  145. return resp, err
  146. }
  147. // ValidatePorterApp takes in a base64 encoded app definition that is potentially partial and returns a complete definition
  148. // using any previous app revisions and defaults
  149. func (c *Client) ValidatePorterApp(
  150. ctx context.Context,
  151. projectID, clusterID uint,
  152. base64AppProto string,
  153. deploymentTarget string,
  154. ) (*porter_app.ValidatePorterAppResponse, error) {
  155. resp := &porter_app.ValidatePorterAppResponse{}
  156. req := &porter_app.ValidatePorterAppRequest{
  157. Base64AppProto: base64AppProto,
  158. DeploymentTargetId: deploymentTarget,
  159. }
  160. err := c.postRequest(
  161. fmt.Sprintf(
  162. "/projects/%d/clusters/%d/apps/validate",
  163. projectID, clusterID,
  164. ),
  165. req,
  166. resp,
  167. )
  168. return resp, err
  169. }
  170. // ApplyPorterApp takes in a base64 encoded app definition and applies it to the cluster
  171. func (c *Client) ApplyPorterApp(
  172. ctx context.Context,
  173. projectID, clusterID uint,
  174. base64AppProto string,
  175. deploymentTarget string,
  176. appRevisionID string,
  177. ) (*porter_app.ApplyPorterAppResponse, error) {
  178. resp := &porter_app.ApplyPorterAppResponse{}
  179. req := &porter_app.ApplyPorterAppRequest{
  180. Base64AppProto: base64AppProto,
  181. DeploymentTargetId: deploymentTarget,
  182. AppRevisionID: appRevisionID,
  183. }
  184. err := c.postRequest(
  185. fmt.Sprintf(
  186. "/projects/%d/clusters/%d/apps/apply",
  187. projectID, clusterID,
  188. ),
  189. req,
  190. resp,
  191. )
  192. return resp, err
  193. }
  194. // DefaultDeploymentTarget returns the default deployment target for a given project and cluster
  195. func (c *Client) DefaultDeploymentTarget(
  196. ctx context.Context,
  197. projectID, clusterID uint,
  198. ) (*porter_app.DefaultDeploymentTargetResponse, error) {
  199. resp := &porter_app.DefaultDeploymentTargetResponse{}
  200. req := &porter_app.DefaultDeploymentTargetRequest{}
  201. err := c.getRequest(
  202. fmt.Sprintf(
  203. "/projects/%d/clusters/%d/default-deployment-target",
  204. projectID, clusterID,
  205. ),
  206. req,
  207. resp,
  208. )
  209. return resp, err
  210. }
  211. // CurrentAppRevision returns the currently deployed app revision for a given project, app name and deployment target
  212. func (c *Client) CurrentAppRevision(
  213. ctx context.Context,
  214. projectID uint, clusterID uint,
  215. appName string, deploymentTarget string,
  216. ) (*porter_app.LatestAppRevisionResponse, error) {
  217. resp := &porter_app.LatestAppRevisionResponse{}
  218. req := &porter_app.LatestAppRevisionRequest{
  219. DeploymentTargetID: deploymentTarget,
  220. }
  221. err := c.getRequest(
  222. fmt.Sprintf(
  223. "/projects/%d/clusters/%d/apps/%s/latest",
  224. projectID, clusterID, appName,
  225. ),
  226. req,
  227. resp,
  228. )
  229. return resp, err
  230. }