Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aio app add/delete service #357

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f34acb4
Support for Prroject and Workspace creation on init
moritzraho Nov 5, 2020
d49270f
Support adding services on init
moritzraho Dec 17, 2020
2faa363
tests + error handling fix
moritzraho Dec 22, 2020
fb8e01b
revert error handling change - avoid breaking change
moritzraho Dec 22, 2020
4adfede
Support for Prroject and Workspace creation on init
moritzraho Nov 5, 2020
7eb4c5e
ongoing
moritzraho Jan 6, 2021
c469f57
add child cert folder
moritzraho Jan 6, 2021
36d7efe
first draft for add command
moritzraho Jan 6, 2021
ce6fb5b
Merge branch 'master' into support-console-add-services
moritzraho Jan 8, 2021
7b3a333
Merge branch 'support-console-add-services' into add-service-cmd
moritzraho Jan 12, 2021
dbfdcfc
Merge branch 'master' into add-service-cmd
moritzraho Jan 12, 2021
b2416d3
Merge branch 'support-console-add-services' into add-service-cmd
moritzraho Jan 12, 2021
219a554
add/delete cmd wip
moritzraho Jan 12, 2021
d4ce743
add missing changes
moritzraho Jan 12, 2021
574e204
fix erroneous change
moritzraho Jan 12, 2021
3247e97
missing header
moritzraho Jan 12, 2021
59e7312
prompt for delete services
moritzraho Jan 19, 2021
5eba4a6
on-going tests
moritzraho Jan 20, 2021
e35a6ff
save
moritzraho Jan 20, 2021
ae52b8f
delete tests
moritzraho Jan 20, 2021
6385c7c
tests for add/delete services
moritzraho Jan 20, 2021
028cef8
Merge branch 'master' into add-service-cmd
moritzraho Jan 21, 2021
5993828
fix weird chdir mock bug
moritzraho Jan 21, 2021
c1be63e
coverage fix
moritzraho Jan 21, 2021
67ae185
Merge branch 'master' into add-service-cmd
shazron Jan 22, 2021
4f9c3e3
bump
moritzraho Jan 22, 2021
3d94143
Merge branch 'master' into add-service-cmd
moritzraho Jan 22, 2021
81e940c
bump generator to fix tests
moritzraho Jan 22, 2021
6c66dc4
fix eslint
moritzraho Jan 22, 2021
fcd12f2
Merge branch 'master' into add-service-cmd
shazron Jan 28, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@adobe/aio-lib-runtime": "^1.2.5",
"@adobe/aio-lib-web": "^4.0.0",
"@adobe/generator-aio-app": "^1.9.0",
"@adobe/generator-aio-console": "^2.0.1",
"@adobe/generator-aio-console": "^2.0.3",
"@oclif/command": "^1.5.11",
"@oclif/config": "^1.12.9",
"@oclif/plugin-help": "^2.2.3",
Expand Down
155 changes: 155 additions & 0 deletions src/commands/app/add/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

const path = require('path')
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:add:service', { provider: 'debug' })
const config = require('@adobe/aio-lib-core-config')
const chalk = require('chalk')

const { getCliInfo } = require('../../../lib/app-helper')
const BaseCommand = require('../../../BaseCommand')
const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli')

const { ENTP_INT_CERTS_FOLDER, CONSOLE_API_KEYS } = require('../../../lib/defaults')

