Skip to content

Commit

Permalink
Pass values from global config in service config (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Oct 31, 2023
1 parent e00af44 commit 3df47f9
Show file tree
Hide file tree
Showing 20 changed files with 242 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-wasps-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": minor
---

Pass values from global config in service config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AWS from "aws-sdk";

AWS.config.update({ region: "us-west-2" });

const client = new AWS.DynamoDB({
region: "us-east-1"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import AWS from "aws-sdk";

import { DynamoDB } from "@aws-sdk/client-dynamodb";

// JS SDK v3 does not support global configuration.
// Codemod has attempted to pass values to each service client in this file.
// You may need to update clients outside of this file, if they use global config.
AWS.config.update({ region: "us-west-2" });

const client = new DynamoDB({
region: "us-east-1"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AWS from "aws-sdk";

AWS.config.update({ region: "us-west-2" });

const client = new AWS.DynamoDB({
logger: console
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import AWS from "aws-sdk";

import { DynamoDB } from "@aws-sdk/client-dynamodb";

// JS SDK v3 does not support global configuration.
// Codemod has attempted to pass values to each service client in this file.
// You may need to update clients outside of this file, if they use global config.
AWS.config.update({ region: "us-west-2" });

const client = new DynamoDB({
logger: console,
region: "us-west-2"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import AWS from "aws-sdk";

AWS.config.update({ region: "us-west-2" });

const client = new AWS.DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import AWS from "aws-sdk";

import { DynamoDB } from "@aws-sdk/client-dynamodb";

// JS SDK v3 does not support global configuration.
// Codemod has attempted to pass values to each service client in this file.
// You may need to update clients outside of this file, if they use global config.
AWS.config.update({ region: "us-west-2" });

const client = new DynamoDB({
region: "us-west-2"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AWS from "aws-sdk";

AWS.config.update({ region: "us-west-2" });

const client = new AWS.Config({
region: "us-east-1"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import AWS from "aws-sdk";

// JS SDK v3 does not support global configuration.
// Codemod has attempted to pass values to each service client in this file.
// You may need to update clients outside of this file, if they use global config.
AWS.config.update({ region: "us-west-2" });

const client = {
region: "us-east-1"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AWS from "aws-sdk";

AWS.config.update({ region: "us-west-2" });

const client = new AWS.Config({
logger: console
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import AWS from "aws-sdk";

// JS SDK v3 does not support global configuration.
// Codemod has attempted to pass values to each service client in this file.
// You may need to update clients outside of this file, if they use global config.
AWS.config.update({ region: "us-west-2" });

const client = {
logger: console,
region: "us-west-2"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import AWS from "aws-sdk";

AWS.config.update({ region: "us-west-2" });

const config = new AWS.Config();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import AWS from "aws-sdk";

// JS SDK v3 does not support global configuration.
// Codemod has attempted to pass values to each service client in this file.
// You may need to update clients outside of this file, if they use global config.
AWS.config.update({ region: "us-west-2" });

const config = {
region: "us-west-2"
};
46 changes: 46 additions & 0 deletions src/transforms/v2-to-v3/client-instances/getAwsGlobalConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Collection, JSCodeshift, ObjectExpression } from "jscodeshift";

export const getAwsGlobalConfig = (
j: JSCodeshift,
source: Collection<unknown>,
v2GlobalName?: string
): ObjectExpression => {
const objectExpression = j.objectExpression([]);

if (!v2GlobalName) return objectExpression;

source
.find(j.CallExpression, {
callee: {
type: "MemberExpression",
object: {
type: "MemberExpression",
object: { type: "Identifier", name: v2GlobalName },
property: { type: "Identifier", name: "config" },
},
property: { type: "Identifier", name: "update" },
},
})
.filter(
({ node }) => node.arguments.length === 1 && node.arguments[0].type === "ObjectExpression"
)
.forEach(({ node }) => {
const objectExpressionProperties = (node.arguments[0] as ObjectExpression).properties;

objectExpressionProperties.forEach((property) => {
objectExpression.properties.push(property);
});

const comments = node.comments || [];
comments.push(
j.commentLine(" JS SDK v3 does not support global configuration."),
j.commentLine(" Codemod has attempted to pass values to each service client in this file."),
j.commentLine(
" You may need to update clients outside of this file, if they use global config."
)
);
node.comments = comments;
});

return objectExpression;
};
20 changes: 16 additions & 4 deletions src/transforms/v2-to-v3/client-instances/getNewClientExpression.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import { ASTPath, JSCodeshift, NewExpression } from "jscodeshift";
import { ASTPath, JSCodeshift, NewExpression, ObjectExpression } from "jscodeshift";
import { getObjectWithUpdatedAwsConfigKeys } from "./getObjectWithUpdatedAwsConfigKeys";

export interface GetNewClientExpressionOptions {
v2ClientNewExpression: ASTPath<NewExpression>;
awsGlobalConfig: ObjectExpression;
}

export const getNewClientExpression = (
j: JSCodeshift,
clientName: string,
v2ClientNewExpression: ASTPath<NewExpression>
{ v2ClientNewExpression, awsGlobalConfig }: GetNewClientExpressionOptions
) => {
const newClientArguments = [];

const v2ClientArguments = v2ClientNewExpression.node.arguments;
if (v2ClientArguments.length === 1 && v2ClientArguments[0].type === "ObjectExpression") {
newClientArguments.push(getObjectWithUpdatedAwsConfigKeys(j, v2ClientArguments[0]));

if (v2ClientArguments.length === 0 && awsGlobalConfig.properties.length > 0) {
newClientArguments.push(
getObjectWithUpdatedAwsConfigKeys(j, j.objectExpression([]), awsGlobalConfig)
);
} else if (v2ClientArguments.length === 1 && v2ClientArguments[0].type === "ObjectExpression") {
newClientArguments.push(
getObjectWithUpdatedAwsConfigKeys(j, v2ClientArguments[0], awsGlobalConfig)
);
} else {
newClientArguments.push(...v2ClientArguments);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JSCodeshift, ObjectExpression, ObjectProperty, Property } from "jscodeshift";
import { Identifier, JSCodeshift, ObjectExpression, ObjectProperty, Property } from "jscodeshift";
import { AWS_CONFIG_KEY_MAP, OBJECT_PROPERTY_TYPE_LIST } from "../config";

const getRenameComment = (keyName: string, newKeyName: string) =>
Expand All @@ -15,12 +15,46 @@ const getCodemodUnsuppportedComments = (keyName: string) => [

export const getObjectWithUpdatedAwsConfigKeys = (
j: JSCodeshift,
objectExpression: ObjectExpression
objectExpression: ObjectExpression,
awsGlobalConfig: ObjectExpression
) => {
const securityCredentialKeys = ["accessKeyId", "secretAccessKey", "sessionToken"];
const credentials = j.objectExpression([]);

const updatedProperties = objectExpression.properties
const propertiesToUpdate = objectExpression.properties;

// Add properties from awsGlobalConfig
for (const property of awsGlobalConfig.properties) {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
propertiesToUpdate.push(property);
continue;
}

const propertyKey = (property as Property | ObjectProperty).key;
if (propertyKey.type !== "Identifier") {
propertiesToUpdate.push(property);
continue;
}

const propertyKeyName = (propertyKey as Identifier).name;
if (
!propertiesToUpdate
.filter((propertyToUpdate) => OBJECT_PROPERTY_TYPE_LIST.includes(propertyToUpdate.type))
.filter(
(propertyToUpdate) =>
(propertyToUpdate as Property | ObjectProperty).key.type === "Identifier"
)
.some(
(propertyToUpdate) =>
((propertyToUpdate as Property | ObjectProperty).key as Identifier).name ===
propertyKeyName
)
) {
propertiesToUpdate.push(property);
}
}

const updatedProperties = propertiesToUpdate
.map((property) => {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
return property;
Expand Down
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/client-instances/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./getAwsGlobalConfig";
export * from "./replaceAwsConfig";
export * from "./replaceClientCreation";
export * from "./replaceDocClientCreation";
21 changes: 16 additions & 5 deletions src/transforms/v2-to-v3/client-instances/replaceAwsConfig.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Collection, JSCodeshift, ObjectExpression } from "jscodeshift";
import { getObjectWithUpdatedAwsConfigKeys } from "./getObjectWithUpdatedAwsConfigKeys";

export interface ReplaceAwsConfigOptions {
v2GlobalName?: string;
awsGlobalConfig: ObjectExpression;
}

export const replaceAwsConfig = (
j: JSCodeshift,
source: Collection<unknown>,
v2GlobalName?: string
{ v2GlobalName, awsGlobalConfig }: ReplaceAwsConfigOptions
) => {
if (!v2GlobalName) return;

Expand All @@ -17,9 +22,15 @@ export const replaceAwsConfig = (
},
})
.filter(
({ node }) => node.arguments.length === 1 && node.arguments[0].type === "ObjectExpression"
({ node }) =>
node.arguments.length === 0 ||
(node.arguments.length === 1 && node.arguments[0].type === "ObjectExpression")
)
.replaceWith(({ node }) =>
getObjectWithUpdatedAwsConfigKeys(j, node.arguments[0] as ObjectExpression)
);
.replaceWith(({ node }) => {
const objectExpression =
node.arguments.length === 0
? j.objectExpression([])
: (node.arguments[0] as ObjectExpression);
return getObjectWithUpdatedAwsConfigKeys(j, objectExpression, awsGlobalConfig);
});
};
15 changes: 11 additions & 4 deletions src/transforms/v2-to-v3/client-instances/replaceClientCreation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { Collection, JSCodeshift, ObjectExpression } from "jscodeshift";
import { getClientNewExpression } from "../utils";
import { getNewClientExpression } from "./getNewClientExpression";

Expand All @@ -7,27 +7,34 @@ export interface ReplaceClientCreationOptions {
v2ClientLocalName: string;
v2GlobalName?: string;
v3ClientName: string;
awsGlobalConfig: ObjectExpression;
}

// Replace v2 client creation with v3 client creation.
export const replaceClientCreation = (
j: JSCodeshift,
source: Collection<unknown>,
{ v2ClientName, v2ClientLocalName, v2GlobalName, v3ClientName }: ReplaceClientCreationOptions
{
v2ClientName,
v2ClientLocalName,
v2GlobalName,
v3ClientName,
awsGlobalConfig,
}: ReplaceClientCreationOptions
): void => {
const clientName = v2ClientName === v2ClientLocalName ? v3ClientName : v2ClientLocalName;

source
.find(j.NewExpression, getClientNewExpression({ v2ClientName, v2ClientLocalName }))
.replaceWith((v2ClientNewExpression) =>
getNewClientExpression(j, clientName, v2ClientNewExpression)
getNewClientExpression(j, clientName, { v2ClientNewExpression, awsGlobalConfig })
);

if (v2GlobalName) {
source
.find(j.NewExpression, getClientNewExpression({ v2GlobalName, v2ClientName }))
.replaceWith((v2ClientNewExpression) =>
getNewClientExpression(j, clientName, v2ClientNewExpression)
getNewClientExpression(j, clientName, { v2ClientNewExpression, awsGlobalConfig })
);
}
};
6 changes: 4 additions & 2 deletions src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "./apis";
import { replaceAwsUtilFunctions } from "./aws-util";
import {
getAwsGlobalConfig,
replaceAwsConfig,
replaceClientCreation,
replaceDocClientCreation,
Expand Down Expand Up @@ -69,6 +70,7 @@ const transformer = async (file: FileInfo, api: API) => {
return source.toSource();
}

const awsGlobalConfig = getAwsGlobalConfig(j, source, v2GlobalName);
for (const [v2ClientName, v3ClientMetadata] of Object.entries(clientMetadataRecord)) {
const clientIdentifiers = clientIdentifiersRecord[v2ClientName];
const { v2ClientLocalName, v3ClientName, v3ClientPackageName } = v3ClientMetadata;
Expand All @@ -93,10 +95,10 @@ const transformer = async (file: FileInfo, api: API) => {

replaceWaiterApi(j, source, clientIdentifiers);

replaceClientCreation(j, source, { ...v2Options, v3ClientName });
replaceClientCreation(j, source, { ...v2Options, v3ClientName, awsGlobalConfig });
replaceDocClientCreation(j, source, v2Options);
}
replaceAwsConfig(j, source, v2GlobalName);
replaceAwsConfig(j, source, { v2GlobalName, awsGlobalConfig });
replaceAwsIdentity(j, source, { v2GlobalName, importType });
replaceAwsUtilFunctions(j, source, v2GlobalName);
removeGlobalModule(j, source, v2GlobalName);
Expand Down

0 comments on commit 3df47f9

Please sign in to comment.