Skip to content

Commit

Permalink
feat: metadata collection for construct methods (#33292)
Browse files Browse the repository at this point in the history
### Issue # (if applicable)

N/A

### Reason for this change

Support construct public method logging. Method name, keys, and property
values of Boolean and ENUM types - When you use an L2 construct method,
we will collect the method name, property keys, and property values of
Boolean and enum types.

### Description of changes

Based on the discussion
#33198. This change adds
`@MetadataMethod` decorator to public construct methods which will allow
metadata logging during synthesis into `AWS::CDK::Metadata` resource.

### Describe any new or updated permissions being added

N/A

### Description of how you validated changes

No integ test is possible as integ test requires analytics reporting to
be false. Unit tests added.

### Checklist
- [ ] My code adheres to the [CONTRIBUTING
GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and
[DESIGN
GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

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

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
GavinZZ and mergify[bot] authored Feb 5, 2025
1 parent 3403da4 commit bc96ee1
Show file tree
Hide file tree
Showing 164 changed files with 10,830 additions and 1,839 deletions.
7 changes: 6 additions & 1 deletion packages/@aws-cdk/aws-amplify-alpha/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { BasicAuth } from './basic-auth';
import { Branch, BranchOptions } from './branch';
import { Domain, DomainOptions } from './domain';
import { renderEnvironmentVariables } from './utils';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
import { addConstructMetadata, MethodMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* An Amplify Console application
Expand Down Expand Up @@ -280,6 +280,7 @@ export class App extends Resource implements IApp, iam.IGrantable {
/**
* Adds a custom rewrite/redirect rule to this application
*/
@MethodMetadata()
public addCustomRule(rule: CustomRule) {
this.customRules.push(rule);
return this;
Expand All @@ -291,6 +292,7 @@ export class App extends Resource implements IApp, iam.IGrantable {
* All environment variables that you add are encrypted to prevent rogue
* access so you can use them to store secret information.
*/
@MethodMetadata()
public addEnvironment(name: string, value: string) {
this.environmentVariables[name] = value;
return this;
Expand All @@ -302,6 +304,7 @@ export class App extends Resource implements IApp, iam.IGrantable {
* All environment variables that you add are encrypted to prevent rogue
* access so you can use them to store secret information.
*/
@MethodMetadata()
public addAutoBranchEnvironment(name: string, value: string) {
this.autoBranchEnvironmentVariables[name] = value;
return this;
Expand All @@ -310,6 +313,7 @@ export class App extends Resource implements IApp, iam.IGrantable {
/**
* Adds a branch to this application
*/
@MethodMetadata()
public addBranch(id: string, options: BranchOptions = {}): Branch {
return new Branch(this, id, {
...options,
Expand All @@ -320,6 +324,7 @@ export class App extends Resource implements IApp, iam.IGrantable {
/**
* Adds a domain to this application
*/
@MethodMetadata()
public addDomain(id: string, options: DomainOptions = {}): Domain {
return new Domain(this, id, {
...options,
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-amplify-alpha/lib/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { IApp } from './app';
import { BasicAuth } from './basic-auth';
import { renderEnvironmentVariables } from './utils';
import { AssetDeploymentIsCompleteFunction, AssetDeploymentOnEventFunction } from '../custom-resource-handlers/dist/aws-amplify-alpha/asset-deployment-provider.generated';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
import { addConstructMetadata, MethodMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* A branch
Expand Down Expand Up @@ -206,6 +206,7 @@ export class Branch extends Resource implements IBranch {
* All environment variables that you add are encrypted to prevent rogue
* access so you can use them to store secret information.
*/
@MethodMetadata()
public addEnvironment(name: string, value: string) {
this.environmentVariables[name] = value;
return this;
Expand Down
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Construct } from 'constructs';
import { CfnDomain } from 'aws-cdk-lib/aws-amplify';
import { IApp } from './app';
import { IBranch } from './branch';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
import { addConstructMetadata, MethodMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* Options to add a domain to an application
Expand Down Expand Up @@ -171,6 +171,7 @@ export class Domain extends Resource {
* @param branch The branch
* @param prefix The prefix. Use '' to map to the root of the domain. Defaults to branch name.
*/
@MethodMetadata()
public mapSubDomain(branch: IBranch, prefix?: string) {
this.subDomains.push({ branch, prefix });
return this;
Expand All @@ -179,6 +180,7 @@ export class Domain extends Resource {
/**
* Maps a branch to the domain root
*/
@MethodMetadata()
public mapRoot(branch: IBranch) {
return this.mapSubDomain(branch, '');
}
Expand Down
5 changes: 4 additions & 1 deletion packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CfnService } from 'aws-cdk-lib/aws-apprunner';
import { IVpcConnector } from './vpc-connector';
import { IAutoScalingConfiguration } from './auto-scaling-configuration';
import { IObservabilityConfiguration } from './observability-configuration';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
import { addConstructMetadata, MethodMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* The image repository types
Expand Down Expand Up @@ -1373,13 +1373,15 @@ export class Service extends cdk.Resource implements IService, iam.IGrantable {
/**
* Adds a statement to the instance role.
*/
@MethodMetadata()
public addToRolePolicy(statement: iam.PolicyStatement) {
this.instanceRole.addToPrincipalPolicy(statement);
}

/**
* This method adds an environment variable to the App Runner service.
*/
@MethodMetadata()
public addEnvironmentVariable(name: string, value: string) {
if (name.startsWith('AWSAPPRUNNER')) {
throw new Error(`Environment variable key ${name} with a prefix of AWSAPPRUNNER is not allowed`);
Expand All @@ -1390,6 +1392,7 @@ export class Service extends cdk.Resource implements IService, iam.IGrantable {
/**
* This method adds a secret as environment variable to the App Runner service.
*/
@MethodMetadata()
public addSecret(name: string, secret: Secret) {
if (name.startsWith('AWSAPPRUNNER')) {
throw new Error(`Environment secret key ${name} with a prefix of AWSAPPRUNNER is not allowed`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Resource, IResource, Stack, ArnFormat, Lazy, Token } from 'aws-cdk-lib/
import { Construct } from 'constructs';
import { IdentityPoolRoleAttachment, IdentityPoolRoleMapping } from './identitypool-role-attachment';
import { IUserPoolAuthenticationProvider } from './identitypool-user-pool-authentication-provider';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
import { addConstructMetadata, MethodMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* Represents a Cognito Identity Pool
Expand Down Expand Up @@ -418,6 +418,7 @@ export class IdentityPool extends Resource implements IIdentityPool {
/**
* Add a User Pool to the Identity Pool and configure the User Pool client to handle identities
*/
@MethodMetadata()
public addUserPoolAuthentication(userPool: IUserPoolAuthenticationProvider): void {
const providers = userPool.bind(this, this);
this.cognitoIdentityProviders = this.cognitoIdentityProviders.concat(providers);
Expand All @@ -426,6 +427,7 @@ export class IdentityPool extends Resource implements IIdentityPool {
/**
* Add Role Mappings to the Identity Pool
*/
@MethodMetadata()
public addRoleMappings(...roleMappings: IdentityPoolRoleMapping[]): void {
if (!roleMappings || !roleMappings.length) return;
this.roleAttachmentCount++;
Expand Down
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-ec2-alpha/lib/ipam.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CfnIPAM, CfnIPAMPool, CfnIPAMPoolCidr, CfnIPAMScope } from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';
import { Lazy, Names, Resource, Stack, Tags } from 'aws-cdk-lib';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
import { addConstructMetadata, MethodMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* Represents the address family for IP addresses in an IPAM pool.
Expand Down Expand Up @@ -373,6 +373,7 @@ class IpamPool extends Resource implements IIpamPool {
* @param options Either a CIDR or netmask length must be provided
* @returns AWS::EC2::IPAMPoolCidr
*/
@MethodMetadata()
public provisionCidr(id: string, options: IpamPoolCidrProvisioningOptions): CfnIPAMPoolCidr {
const cidr = new CfnIPAMPoolCidr(this, id, {
...options,
Expand Down Expand Up @@ -549,6 +550,7 @@ export class Ipam extends Resource {
* Function to add custom scope to an existing IPAM
* Custom scopes can only be private
*/
@MethodMetadata()
public addScope(scope: Construct, id: string, options: IpamScopeOptions): IIpamScopeBase {
const ipamScope = new IpamScope(scope, id, {
...options,
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-ec2-alpha/lib/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Annotations, Duration, IResource, Resource, Tags } from 'aws-cdk-lib/co
import { IVpcV2, VPNGatewayV2Options } from './vpc-v2-base';
import { NetworkUtils, allRouteTableIds, CidrBlock } from './util';
import { ISubnetV2 } from './subnet-v2';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
import { addConstructMetadata, MethodMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* Indicates whether the NAT gateway supports public or private connectivity.
Expand Down Expand Up @@ -819,6 +819,7 @@ export class RouteTable extends Resource implements IRouteTable {
* @param target The gateway or endpoint targeted by the route.
* @param routeName The resource name of the route.
*/
@MethodMetadata()
public addRoute(id: string, destination: string, target: RouteTargetType, routeName?: string) {
new Route(this, id, {
routeTable: this,
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-ec2-alpha/lib/subnet-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Construct, DependencyGroup, IDependable } from 'constructs';
import { IVpcV2 } from './vpc-v2-base';
import { CidrBlock, CidrBlockIpv6 } from './util';
import { RouteTable } from './route';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
import { addConstructMetadata, MethodMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* Interface to define subnet CIDR
Expand Down Expand Up @@ -330,6 +330,7 @@ export class SubnetV2 extends Resource implements ISubnetV2 {
* @param networkAcl The Network ACL to associate with this subnet.
* This allows controlling inbound and outbound traffic for instances in this subnet.
*/
@MethodMetadata()
public associateNetworkAcl(id: string, networkAcl: INetworkAcl) {
this._networkAcl = networkAcl;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ITransitGatewayRouteTable } from './transit-gateway-route-table';
import { CfnTransitGatewayRouteTableAssociation } from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';
import { ITransitGatewayAssociation, TransitGatewayAssociationBase } from './transit-gateway-association';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* Represents a Transit Gateway Route Table Association.
Expand Down Expand Up @@ -44,6 +45,8 @@ export class TransitGatewayRouteTableAssociation extends TransitGatewayAssociati

constructor(scope: Construct, id: string, props: TransitGatewayRouteTableAssociationProps) {
super(scope, id);
// Enhanced CDK Analytics Telemetry
addConstructMetadata(this, props);

const resource = new CfnTransitGatewayRouteTableAssociation(this, id, {
transitGatewayAttachmentId: props.transitGatewayVpcAttachment.transitGatewayAttachmentId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CfnTransitGatewayRouteTablePropagation } from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';
import { ITransitGatewayAttachment } from './transit-gateway-attachment';
import { ITransitGatewayRouteTable } from './transit-gateway-route-table';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* Represents a Transit Gateway Route Table Propagation.
Expand Down Expand Up @@ -50,6 +51,8 @@ export class TransitGatewayRouteTablePropagation extends Resource implements ITr

constructor(scope: Construct, id: string, props: TransitGatewayRouteTablePropagationProps) {
super(scope, id);
// Enhanced CDK Analytics Telemetry
addConstructMetadata(this, props);

const resource = new CfnTransitGatewayRouteTablePropagation(this, id, {
transitGatewayAttachmentId: props.transitGatewayVpcAttachment.transitGatewayAttachmentId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ITransitGatewayAttachment } from './transit-gateway-attachment';
import { TransitGatewayRoute, TransitGatewayBlackholeRoute, ITransitGatewayRoute } from './transit-gateway-route';
import { ITransitGatewayRouteTableAssociation, TransitGatewayRouteTableAssociation } from './transit-gateway-route-table-association';
import { ITransitGatewayRouteTablePropagation, TransitGatewayRouteTablePropagation } from './transit-gateway-route-table-propagation';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* Represents a Transit Gateway Route Table.
Expand Down Expand Up @@ -109,6 +110,8 @@ export class TransitGatewayRouteTable extends TransitGatewayRouteTableBase {

constructor(scope: Construct, id: string, props: TransitGatewayRouteTableProps) {
super(scope, id);
// Enhanced CDK Analytics Telemetry
addConstructMetadata(this, props);

const resource = new CfnTransitGatewayRouteTable(this, id, {
transitGatewayId: props.transitGateway.transitGatewayId,
Expand Down
5 changes: 5 additions & 0 deletions packages/@aws-cdk/aws-ec2-alpha/lib/transit-gateway-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Construct } from 'constructs';
import { CfnTransitGatewayRoute } from 'aws-cdk-lib/aws-ec2';
import { ITransitGatewayRouteTable } from './transit-gateway-route-table';
import { ITransitGatewayAttachment } from './transit-gateway-attachment';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* Represents a Transit Gateway Route.
Expand Down Expand Up @@ -87,6 +88,8 @@ export class TransitGatewayRoute extends TransitGatewayRouteBase {

constructor(scope: Construct, id: string, props: TransitGatewayRouteProps) {
super(scope, id);
// Enhanced CDK Analytics Telemetry
addConstructMetadata(this, props);

this.resource = new CfnTransitGatewayRoute(this, 'TransitGatewayRoute', {
blackhole: false,
Expand All @@ -112,6 +115,8 @@ export class TransitGatewayBlackholeRoute extends TransitGatewayRouteBase {

constructor(scope: Construct, id: string, props: TransitGatewayBlackholeRouteProps) {
super(scope, id);
// Enhanced CDK Analytics Telemetry
addConstructMetadata(this, props);

const resource = new CfnTransitGatewayRoute(this, id, {
blackhole: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ITransitGatewayAttachment, TransitGatewayAttachmentBase } from './trans
import { getFeatureStatus } from './util';
import { ITransitGatewayRouteTable } from './transit-gateway-route-table';
import { Annotations } from 'aws-cdk-lib';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* Options for Transit Gateway VPC Attachment.
Expand Down Expand Up @@ -131,6 +132,8 @@ export class TransitGatewayVpcAttachment extends TransitGatewayAttachmentBase im

constructor(scope: Construct, id: string, props: TransitGatewayVpcAttachmentProps) {
super(scope, id);
// Enhanced CDK Analytics Telemetry
addConstructMetadata(this, props);

this._resource = new CfnTransitGatewayAttachment(this, id, {
subnetIds: props.subnets.map((subnet) => subnet.subnetId),
Expand Down
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-ec2-alpha/lib/transit-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { IRouteTarget } from './route';
import { TransitGatewayRouteTableAssociation } from './transit-gateway-route-table-association';
import { TransitGatewayRouteTablePropagation } from './transit-gateway-route-table-propagation';
import { getFeatureStatus, TransitGatewayFeatureStatus } from './util';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* Represents a Transit Gateway.
Expand Down Expand Up @@ -222,6 +223,8 @@ export class TransitGateway extends TransitGatewayBase {

constructor(scope: Construct, id: string, props: TransitGatewayProps = {}) {
super(scope, id);
// Enhanced CDK Analytics Telemetry
addConstructMetadata(this, props);

const resource = new CfnTransitGateway(this, id, {
amazonSideAsn: props.amazonSideAsn ?? undefined,
Expand Down
4 changes: 4 additions & 0 deletions packages/@aws-cdk/aws-eks-v2-alpha/lib/access-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CfnAccessEntry } from 'aws-cdk-lib/aws-eks';
import {
Resource, IResource, Aws, Lazy,
} from 'aws-cdk-lib/core';
import { MethodMetadata, addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* Represents an access entry in an Amazon EKS cluster.
Expand Down Expand Up @@ -325,6 +326,8 @@ export class AccessEntry extends Resource implements IAccessEntry {

constructor(scope: Construct, id: string, props: AccessEntryProps ) {
super(scope, id);
// Enhanced CDK Analytics Telemetry
addConstructMetadata(this, props);

this.cluster = props.cluster;
this.principal = props.principal;
Expand Down Expand Up @@ -356,6 +359,7 @@ export class AccessEntry extends Resource implements IAccessEntry {
* Add the access policies for this entry.
* @param newAccessPolicies - The new access policies to add.
*/
@MethodMetadata()
public addAccessPolicies(newAccessPolicies: IAccessPolicy[]): void {
// add newAccessPolicies to this.accessPolicies
this.accessPolicies.push(...newAccessPolicies);
Expand Down
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-eks-v2-alpha/lib/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Construct } from 'constructs';
import { ICluster } from './cluster';
import { CfnAddon } from 'aws-cdk-lib/aws-eks';
import { ArnFormat, IResource, Resource, Stack, Fn } from 'aws-cdk-lib/core';
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';

/**
* Represents an Amazon EKS Add-On.
Expand Down Expand Up @@ -127,6 +128,8 @@ export class Addon extends Resource implements IAddon {
super(scope, id, {
physicalName: props.addonName,
});
// Enhanced CDK Analytics Telemetry
addConstructMetadata(this, props);

this.clusterName = props.cluster.clusterName;
this.addonName = props.addonName;
Expand Down
Loading

0 comments on commit bc96ee1

Please sign in to comment.