porter_app.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. // CurrentAppRevisionInput is the input struct to CurrentAppRevision
  304. type CurrentAppRevisionInput struct {
  305. ProjectID uint
  306. ClusterID uint
  307. AppName string
  308. // DeploymentTargetName is the name of the deployment target to get the current app revision for. One of this or DeploymentTargetID must be set.
  309. DeploymentTargetName string
  310. // DeploymentTargetID is the id of the deployment target to get the current app revision for. One of this or DeploymentTargetName must be set.
  311. DeploymentTargetID string
  312. }
  313. // CurrentAppRevision returns the currently deployed app revision for a given project, app name and deployment target
  314. func (c *Client) CurrentAppRevision(
  315. ctx context.Context,
  316. input CurrentAppRevisionInput,
  317. ) (*porter_app.LatestAppRevisionResponse, error) {
  318. resp := &porter_app.LatestAppRevisionResponse{}
  319. req := &porter_app.LatestAppRevisionRequest{
  320. DeploymentTargetName: input.DeploymentTargetName,
  321. DeploymentTargetID: input.DeploymentTargetID,
  322. }
  323. err := c.getRequest(
  324. fmt.Sprintf(
  325. "/projects/%d/clusters/%d/apps/%s/latest",
  326. input.ProjectID, input.ClusterID, input.AppName,
  327. ),
  328. req,
  329. resp,
  330. )
  331. return resp, err
  332. }
  333. // CreatePorterAppDBEntryInput is the input struct to CreatePorterAppDBEntry
  334. type CreatePorterAppDBEntryInput struct {
  335. AppName string
  336. GitRepoName string
  337. GitRepoID uint
  338. GitBranch string
  339. ImageRepository string
  340. PorterYamlPath string
  341. ImageTag string
  342. Local bool
  343. DeploymentTargetID string
  344. }
  345. // CreatePorterAppDBEntry creates an entry in the porter app
  346. func (c *Client) CreatePorterAppDBEntry(
  347. ctx context.Context,
  348. projectID uint, clusterID uint,
  349. inp CreatePorterAppDBEntryInput,
  350. ) error {
  351. var sourceType appInternal.SourceType
  352. var image *appInternal.Image
  353. if inp.Local {
  354. sourceType = appInternal.SourceType_Local
  355. }
  356. if inp.GitRepoName != "" {
  357. sourceType = appInternal.SourceType_Github
  358. }
  359. if inp.ImageRepository != "" {
  360. sourceType = appInternal.SourceType_DockerRegistry
  361. image = &appInternal.Image{
  362. Repository: inp.ImageRepository,
  363. Tag: inp.ImageTag,
  364. }
  365. }
  366. req := &porter_app.CreateAppRequest{
  367. Name: inp.AppName,
  368. SourceType: sourceType,
  369. GitSource: porter_app.GitSource{
  370. GitBranch: inp.GitBranch,
  371. GitRepoName: inp.GitRepoName,
  372. GitRepoID: inp.GitRepoID,
  373. },
  374. Image: image,
  375. PorterYamlPath: inp.PorterYamlPath,
  376. DeploymentTargetID: inp.DeploymentTargetID,
  377. }
  378. err := c.postRequest(
  379. fmt.Sprintf(
  380. "/projects/%d/clusters/%d/apps/create",
  381. projectID, clusterID,
  382. ),
  383. req,
  384. &types.PorterApp{},
  385. )
  386. return err
  387. }
  388. // CreateSubdomain returns a subdomain for a given service that point to the ingress-nginx service in the cluster
  389. func (c *Client) CreateSubdomain(
  390. ctx context.Context,
  391. projectID uint, clusterID uint,
  392. appName string, serviceName string,
  393. ) (*porter_app.CreateSubdomainResponse, error) {
  394. resp := &porter_app.CreateSubdomainResponse{}
  395. req := &porter_app.CreateSubdomainRequest{
  396. ServiceName: serviceName,
  397. }
  398. err := c.postRequest(
  399. fmt.Sprintf(
  400. "/projects/%d/clusters/%d/apps/%s/subdomain",
  401. projectID, clusterID, appName,
  402. ),
  403. req,
  404. resp,
  405. )
  406. return resp, err
  407. }
  408. // PredeployStatus checks the current status of a predeploy job for an app revision
  409. func (c *Client) PredeployStatus(
  410. ctx context.Context,
  411. projectID uint, clusterID uint,
  412. appName string, appRevisionId string,
  413. ) (*porter_app.PredeployStatusResponse, error) {
  414. resp := &porter_app.PredeployStatusResponse{}
  415. err := c.getRequest(
  416. fmt.Sprintf(
  417. "/projects/%d/clusters/%d/apps/%s/%s/predeploy-status",
  418. projectID, clusterID, appName, appRevisionId,
  419. ),
  420. nil,
  421. resp,
  422. )
  423. if resp.Status == "" {
  424. return nil, fmt.Errorf("no predeploy status found")
  425. }
  426. return resp, err
  427. }
  428. // GetRevision returns an app revision
  429. func (c *Client) GetRevision(
  430. ctx context.Context,
  431. projectID uint, clusterID uint,
  432. appName string, appRevisionId string,
  433. ) (*porter_app.GetAppRevisionResponse, error) {
  434. resp := &porter_app.GetAppRevisionResponse{}
  435. err := c.getRequest(
  436. fmt.Sprintf(
  437. "/projects/%d/clusters/%d/apps/%s/revisions/%s",
  438. projectID, clusterID, appName, appRevisionId,
  439. ),
  440. nil,
  441. resp,
  442. )
  443. return resp, err
  444. }
  445. // GetRevisionStatus returns the status of an app revision
  446. func (c *Client) GetRevisionStatus(
  447. ctx context.Context,
  448. projectID uint, clusterID uint,
  449. appName string, appRevisionId string,
  450. ) (*porter_app.GetAppRevisionStatusResponse, error) {
  451. resp := &porter_app.GetAppRevisionStatusResponse{}
  452. err := c.getRequest(
  453. fmt.Sprintf(
  454. "/projects/%d/clusters/%d/apps/%s/revisions/%s/status",
  455. projectID, clusterID, appName, appRevisionId,
  456. ),
  457. nil,
  458. resp,
  459. )
  460. return resp, err
  461. }
  462. // UpdateRevisionStatus updates the status of an app revision
  463. func (c *Client) UpdateRevisionStatus(
  464. ctx context.Context,
  465. projectID uint, clusterID uint,
  466. appName string, appRevisionId string,
  467. status models.AppRevisionStatus,
  468. ) (*porter_app.UpdateAppRevisionStatusResponse, error) {
  469. resp := &porter_app.UpdateAppRevisionStatusResponse{}
  470. req := &porter_app.UpdateAppRevisionStatusRequest{
  471. Status: status,
  472. }
  473. err := c.postRequest(
  474. fmt.Sprintf(
  475. "/projects/%d/clusters/%d/apps/%s/revisions/%s",
  476. projectID, clusterID, appName, appRevisionId,
  477. ),
  478. req,
  479. resp,
  480. )
  481. return resp, err
  482. }
  483. // GetBuildEnv returns the build environment for a given app proto
  484. func (c *Client) GetBuildEnv(
  485. ctx context.Context,
  486. projectID uint, clusterID uint,
  487. appName string, appRevisionId string,
  488. ) (*porter_app.GetBuildEnvResponse, error) {
  489. resp := &porter_app.GetBuildEnvResponse{}
  490. err := c.getRequest(
  491. fmt.Sprintf(
  492. "/projects/%d/clusters/%d/apps/%s/revisions/%s/build-env",
  493. projectID, clusterID, appName, appRevisionId,
  494. ),
  495. nil,
  496. resp,
  497. )
  498. return resp, err
  499. }
  500. // GetAppEnvVariables returns all env variables for a given app
  501. func (c *Client) GetAppEnvVariables(
  502. ctx context.Context,
  503. projectID uint, clusterID uint,
  504. appName string,
  505. ) (*porter_app.AppEnvVariablesResponse, error) {
  506. resp := &porter_app.AppEnvVariablesResponse{}
  507. req := &porter_app.AppEnvVariablesRequest{}
  508. err := c.getRequest(
  509. fmt.Sprintf(
  510. "/projects/%d/clusters/%d/apps/%s/env-variables",
  511. projectID, clusterID, appName,
  512. ),
  513. req,
  514. resp,
  515. )
  516. return resp, err
  517. }
  518. // GetBuildFromRevision returns the build environment for a given app proto
  519. func (c *Client) GetBuildFromRevision(
  520. ctx context.Context,
  521. projectID uint, clusterID uint,
  522. appName string, appRevisionId string,
  523. ) (*porter_app.GetBuildFromRevisionResponse, error) {
  524. resp := &porter_app.GetBuildFromRevisionResponse{}
  525. err := c.getRequest(
  526. fmt.Sprintf(
  527. "/projects/%d/clusters/%d/apps/%s/revisions/%s/build",
  528. projectID, clusterID, appName, appRevisionId,
  529. ),
  530. nil,
  531. resp,
  532. )
  533. return resp, err
  534. }
  535. // ReportRevisionStatusInput is the input struct to ReportRevisionStatus
  536. type ReportRevisionStatusInput struct {
  537. ProjectID uint
  538. ClusterID uint
  539. AppName string
  540. AppRevisionID string
  541. PRNumber int
  542. CommitSHA string
  543. }
  544. // ReportRevisionStatus reports the status of an app revision to external services
  545. func (c *Client) ReportRevisionStatus(
  546. ctx context.Context,
  547. inp ReportRevisionStatusInput,
  548. ) (*porter_app.ReportRevisionStatusResponse, error) {
  549. resp := &porter_app.ReportRevisionStatusResponse{}
  550. req := &porter_app.ReportRevisionStatusRequest{
  551. PRNumber: inp.PRNumber,
  552. CommitSHA: inp.CommitSHA,
  553. }
  554. err := c.postRequest(
  555. fmt.Sprintf(
  556. "/projects/%d/clusters/%d/apps/%s/revisions/%s/status",
  557. inp.ProjectID, inp.ClusterID, inp.AppName, inp.AppRevisionID,
  558. ),
  559. req,
  560. resp,
  561. )
  562. return resp, err
  563. }
  564. // CreateOrUpdateAppEnvironment updates the app environment group and creates it if it doesn't exist
  565. func (c *Client) CreateOrUpdateAppEnvironment(
  566. ctx context.Context,
  567. projectID uint, clusterID uint,
  568. appName string,
  569. deploymentTargetID string,
  570. variables map[string]string,
  571. secrets map[string]string,
  572. Base64AppProto string,
  573. ) (*porter_app.UpdateAppEnvironmentResponse, error) {
  574. resp := &porter_app.UpdateAppEnvironmentResponse{}
  575. req := &porter_app.UpdateAppEnvironmentRequest{
  576. DeploymentTargetID: deploymentTargetID,
  577. Variables: variables,
  578. Secrets: secrets,
  579. HardUpdate: false,
  580. Base64AppProto: Base64AppProto,
  581. }
  582. err := c.postRequest(
  583. fmt.Sprintf(
  584. "/projects/%d/clusters/%d/apps/%s/update-environment",
  585. projectID, clusterID, appName,
  586. ),
  587. req,
  588. resp,
  589. )
  590. return resp, err
  591. }
  592. // PorterYamlV2Pods gets all pods for a given deployment target id and app name
  593. func (c *Client) PorterYamlV2Pods(
  594. ctx context.Context,
  595. projectID, clusterID uint,
  596. porterAppName string,
  597. deploymentTargetName string,
  598. ) (*types.GetReleaseAllPodsResponse, error) {
  599. req := &porter_app.PodStatusRequest{
  600. DeploymentTargetName: deploymentTargetName,
  601. }
  602. resp := &types.GetReleaseAllPodsResponse{}
  603. err := c.getRequest(
  604. fmt.Sprintf(
  605. "/projects/%d/clusters/%d/apps/%s/pods",
  606. projectID, clusterID,
  607. porterAppName,
  608. ),
  609. req,
  610. resp,
  611. )
  612. return resp, err
  613. }
  614. // UpdateImage updates the image for a porter app (porter yaml v2 only)
  615. func (c *Client) UpdateImage(
  616. ctx context.Context,
  617. projectID, clusterID uint,
  618. appName, deploymentTargetName, tag string,
  619. ) (*porter_app.UpdateImageResponse, error) {
  620. req := &porter_app.UpdateImageRequest{
  621. Tag: tag,
  622. DeploymentTargetName: deploymentTargetName,
  623. }
  624. resp := &porter_app.UpdateImageResponse{}
  625. err := c.postRequest(
  626. fmt.Sprintf(
  627. "/projects/%d/clusters/%d/apps/%s/update-image",
  628. projectID, clusterID, appName,
  629. ),
  630. &req,
  631. resp,
  632. )
  633. return resp, err
  634. }
  635. // ListAppRevisions lists the last ten app revisions for a given app
  636. func (c *Client) ListAppRevisions(
  637. ctx context.Context,
  638. projectID, clusterID uint,
  639. appName string,
  640. deploymentTargetID string,
  641. ) (*porter_app.ListAppRevisionsResponse, error) {
  642. resp := &porter_app.ListAppRevisionsResponse{}
  643. req := &porter_app.ListAppRevisionsRequest{
  644. DeploymentTargetID: deploymentTargetID,
  645. }
  646. err := c.getRequest(
  647. fmt.Sprintf(
  648. "/projects/%d/clusters/%d/apps/%s/revisions",
  649. projectID, clusterID,
  650. appName,
  651. ),
  652. req,
  653. resp,
  654. )
  655. return resp, err
  656. }
  657. // RollbackRevision reverts an app to a previous revision
  658. func (c *Client) RollbackRevision(
  659. ctx context.Context,
  660. projectID, clusterID uint,
  661. appName string,
  662. deploymentTargetName string,
  663. ) (*porter_app.RollbackAppRevisionResponse, error) {
  664. resp := &porter_app.RollbackAppRevisionResponse{}
  665. req := &porter_app.RollbackAppRevisionRequest{
  666. DeploymentTargetName: deploymentTargetName,
  667. }
  668. err := c.postRequest(
  669. fmt.Sprintf(
  670. "/projects/%d/clusters/%d/apps/%s/rollback",
  671. projectID, clusterID,
  672. appName,
  673. ),
  674. req,
  675. resp,
  676. )
  677. return resp, err
  678. }
  679. // UseNewApplyLogic checks whether the CLI should use the new apply logic
  680. func (c *Client) UseNewApplyLogic(
  681. ctx context.Context,
  682. projectID, clusterID uint,
  683. ) (*porter_app.UseNewApplyLogicResponse, error) {
  684. resp := &porter_app.UseNewApplyLogicResponse{}
  685. req := &porter_app.UseNewApplyLogicRequest{}
  686. err := c.getRequest(
  687. fmt.Sprintf(
  688. "/projects/%d/clusters/%d/apps/use-new-apply-logic",
  689. projectID, clusterID,
  690. ),
  691. req,
  692. resp,
  693. )
  694. return resp, err
  695. }
  696. // RunAppJob runs a job for an app
  697. func (c *Client) RunAppJob(
  698. ctx context.Context,
  699. projectID, clusterID uint,
  700. appName string, jobName string,
  701. deploymentTargetName string,
  702. ) (*porter_app.RunAppJobResponse, error) {
  703. resp := &porter_app.RunAppJobResponse{}
  704. req := &porter_app.RunAppJobRequest{
  705. ServiceName: jobName,
  706. DeploymentTargetName: deploymentTargetName,
  707. }
  708. err := c.postRequest(
  709. fmt.Sprintf(
  710. "/projects/%d/clusters/%d/apps/%s/run",
  711. projectID, clusterID,
  712. appName,
  713. ),
  714. req,
  715. resp,
  716. )
  717. return resp, err
  718. }
  719. // RunAppJobStatusInput contains all the information necessary to check the status of a job
  720. type RunAppJobStatusInput struct {
  721. // AppName is the name of the app associated with the job
  722. AppName string
  723. // Cluster is the id of the cluster against which to retrieve a helm agent for
  724. ClusterID uint
  725. // DeploymentTargetName is the id of the deployment target the job was run against
  726. DeploymentTargetName string
  727. // ServiceName is the name of the app service that was triggered
  728. ServiceName string
  729. // JobRunID is the UID returned from the /apps/{porter_app_name}/run endpoint
  730. JobRunID string
  731. // ProjectID is the project in which the cluster exists
  732. ProjectID uint
  733. }
  734. // RunAppJobStatus gets the status for a job app run
  735. func (c *Client) RunAppJobStatus(
  736. ctx context.Context,
  737. input RunAppJobStatusInput,
  738. ) (*porter_app.AppJobRunStatusResponse, error) {
  739. resp := &porter_app.AppJobRunStatusResponse{}
  740. req := &porter_app.AppJobRunStatusRequest{
  741. DeploymentTargetName: input.DeploymentTargetName,
  742. JobRunID: input.JobRunID,
  743. ServiceName: input.ServiceName,
  744. }
  745. err := c.getRequest(
  746. fmt.Sprintf(
  747. "/projects/%d/clusters/%d/apps/%s/run-status",
  748. input.ProjectID, input.ClusterID,
  749. input.AppName,
  750. ),
  751. req,
  752. resp,
  753. )
  754. return resp, err
  755. }