1 | const { run } = require('./commands/api/console');
|
2 | const fs = require('fs-extra');
|
3 | const path = require('path');
|
4 |
|
5 | const category = 'api';
|
6 |
|
7 | const categories = 'categories';
|
8 |
|
9 | async function console(context) {
|
10 | await run(context);
|
11 | }
|
12 |
|
13 | async function migrate(context, serviceName) {
|
14 | const { projectPath, amplifyMeta } = context.migrationInfo;
|
15 | const migrateResourcePromises = [];
|
16 | Object.keys(amplifyMeta).forEach(categoryName => {
|
17 | if (categoryName === category) {
|
18 | Object.keys(amplifyMeta[category]).forEach(resourceName => {
|
19 | try {
|
20 | if (amplifyMeta[category][resourceName].providerPlugin) {
|
21 | const providerController = require(`./provider-utils/${amplifyMeta[category][resourceName].providerPlugin}/index`);
|
22 | if (providerController) {
|
23 | if (!serviceName || serviceName === amplifyMeta[category][resourceName].service) {
|
24 | migrateResourcePromises.push(
|
25 | providerController.migrateResource(context, projectPath, amplifyMeta[category][resourceName].service, resourceName)
|
26 | );
|
27 | }
|
28 | }
|
29 | } else {
|
30 | context.print.error(`Provider not configured for ${category}: ${resourceName}`);
|
31 | }
|
32 | } catch (e) {
|
33 | context.print.warning(`Could not run migration for ${category}: ${resourceName}`);
|
34 | throw e;
|
35 | }
|
36 | });
|
37 | }
|
38 | });
|
39 |
|
40 | await Promise.all(migrateResourcePromises);
|
41 | }
|
42 |
|
43 | async function initEnv(context) {
|
44 | const datasource = 'Aurora Serverless';
|
45 | const service = 'service';
|
46 | const rdsInit = 'rdsInit';
|
47 | const rdsRegion = 'rdsRegion';
|
48 | const rdsClusterIdentifier = 'rdsClusterIdentifier';
|
49 | const rdsSecretStoreArn = 'rdsSecretStoreArn';
|
50 | const rdsDatabaseName = 'rdsDatabaseName';
|
51 |
|
52 | const { amplify } = context;
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 | const backendConfigFilePath = amplify.pathManager.getBackendConfigFilePath();
|
59 | const backendConfig = amplify.readJsonFile(backendConfigFilePath);
|
60 |
|
61 | if (!backendConfig[category]) {
|
62 | return;
|
63 | }
|
64 |
|
65 | let resourceName;
|
66 | const apis = Object.keys(backendConfig[category]);
|
67 | for (let i = 0; i < apis.length; i += 1) {
|
68 | if (backendConfig[category][apis[i]][service] === 'AppSync') {
|
69 | resourceName = apis[i];
|
70 | break;
|
71 | }
|
72 | }
|
73 |
|
74 |
|
75 | if (!resourceName) {
|
76 | return;
|
77 | }
|
78 |
|
79 |
|
80 | if (!backendConfig[category][resourceName][rdsInit]) {
|
81 | return;
|
82 | }
|
83 |
|
84 | const providerController = require('./provider-utils/awscloudformation/index');
|
85 |
|
86 | if (!providerController) {
|
87 | context.print.error('Provider not configured for this category');
|
88 | return;
|
89 | }
|
90 |
|
91 | |
92 |
|
93 |
|
94 | const currEnv = amplify.getEnvInfo().envName;
|
95 | const teamProviderInfoFilePath = amplify.pathManager.getProviderInfoFilePath();
|
96 | const teamProviderInfo = amplify.readJsonFile(teamProviderInfoFilePath);
|
97 | if (
|
98 | teamProviderInfo[currEnv][categories] &&
|
99 | teamProviderInfo[currEnv][categories][category] &&
|
100 | teamProviderInfo[currEnv][categories][category][resourceName] &&
|
101 | teamProviderInfo[currEnv][categories][category][resourceName] &&
|
102 | teamProviderInfo[currEnv][categories][category][resourceName][rdsRegion]
|
103 | ) {
|
104 | return;
|
105 | }
|
106 |
|
107 | |
108 |
|
109 |
|
110 | return providerController
|
111 | .addDatasource(context, category, datasource)
|
112 | .then(answers => {
|
113 | |
114 |
|
115 |
|
116 | if (!teamProviderInfo[currEnv][categories]) {
|
117 | teamProviderInfo[currEnv][categories] = {};
|
118 | }
|
119 | if (!teamProviderInfo[currEnv][categories][category]) {
|
120 | teamProviderInfo[currEnv][categories][category] = {};
|
121 | }
|
122 | if (!teamProviderInfo[currEnv][categories][category][resourceName]) {
|
123 | teamProviderInfo[currEnv][categories][category][resourceName] = {};
|
124 | }
|
125 |
|
126 | teamProviderInfo[currEnv][categories][category][resourceName][rdsRegion] = answers.region;
|
127 | teamProviderInfo[currEnv][categories][category][resourceName][rdsClusterIdentifier] = answers.dbClusterArn;
|
128 | teamProviderInfo[currEnv][categories][category][resourceName][rdsSecretStoreArn] = answers.secretStoreArn;
|
129 | teamProviderInfo[currEnv][categories][category][resourceName][rdsDatabaseName] = answers.databaseName;
|
130 |
|
131 | fs.writeFileSync(teamProviderInfoFilePath, JSON.stringify(teamProviderInfo, null, 4));
|
132 | })
|
133 | .then(() => {
|
134 | context.amplify.executeProviderUtils(context, 'awscloudformation', 'compileSchema', { forceCompile: true });
|
135 | });
|
136 | }
|
137 |
|
138 | async function getPermissionPolicies(context, resourceOpsMapping) {
|
139 | const amplifyMetaFilePath = context.amplify.pathManager.getAmplifyMetaFilePath();
|
140 | const amplifyMeta = context.amplify.readJsonFile(amplifyMetaFilePath);
|
141 | const permissionPolicies = [];
|
142 | const resourceAttributes = [];
|
143 |
|
144 | Object.keys(resourceOpsMapping).forEach(resourceName => {
|
145 | try {
|
146 | const providerController = require(`./provider-utils/${amplifyMeta[category][resourceName].providerPlugin}/index`);
|
147 | if (providerController) {
|
148 | const { policy, attributes } = providerController.getPermissionPolicies(
|
149 | context,
|
150 | amplifyMeta[category][resourceName].service,
|
151 | resourceName,
|
152 | resourceOpsMapping[resourceName]
|
153 | );
|
154 | permissionPolicies.push(policy);
|
155 | resourceAttributes.push({ resourceName, attributes, category });
|
156 | } else {
|
157 | context.print.error(`Provider not configured for ${category}: ${resourceName}`);
|
158 | }
|
159 | } catch (e) {
|
160 | context.print.warning(`Could not get policies for ${category}: ${resourceName}`);
|
161 | throw e;
|
162 | }
|
163 | });
|
164 | return { permissionPolicies, resourceAttributes };
|
165 | }
|
166 |
|
167 | async function executeAmplifyCommand(context) {
|
168 | let commandPath = path.normalize(path.join(__dirname, 'commands'));
|
169 | if (context.input.command === 'help') {
|
170 | commandPath = path.join(commandPath, category);
|
171 | } else {
|
172 | commandPath = path.join(commandPath, category, context.input.command);
|
173 | }
|
174 |
|
175 | const commandModule = require(commandPath);
|
176 | await commandModule.run(context);
|
177 | }
|
178 |
|
179 | async function handleAmplifyEvent(context, args) {
|
180 | context.print.info(`${category} handleAmplifyEvent to be implemented`);
|
181 | context.print.info(`Received event args ${args}`);
|
182 | }
|
183 |
|
184 | module.exports = {
|
185 | console,
|
186 | migrate,
|
187 | initEnv,
|
188 | getPermissionPolicies,
|
189 | executeAmplifyCommand,
|
190 | handleAmplifyEvent,
|
191 | };
|