Skip to content

Conversation

@otaviomacedo
Copy link
Contributor

@otaviomacedo otaviomacedo commented Sep 12, 2025

For every L1 resource, generate a static factory method called from<Resource>Arn():

/**
 * Creates a new ITableRef from an ARN
 */
public static fromTableArn(scope: constructs.Construct, id: string, arn: string): ITableRef {
  class Import extends cdk.Resource {
    public tableRef: TableReference;

    /**
     * @param scope Scope in which this resource is defined
     * @param id Construct identifier for this resource (unique in its scope)
     */
    public constructor(scope: constructs.Construct, id: string, arn: string) {
      super(scope, id, {
        "environmentFromArn": arn
      });

      const variables = new cfn_parse.TemplateString("arn:${Partition}:dynamodb:${Region}:${Account}:table/${TableName}").parse(arn);
      this.tableRef = {
        "tableName": variables.TableName,
        "tableArn": arn
      };
    }
  }
  return new Import(scope, id, arn);
}

as well as a from<Prop>() (where <Prop> is the single field in the primary identifier):

/**
 * Creates a new ITableRef from a tableName
 */
public static fromTableName(scope: constructs.Construct, id: string, tableName: string): ITableRef {
  class Import extends cdk.Resource {
    public tableRef: TableReference;

    /**
     * @param scope Scope in which this resource is defined
     * @param id Construct identifier for this resource (unique in its scope)
     */
    public constructor(scope: constructs.Construct, id: string, tableName: string) {
      const arn = new cfn_parse.TemplateString("arn:${Partition}:dynamodb:${Region}:${Account}:table/${TableName}").interpolate({
        "Partition": cdk.Stack.of(scope).partition,
        "Region": cdk.Stack.of(scope).region,
        "Account": cdk.Stack.of(scope).account,
        "TableName": tableName
      });
      super(scope, id, {
        "environmentFromArn": arn
      });

      this.tableRef = {
        "tableName": tableName,
        "tableArn": arn
      };
    }
  }
  return new Import(scope, id, tableName);
}

Note: If the primary identifier for a given resource type has more than one field, we skip the generation of the factory methods for the corresponding class.

TemplateStringParser is a new class with two static methods (inverses of each other):

  • parse: matches a given ARN template with a concrete ARN string, and returns the values of each variable.
  • interpolate: given an ARN template and a map of variables, returns a string with the variables replaced with their respective values.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team September 12, 2025 13:13
@github-actions github-actions bot added the p2 label Sep 12, 2025
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Sep 12, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@otaviomacedo otaviomacedo changed the title feat(spec2cdk): generate from<Resource>Arn in every L1 feat(spec2cdk): generate from<Resource>Arn and from<Resource><Prop> in every L1 Sep 17, 2025
@otaviomacedo otaviomacedo marked this pull request as ready for review September 18, 2025 07:46
@otaviomacedo otaviomacedo requested a review from a team as a code owner September 18, 2025 07:46
});
});

describe('L1 static factory methods', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choosing one representative to test that the generated methods actually return the right thing.

@otaviomacedo otaviomacedo added the pr/do-not-merge This PR should not be merged at this time. label Sep 19, 2025
- environmentFromArn
- storing stackOfScope
# Conflicts:
#	packages/aws-cdk-lib/package.json
#	tools/@aws-cdk/spec2cdk/package.json
#	yarn.lock
@otaviomacedo otaviomacedo removed the pr/do-not-merge This PR should not be merged at this time. label Sep 26, 2025
@otaviomacedo otaviomacedo added pr-linter/exempt-readme The PR linter will not require README changes pr-linter/exempt-integ-test The PR linter will not require integ test changes labels Sep 26, 2025
@aws-cdk-automation aws-cdk-automation dismissed their stale review September 26, 2025 10:38

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@mergify mergify bot added the queued label Sep 26, 2025
@mergify
Copy link
Contributor

mergify bot commented Sep 26, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

1 similar comment
@mergify
Copy link
Contributor

mergify bot commented Sep 26, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

mergify bot added a commit that referenced this pull request Sep 26, 2025
@mergify mergify bot merged commit c588061 into main Sep 26, 2025
51 checks passed
@mergify mergify bot removed the queued label Sep 26, 2025
@mergify mergify bot deleted the otaviom/from-arn-l1 branch September 26, 2025 11:55
@github-actions
Copy link
Contributor

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 26, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

contribution/core This is a PR that came from AWS. p2 pr-linter/exempt-integ-test The PR linter will not require integ test changes pr-linter/exempt-readme The PR linter will not require README changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants