Skip to content

Commit

Permalink
chore(example-library): update example resource (#32802)
Browse files Browse the repository at this point in the history
### Issue # (if applicable)

N/A

### Reason for this change

The `example-resource.ts` library is slightly out of date with how CDK is structured today. These changes aim to align the file with what we do today.

### Description of changes

Altered the core import, as we do not (AFAIK) follow this convention any more:
```ts
import * as core from 'aws-cdk-lib/core';  // before
import { ... } from 'aws-cdk-lib/core';    // after
```

In addition, slightly changed around some comment phrasing.

### Describe any new or updated permissions being added

N/A

### Description of how you validated changes

Semantic changes; tests still continue to pass.

### Checklist
- [x] 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*
  • Loading branch information
Leo10Gama authored Jan 9, 2025
1 parent 8a96454 commit f2ade56
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions packages/@aws-cdk/example-construct-library/lib/example-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as events from 'aws-cdk-lib/aws-events';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as core from 'aws-cdk-lib/core';
import { Construct } from 'constructs';
// for files that are part of this package, we do import individual classes or functions
// for files that are part of this package or part of core, we do import individual classes or functions
import { CfnWaitCondition, CfnWaitConditionHandle, Fn, IResource, RemovalPolicy, Resource, Stack, Token } from 'aws-cdk-lib/core';
import { exampleResourceArnComponents } from './private/example-resource-common';

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ import { exampleResourceArnComponents } from './private/example-resource-common'
*/
export interface IExampleResource extends
// all L2 interfaces need to extend IResource
core.IResource,
IResource,

// Only for resources that have an associated IAM Role.
// Allows this resource to be the target in calls like bucket.grantRead(exampleResource).
Expand Down Expand Up @@ -149,7 +149,7 @@ export interface IExampleResource extends
*
* Notice that the class is not exported - it's not part of the public API of this module!
*/
abstract class ExampleResourceBase extends core.Resource implements IExampleResource {
abstract class ExampleResourceBase extends Resource implements IExampleResource {
// these stay abstract at this level
public abstract readonly exampleResourceArn: string;
public abstract readonly exampleResourceName: string;
Expand Down Expand Up @@ -319,7 +319,7 @@ export interface ExampleResourceProps {
*
* @default RemovalPolicy.RETAIN
*/
readonly removalPolicy?: core.RemovalPolicy;
readonly removalPolicy?: RemovalPolicy;
}

/**
Expand Down Expand Up @@ -364,7 +364,7 @@ export class ExampleResource extends ExampleResourceBase {
// using the Stack.formatArn helper method from the core library.
// We have to know the ARN components of ExampleResource in a few places, so,
// to avoid duplication, extract that into a module-private function
public readonly exampleResourceArn = core.Stack.of(scope)
public readonly exampleResourceArn = Stack.of(scope)
.formatArn(exampleResourceArnComponents(exampleResourceName));
}

Expand All @@ -382,8 +382,8 @@ export class ExampleResource extends ExampleResourceBase {

/**
* The constructor of a construct has always 3 arguments:
* the parent Construct, the string identifier,
* locally unique within the scope of the parent,
* the parent Construct, the string identifier
* (locally unique within the scope of the parent),
* and a properties struct.
*
* If the props only have optional properties, like in our case,
Expand Down Expand Up @@ -412,7 +412,7 @@ export class ExampleResource extends ExampleResourceBase {
// so, we need to use the Token.isUnresolved() method from the core library
// to skip validation in that case.
if (props.waitConditionHandleName !== undefined &&
!core.Token.isUnresolved(props.waitConditionHandleName) &&
!Token.isUnresolved(props.waitConditionHandleName) &&
!/^[_a-zA-Z]+$/.test(props.waitConditionHandleName)) {
throw new Error('waitConditionHandleName must be non-empty and contain only letters and underscores, ' +
`got: '${props.waitConditionHandleName}'`);
Expand All @@ -435,12 +435,12 @@ export class ExampleResource extends ExampleResourceBase {
// This guarantees that they get scoped correctly,
// and the CDK will make sure their locally-unique identifiers
// are globally unique, which makes your L2 compose.
const waitConditionHandle = new core.CfnWaitConditionHandle(this, 'WaitConditionHandle');
const waitConditionHandle = new CfnWaitConditionHandle(this, 'WaitConditionHandle');

// The 'main' L1 you create should always have the logical ID 'Resource'.
// This is important, so that the ConstructNode.defaultChild method works correctly.
// The local variable representing the L1 is often called 'resource' as well.
const resource = new core.CfnWaitCondition(this, 'Resource', {
const resource = new CfnWaitCondition(this, 'Resource', {
count: 0,
handle: waitConditionHandle.ref,
timeout: '10',
Expand All @@ -460,7 +460,7 @@ export class ExampleResource extends ExampleResourceBase {
// and the ARN for your resource is of the form 'arn:aws:<service>:<region>:<account>:resource/physical-name',
// which is quite common,
// you can use Fn::Select and Fn::Split to take out the part after the '/' from the ARN:
core.Fn.select(1, core.Fn.split('/', resource.ref)),
Fn.select(1, Fn.split('/', resource.ref)),
);
this.exampleResourceArn = this.getResourceArnAttribute(
// A lot of the L1 classes have an 'attrArn' property -
Expand All @@ -469,7 +469,7 @@ export class ExampleResource extends ExampleResourceBase {
// you can often formulate the ARN yourself,
// using the Stack.formatArn helper function.
// Here, we assume resource.ref returns the physical name of the resource.
core.Stack.of(this).formatArn(exampleResourceArnComponents(resource.ref)),
Stack.of(this).formatArn(exampleResourceArnComponents(resource.ref)),
// always use the protected physicalName property for this second argument
exampleResourceArnComponents(this.physicalName));

Expand Down Expand Up @@ -505,7 +505,7 @@ export class ExampleResource extends ExampleResourceBase {
// this is how you apply the removal policy
resource.applyRemovalPolicy(props.removalPolicy, {
// this is the default to apply if props.removalPolicy is undefined
default: core.RemovalPolicy.RETAIN,
default: RemovalPolicy.RETAIN,
});
}
}

0 comments on commit f2ade56

Please sign in to comment.