Skip to content

Commit 4fd733b

Browse files
committed
fix: delete issues
1 parent 64160ad commit 4fd733b

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

layers/si-bootstrap-sdk/src/clients/index.ts

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import Tablestore20201209, * as ots from '@alicloud/tablestore20201209';
1+
import Client, {
2+
DeleteInstanceRequest,
3+
CreateInstanceRequestTags,
4+
CreateInstanceRequest,
5+
GetInstanceRequest,
6+
} from '@alicloud/tablestore20201209';
27
import { Config } from '@alicloud/credentials';
38
import TableStore from 'tablestore';
49
import * as teaUtil from '@alicloud/tea-util';
510
import { Credentials } from '../tyes';
611
import { formatOtsTableParam, logger } from '../common';
712

8-
const loadAlicloudTableStoreClient = (
9-
region: string,
10-
credentials: Credentials,
11-
): Tablestore20201209 => {
13+
const loadAlicloudTableStoreClient = (region: string, credentials: Credentials): Client => {
1214
const credentialsConfig = new Config({ ...credentials, type: 'access_key' });
1315
credentialsConfig.endpoint = `tablestore.${region}.aliyuncs.com`;
1416

15-
return new Tablestore20201209(credentialsConfig);
17+
return new Client(credentialsConfig);
1618
};
1719

1820
const loadTableStore = (
@@ -46,12 +48,12 @@ export const loadTableStoreClient = (regionId: string, credentials: Credentials)
4648
tags?: Array<{ key: string; value: string }>;
4749
}) => {
4850
const tags = rawTags?.map(
49-
(tag) => new ots.CreateInstanceRequestTags({ key: tag.key, value: tag.value }),
51+
(tag) => new CreateInstanceRequestTags({ key: tag.key, value: tag.value }),
5052
);
5153
const networkTypeACL =
5254
network.type === 'PRIVATE' ? ['VPC'] : network.type === 'PUBLIC' ? ['PUBLIC'] : [];
5355

54-
const createInstanceRequest = new ots.CreateInstanceRequest({
56+
const createInstanceRequest = new CreateInstanceRequest({
5557
instanceName: instanceName,
5658
clusterType: clusterType,
5759
networkTypeACL,
@@ -93,7 +95,7 @@ export const loadTableStoreClient = (regionId: string, credentials: Credentials)
9395
const runtime = new teaUtil.RuntimeOptions({});
9496
const headers: { [key: string]: string } = {};
9597
const response = await alicloudTableStoreClient.getInstanceWithOptions(
96-
new ots.GetInstanceRequest({ instanceName }),
98+
new GetInstanceRequest({ instanceName }),
9799
headers,
98100
runtime,
99101
);
@@ -161,6 +163,7 @@ export const loadTableStoreClient = (regionId: string, credentials: Credentials)
161163
const {
162164
tableMeta: { tableName, primaryKey, definedColumn: columns },
163165
reservedThroughput,
166+
tableOptions,
164167
} = formatOtsTableParam(params);
165168

166169
const table = await client.describeTable({ tableName });
@@ -191,10 +194,10 @@ export const loadTableStoreClient = (regionId: string, credentials: Credentials)
191194

192195
if (!matchingCol) {
193196
// Column exists in table but not in new columns - delete
194-
acc.deletedColumns.push(existingCol);
197+
acc.deletedColumns!.push(existingCol);
195198
} else if (matchingCol.type !== existingCol.type) {
196199
// Column exists in both but type is different
197-
acc.typeChangedColumns.push(matchingCol);
200+
acc.modifiedColumns!.push(matchingCol);
198201
}
199202

200203
return acc;
@@ -219,7 +222,7 @@ export const loadTableStoreClient = (regionId: string, credentials: Credentials)
219222

220223
// @TODO: Handle column deletion and addition if needed
221224

222-
const result = await client.updateTable(tableParam);
225+
const result = await client.updateTable({ tableName, tableOptions, reservedThroughput });
223226

224227
logger.info(result, `Table updated successfully`);
225228

@@ -238,9 +241,40 @@ export const loadTableStoreClient = (regionId: string, credentials: Credentials)
238241
tableName: string;
239242
}) => {
240243
const client = loadTableStore(instanceName, regionId, credentials);
241-
const result = (await client.deleteTable({ tableName })) as { RequestId: string };
244+
try {
245+
const result = (await client.deleteTable({ tableName })) as { RequestId: string };
246+
return { instanceName, tableName, requestId: result?.RequestId };
247+
} catch (error) {
248+
logger.error({ instanceName, tableName, error }, `Failed to delete table`);
249+
if ((error as { code: number }).code === 404) {
250+
return { instanceName, tableName };
251+
}
252+
throw error;
253+
}
254+
},
255+
256+
deleteInstance: async (instanceName: string) => {
257+
const client = loadTableStore(instanceName, regionId, credentials);
258+
259+
const tables = await client.listTable({ instanceName });
260+
if (tables.tableNames.length != 0) {
261+
return;
262+
}
263+
try {
264+
await alicloudTableStoreClient.deleteInstance(new DeleteInstanceRequest({ instanceName }));
265+
} catch (error) {
266+
logger.error({ instanceName, error }, `Failed to delete instance`);
267+
const { statusCode, code } = error as { statusCode: number; code: string };
268+
if ((statusCode === 400 && code === 'IllegalOp') || statusCode === 404) {
269+
return { instanceName };
270+
}
271+
272+
throw error;
273+
}
274+
275+
logger.info({ instanceName }, `Instance deleted successfully`);
242276

243-
return { instanceName, tableName, requestId: result?.RequestId };
277+
return { instanceName };
244278
},
245279
};
246280
};

layers/si-bootstrap-sdk/src/resources/tablestore.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,15 @@ const updateTableStore = async (
7676
) => {
7777
const tableStoreClient = loadTableStoreClient(regionId, credentials);
7878

79-
const { instanceName, tableName, reservedThroughput } = properties;
79+
const { instanceName, tableName } = properties;
8080

8181
const instance = await tableStoreClient.getInstance(instanceName);
8282
if (!instance) {
8383
throw new Error(`Instance ${instanceName} does not exist.`);
8484
}
8585

8686
try {
87-
const updatedTable = await tableStoreClient.updateTable({
88-
instanceName,
89-
tableName,
90-
reservedThroughput,
91-
});
87+
const updatedTable = await tableStoreClient.updateTable(properties);
9288

9389
return {
9490
physicalResourceId: updatedTable.tableName,
@@ -124,6 +120,8 @@ const deleteTableStore = async (
124120
tableName,
125121
});
126122

123+
await tableStoreClient.deleteInstance(instanceName);
124+
127125
return { physicalResourceId: tableName, data: result };
128126
} catch (error) {
129127
logger.error({ instanceName, tableName, error }, 'Failed to delete table');

src/stack/rosStack/bootstrap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const getBootstrapTemplate = async (context: Context) => {
3838
Description: 'ServerlessInsight Bootstrap API',
3939
Handler: 'index.handler',
4040
Runtime: 'nodejs20',
41-
Layers: ['acs:fc:cn-hangzhou:1990893136649406:layers/si-bootstrap-sdk/versions/9'],
41+
Layers: ['acs:fc:cn-hangzhou:1990893136649406:layers/si-bootstrap-sdk/versions/18'],
4242
Code: {
4343
SourceCode: `
4444
const { bootstrapHandler } = require('@geek-fun/si-bootstrap-sdk');

0 commit comments

Comments
 (0)