porter_app.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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/internal/models"
  7. appInternal "github.com/porter-dev/porter/internal/porter_app"
  8. "github.com/porter-dev/porter/api/types"
  9. )
  10. func (c *Client) NewGetPorterApp(
  11. ctx context.Context,
  12. projectID, clusterID uint,
  13. appName string,
  14. ) (*types.PorterApp, error) {
  15. resp := &types.PorterApp{}
  16. err := c.getRequest(
  17. fmt.Sprintf(
  18. "/projects/%d/clusters/%d/applications/%s",
  19. projectID, clusterID, appName,
  20. ),
  21. nil,
  22. resp,
  23. )
  24. return resp, err
  25. }
  26. func (c *Client) NewCreatePorterApp(
  27. ctx context.Context,
  28. projectID, clusterID uint,
  29. appName string,
  30. req *types.CreatePorterAppRequest,
  31. ) (*types.PorterApp, error) {
  32. resp := &types.PorterApp{}
  33. err := c.postRequest(
  34. fmt.Sprintf(
  35. "/projects/%d/clusters/%d/applications/%s",
  36. projectID, clusterID, appName,
  37. ),
  38. req,
  39. resp,
  40. )
  41. return resp, err
  42. }
  43. // 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
  44. func (c *Client) NewCreateOrUpdatePorterAppEvent(
  45. ctx context.Context,
  46. projectID, clusterID uint,
  47. appName string,
  48. req *types.CreateOrUpdatePorterAppEventRequest,
  49. ) (types.PorterAppEvent, error) {
  50. resp := &types.PorterAppEvent{}
  51. err := c.postRequest(
  52. fmt.Sprintf(
  53. "/projects/%d/clusters/%d/applications/%s/events",
  54. projectID, clusterID, appName,
  55. ),
  56. req,
  57. resp,
  58. )
  59. return *resp, err
  60. }
  61. // TODO: remove these functions once they are no longer called (check telemetry)
  62. func (c *Client) GetPorterApp(
  63. ctx context.Context,
  64. projectID, clusterID uint,
  65. stackName string,
  66. ) (*types.PorterApp, error) {
  67. resp := &types.PorterApp{}
  68. err := c.getRequest(
  69. fmt.Sprintf(
  70. "/projects/%d/clusters/%d/stacks/%s",
  71. projectID, clusterID, stackName,
  72. ),
  73. nil,
  74. resp,
  75. )
  76. return resp, err
  77. }
  78. func (c *Client) CreatePorterApp(
  79. ctx context.Context,
  80. projectID, clusterID uint,
  81. name string,
  82. req *types.CreatePorterAppRequest,
  83. ) (*types.PorterApp, error) {
  84. resp := &types.PorterApp{}
  85. err := c.postRequest(
  86. fmt.Sprintf(
  87. "/projects/%d/clusters/%d/stacks/%s",
  88. projectID, clusterID, name,
  89. ),
  90. req,
  91. resp,
  92. )
  93. return resp, err
  94. }
  95. // 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
  96. func (c *Client) CreateOrUpdatePorterAppEvent(
  97. ctx context.Context,
  98. projectID, clusterID uint,
  99. name string,
  100. req *types.CreateOrUpdatePorterAppEventRequest,
  101. ) (types.PorterAppEvent, error) {
  102. resp := &types.PorterAppEvent{}
  103. err := c.postRequest(
  104. fmt.Sprintf(
  105. "/projects/%d/clusters/%d/stacks/%s/events",
  106. projectID, clusterID, name,
  107. ),
  108. req,
  109. resp,
  110. )
  111. return *resp, err
  112. }
  113. // ListEnvGroups (List all Env Groups for a given cluster)
  114. func (c *Client) ListEnvGroups(
  115. ctx context.Context,
  116. projectID, clusterID uint,
  117. ) (types.ListEnvironmentGroupsResponse, error) {
  118. resp := &types.ListEnvironmentGroupsResponse{}
  119. err := c.getRequest(
  120. fmt.Sprintf(
  121. "/projects/%d/clusters/%d/environment-groups",
  122. projectID, clusterID,
  123. ),
  124. nil,
  125. resp,
  126. )
  127. return *resp, err
  128. }
  129. // ParseYAML takes in a base64 encoded porter yaml and returns an app proto
  130. func (c *Client) ParseYAML(
  131. ctx context.Context,
  132. projectID, clusterID uint,
  133. b64Yaml string,
  134. appName string,
  135. ) (*porter_app.ParsePorterYAMLToProtoResponse, error) {
  136. resp := &porter_app.ParsePorterYAMLToProtoResponse{}
  137. req := &porter_app.ParsePorterYAMLToProtoRequest{
  138. B64Yaml: b64Yaml,
  139. AppName: appName,
  140. }
  141. err := c.postRequest(
  142. fmt.Sprintf(
  143. "/projects/%d/clusters/%d/apps/parse",
  144. projectID, clusterID,
  145. ),
  146. req,
  147. resp,
  148. )
  149. return resp, err
  150. }
  151. // GetAppManifests returns the manifests for a given app based on the latest successful app revision
  152. func (c *Client) GetAppManifests(
  153. ctx context.Context,
  154. projectID, clusterID uint,
  155. appName string,
  156. ) (*porter_app.AppManifestsResponse, error) {
  157. resp := &porter_app.AppManifestsResponse{}
  158. err := c.getRequest(
  159. fmt.Sprintf(
  160. "/projects/%d/clusters/%d/apps/%s/manifests",
  161. projectID, clusterID, appName,
  162. ),
  163. nil,
  164. resp,
  165. )
  166. return resp, err
  167. }
  168. // ValidatePorterAppInput is the input struct to ValidatePorterApp
  169. type ValidatePorterAppInput struct {
  170. ProjectID uint
  171. ClusterID uint
  172. AppName string
  173. Base64AppProto string
  174. Base64AppOverrides string
  175. DeploymentTarget string
  176. CommitSHA string
  177. ImageTagOverride string
  178. }
  179. // ValidatePorterApp takes in a base64 encoded app definition that is potentially partial and returns a complete definition
  180. // using any previous app revisions and defaults
  181. func (c *Client) ValidatePorterApp(
  182. ctx context.Context,
  183. inp ValidatePorterAppInput,
  184. ) (*porter_app.ValidatePorterAppResponse, error) {
  185. resp := &porter_app.ValidatePorterAppResponse{}
  186. req := &porter_app.ValidatePorterAppRequest{
  187. AppName: inp.AppName,
  188. Base64AppProto: inp.Base64AppProto,
  189. Base64AppOverrides: inp.Base64AppOverrides,
  190. DeploymentTargetId: inp.DeploymentTarget,
  191. CommitSHA: inp.CommitSHA,
  192. ImageTagOverride: inp.ImageTagOverride,
  193. }
  194. err := c.postRequest(
  195. fmt.Sprintf(
  196. "/projects/%d/clusters/%d/apps/validate",
  197. inp.ProjectID, inp.ClusterID,
  198. ),
  199. req,
  200. resp,
  201. )
  202. return resp, err
  203. }
  204. // ApplyPorterAppInput is the input struct to ApplyPorterApp
  205. type ApplyPorterAppInput struct {
  206. ProjectID uint
  207. ClusterID uint
  208. Base64AppProto string
  209. DeploymentTarget string
  210. AppRevisionID string
  211. ForceBuild bool
  212. Variables map[string]string
  213. Secrets map[string]string
  214. HardEnvUpdate bool
  215. }
  216. // ApplyPorterApp takes in a base64 encoded app definition and applies it to the cluster
  217. func (c *Client) ApplyPorterApp(
  218. ctx context.Context,
  219. inp ApplyPorterAppInput,
  220. ) (*porter_app.ApplyPorterAppResponse, error) {
  221. resp := &porter_app.ApplyPorterAppResponse{}
  222. req := &porter_app.ApplyPorterAppRequest{
  223. Base64AppProto: inp.Base64AppProto,
  224. DeploymentTargetId: inp.DeploymentTarget,
  225. AppRevisionID: inp.AppRevisionID,
  226. ForceBuild: inp.ForceBuild,
  227. Variables: inp.Variables,
  228. Secrets: inp.Secrets,
  229. HardEnvUpdate: inp.HardEnvUpdate,
  230. }
  231. err := c.postRequest(
  232. fmt.Sprintf(
  233. "/projects/%d/clusters/%d/apps/apply",
  234. inp.ProjectID, inp.ClusterID,
  235. ),
  236. req,
  237. resp,
  238. postRequestOpts{
  239. retryCount: 3,
  240. onlyRetry500: true,
  241. },
  242. )
  243. return resp, err
  244. }
  245. // UpdateAppInput is the input struct to UpdateApp
  246. type UpdateAppInput struct {
  247. ProjectID uint
  248. ClusterID uint
  249. Name string
  250. ImageTagOverride string
  251. GitSource porter_app.GitSource
  252. DeploymentTargetId string
  253. CommitSHA string
  254. AppRevisionID string
  255. Base64AppProto string
  256. Base64PorterYAML string
  257. IsEnvOverride bool
  258. }
  259. // UpdateApp updates a porter app
  260. func (c *Client) UpdateApp(
  261. ctx context.Context,
  262. inp UpdateAppInput,
  263. ) (*porter_app.UpdateAppResponse, error) {
  264. resp := &porter_app.UpdateAppResponse{}
  265. req := &porter_app.UpdateAppRequest{
  266. Name: inp.Name,
  267. GitSource: inp.GitSource,
  268. DeploymentTargetId: inp.DeploymentTargetId,
  269. CommitSHA: inp.CommitSHA,
  270. ImageTagOverride: inp.ImageTagOverride,
  271. AppRevisionID: inp.AppRevisionID,
  272. Base64AppProto: inp.Base64AppProto,
  273. Base64PorterYAML: inp.Base64PorterYAML,
  274. IsEnvOverride: inp.IsEnvOverride,
  275. }
  276. err := c.postRequest(
  277. fmt.Sprintf(
  278. "/projects/%d/clusters/%d/apps/update",
  279. inp.ProjectID, inp.ClusterID,
  280. ),
  281. req,
  282. resp,
  283. )
  284. return resp, err
  285. }
  286. // DefaultDeploymentTarget returns the default deployment target for a given project and cluster
  287. func (c *Client) DefaultDeploymentTarget(
  288. ctx context.Context,
  289. projectID, clusterID uint,
  290. ) (*porter_app.DefaultDeploymentTargetResponse, error) {
  291. resp := &porter_app.DefaultDeploymentTargetResponse{}
  292. req := &porter_app.DefaultDeploymentTargetRequest{}
  293. err := c.getRequest(
  294. fmt.Sprintf(
  295. "/projects/%d/clusters/%d/default-deployment-target",
  296. projectID, clusterID,
  297. ),
  298. req,
  299. resp,
  300. )
  301. return resp, err
  302. }
  303. // CurrentAppRevision returns the currently deployed app revision for a given project, app name and deployment target
  304. func (c *Client) CurrentAppRevision(
  305. ctx context.Context,
  306. projectID uint, clusterID uint,
  307. appName string, deploymentTarget string,
  308. ) (*porter_app.LatestAppRevisionResponse, error) {
  309. resp := &porter_app.LatestAppRevisionResponse{}
  310. req := &porter_app.LatestAppRevisionRequest{
  311. DeploymentTargetID: deploymentTarget,
  312. }
  313. err := c.getRequest(
  314. fmt.Sprintf(
  315. "/projects/%d/clusters/%d/apps/%s/latest",
  316. projectID, clusterID, appName,
  317. ),
  318. req,
  319. resp,
  320. )
  321. return resp, err
  322. }
  323. // CreatePorterAppDBEntryInput is the input struct to CreatePorterAppDBEntry
  324. type CreatePorterAppDBEntryInput struct {
  325. AppName string
  326. GitRepoName string
  327. GitRepoID uint
  328. GitBranch string
  329. ImageRepository string
  330. PorterYamlPath string
  331. ImageTag string
  332. Local bool
  333. DeploymentTargetID string
  334. }
  335. // CreatePorterAppDBEntry creates an entry in the porter app
  336. func (c *Client) CreatePorterAppDBEntry(
  337. ctx context.Context,
  338. projectID uint, clusterID uint,
  339. inp CreatePorterAppDBEntryInput,
  340. ) error {
  341. var sourceType appInternal.SourceType
  342. var image *appInternal.Image
  343. if inp.Local {
  344. sourceType = appInternal.SourceType_Local
  345. }
  346. if inp.GitRepoName != "" {
  347. sourceType = appInternal.SourceType_Github
  348. }
  349. if inp.ImageRepository != "" {
  350. sourceType = appInternal.SourceType_DockerRegistry
  351. image = &appInternal.Image{
  352. Repository: inp.ImageRepository,
  353. Tag: inp.ImageTag,
  354. }
  355. }
  356. req := &porter_app.CreateAppRequest{
  357. Name: inp.AppName,
  358. SourceType: sourceType,
  359. GitSource: porter_app.GitSource{
  360. GitBranch: inp.GitBranch,
  361. GitRepoName: inp.GitRepoName,
  362. GitRepoID: inp.GitRepoID,
  363. },
  364. Image: image,
  365. PorterYamlPath: inp.PorterYamlPath,
  366. DeploymentTargetID: inp.DeploymentTargetID,
  367. }
  368. err := c.postRequest(
  369. fmt.Sprintf(
  370. "/projects/%d/clusters/%d/apps/create",
  371. projectID, clusterID,
  372. ),
  373. req,
  374. &types.PorterApp{},
  375. )
  376. return err
  377. }
  378. // CreateSubdomain returns a subdomain for a given service that point to the ingress-nginx service in the cluster
  379. func (c *Client) CreateSubdomain(
  380. ctx context.Context,
  381. projectID uint, clusterID uint,
  382. appName string, serviceName string,
  383. ) (*porter_app.CreateSubdomainResponse, error) {
  384. resp := &porter_app.CreateSubdomainResponse{}
  385. req := &porter_app.CreateSubdomainRequest{
  386. ServiceName: serviceName,
  387. }
  388. err := c.postRequest(
  389. fmt.Sprintf(
  390. "/projects/%d/clusters/%d/apps/%s/subdomain",
  391. projectID, clusterID, appName,
  392. ),
  393. req,
  394. resp,
  395. )
  396. return resp, err
  397. }
  398. // PredeployStatus checks the current status of a predeploy job for an app revision
  399. func (c *Client) PredeployStatus(
  400. ctx context.Context,
  401. projectID uint, clusterID uint,
  402. appName string, appRevisionId string,
  403. ) (*porter_app.PredeployStatusResponse, error) {
  404. resp := &porter_app.PredeployStatusResponse{}
  405. err := c.getRequest(
  406. fmt.Sprintf(
  407. "/projects/%d/clusters/%d/apps/%s/%s/predeploy-status",
  408. projectID, clusterID, appName, appRevisionId,
  409. ),
  410. nil,
  411. resp,
  412. )
  413. if resp.Status == "" {
  414. return nil, fmt.Errorf("no predeploy status found")
  415. }
  416. return resp, err
  417. }
  418. // GetRevision returns an app revision
  419. func (c *Client) GetRevision(
  420. ctx context.Context,
  421. projectID uint, clusterID uint,
  422. appName string, appRevisionId string,
  423. ) (*porter_app.GetAppRevisionResponse, error) {
  424. resp := &porter_app.GetAppRevisionResponse{}
  425. err := c.getRequest(
  426. fmt.Sprintf(
  427. "/projects/%d/clusters/%d/apps/%s/revisions/%s",
  428. projectID, clusterID, appName, appRevisionId,
  429. ),
  430. nil,
  431. resp,
  432. )
  433. return resp, err
  434. }
  435. // GetRevisionStatus returns the status of an app revision
  436. func (c *Client) GetRevisionStatus(
  437. ctx context.Context,
  438. projectID uint, clusterID uint,
  439. appName string, appRevisionId string,
  440. ) (*porter_app.GetAppRevisionStatusResponse, error) {
  441. resp := &porter_app.GetAppRevisionStatusResponse{}
  442. err := c.getRequest(
  443. fmt.Sprintf(
  444. "/projects/%d/clusters/%d/apps/%s/revisions/%s/status",
  445. projectID, clusterID, appName, appRevisionId,
  446. ),
  447. nil,
  448. resp,
  449. )
  450. return resp, err
  451. }
  452. // UpdateRevisionStatus updates the status of an app revision
  453. func (c *Client) UpdateRevisionStatus(
  454. ctx context.Context,
  455. projectID uint, clusterID uint,
  456. appName string, appRevisionId string,
  457. status models.AppRevisionStatus,
  458. ) (*porter_app.UpdateAppRevisionStatusResponse, error) {
  459. resp := &porter_app.UpdateAppRevisionStatusResponse{}
  460. req := &porter_app.UpdateAppRevisionStatusRequest{
  461. Status: status,
  462. }
  463. err := c.postRequest(
  464. fmt.Sprintf(
  465. "/projects/%d/clusters/%d/apps/%s/revisions/%s",
  466. projectID, clusterID, appName, appRevisionId,
  467. ),
  468. req,
  469. resp,
  470. )
  471. return resp, err
  472. }
  473. // GetBuildEnv returns the build environment for a given app proto
  474. func (c *Client) GetBuildEnv(
  475. ctx context.Context,
  476. projectID uint, clusterID uint,
  477. appName string, appRevisionId string,
  478. ) (*porter_app.GetBuildEnvResponse, error) {
  479. resp := &porter_app.GetBuildEnvResponse{}
  480. err := c.getRequest(
  481. fmt.Sprintf(
  482. "/projects/%d/clusters/%d/apps/%s/revisions/%s/build-env",
  483. projectID, clusterID, appName, appRevisionId,
  484. ),
  485. nil,
  486. resp,
  487. )
  488. return resp, err
  489. }
  490. // GetAppEnvVariables returns all env variables for a given app
  491. func (c *Client) GetAppEnvVariables(
  492. ctx context.Context,
  493. projectID uint, clusterID uint,
  494. appName string,
  495. ) (*porter_app.AppEnvVariablesResponse, error) {
  496. resp := &porter_app.AppEnvVariablesResponse{}
  497. req := &porter_app.AppEnvVariablesRequest{}
  498. err := c.getRequest(
  499. fmt.Sprintf(
  500. "/projects/%d/clusters/%d/apps/%s/env-variables",
  501. projectID, clusterID, appName,
  502. ),
  503. req,
  504. resp,
  505. )
  506. return resp, err
  507. }
  508. // GetBuildFromRevision returns the build environment for a given app proto
  509. func (c *Client) GetBuildFromRevision(
  510. ctx context.Context,
  511. projectID uint, clusterID uint,
  512. appName string, appRevisionId string,
  513. ) (*porter_app.GetBuildFromRevisionResponse, error) {
  514. resp := &porter_app.GetBuildFromRevisionResponse{}
  515. err := c.getRequest(
  516. fmt.Sprintf(
  517. "/projects/%d/clusters/%d/apps/%s/revisions/%s/build",
  518. projectID, clusterID, appName, appRevisionId,
  519. ),
  520. nil,
  521. resp,
  522. )
  523. return resp, err
  524. }
  525. // ReportRevisionStatusInput is the input struct to ReportRevisionStatus
  526. type ReportRevisionStatusInput struct {
  527. ProjectID uint
  528. ClusterID uint
  529. AppName string
  530. AppRevisionID string
  531. PRNumber int
  532. CommitSHA string
  533. }
  534. // ReportRevisionStatus reports the status of an app revision to external services
  535. func (c *Client) ReportRevisionStatus(
  536. ctx context.Context,
  537. inp ReportRevisionStatusInput,
  538. ) (*porter_app.ReportRevisionStatusResponse, error) {
  539. resp := &porter_app.ReportRevisionStatusResponse{}
  540. req := &porter_app.ReportRevisionStatusRequest{
  541. PRNumber: inp.PRNumber,
  542. CommitSHA: inp.CommitSHA,
  543. }
  544. err := c.postRequest(
  545. fmt.Sprintf(
  546. "/projects/%d/clusters/%d/apps/%s/revisions/%s/status",
  547. inp.ProjectID, inp.ClusterID, inp.AppName, inp.AppRevisionID,
  548. ),
  549. req,
  550. resp,
  551. )
  552. return resp, err
  553. }
  554. // CreateOrUpdateAppEnvironment updates the app environment group and creates it if it doesn't exist
  555. func (c *Client) CreateOrUpdateAppEnvironment(
  556. ctx context.Context,
  557. projectID uint, clusterID uint,
  558. appName string,
  559. deploymentTargetID string,
  560. variables map[string]string,
  561. secrets map[string]string,
  562. Base64AppProto string,
  563. ) (*porter_app.UpdateAppEnvironmentResponse, error) {
  564. resp := &porter_app.UpdateAppEnvironmentResponse{}
  565. req := &porter_app.UpdateAppEnvironmentRequest{
  566. DeploymentTargetID: deploymentTargetID,
  567. Variables: variables,
  568. Secrets: secrets,
  569. HardUpdate: false,
  570. Base64AppProto: Base64AppProto,
  571. }
  572. err := c.postRequest(
  573. fmt.Sprintf(
  574. "/projects/%d/clusters/%d/apps/%s/update-environment",
  575. projectID, clusterID, appName,
  576. ),
  577. req,
  578. resp,
  579. )
  580. return resp, err
  581. }
  582. // PorterYamlV2Pods gets all pods for a given deployment target id and app name
  583. func (c *Client) PorterYamlV2Pods(
  584. ctx context.Context,
  585. projectID, clusterID uint,
  586. porterAppName string,
  587. deploymentTargetName string,
  588. ) (*types.GetReleaseAllPodsResponse, error) {
  589. req := &porter_app.PodStatusRequest{
  590. DeploymentTargetName: deploymentTargetName,
  591. }
  592. resp := &types.GetReleaseAllPodsResponse{}
  593. err := c.getRequest(
  594. fmt.Sprintf(
  595. "/projects/%d/clusters/%d/apps/%s/pods",
  596. projectID, clusterID,
  597. porterAppName,
  598. ),
  599. req,
  600. resp,
  601. )
  602. return resp, err
  603. }
  604. // UpdateImage updates the image for a porter app (porter yaml v2 only)
  605. func (c *Client) UpdateImage(
  606. ctx context.Context,
  607. projectID, clusterID uint,
  608. appName, deploymentTargetName, tag string,
  609. ) (*porter_app.UpdateImageResponse, error) {
  610. req := &porter_app.UpdateImageRequest{
  611. Tag: tag,
  612. DeploymentTargetName: deploymentTargetName,
  613. }
  614. resp := &porter_app.UpdateImageResponse{}
  615. err := c.postRequest(
  616. fmt.Sprintf(
  617. "/projects/%d/clusters/%d/apps/%s/update-image",
  618. projectID, clusterID, appName,
  619. ),
  620. &req,
  621. resp,
  622. )
  623. return resp, err
  624. }
  625. // ListAppRevisions lists the last ten app revisions for a given app
  626. func (c *Client) ListAppRevisions(
  627. ctx context.Context,
  628. projectID, clusterID uint,
  629. appName string,
  630. deploymentTargetID string,
  631. ) (*porter_app.ListAppRevisionsResponse, error) {
  632. resp := &porter_app.ListAppRevisionsResponse{}
  633. req := &porter_app.ListAppRevisionsRequest{
  634. DeploymentTargetID: deploymentTargetID,
  635. }
  636. err := c.getRequest(
  637. fmt.Sprintf(
  638. "/projects/%d/clusters/%d/apps/%s/revisions",
  639. projectID, clusterID,
  640. appName,
  641. ),
  642. req,
  643. resp,
  644. )
  645. return resp, err
  646. }
  647. // RollbackRevision reverts an app to a previous revision
  648. func (c *Client) RollbackRevision(
  649. ctx context.Context,
  650. projectID, clusterID uint,
  651. appName string,
  652. deploymentTargetID string,
  653. ) (*porter_app.RollbackAppRevisionResponse, error) {
  654. resp := &porter_app.RollbackAppRevisionResponse{}
  655. req := &porter_app.RollbackAppRevisionRequest{
  656. DeploymentTargetID: deploymentTargetID,
  657. }
  658. err := c.postRequest(
  659. fmt.Sprintf(
  660. "/projects/%d/clusters/%d/apps/%s/rollback",
  661. projectID, clusterID,
  662. appName,
  663. ),
  664. req,
  665. resp,
  666. )
  667. return resp, err
  668. }
  669. // UseNewApplyLogic checks whether the CLI should use the new apply logic
  670. func (c *Client) UseNewApplyLogic(
  671. ctx context.Context,
  672. projectID, clusterID uint,
  673. ) (*porter_app.UseNewApplyLogicResponse, error) {
  674. resp := &porter_app.UseNewApplyLogicResponse{}
  675. req := &porter_app.UseNewApplyLogicRequest{}
  676. err := c.getRequest(
  677. fmt.Sprintf(
  678. "/projects/%d/clusters/%d/apps/use-new-apply-logic",
  679. projectID, clusterID,
  680. ),
  681. req,
  682. resp,
  683. )
  684. return resp, err
  685. }
  686. // RunAppJob runs a job for an app
  687. func (c *Client) RunAppJob(
  688. ctx context.Context,
  689. projectID, clusterID uint,
  690. appName string, jobName string,
  691. deploymentTargetID string,
  692. ) (*porter_app.RunAppJobResponse, error) {
  693. resp := &porter_app.RunAppJobResponse{}
  694. req := &porter_app.RunAppJobRequest{
  695. ServiceName: jobName,
  696. DeploymentTargetID: deploymentTargetID,
  697. }
  698. err := c.postRequest(
  699. fmt.Sprintf(
  700. "/projects/%d/clusters/%d/apps/%s/run",
  701. projectID, clusterID,
  702. appName,
  703. ),
  704. req,
  705. resp,
  706. )
  707. return resp, err
  708. }
  709. // RunAppJobStatusInput contains all the information necessary to check the status of a job
  710. type RunAppJobStatusInput struct {
  711. // AppName is the name of the app associated with the job
  712. AppName string
  713. // Cluster is the id of the cluster against which to retrieve a helm agent for
  714. ClusterID uint
  715. // DeploymentTargetID is the id of the deployment target the job was run against
  716. DeploymentTargetID string
  717. // DeploymentTargetNamespace is the namespace in which the job was deployed
  718. DeploymentTargetNamespace string
  719. // ServiceName is the name of the app service that was triggered
  720. ServiceName string
  721. // JobRunID is the UID returned from the /apps/{porter_app_name}/run endpoint
  722. JobRunID string
  723. // ProjectID is the project in which the cluster exists
  724. ProjectID uint
  725. }
  726. // RunAppJobStatus gets the status for a job app run
  727. func (c *Client) RunAppJobStatus(
  728. ctx context.Context,
  729. input RunAppJobStatusInput,
  730. ) (*porter_app.AppJobRunStatusResponse, error) {
  731. resp := &porter_app.AppJobRunStatusResponse{}
  732. req := &porter_app.AppJobRunStatusRequest{
  733. DeploymentTargetID: input.DeploymentTargetID,
  734. JobRunID: input.JobRunID,
  735. Namespace: input.DeploymentTargetNamespace,
  736. ServiceName: input.ServiceName,
  737. }
  738. err := c.getRequest(
  739. fmt.Sprintf(
  740. "/projects/%d/clusters/%d/apps/%s/run-status",
  741. input.ProjectID, input.ClusterID,
  742. input.AppName,
  743. ),
  744. req,
  745. resp,
  746. )
  747. return resp, err
  748. }