class AddServiceCommand extends BaseCommand {
async run () {
const { flags } = this.parse(AddServiceCommand)

aioLogger.debug(`adding services to the current workspace, using flags: ${flags}`)

// login
const { accessToken, env } = await getCliInfo()
const consoleCLI = await LibConsoleCLI.init({ accessToken, env, apiKey: CONSOLE_API_KEYS[env] })

// load console configuration from .aio and .env files
const projectConfig = config.get('project')
if (!projectConfig) {
this.error('Incomplete .aio configuration, please import a valid Adobe Developer Console configuration via `aio app use` first.')
}
const orgId = projectConfig.org.id
const project = { name: projectConfig.name, id: projectConfig.id }
const workspace = { name: projectConfig.workspace.name, id: projectConfig.workspace.id }

// get latest support services
const supportedServices = await consoleCLI.getEnabledServicesForOrg(orgId)

// get current service properties
const currentServiceProperties = await consoleCLI.getServicePropertiesFromWorkspace(
orgId,
project.id,
workspace,
supportedServices
)

// update the service config, subscriptions and supported services
// Note: the service config could be replaced by always fetching latest serviceProperties
config.set('project.workspace.details.services', currentServiceProperties.map(s => ({
name: s.name,
code: s.sdkCode
})))
config.set('project.org.details.services', supportedServices.map(s => ({
name: s.name,
code: s.code,
type: s.type
})))

// log currently selected services (messages on stderr)
const currentServiceNames = currentServiceProperties.map(s => s.name)
console.error(`Workspace ${workspace.name} is currently subscribed to the following services:\n${JSON.stringify(currentServiceNames, null, 2)}`)

// prompt user to decide on how to add services:
// - select service subscription manually
// - or clone from existing workspace
const op = await consoleCLI.promptForServiceSubscriptionsOperation(
workspace.name,
{ cloneChoice: true, nopChoice: true }
)

if (op === 'nop') {
return null
}

let serviceProperties = []
if (op === 'select') {
// filter out already added services for selection
const currentServiceCodesSet = new Set(currentServiceProperties.map(s => s.sdkCode))
const filteredServices = supportedServices.filter(s => s.type === 'entp' && !currentServiceCodesSet.has(s.code))
if (filteredServices.length <= 0) {
this.error(`All supported Services in the Organization have already been added to Workspace ${workspace.name}`)
}
// prompt to manually select services
serviceProperties = await consoleCLI.promptForSelectServiceProperties(
workspace.name,
filteredServices
)
// now past services are appended to the selection for subscription
serviceProperties.push(...currentServiceProperties)
}
if (op === 'clone') {
// get latest workspaces which are not the current
const otherWorkspaces = (
await consoleCLI.getWorkspaces(orgId, project.id)
).filter(w => w.id !== workspace.id)
// prompt to select one of those as a source for clone
const workspaceFrom = await consoleCLI.promptForSelectWorkspace(
otherWorkspaces,
{},
{ allowCreate: false }
)
// get serviceProperties from source workspace
serviceProperties = await consoleCLI.getServicePropertiesFromWorkspace(
orgId,
project.id,
workspaceFrom,
supportedServices
)
console.error(`Note: Service Subscriptions in Workspace ${workspace.name} will be overwritten by services in Workspace ${workspaceFrom}`)
}
// prompt confirm the new service subscription list
const confirm = await consoleCLI.confirmNewServiceSubscriptions(
workspace.name,
serviceProperties
)
if (confirm) {
// if confirmed update the services
await consoleCLI.subscribeToServices(
orgId,
project,
workspace,
path.join(this.config.dataDir, ENTP_INT_CERTS_FOLDER),
serviceProperties
)
// update the service configuration with the latest subscriptions
config.set('project.workspace.details.services', serviceProperties.map(s => ({
name: s.name,
code: s.sdkCode
})))
// success !
this.log(chalk.green(chalk.bold(`Successfully updated Service Subscriptions in Workspace ${workspace.name}`)))
return serviceProperties
}
// confirm == false, do nothing
return null
}
}

AddServiceCommand.description = `Subscribe to services in the current Workspace
`

AddServiceCommand.flags = {
...BaseCommand.flags
}

AddServiceCommand.aliases = ['app:add:services']
AddServiceCommand.args = []

module.exports = AddServiceCommand
115 changes: 115 additions & 0 deletions src/commands/app/delete/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:delete:service', { provider: 'debug' })
const config = require('@adobe/aio-lib-core-config')
const chalk = require('chalk')

const { getCliInfo } = require('../../../lib/app-helper')
const BaseCommand = require('../../../BaseCommand')
const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli')

const { CONSOLE_API_KEYS } = require('../../../lib/defaults')

