Skip to content

Commit

Permalink
chore: move context queries to cloud assembly schema
Browse files Browse the repository at this point in the history
There was another protocol of implicit "props" queries in
the cloud assembly, which is the "missing context queries"
protocol.

Move it into the cloud-assembly-schema package so we can
properly version updates to it.

There was still some additional stuff in the protocol around constants
and return types. Did not move those yet since I'm not sure
how they fit into the schema. Taking it one step at a time.
  • Loading branch information
rix0rrr authored May 7, 2020
1 parent e277bbd commit 87860ca
Show file tree
Hide file tree
Showing 39 changed files with 533 additions and 253 deletions.
3 changes: 3 additions & 0 deletions allowed-breaking-changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ incompatible-argument:@aws-cdk/aws-iam.PrincipalPolicyFragment.<initializer>
changed-type:@aws-cdk/aws-iam.FederatedPrincipal.conditions
changed-type:@aws-cdk/aws-iam.PrincipalPolicyFragment.conditions
changed-type:@aws-cdk/aws-iam.PrincipalWithConditions.conditions
# Changing untyped property blob into typed property blob
change-return-type:@aws-cdk/cloud-assembly-schema.Manifest.load
incompatible-argument:@aws-cdk/cloud-assembly-schema.Manifest.save
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ export = {
const app = new App();
const stack = new Stack(app, 'ParentStack', { env: { account: '1234account', region: 'us-east-44' } });
const nestedStack = new NestedStack(stack, 'nested');
const provider = 'dummyProvider';
const provider = 'availability-zones';
const expectedKey = ContextProvider.getKey(nestedStack, {
provider,
}).key;
Expand Down
5 changes: 3 additions & 2 deletions packages/@aws-cdk/aws-ec2/lib/machine-image.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ssm from '@aws-cdk/aws-ssm';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { Construct, ContextProvider, Stack, Token } from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { UserData } from './user-data';
Expand Down Expand Up @@ -416,11 +417,11 @@ export class LookupMachineImage implements IMachineImage {
Object.assign(filters, this.props.filters);

const value = ContextProvider.getValue(scope, {
provider: cxapi.AMI_PROVIDER,
provider: cxschema.ContextProvider.AMI_PROVIDER,
props: {
owners: this.props.owners,
filters,
} as cxapi.AmiContextQuery,
} as cxschema.AmiContextQuery,
dummyValue: 'ami-1234',
}).value as cxapi.AmiContextResponse;

Expand Down
5 changes: 3 additions & 2 deletions packages/@aws-cdk/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { ConcreteDependable, Construct, ContextProvider, DependableTrait, IConstruct,
IDependable, IResource, Lazy, Resource, Stack, Tag, Token } from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
Expand Down Expand Up @@ -1008,12 +1009,12 @@ export class Vpc extends VpcBase {
}

const attributes: cxapi.VpcContextResponse = ContextProvider.getValue(scope, {
provider: cxapi.VPC_PROVIDER,
provider: cxschema.ContextProvider.VPC_PROVIDER,
props: {
filter,
returnAsymmetricSubnets: true,
subnetGroupNameTag: options.subnetGroupNameTag,
} as cxapi.VpcContextQuery,
} as cxschema.VpcContextQuery,
dummyValue: undefined,
}).value;

Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-ec2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@aws-cdk/aws-ssm": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/cx-api": "0.0.0",
"@aws-cdk/cloud-assembly-schema": "0.0.0",
"@aws-cdk/region-info": "0.0.0",
"constructs": "^3.0.2"
},
Expand All @@ -91,6 +92,7 @@
"@aws-cdk/aws-ssm": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/cx-api": "0.0.0",
"@aws-cdk/cloud-assembly-schema": "0.0.0",
"@aws-cdk/region-info": "0.0.0",
"constructs": "^3.0.2"
},
Expand Down
7 changes: 4 additions & 3 deletions packages/@aws-cdk/aws-ec2/test/test.vpc.from-lookup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { Construct, ContextProvider, GetContextValueOptions, GetContextValueResult, Lazy, Stack } from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { Test } from 'nodeunit';
Expand Down Expand Up @@ -218,12 +219,12 @@ interface MockVcpContextResponse {

function mockVpcContextProviderWith(
test: Test, response: MockVcpContextResponse,
paramValidator?: (options: cxapi.VpcContextQuery) => void) {
paramValidator?: (options: cxschema.VpcContextQuery) => void) {
const previous = ContextProvider.getValue;
ContextProvider.getValue = (_scope: Construct, options: GetContextValueOptions) => {
// do some basic sanity checks
test.equal(options.provider, cxapi.VPC_PROVIDER,
`Expected provider to be: '${cxapi.VPC_PROVIDER}', got: '${options.provider}'`);
test.equal(options.provider, cxschema.ContextProvider.VPC_PROVIDER,
`Expected provider to be: '${cxschema.ContextProvider.VPC_PROVIDER}', got: '${options.provider}'`);
test.equal((options.props || {}).returnAsymmetricSubnets, true,
`Expected options.props.returnAsymmetricSubnets to be true, got: '${(options.props || {}).returnAsymmetricSubnets}'`);

Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-route53/lib/hosted-zone.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { Construct, ContextProvider, Duration, Lazy, Resource, Stack } from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { HostedZoneProviderProps } from './hosted-zone-provider';
import { HostedZoneAttributes, IHostedZone } from './hosted-zone-ref';
import { CaaAmazonRecord, ZoneDelegationRecord } from './record-set';
Expand Down Expand Up @@ -107,7 +107,7 @@ export class HostedZone extends Resource implements IHostedZone {
}

const response: HostedZoneContextResponse = ContextProvider.getValue(scope, {
provider: cxapi.HOSTED_ZONE_PROVIDER,
provider: cxschema.ContextProvider.HOSTED_ZONE_PROVIDER,
dummyValue: DEFAULT_HOSTED_ZONE,
props: query,
}).value;
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-route53/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-logs": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/cx-api": "0.0.0",
"@aws-cdk/cloud-assembly-schema": "0.0.0",
"constructs": "^3.0.2"
},
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-logs": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/cx-api": "0.0.0",
"@aws-cdk/cloud-assembly-schema": "0.0.0",
"constructs": "^3.0.2"
},
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ssm/lib/parameter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as iam from '@aws-cdk/aws-iam';
import * as kms from '@aws-cdk/aws-kms';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import {
CfnDynamicReference, CfnDynamicReferenceService, CfnParameter,
Construct, ContextProvider, Fn, IResource, Resource, Stack, Token,
} from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import * as ssm from './ssm.generated';
import { arnForParameterName, AUTOGEN_MARKER } from './util';

Expand Down Expand Up @@ -362,7 +362,7 @@ export class StringParameter extends ParameterBase implements IStringParameter {
*/
public static valueFromLookup(scope: Construct, parameterName: string): string {
const value = ContextProvider.getValue(scope, {
provider: cxapi.SSM_PARAMETER_PROVIDER,
provider: cxschema.ContextProvider.SSM_PARAMETER_PROVIDER,
props: { parameterName },
dummyValue: `dummy-value-for-${parameterName}`,
}).value;
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ssm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-kms": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/cx-api": "0.0.0",
"@aws-cdk/cloud-assembly-schema": "0.0.0",
"constructs": "^3.0.2"
},
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-kms": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/cx-api": "0.0.0",
"@aws-cdk/cloud-assembly-schema": "0.0.0",
"constructs": "^3.0.2"
},
"engines": {
Expand Down
177 changes: 177 additions & 0 deletions packages/@aws-cdk/cloud-assembly-schema/lib/context-queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@

/**
* Identifier for the context provider
*/
export enum ContextProvider {
/**
* AMI provider
*/
AMI_PROVIDER = 'ami',

/**
* AZ provider
*/
AVAILABILITY_ZONE_PROVIDER = 'availability-zones',

/**
* Route53 Hosted Zone provider
*/
HOSTED_ZONE_PROVIDER = 'hosted-zone',

/**
* SSM Parameter Provider
*/
SSM_PARAMETER_PROVIDER = 'ssm',

/**
* VPC Provider
*/
VPC_PROVIDER = 'vpc-provider',
}

/**
* Query to AMI context provider
*/
export interface AmiContextQuery {
/**
* Account to query
*/
readonly account: string;

/**
* Region to query
*/
readonly region: string;

/**
* Owners to DescribeImages call
*
* @default - All owners
*/
readonly owners?: string[];

/**
* Filters to DescribeImages call
*/
readonly filters: {[key: string]: string[]};
}

/**
* Query to availability zone context provider
*/
export interface AvailabilityZonesContextQuery {
/**
* Query account
*/
readonly account: string;

/**
* Query region
*/
readonly region: string;
}

/**
* Query to hosted zone context provider
*/
export interface HostedZoneContextQuery {
/**
* Query account
*/
readonly account: string;

/**
* Query region
*/
readonly region: string;

/**
* The domain name e.g. example.com to lookup
*/
readonly domainName: string;

/**
* True if the zone you want to find is a private hosted zone
*
* @default false
*/
readonly privateZone?: boolean;

/**
* The VPC ID to that the private zone must be associated with
*
* If you provide VPC ID and privateZone is false, this will return no results
* and raise an error.
*
* @default - Required if privateZone=true
*/
readonly vpcId?: string;
}

/**
* Query to SSM Parameter Context Provider
*/
export interface SSMParameterContextQuery {
/**
* Query account
*/
readonly account: string;

/**
* Query region
*/
readonly region: string;

/**
* Parameter name to query
*/
readonly parameterName: string;
}

/**
* Query input for looking up a VPC
*/
export interface VpcContextQuery {
/**
* Query account
*/
readonly account: string;

/**
* Query region
*/
readonly region: string;

/**
* Filters to apply to the VPC
*
* Filter parameters are the same as passed to DescribeVpcs.
*
* @see https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVpcs.html
*/
readonly filter: {[key: string]: string};

/**
* Whether to populate the subnetGroups field of the {@link VpcContextResponse},
* which contains potentially asymmetric subnet groups.
*
* @default false
*/
readonly returnAsymmetricSubnets?: boolean;

/**
* Optional tag for subnet group name.
* If not provided, we'll look at the aws-cdk:subnet-name tag.
* If the subnet does not have the specified tag,
* we'll use its type as the name.
*
* @default 'aws-cdk:subnet-name'
*/
readonly subnetGroupNameTag?: string;
}

export type ContextQueryProperties = AmiContextQuery
| AvailabilityZonesContextQuery
| HostedZoneContextQuery
| SSMParameterContextQuery
| VpcContextQuery;
1 change: 1 addition & 0 deletions packages/@aws-cdk/cloud-assembly-schema/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './manifest';
export * from './schema';
export * from './context-queries';
10 changes: 4 additions & 6 deletions packages/@aws-cdk/cloud-assembly-schema/lib/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ContextProvider, ContextQueryProperties } from './context-queries';

/**
* Common properties for asset metadata.
*/
Expand Down Expand Up @@ -256,16 +258,12 @@ export interface MissingContext {
/**
* The provider from which we expect this context key to be obtained.
*/
readonly provider: string;
readonly provider: ContextProvider;

/**
* A set of provider-specific options.
*/
readonly props: {
account?: string;
region?: string;
[key: string]: any;
};
readonly props: ContextQueryProperties;
}

/**
Expand Down
Loading

0 comments on commit 87860ca

Please sign in to comment.