diff --git a/CHANGELOG.md b/CHANGELOG.md index b7e65a3..0a7a42f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## {{ UNRELEASED_VERSION }} - [{{ UNRELEASED_DATE }}]({{ UNRELEASED_LINK }}) +* Fixed bugs present in various interactions with the Pantheon API. + ## v1.9.1 - [December 11, 2024](https://github.com/lando/pantheon/releases/tag/v1.9.1) * Fixed bug preventing `lando pull` and `lando push` from correctly surfacing available environments diff --git a/app.js b/app.js index 26db56b..e9b67a7 100644 --- a/app.js +++ b/app.js @@ -36,7 +36,8 @@ module.exports = (app, lando) => { // this allows these commands to all be overriden without causing a failure here if (answers.auth && (answers.auth !== token || !email)) { const api = new PantheonApiClient(answers.auth, app.log); - return api.auth().then(() => api.getUser().then(results => { + return api.auth().then(async () => { + const results = await api.getUser(); const cache = {token: answers.auth, email: results.email, date: _.toInteger(_.now() / 1000)}; // Reset this apps metacache lando.cache.set(app.metaCache, _.merge({}, app.meta, cache), {persist: true}); @@ -44,7 +45,7 @@ module.exports = (app, lando) => { lando.cache.set(app.pantheonTokenCache, utils.sortTokens(app.pantheonTokens, [cache]), {persist: true}); // Wipe out the apps tooling cache to reset with the new MT lando.cache.remove(`${app.name}.tooling.cache`); - })) + }) // Throw some sort of error // NOTE: this provides some error handling when we are completely non-interactive .catch(err => { diff --git a/inits/pantheon.js b/inits/pantheon.js index c6ffddf..f3103f8 100644 --- a/inits/pantheon.js +++ b/inits/pantheon.js @@ -37,18 +37,17 @@ const showTokenList = (data, tokens = []) => data === 'pantheon' && !_.isEmpty(t // Helper to determine whether to show token password entry or not const showTokenEntry = (data, answer, tokens = []) => data === 'pantheon' && (_.isEmpty(tokens) || answer === 'more'); -// Helper to get sites for autocomplete -const getAutoCompleteSites = (answers, lando, input = null) => { +// Helper to get sites for autocomplete - simplify Promise chain +const getAutoCompleteSites = async (answers, lando, input = null) => { if (!_.isEmpty(pantheonSites)) { - return lando.Promise.resolve(pantheonSites).filter(site => _.startsWith(site.name, input)); - } else { - const api = new PantheonApiClient(answers['pantheon-auth'], lando.log); - return api.auth().then(async () => { - const sites = await api.getSites(); - pantheonSites = sites.map(site => ({name: site.name, value: site.name})); - return pantheonSites; - }); + return pantheonSites.filter(site => _.startsWith(site.name, input)); } + + const api = new PantheonApiClient(answers['pantheon-auth'], lando.log); + await api.auth(); + const sites = await api.getSites(); + pantheonSites = sites.map(site => ({name: site.name, value: site.name})); + return pantheonSites; }; /* @@ -117,73 +116,78 @@ module.exports = { build: () => ([ {name: 'wait-for-user', cmd: '/helpers/pantheon-wait-for-user.sh'}, {name: 'generate-key', cmd: `/helpers/generate-key.sh ${pantheonLandoKey} ${pantheonLandoKeyComment}`}, - {name: 'post-key', func: (options, lando) => { + {name: 'post-key', func: async (options, lando) => { const api = new PantheonApiClient(options['pantheon-auth'], lando.log); const pubKey = path.join(lando.config.userConfRoot, 'keys', `${pantheonLandoKey}.pub`); - return api.auth().then(() => api.postKey(pubKey)); + await api.auth(); + return api.postKey(pubKey); }}, - {name: 'get-git-url', func: (options, lando) => { + {name: 'get-git-url', func: async (options, lando) => { const api = new PantheonApiClient(options['pantheon-auth'], lando.log); - return api.auth().then(() => api.getSite(options['pantheon-site'], false)).then(site => { - options['pantheon-git-url'] = getGitUrl(site); - }); + await api.auth(); + const site = await api.getSite(options['pantheon-site'], false); + options['pantheon-git-url'] = getGitUrl(site); }}, {name: 'reload-keys', cmd: '/helpers/load-keys.sh --silent', user: 'root'}, {name: 'clone-repo', cmd: options => `/helpers/get-remote-url.sh ${options['pantheon-git-url']}`, remove: 'true'}, ]), }], - build: (options, lando) => { + build: async (options, lando) => { const api = new PantheonApiClient(options['pantheon-auth'], lando.log); const pubKey = path.join(lando.config.userConfRoot, 'keys', `${pantheonLandoKey}.pub`); - return lando.Promise.try(() => { - // If we don't have the keys, then resend to be make sure they are there - if (!fs.existsSync(pubKey)) { - return lando.engine.run({ - id: 'generate-key', - cmd: `/helpers/generate-key.sh ${pantheonLandoKey} ${pantheonLandoKeyComment}`, - compose: utils.generateComposeFiles(options, lando), - project: lando.config.landoFileConfig.project, - opts: { - mode: 'attach', - services: ['init'], - }, - }); - } - }) - .then(() => api.auth()) - // Post our keys again. - .then(() => api.postKey(pubKey)) - .then(() => api.auth()) - // Get our sites and user - .then(() => { - return Promise.all([api.getSite(options['pantheon-site']), api.getUser()]); - }) - // Parse the dataz and set the things - .then(results => { - // Get our site and email - const site = results[0]; - const user = results[1]; - - // Error if site doesn't exist - if (_.isEmpty(site)) throw Error(`${site} does not appear to be a Pantheon site!`); - - // This is a good token, lets update our cache - const cache = {token: options['pantheon-auth'], email: user.email, date: _.toInteger(_.now() / 1000)}; - - // Update lando's store of pantheon machine tokens - const tokens = lando.cache.get(pantheonTokenCache) || []; - lando.cache.set(pantheonTokenCache, utils.sortTokens(tokens, [cache]), {persist: true}); - // Update app metdata - const metaData = lando.cache.get(`${options.name}.meta.cache`); - lando.cache.set(`${options.name}.meta.cache`, _.merge({}, metaData, cache), {persist: true}); - - // Add some stuff to our landofile - return {config: { - framework: _.get(site, 'framework', 'drupal'), - site: _.get(site, 'name', options.name), - id: _.get(site, 'id', 'lando'), - }}; + // If we don't have the keys, generate them + if (!fs.existsSync(pubKey)) { + await lando.engine.run({ + id: 'generate-key', + cmd: `/helpers/generate-key.sh ${pantheonLandoKey} ${pantheonLandoKeyComment}`, + compose: utils.generateComposeFiles(options, lando), + project: lando.config.landoFileConfig.project, + opts: { + mode: 'attach', + services: ['init'], + }, }); + } + + // Authenticate and post keys + await api.auth(); + await api.postKey(pubKey); + await api.auth(); + + // Get site and user info + const [site, user] = await Promise.all([ + api.getSite(options['pantheon-site']), + api.getUser(), + ]); + + // Error if site doesn't exist + if (_.isEmpty(site)) { + throw new Error(`${site} does not appear to be a Pantheon site!`); + } + + // This is a good token, lets update our cache + const cache = { + token: options['pantheon-auth'], + email: user.email, + date: _.toInteger(_.now() / 1000), + }; + + // Update lando's store of pantheon machine tokens + const tokens = lando.cache.get(pantheonTokenCache) || []; + lando.cache.set(pantheonTokenCache, utils.sortTokens(tokens, [cache]), {persist: true}); + + // Update app metadata + const metaData = lando.cache.get(`${options.name}.meta.cache`); + lando.cache.set(`${options.name}.meta.cache`, _.merge({}, metaData, cache), {persist: true}); + + // Return config to be added to the landofile + return { + config: { + framework: _.get(site, 'framework', 'drupal'), + site: _.get(site, 'name', options.name), + id: _.get(site, 'id', 'lando'), + }, + }; }, }; diff --git a/lib/client.js b/lib/client.js index 2527c47..846855e 100644 --- a/lib/client.js +++ b/lib/client.js @@ -167,8 +167,8 @@ module.exports = class PantheonApiClient { * * @return {Promise} User account data */ - getUser() { - return pantheonRequest(this.request, this.log, 'get', ['users', _.get(this.session, 'user_id')]); + async getUser() { + return await pantheonRequest(this.request, this.log, 'get', ['users', _.get(this.session, 'user_id')]); } /**