class AddServiceCommand extends BaseCommand {
async run () {
const { flags } = this.parse(AddServiceCommand)

aioLogger.debug(`deleting services in the current workspace, using flags: ${flags}`)

// login
const { accessToken, env } = await getCliInfo()
const consoleCLI = await LibConsoleCLI.init({ accessToken, env, apiKey: CONSOLE_API_KEYS[env] })

// load console configuration from .aio and .env files
const projectConfig = config.get('project')
if (!projectConfig) {
this.error('Incomplete .aio configuration, please import a valid Adobe Developer Console configuration via `aio app use` first.')
}
const orgId = projectConfig.org.id
const project = { name: projectConfig.name, id: projectConfig.id }
const workspace = { name: projectConfig.workspace.name, id: projectConfig.workspace.id }

// get latest support services
const supportedServices = await consoleCLI.getEnabledServicesForOrg(orgId)

// get current service properties
const currentServiceProperties = await consoleCLI.getServicePropertiesFromWorkspace(
orgId,
project.id,
workspace,
supportedServices
)

// update the service config, subscriptions and supported services
// Note: the service config could be replaced by always fetching latest serviceProperties
config.set('project.workspace.details.services', currentServiceProperties.map(s => ({
name: s.name,
code: s.sdkCode
})))
config.set('project.org.details.services', supportedServices.map(s => ({
name: s.name,
code: s.code,
type: s.type
})))

if (currentServiceProperties.length <= 0) {
this.error(`No Services are attached to Workspace ${workspace.name}`)
}

// const currentServiceChoices = currentServiceProperties.map(s => ({ name: s.name, code: s.sdkCode }))
const newServiceProperties = await consoleCLI.promptForRemoveServiceSubscriptions(
workspace.name,
currentServiceProperties
)
if (newServiceProperties === null) {
this.log('No services selected, nothing to be done')
return null
}
// prompt confirm the new service subscription list
const confirm = await consoleCLI.confirmNewServiceSubscriptions(
workspace.name,
newServiceProperties
)
if (confirm) {
// if confirmed update the services
await consoleCLI.subscribeToServices(
orgId,
project,
workspace,
null, // no need to specify certDir, here we are sure that credentials are attached
newServiceProperties
)
// update the service configuration with the latest subscriptions
config.set('project.workspace.details.services', newServiceProperties.map(s => ({
name: s.name,
code: s.sdkCode
})))
// success !
this.log(chalk.green(chalk.bold(`Successfully deleted selected Service Subscriptions in Workspace ${workspace.name}`)))
return newServiceProperties
}
// confirm == false, do nothing
return null
}
}

AddServiceCommand.description = `Subscribe to services in the current Workspace
`

AddServiceCommand.flags = {
...BaseCommand.flags
}

AddServiceCommand.aliases = ['app:add:services']
AddServiceCommand.args = []

module.exports = AddServiceCommand
3 changes: 1 addition & 2 deletions src/commands/app/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ const { getCliInfo } = require('../../lib/app-helper')
const chalk = require('chalk')
const { servicesToGeneratorInput } = require('../../lib/app-helper')

const SERVICE_API_KEY_ENV = 'SERVICE_API_KEY'
const ENTP_INT_CERTS_FOLDER = 'entp-int-certs'
const { ENTP_INT_CERTS_FOLDER, SERVICE_API_KEY_ENV } = require('../../lib/defaults')

class InitCommand extends BaseCommand {
async run () {
Expand Down
3 changes: 1 addition & 2 deletions src/commands/app/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ const { EOL } = require('os')
const yeoman = require('yeoman-environment')
const { getCliInfo } = require('../../lib/app-helper')
const fs = require('fs-extra')

const SERVICE_API_KEY_ENV = 'SERVICE_API_KEY'
const { SERVICE_API_KEY_ENV } = require('../../lib/defaults')

class Use extends BaseCommand {
async consoleConfigString (consoleConfig) {
Expand Down
17 changes: 17 additions & 0 deletions src/lib/defaults.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

// defaults

module.exports = {
Expand All @@ -9,5 +20,11 @@ module.exports = {
defaultCSSCacheDuration: '604800',
defaultImageCacheDuration: '604800',
AIO_CONFIG_IMS_ORG_ID: 'project.org.ims_org_id',
SERVICE_API_KEY_ENV: 'SERVICE_API_KEY',
ENTP_INT_CERTS_FOLDER: 'entp-int-certs',
CONSOLE_API_KEYS: {
prod: 'aio-cli-console-auth',
stage: 'aio-cli-console-auth-stage'
},
defaultHttpServerPort: 9080
}
Loading