From 24fe6666e4391e5e5524b519612ce4580fdad4ff Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Mon, 30 Aug 2021 22:10:54 +0000 Subject: [PATCH] balena deploy: Retrieve the cpu arch as part of the device type resource Connects-to: #2318 Change-type: patch Signed-off-by: Thodoris Greasidis --- lib/utils/helpers.ts | 28 +++++----- tests/commands/deploy.spec.ts | 3 +- tests/nock/balena-api-mock.ts | 8 ++- ...ion-GET-v6-expanded-app-type-cpu-arch.json | 53 +++++++++++++++++++ 4 files changed, 75 insertions(+), 17 deletions(-) create mode 100644 tests/test-data/api-response/application-GET-v6-expanded-app-type-cpu-arch.json diff --git a/lib/utils/helpers.ts b/lib/utils/helpers.ts index 1e890b6c..f0b32743 100644 --- a/lib/utils/helpers.ts +++ b/lib/utils/helpers.ts @@ -148,23 +148,18 @@ export async function osProgressHandler(step: InitializeEmitter) { }); } -export function getAppWithArch( +export async function getAppWithArch( applicationName: string, ): Promise { - return Promise.all([ - getApplication(applicationName), - getBalenaSdk().models.config.getDeviceTypes(), - ]).then(function ([app, deviceTypes]) { - const config = _.find(deviceTypes, { - slug: app.is_for__device_type[0].slug, - }); + const app = await getApplication(applicationName); + const { getExpanded } = await import('./pine'); - if (!config) { - throw new Error(`balena API request failed for fleet ${applicationName}`); - } - - return { ...app, arch: config.arch }; - }); + return { + ...app, + arch: getExpanded( + getExpanded(app.is_for__device_type)!.is_of__cpu_architecture, + )!.slug, + }; } // TODO: Drop this. The sdk now has this baked in application.get(). @@ -182,6 +177,11 @@ function getApplication( }, is_for__device_type: { $select: 'slug', + $expand: { + is_of__cpu_architecture: { + $select: 'slug', + }, + }, }, }, }; diff --git a/tests/commands/deploy.spec.ts b/tests/commands/deploy.spec.ts index 54136a6a..63a1d10b 100644 --- a/tests/commands/deploy.spec.ts +++ b/tests/commands/deploy.spec.ts @@ -79,8 +79,7 @@ describe('balena deploy', function () { docker = new DockerMock(); api.expectGetWhoAmI({ optional: true, persist: true }); api.expectGetMixpanel({ optional: true }); - api.expectGetConfigDeviceTypes(); - api.expectGetApplication(); + api.expectGetApplication({ expandArchitecture: true }); api.expectGetRelease(); api.expectGetUser(); api.expectGetService({ serviceName: 'main' }); diff --git a/tests/nock/balena-api-mock.ts b/tests/nock/balena-api-mock.ts index 19294f61..33565eee 100644 --- a/tests/nock/balena-api-mock.ts +++ b/tests/nock/balena-api-mock.ts @@ -35,6 +35,7 @@ export class BalenaAPIMock extends NockMock { notFound = false, optional = false, persist = false, + expandArchitecture = false, } = {}) { const interceptor = this.optGet(/^\/v6\/application($|[(?])/, { optional, @@ -45,7 +46,12 @@ export class BalenaAPIMock extends NockMock { } else { interceptor.replyWithFile( 200, - path.join(apiResponsePath, 'application-GET-v6-expanded-app-type.json'), + path.join( + apiResponsePath, + !expandArchitecture + ? 'application-GET-v6-expanded-app-type.json' + : 'application-GET-v6-expanded-app-type-cpu-arch.json', + ), jHeader, ); } diff --git a/tests/test-data/api-response/application-GET-v6-expanded-app-type-cpu-arch.json b/tests/test-data/api-response/application-GET-v6-expanded-app-type-cpu-arch.json new file mode 100644 index 00000000..4c543516 --- /dev/null +++ b/tests/test-data/api-response/application-GET-v6-expanded-app-type-cpu-arch.json @@ -0,0 +1,53 @@ +{ + "d": [ + { + "application_type": [ + { + "name": "Starter", + "slug": "microservices-starter", + "supports_multicontainer": true, + "is_legacy": false, + "__metadata": {} + } + ], + "id": 1301645, + "user": { + "__deferred": { + "uri": "/resin/user(43699)" + }, + "__id": 43699 + }, + "organization": [ + { + "handle": "gh_user" + } + ], + "depends_on__application": null, + "actor": 3423895, + "app_name": "testApp", + "slug": "gh_user/testApp", + "should_be__running_release": [ + { + "commit": "96eec431d57e6976d3a756df33fde7e2" + } + ], + "is_for__device_type": [ + { + "slug": "raspberrypi3", + "is_of__cpu_architecture": [ + { + "slug": "armv7hf" + } + ] + } + ], + "should_track_latest_release": true, + "is_accessible_by_support_until__date": null, + "is_public": false, + "is_host": false, + "__metadata": { + "uri": "/resin/application(@id)?@id=1301645" + } + } + ] +}