Skip to content

Commit

Permalink
feat: add destroy and create options for generate physical name (#758)
Browse files Browse the repository at this point in the history
* feat: add destroy and create options for generate physical name

FIXES: 734

---------

Signed-off-by: Scott Schreckengaust <[email protected]>
Signed-off-by: github-actions <[email protected]>
Co-authored-by: github-actions <[email protected]>
Co-authored-by: Alain Krok <[email protected]>
  • Loading branch information
3 people authored Oct 21, 2024
1 parent 52fd030 commit 06e0f9d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/cdk-lib/bedrock/data-sources/s3-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ export class S3DataSource extends DataSourceNew {
// Assign attributes
this.knowledgeBase = props.knowledgeBase;
this.dataSourceType = DataSourceType.S3;
this.dataSourceName = props.dataSourceName ?? generatePhysicalNameV2(this, 's3-ds', { maxLength: 40, lower: true, separator: '-' });;

// Turns out chunking and parsing are not replace so pass
const chunkingStrategy = props.chunkingStrategy;
const parsingStrategy = props.parsingStrategy;
const theseAreNotReplacable = { chunkingStrategy, parsingStrategy };
this.dataSourceName = props.dataSourceName ?? generatePhysicalNameV2(this, 's3-ds', { maxLength: 40, lower: true, separator: '-', destroyCreate: theseAreNotReplacable });;
this.bucket = props.bucket;
this.kmsKey = props.kmsKey;

Expand Down
29 changes: 26 additions & 3 deletions src/common/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import { createHash } from 'crypto';
import * as cdk from 'aws-cdk-lib';
import { IConstruct } from 'constructs';
import { CfnNagSuppressRule } from '../../patterns/gen-ai/aws-rag-appsync-stepfn-kendra/types';
Expand Down Expand Up @@ -76,6 +77,12 @@ export interface GeneratePhysicalNameV2Options extends cdk.UniqueResourceNameOpt
* @default false
*/
lower?: boolean;

/**
* This object is hashed for uniqueness and can force a destroy instead of a replace.
* @default: undefined
*/
destroyCreate?: any;
}
/**
* @internal This is an internal core function and should not be called directly by Solutions Constructs clients.
Expand All @@ -102,20 +109,36 @@ export function generatePhysicalNameV2(
*/
options?: GeneratePhysicalNameV2Options,
): string {
function objectToHash(obj: any): string {
// Nothing to hash if undefined
if (obj === undefined) { return ''; }

// Convert the object to a JSON string
const jsonString = JSON.stringify(obj);

// Create a SHA-256 hash
const hash = createHash('sha256');

// Update the hash with the JSON string and get the digest in hexadecimal format
// Shorten it (modeled after seven characters like git commit hash shortening)
return hash.update(jsonString).digest('hex').slice(0, 7);
}
const {
maxLength = 256,
lower = false,
separator = '',
allowedSpecialCharacters = undefined,
destroyCreate = undefined,
} = options ?? {};
if (maxLength < (prefix + separator).length) {
const hash = objectToHash(destroyCreate);
if (maxLength < (prefix + hash + separator).length) {
throw new Error('The prefix is longer than the maximum length.');
}
const uniqueName = cdk.Names.uniqueResourceName(
scope,
{ maxLength: maxLength - (prefix + separator).length, separator, allowedSpecialCharacters },
{ maxLength: maxLength - (prefix + hash + separator).length, separator, allowedSpecialCharacters },
);
const name = `${prefix}${separator}${uniqueName}`;
const name = `${prefix}${hash}${separator}${uniqueName}`;
if (name.length > maxLength) {
throw new Error(`The generated name is longer than the maximum length of ${maxLength}`);
}
Expand Down
12 changes: 12 additions & 0 deletions test/common/helpers/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,18 @@ describe('generatePhysicalNameV2', () => {
}).toThrow(new RegExp('^The generated name is longer than the maximum length of'));
});

test('hash for a more unique name', () => {
const hashedName = generatePhysicalNameV2(
testResourceB,
'test',
{
destroyCreate: { one: 'XXX', two: true, three: undefined },
});

expect(hashedName).not.toMatch(new RegExp('^test' + testResourceB.stack.stackName));
expect(hashedName).toMatch(new RegExp('^test' + '[0-9a-f]{7}' + testResourceB.stack.stackName));
expect(hashedName).toEqual('test0221ffeTestStackAB27595CD3');
});

describe('kendra general utils', () => {
describe('addCfnSuppressRules', () => {
Expand Down

0 comments on commit 06e0f9d

Please sign in to comment.