Skip to content

Commit

Permalink
Pseudo parameters from new Aws.Xxx() to Aws.xxx
Browse files Browse the repository at this point in the history
  • Loading branch information
Elad Ben-Israel committed Dec 25, 2018
1 parent 71b60e4 commit 69da340
Show file tree
Hide file tree
Showing 23 changed files with 92 additions and 54 deletions.
2 changes: 1 addition & 1 deletion docs/src/cloudformation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ Pseudo Parameters
.. code-block:: js
import cdk = require('@aws-cdk/cdk');
new cdk.AwsRegion()
cdk.Aws.region
.. Add a new topic in "Advanced Topics" about integrating
cdk synch > mytemplate
Expand Down
2 changes: 1 addition & 1 deletion docs/src/constructs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Construct IDs may be any string with the following caveats:
* Path separators (``/``s) will be replaced by double-dashes ``--``. This means
that if you are trying to look up a child construct that may have a path separator,
you will need to manually replace it with ``--``.
* Construct IDs may not include unresolved tokens (such as `new AwsRegion()`). This is
* Construct IDs may not include unresolved tokens (such as `Aws.region`). This is
because those tokens are only resolved during deployment, and therefore cannot be used
to render a stable logical ID for any resources in this tree.

Expand Down
18 changes: 9 additions & 9 deletions examples/cdk-examples-typescript/advanced-usage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class CloudFormationExample extends cdk.Stack {
// outputs are constructs the synthesize into the template's "Outputs" section
new cdk.Output(this, 'Output', {
description: 'This is an output of the template',
value: `${new cdk.AwsAccountId()}/${param.ref}`
value: `${cdk.Aws.accountId}/${param.ref}`
});

// stack.templateOptions can be used to specify template-level options
Expand All @@ -166,14 +166,14 @@ class CloudFormationExample extends cdk.Stack {

// all CloudFormation's pseudo-parameters are supported via the `cdk.AwsXxx` classes
PseudoParameters: [
new cdk.AwsAccountId(),
new cdk.AwsDomainSuffix(),
new cdk.AwsNotificationARNs(),
new cdk.AwsNoValue(),
new cdk.AwsPartition(),
new cdk.AwsRegion(),
new cdk.AwsStackId(),
new cdk.AwsStackName(),
cdk.Aws.accountId,
cdk.Aws.domainSuffix,
cdk.Aws.notificationARNs,
cdk.Aws.noValue,
cdk.Aws.partition,
cdk.Aws.region,
cdk.Aws.stackId,
cdk.Aws.stackName,
],

// all CloudFormation's intrinsic functions are supported via the `cdk.Fn.xxx` static methods.
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-apigateway/lib/stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class Stage extends cdk.Construct implements cdk.IDependable {
if (!path.startsWith('/')) {
throw new Error(`Path must begin with "/": ${path}`);
}
return `https://${this.restApi.restApiId}.execute-api.${new cdk.AwsRegion()}.amazonaws.com/${this.stageName}${path}`;
return `https://${this.restApi.restApiId}.execute-api.${cdk.Aws.region}.amazonaws.com/${this.stageName}${path}`;
}

private renderMethodSettings(props: StageProps): CfnStage.MethodSettingProperty[] | undefined {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-cloudtrail/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class CloudTrail extends cdk.Construct {
.addServicePrincipal(cloudTrailPrincipal));

s3bucket.addToResourcePolicy(new iam.PolicyStatement()
.addResource(s3bucket.arnForObjects(`AWSLogs/${new cdk.AwsAccountId()}/*`))
.addResource(s3bucket.arnForObjects(`AWSLogs/${cdk.Aws.accountId}/*`))
.addActions("s3:PutObject")
.addServicePrincipal(cloudTrailPrincipal)
.setCondition("StringEquals", {'s3:x-amz-acl': "bucket-owner-full-control"}));
Expand Down
8 changes: 4 additions & 4 deletions packages/@aws-cdk/aws-cloudwatch/lib/graph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AwsRegion } from "@aws-cdk/cdk";
import { Aws } from "@aws-cdk/cdk";
import { Alarm } from "./alarm";
import { Metric } from "./metric";
import { parseStatistic } from './util.statistic';
Expand Down Expand Up @@ -73,7 +73,7 @@ export class AlarmWidget extends ConcreteWidget {
properties: {
view: 'timeSeries',
title: this.props.title,
region: this.props.region || new AwsRegion(),
region: this.props.region || Aws.region,
annotations: {
alarms: [this.props.alarm.alarmArn]
},
Expand Down Expand Up @@ -150,7 +150,7 @@ export class GraphWidget extends ConcreteWidget {
properties: {
view: 'timeSeries',
title: this.props.title,
region: this.props.region || new AwsRegion(),
region: this.props.region || Aws.region,
metrics: (this.props.left || []).map(m => metricJson(m, 'left')).concat(
(this.props.right || []).map(m => metricJson(m, 'right'))),
annotations: {
Expand Down Expand Up @@ -197,7 +197,7 @@ export class SingleValueWidget extends ConcreteWidget {
properties: {
view: 'singleValue',
title: this.props.title,
region: this.props.region || new AwsRegion(),
region: this.props.region || Aws.region,
metrics: this.props.metrics.map(m => metricJson(m, 'left'))
}
}];
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ export class Project extends ProjectRef {

let cache: CfnProject.ProjectCacheProperty | undefined;
if (props.cacheBucket) {
const cacheDir = props.cacheDir != null ? props.cacheDir : new cdk.AwsNoValue().toString();
const cacheDir = props.cacheDir != null ? props.cacheDir : cdk.Aws.noValue;
cache = {
type: 'S3',
location: cdk.Fn.join('/', [props.cacheBucket.bucketName, cacheDir]),
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-codecommit/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class ImportedRepositoryRef extends RepositoryRef {
}

private repositoryCloneUrl(protocol: 'https' | 'ssh'): string {
return `${protocol}://git-codecommit.${new cdk.AwsRegion()}.${new cdk.AwsURLSuffix()}/v1/repos/${this.repositoryName}`;
return `${protocol}://git-codecommit.${cdk.Aws.region}.${cdk.Aws.urlSuffix}/v1/repos/${this.repositoryName}`;
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-codedeploy/lib/deployment-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export class ServerDeploymentGroup extends ServerDeploymentGroupRef {

this._autoScalingGroups = props.autoScalingGroups || [];
this.installAgent = props.installAgent === undefined ? true : props.installAgent;
const region = (new cdk.AwsRegion()).toString();
const region = (cdk.Aws.region).toString();
this.codeDeployBucket = s3.BucketRef.import(this, 'CodeDeployBucket', {
bucketName: `aws-codedeploy-${region}`,
});
Expand Down Expand Up @@ -371,7 +371,7 @@ export class ServerDeploymentGroup extends ServerDeploymentGroupRef {

this.codeDeployBucket.grantRead(asg.role, 'latest/*');

const region = (new cdk.AwsRegion()).toString();
const region = (cdk.Aws.region).toString();
switch (asg.osType) {
case ec2.OperatingSystemType.Linux:
asg.addUserData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class AwsLogDriver extends LogDriver {
options: removeEmpty({
'awslogs-group': this.logGroup.logGroupName,
'awslogs-stream-prefix': this.props.streamPrefix,
'awslogs-region': `${new cdk.AwsRegion()}`,
'awslogs-region': cdk.Aws.region,
'awslogs-datetime-format': this.props.datetimeFormat,
'awslogs-multiline-pattern': this.props.multilinePattern,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export class BaseImportedTargetGroup extends cdk.Construct {
super(parent, id);

this.targetGroupArn = props.targetGroupArn;
this.loadBalancerArns = props.loadBalancerArns || new cdk.AwsNoValue().toString();
this.loadBalancerArns = props.loadBalancerArns || cdk.Aws.noValue;
}
}
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-iam/lib/policy-document.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AwsAccountId, AwsPartition, Token } from '@aws-cdk/cdk';
import { Aws, Token } from '@aws-cdk/cdk';

export class PolicyDocument extends Token {
private statements = new Array<PolicyStatement>();
Expand Down Expand Up @@ -82,7 +82,7 @@ export class ArnPrincipal extends PolicyPrincipal {

export class AccountPrincipal extends ArnPrincipal {
constructor(public readonly accountId: any) {
super(`arn:${new AwsPartition()}:iam::${accountId}:root`);
super(`arn:${Aws.partition}:iam::${accountId}:root`);
}
}

Expand Down Expand Up @@ -137,7 +137,7 @@ export class FederatedPrincipal extends PolicyPrincipal {

export class AccountRootPrincipal extends AccountPrincipal {
constructor() {
super(new AwsAccountId());
super(Aws.accountId);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-kinesis/lib/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export abstract class StreamRef extends cdk.Construct implements logs.ILogSubscr
if (!this.cloudWatchLogsRole) {
// Create a role to be assumed by CWL that can write to this stream and pass itself.
this.cloudWatchLogsRole = new iam.Role(this, 'CloudWatchLogsCanPutRecords', {
assumedBy: new iam.ServicePrincipal(`logs.${new cdk.AwsRegion()}.amazonaws.com`)
assumedBy: new iam.ServicePrincipal(`logs.${cdk.Aws.region}.amazonaws.com`)
});
this.cloudWatchLogsRole.addToPolicy(new iam.PolicyStatement().addAction('kinesis:PutRecord').addResource(this.streamArn));
this.cloudWatchLogsRole.addToPolicy(new iam.PolicyStatement().addAction('iam:PassRole').addResource(this.cloudWatchLogsRole.roleArn));
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-kms/test/integ.key.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PolicyStatement } from '@aws-cdk/aws-iam';
import { App, AwsAccountId, Stack } from '@aws-cdk/cdk';
import { App, Aws, Stack } from '@aws-cdk/cdk';
import { EncryptionKey } from '../lib';

const app = new App();
Expand All @@ -11,7 +11,7 @@ const key = new EncryptionKey(stack, 'MyKey');
key.addToResourcePolicy(new PolicyStatement()
.addAllResources()
.addAction('kms:encrypt')
.addAwsPrincipal(new AwsAccountId().toString()));
.addAwsPrincipal(Aws.accountId));

key.addAlias('alias/bar');

Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-lambda/lib/lambda-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export abstract class FunctionRef extends cdk.Construct
//
// (Wildcards in principals are unfortunately not supported.
this.addPermission('InvokedByCloudWatchLogs', {
principal: new iam.ServicePrincipal(`logs.${new cdk.AwsRegion()}.amazonaws.com`),
principal: new iam.ServicePrincipal(`logs.${cdk.Aws.region}.amazonaws.com`),
sourceArn: arn
});
this.logSubscriptionDestinationPolicyAddedFor.push(arn);
Expand Down Expand Up @@ -348,7 +348,7 @@ export abstract class FunctionRef extends cdk.Construct
const permissionId = `AllowBucketNotificationsFrom${bucketId}`;
if (!this.tryFindChild(permissionId)) {
this.addPermission(permissionId, {
sourceAccount: new cdk.AwsAccountId().toString(),
sourceAccount: cdk.Aws.accountId.toString(),
principal: new iam.ServicePrincipal('s3.amazonaws.com'),
sourceArn: bucketArn,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda/test/test.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export = {
fn.addPermission('S3Permission', {
action: 'lambda:*',
principal: new iam.ServicePrincipal('s3.amazonaws.com'),
sourceAccount: new cdk.AwsAccountId().toString(),
sourceAccount: cdk.Aws.accountId,
sourceArn: 'arn:aws:s3:::my_bucket'
});

Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-route53/lib/hosted-zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class PrivateHostedZone extends HostedZoneRef {
}

function toVpcProperty(vpc: ec2.VpcNetworkRef): CfnHostedZone.VPCProperty {
return { vpcId: vpc.vpcId, vpcRegion: new cdk.AwsRegion() };
return { vpcId: vpc.vpcId, vpcRegion: cdk.Aws.region };
}

function determineHostedZoneProps(props: PublicHostedZoneProps) {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-s3/lib/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export abstract class BucketRef extends cdk.Construct {
* @returns an ObjectS3Url token
*/
public urlForObject(key?: string): string {
const components = [ 'https://', 's3.', new cdk.AwsRegion(), '.', new cdk.AwsURLSuffix(), '/', this.bucketName ];
const components = [ 'https://', 's3.', cdk.Aws.region, '.', cdk.Aws.urlSuffix, '/', this.bucketName ];
if (key) {
// trim prepending '/'
if (typeof key === 'string' && key.startsWith('/')) {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class StateMachine extends cdk.Construct implements IStateMachine {
super(parent, id);

this.role = props.role || new iam.Role(this, 'Role', {
assumedBy: new iam.ServicePrincipal(`states.${new cdk.AwsRegion()}.amazonaws.com`),
assumedBy: new iam.ServicePrincipal(`states.${cdk.Aws.region}.amazonaws.com`)
});

const graph = new StateGraph(props.definition.startState, `State Machine ${id} definition`);
Expand Down
8 changes: 4 additions & 4 deletions packages/@aws-cdk/cdk/lib/cloudformation/arn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AwsAccountId, AwsPartition, AwsRegion } from '..';
import { Aws } from '..';
import { Fn } from '../cloudformation/fn';
import { unresolved } from '../core/tokens';

Expand All @@ -23,13 +23,13 @@ export class ArnUtils {
*/
public static fromComponents(components: ArnComponents): string {
const partition = components.partition == null
? new AwsPartition()
? Aws.partition
: components.partition;
const region = components.region == null
? new AwsRegion()
? Aws.region
: components.region;
const account = components.account == null
? new AwsAccountId()
? Aws.accountId
: components.account;

const values = [ 'arn', ':', partition, ':', components.service, ':', region, ':', account, ':', components.resource ];
Expand Down
58 changes: 48 additions & 10 deletions packages/@aws-cdk/cdk/lib/cloudformation/pseudo.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,98 @@
import { CloudFormationToken } from './cloudformation-token';

export class PseudoParameter extends CloudFormationToken {
export class Aws {
public static get accountId(): string {
return new AwsAccountId().toString();
}

public static get domainSuffix(): string {
return new AwsDomainSuffix().toString();
}

public static get urlSuffix(): string {
return new AwsURLSuffix().toString();
}

public static get notificationARNs(): string[] {
return new AwsNotificationARNs().toList();
}

public static get noValue(): string {
return new AwsNoValue().toString();
}

public static get partition(): string {
return new AwsPartition().toString();
}

public static get region(): string {
return new AwsRegion().toString();
}

public static get stackId(): string {
return new AwsStackId().toString();
}

public static get stackName(): string {
return new AwsStackName().toString();
}
}

class PseudoParameter extends CloudFormationToken {
constructor(name: string) {
super({ Ref: name }, name);
}
}

export class AwsAccountId extends PseudoParameter {
class AwsAccountId extends PseudoParameter {
constructor() {
super('AWS::AccountId');
}
}

export class AwsDomainSuffix extends PseudoParameter {
class AwsDomainSuffix extends PseudoParameter {
constructor() {
super('AWS::DomainSuffix');
}
}

export class AwsURLSuffix extends PseudoParameter {
class AwsURLSuffix extends PseudoParameter {
constructor() {
super('AWS::URLSuffix');
}
}

export class AwsNotificationARNs extends PseudoParameter {
class AwsNotificationARNs extends PseudoParameter {
constructor() {
super('AWS::NotificationARNs');
}
}

export class AwsNoValue extends PseudoParameter {
class AwsNoValue extends PseudoParameter {
constructor() {
super('AWS::NoValue');
}
}

export class AwsPartition extends PseudoParameter {
class AwsPartition extends PseudoParameter {
constructor() {
super('AWS::Partition');
}
}

export class AwsRegion extends PseudoParameter {
class AwsRegion extends PseudoParameter {
constructor() {
super('AWS::Region');
}
}

export class AwsStackId extends PseudoParameter {
class AwsStackId extends PseudoParameter {
constructor() {
super('AWS::StackId');
}
}

export class AwsStackName extends PseudoParameter {
class AwsStackName extends PseudoParameter {
constructor() {
super('AWS::StackName');
}
Expand Down
Loading

0 comments on commit 69da340

Please sign in to comment.