-
Notifications
You must be signed in to change notification settings - Fork 816
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrates analytics category to support in app messaging channel…
… notifications (#11158) * feat: migrates analytics category to support in app messaging channel notifications
- Loading branch information
Showing
34 changed files
with
403 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 12 additions & 23 deletions
35
packages/amplify-category-analytics/src/commands/analytics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,44 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
/* eslint-disable global-require */ | ||
/* eslint-disable import/no-dynamic-require */ | ||
import { $TSAny, $TSContext } from 'amplify-cli-core'; | ||
import { printer } from 'amplify-prompts'; | ||
|
||
const featureName = 'analytics'; | ||
export { run as analyticsPush } from './analytics/push'; | ||
export const name = 'analytics'; | ||
|
||
/** | ||
* Analytics category command router. Invokes functionality for all CLI calls | ||
* @param context amplify cli context | ||
*/ | ||
const analyticsRun = async (context:$TSContext): Promise<$TSAny> => { | ||
export const run = async (context: $TSContext): Promise<$TSAny> => { | ||
if (/^win/.test(process.platform)) { | ||
try { | ||
const { run } = require(`./${featureName}/${context.parameters.first}`); | ||
return run(context); | ||
} catch (e) { | ||
printer.error('Command not found'); | ||
} | ||
const { run: runCommand } = await import(`./${name}/${context.parameters.first}`); | ||
return runCommand(context); | ||
} | ||
const header = `amplify ${featureName} <subcommand>`; | ||
const header = `amplify ${name} <subcommand>`; | ||
|
||
const commands = [ | ||
{ | ||
name: 'add', | ||
description: `Takes you through a CLI flow to add an ${featureName} resource to your local backend`, | ||
description: `Takes you through a CLI flow to add an ${name} resource to your local backend`, | ||
}, | ||
{ | ||
name: 'update', | ||
description: `Takes you through steps in the CLI to update an ${featureName} resource`, | ||
description: `Takes you through steps in the CLI to update an ${name} resource`, | ||
}, | ||
{ | ||
name: 'push', | ||
description: `Provisions only ${featureName} cloud resources with the latest local developments`, | ||
description: `Provisions only ${name} cloud resources with the latest local developments`, | ||
}, | ||
{ | ||
name: 'remove', | ||
description: `Removes ${featureName} resource from your local backend. The resource is removed from the cloud on the next push command.`, | ||
description: `Removes ${name} resource from your local backend. The resource is removed from the cloud on the next push command.`, | ||
}, | ||
{ | ||
name: 'console', | ||
description: `Opens the web console for the ${featureName} category`, | ||
description: `Opens the web console for the ${name} category`, | ||
}, | ||
]; | ||
|
||
context.amplify.showHelp(header, commands); | ||
|
||
printer.info(''); | ||
printer.blankLine(); | ||
return context; | ||
}; | ||
|
||
export { run as analyticsPush } from './analytics/push'; | ||
export const name = featureName; | ||
export const run = analyticsRun; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/amplify-category-analytics/src/migrations/in-app-messaging-migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { | ||
$TSAny, | ||
$TSContext, AmplifyCategories, JSONUtilities, pathManager, stateManager, | ||
} from 'amplify-cli-core'; | ||
import fs from 'fs-extra'; | ||
import * as path from 'path'; | ||
import { pinpointHasInAppMessagingPolicy, pinpointInAppMessagingPolicyName } from '../utils/pinpoint-helper'; | ||
|
||
/** | ||
* checks if the project has been migrated to the latest version of in-app messaging | ||
*/ | ||
export const inAppMessagingMigrationCheck = async (context: $TSContext): Promise<void> => { | ||
if (['add', 'update', 'push'].includes(context.input.command) && !pinpointHasInAppMessagingPolicy(context)) { | ||
const projectBackendDirPath = pathManager.getBackendDirPath(); | ||
const amplifyMeta = stateManager.getMeta(); | ||
const analytics = amplifyMeta[AmplifyCategories.ANALYTICS] || {}; | ||
Object.keys(analytics).forEach(resourceName => { | ||
const resourcePath = path.join(projectBackendDirPath, AmplifyCategories.ANALYTICS, resourceName); | ||
const templateFilePath = path.join(resourcePath, 'pinpoint-cloudformation-template.json'); | ||
const cfn = JSONUtilities.readJson(templateFilePath); | ||
const updatedCfn = migratePinpointCFN(cfn); | ||
fs.ensureDirSync(resourcePath); | ||
JSONUtilities.writeJson(templateFilePath, updatedCfn); | ||
}); | ||
} | ||
}; | ||
|
||
const migratePinpointCFN = (cfn: $TSAny): $TSAny => { | ||
const { Parameters, Conditions, Resources } = cfn; | ||
|
||
Parameters[pinpointInAppMessagingPolicyName] = { | ||
Type: 'String', | ||
Default: 'NONE', | ||
}; | ||
|
||
Conditions.ShouldEnablePinpointInAppMessaging = { | ||
'Fn::Not': [ | ||
{ | ||
'Fn::Equals': [ | ||
{ | ||
Ref: 'pinpointInAppMessagingPolicyName', | ||
}, | ||
'NONE', | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
Resources.PinpointInAppMessagingPolicy = { | ||
Condition: 'ShouldEnablePinpointInAppMessaging', | ||
Type: 'AWS::IAM::Policy', | ||
Properties: { | ||
PolicyName: { | ||
Ref: 'pinpointInAppMessagingPolicyName', | ||
}, | ||
Roles: [ | ||
{ | ||
Ref: 'unauthRoleName', | ||
}, | ||
{ | ||
Ref: 'authRoleName', | ||
}, | ||
], | ||
PolicyDocument: { | ||
Version: '2012-10-17', | ||
Statement: [ | ||
{ | ||
Effect: 'Allow', | ||
Action: [ | ||
'mobiletargeting:GetInAppMessages', | ||
], | ||
Resource: [ | ||
{ | ||
'Fn::Join': [ | ||
'', | ||
[ | ||
'arn:aws:mobiletargeting:', | ||
{ | ||
'Fn::FindInMap': [ | ||
'RegionMapping', | ||
{ | ||
Ref: 'AWS::Region', | ||
}, | ||
'pinpointRegion', | ||
], | ||
}, | ||
':', | ||
{ | ||
Ref: 'AWS::AccountId', | ||
}, | ||
':apps/', | ||
{ | ||
'Fn::GetAtt': [ | ||
'PinpointFunctionOutputs', | ||
'Id', | ||
], | ||
}, | ||
'*', | ||
], | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
return cfn; | ||
}; |
17 changes: 17 additions & 0 deletions
17
packages/amplify-category-analytics/src/migrations/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { $TSContext } from 'amplify-cli-core'; | ||
import { inAppMessagingMigrationCheck } from './in-app-messaging-migration'; | ||
|
||
/** | ||
* Analytics plugin migrations | ||
* @param context amplify CLI context | ||
*/ | ||
export const analyticsMigrations = async (context: $TSContext): Promise<void> => { | ||
await inAppMessagingMigrationCheck(context); | ||
}; | ||
|
||
/** | ||
* checks if the project has been migrated to the latest version | ||
*/ | ||
export const migrationCheck = async (context: $TSContext): Promise<void> => { | ||
await analyticsMigrations(context); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/amplify-category-analytics/src/utils/analytics-helper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
$TSContext, AmplifyCategories, IAnalyticsResource, stateManager, | ||
} from 'amplify-cli-core'; | ||
|
||
/** | ||
* Get all analytics resources. If resourceProviderService name is provided, | ||
* then only return resources matching the service. | ||
* @returns Array of resources in Analytics category (IAmplifyResource type) | ||
*/ | ||
export const getAnalyticsResources = (context?: $TSContext, resourceProviderServiceName?: string): IAnalyticsResource[] => { | ||
const resourceList: Array<IAnalyticsResource> = []; | ||
const amplifyMeta = (context) ? context.exeInfo.amplifyMeta : stateManager.getMeta(); | ||
if (amplifyMeta?.[AmplifyCategories.ANALYTICS]) { | ||
const categoryResources = amplifyMeta[AmplifyCategories.ANALYTICS]; | ||
Object.keys(categoryResources).forEach(resource => { | ||
// if resourceProviderService is provided, then only return resources provided by that service | ||
// else return all resources. e.g. Pinpoint, Kinesis | ||
if (!resourceProviderServiceName || categoryResources[resource].service === resourceProviderServiceName) { | ||
resourceList.push({ | ||
category: AmplifyCategories.ANALYTICS, | ||
resourceName: resource, | ||
service: categoryResources[resource].service, | ||
region: categoryResources[resource]?.output?.Region, | ||
id: categoryResources[resource]?.output?.Id, | ||
output: categoryResources[resource]?.output, | ||
}); | ||
} | ||
}); | ||
} | ||
return resourceList; | ||
}; |
Oops, something went wrong.