Skip to content

Commit

Permalink
refactor(servicecatalogappregistry): prepare ApplicationAssociator fo…
Browse files Browse the repository at this point in the history
…r future extensions (#22644)

Updated Input Structure for Application Associator L2 Construct 
 * This helps user to pass the input using factory pattern
 * Factory Pattern makes construct extensible 
----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

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

Co-authored by: Santanu Ghosh
  • Loading branch information
rohitagg0807 committed Nov 3, 2022
1 parent 1f50814 commit c4e07a6
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 85 deletions.
30 changes: 14 additions & 16 deletions packages/@aws-cdk/aws-servicecatalogappregistry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ and want to associate all stacks in the `App` scope to `MyAssociatedApplication`
```ts
const app = new App();
const associatedApp = new appreg.ApplicationAssociator(app, 'AssociatedApplication', {
applications: [appreg.TargetApplication.createApplicationStack({
applicationName: 'MyAssociatedApplication',
description: 'Testing associated application',
stackProps: {
stackName: 'MyAssociatedApplicationStack',
env: {account: '123456789012', region: 'us-east-1'},
},
stackName: 'MyAssociatedApplicationStack',
env: { account: '123456789012', region: 'us-east-1' },
})],
});
```

Expand All @@ -88,15 +87,15 @@ and want to associate all stacks in the `App` scope to your imported application
```ts
const app = new App();
const associatedApp = new appreg.ApplicationAssociator(app, 'AssociatedApplication', {
applications: [appreg.TargetApplication.existingApplicationFromArn({
applicationArnValue: 'arn:aws:servicecatalog:us-east-1:123456789012:/applications/applicationId',
stackProps: {
stackName: 'MyAssociatedApplicationStack',
},
stackName: 'MyAssociatedApplicationStack',
})],
});
```

If you are using CDK Pipelines to deploy your application, the application stacks will be inside Stages, and
ApplicationAssociator will not be able to find them. Call `associateStage` on each Stage object before adding it to the
If you are using CDK Pipelines to deploy your application, the application stacks will be inside Stages, and
ApplicationAssociator will not be able to find them. Call `associateStage` on each Stage object before adding it to the
Pipeline, as shown in the example below:

```ts
Expand All @@ -109,7 +108,7 @@ declare const beta: cdk.Stage;
class ApplicationPipelineStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props: ApplicationPipelineStackProps) {
super(scope, id, props);

//associate the stage to application associator.
props.application.associateStage(beta);
pipeline.addStage(beta);
Expand All @@ -122,12 +121,11 @@ interface ApplicationPipelineStackProps extends cdk.StackProps {

const app = new App();
const associatedApp = new appreg.ApplicationAssociator(app, 'AssociatedApplication', {
applications: [appreg.TargetApplication.createApplicationStack({
applicationName: 'MyPipelineAssociatedApplication',
description: 'Testing pipeline associated app',
stackProps: {
stackName: 'MyPipelineAssociatedApplicationStack',
env: {account: '123456789012', region: 'us-east-1'},
},
stackName: 'MyPipelineAssociatedApplicationStack',
env: { account: '123456789012', region: 'us-east-1' },
})],
});

const cdkPipeline = new ApplicationPipelineStack(app, 'CDKApplicationPipelineStack', {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { IApplication, Application } from './application';
import { IApplication } from './application';
import { CheckedStageStackAssociator } from './aspects/stack-associator';
import { TargetApplication } from './target-application';

/**
* Properties for a Service Catalog AppRegistry AutoApplication
* Properties for Service Catalog AppRegistry Application Associator
*/
export interface ApplicationAssociatorProps {
/**
* Enforces a particular physical application name.
* Application associator properties.
*
* @default - No name.
* @default - Empty array.
*/
readonly applicationName?: string;

/**
* Enforces a particular application arn.
*
* @default - No application arn.
*/
readonly applicationArnValue?: string;

/**
* Application description.
*
* @default - No description.
*/
readonly description?: string;

/**
* Stack properties.
*
*/
readonly stackProps: cdk.StackProps;
readonly applications: TargetApplication[];
}

/**
Expand All @@ -56,19 +37,12 @@ export class ApplicationAssociator extends Construct {
constructor(scope: cdk.App, id: string, props: ApplicationAssociatorProps) {
super(scope, id);

const applicationStack = new cdk.Stack(scope, 'ApplicationAssociatorStack', props.stackProps);

if (!!props.applicationArnValue) {
this.application = Application.fromApplicationArn(applicationStack, 'ImportedApplication', props.applicationArnValue);
} else if (!!props.applicationName) {
this.application = new Application(applicationStack, 'DefaultCdkApplication', {
applicationName: props.applicationName,
description: props.description,
});
} else {
throw new Error('Please provide either ARN or application name.');
if (props.applications.length != 1) {
throw new Error('Please pass exactly 1 instance of TargetApplication.createApplicationStack() or TargetApplication.existingApplicationFromArn() into the "applications" property');
}

const targetApplication = props.applications[0];
this.application = targetApplication.bind(this).application;
cdk.Aspects.of(scope).add(new CheckedStageStackAssociator(this));
}

Expand All @@ -94,7 +68,7 @@ export class ApplicationAssociator extends Construct {
* Get the AppRegistry application.
*
*/
get appRegistryApplication() {
public appRegistryApplication(): IApplication {
return this.application;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class CheckedStageStackAssociator extends StackAssociatorBase {

constructor(app: ApplicationAssociator) {
super();
this.application = app.appRegistryApplication;
this.application = app.appRegistryApplication();
this.applicationAssociator = app;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './application';
export * from './attribute-group';
export * from './application-associator';
export * from './target-application';
export * from './common';

// AWS::ServiceCatalogAppRegistry CloudFormation Resources:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { IApplication, Application } from './application';

/**
* Properties used to define targetapplication.
*/
export interface TargetApplicationCommonOptions extends cdk.StackProps {
/**
* Stack ID in which application will be created or imported.
*
* @default - ApplicationAssociatorStack
*/
readonly stackId?: string;
}


/**
* Properties used to define New TargetApplication.
*/
export interface CreateTargetApplicationOptions extends TargetApplicationCommonOptions {
/**
* Enforces a particular physical application name.
*/
readonly applicationName: string;

/**
* Application description.
*
* @default - No description.
*/
readonly applicationDescription?: string;
}

/**
* Properties used to define Existing TargetApplication.
*/
export interface ExistingTargetApplicationOptions extends TargetApplicationCommonOptions {
/**
* Enforces a particular application arn.
*/
readonly applicationArnValue: string;
}

/**
* Contains static factory methods with which you can build the input
* needed for application associator to work
*/
export abstract class TargetApplication {
/**
* Factory method to build the input using the provided
* application ARN.
*/
public static existingApplicationFromArn(options: ExistingTargetApplicationOptions) : TargetApplication {
return new ExistingTargetApplication(options);
}

/**
* Factory method to build the input using the provided
* application name and stack props.
*/
public static createApplicationStack(options: CreateTargetApplicationOptions) : TargetApplication {
return new CreateTargetApplication(options);
}

/**
* Called when the ApplicationAssociator is initialized
*/
public abstract bind(scope: Construct): BindTargetApplicationResult;
}

/**
* Properties for Service Catalog AppRegistry Application Associator to work with
*/
export interface BindTargetApplicationResult {
/**
* Created or imported application.
*/
readonly application: IApplication;
}

/**
* Class which constructs the input from provided application name and stack props.
* With this input, the construct will create the Application.
*/
class CreateTargetApplication extends TargetApplication {
constructor(
private readonly applicationOptions: CreateTargetApplicationOptions) {
super();
}
public bind(scope: Construct): BindTargetApplicationResult {
const stackId = this.applicationOptions.stackId ?? 'ApplicationAssociatorStack';
const applicationStack = new cdk.Stack(scope, stackId, this.applicationOptions);
const appRegApplication = new Application(applicationStack, 'DefaultCdkApplication', {
applicationName: this.applicationOptions.applicationName,
description: this.applicationOptions.applicationDescription,
});

return {
application: appRegApplication,
};
}
}

/**
* Class which constructs the input from provided Application ARN.
*/
class ExistingTargetApplication extends TargetApplication {
constructor(
private readonly applicationOptions: ExistingTargetApplicationOptions) {
super();
}
public bind(scope: Construct): BindTargetApplicationResult {
const stackId = this.applicationOptions.stackId ?? 'ApplicationAssociatorStack';
const applicationStack = new cdk.Stack(scope, stackId, this.applicationOptions);
const appRegApplication = Application.fromApplicationArn(applicationStack, 'ExistingApplication', this.applicationOptions.applicationArnValue);
return {
application: appRegApplication,
};
}
}

0 comments on commit c4e07a6

Please sign in to comment.