Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rds): add RDS DatabaseInstance.fromLookup #32901

Closed
wants to merge 2 commits into from

Conversation

pcheungamz
Copy link

Issue # (if applicable)

#31720

Needs this PR: cdklabs/cloud-assembly-schema#124

Reason for this change

Add DatabaseInstance.fromLookup() feature

Description of changes

Use CloudControl API as ContextProvider to query for DatabaseInstance.

Describe any new or updated permissions being added

Use will need to have permission to run CloudControl API.

Description of how you validated changes

Included unit tests for CC API and DatabaseInstance changes.

Checklist


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

@pcheungamz pcheungamz requested a review from a team as a code owner January 13, 2025 20:36
@github-actions github-actions bot added p2 beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK labels Jan 13, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team January 13, 2025 20:36
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter fails with the following errors:

❌ Features must contain a change to an integration test file and the resulting snapshot.
❌ CLI code has changed. A maintainer must run the code through the testing pipeline (git fetch origin pull/32901/head && git push -f origin FETCH_HEAD:test-main-pipeline), then add the 'pr-linter/cli-integ-tested' label when the pipeline succeeds.

If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.

@aws-cdk-automation aws-cdk-automation added the pr/needs-cli-test-run This PR needs CLI tests run against it. label Jan 13, 2025
@pcheungamz pcheungamz changed the title feat(rds): Add DatabaseInstance.fromLookup feat(rds): add RDS DatabaseInstance.fromLookup Jan 13, 2025
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 285be37
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ This looks great, thanks!

I have couple of small comments.

});

// THEN
expect(result).toEqual({});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we return some sort of error value? Perhaps throw an exception?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will throw error instead of returning {}.

});

// THEN
const propsObj = result['my-db-instance-1'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the API is returning a dictionary { id1 -> props1, id2 -> props2, ... }.

What's the advantage of mapping by ID? I'm honestly asking because I don't know if there's a reason for it. Wouldn't it make more sense to return a list? Just [ props1, props2, ... ] ?

I don't think there is a way for the consumer of this API to know the id in advance, so they will always need to iterate over Object.entries() anyway to get both [id, props] at the same time... but the identifier will be in the propsX object already anyway? (Or at least, if it is requested, which it should be)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point. Will change it to something like { results: [props1, props2, ...]}

public async getValue(args: CcApiContextQuery) {
const cloudControl = (await initContextProviderSdk(this.aws, args)).cloudControl();

const result = await this.findResources(cloudControl, args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an exception handler around this already?

What if findJsonValue throws because the user made a type in their path, will the error be handled sensibly? (I.e., descriptive error shown to the user, not a gory stack trace pointing somewhere to the innards of the CLI?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current code returns undefined so no property of that name is set.
Since we are throwing exceptions in findJsonValue, I will add error handling and unit tests for that.

* @param path
*/
export function findJsonValue(jsonObject: any, path: string): any {
return path.split('.').reduce((r, k) => r[k], jsonObject);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is clever and short, but I think I would like some more type checking on it and throwing descriptive error messages. Likely things users are going to do wrong: mistype a key, reference a key that's missing, reference a key of the wrong type...

Some errors of the kind:

Cannot read field 'foo.bar.quux': expected an object at 'foo.bar' but got "hello" 
Cannot read field 'foo.bar.quux': expected an object at 'foo.bar' but got undefined

Or something to that effect.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I will add better error handling.

* @param propertiesToReturn
* @returns
*/
export function toResultObj(jsonObject: any, propertiesToReturn: string[]): {[key: string]: any} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract all this JSON object manipulation out to a separate file? It's independent and worthwhile enough to not confine it to the "cc api provider", where no one will ever find it again :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. How about aws-cdk/lib/util/json.ts?

dummyValue: {},
}).value;

const instance = response[options.instanceIdentifier];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to account for the fact that it wasn't found, right? What does that do?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add try-catch and throw an Error when there is a problem with CC API lookup.

/**
* The instance identifier of the DatabaseInstance
*/
readonly instanceIdentifier: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue this PR is trying to close says to use databaseIdentifier. Is instanceIdentifier the same thing as databaseIdentifier?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In RDS doc, DatabaseInstance has an instanceIdentifier property. So I try to be consistent with the doc. https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_rds.DatabaseInstance.html#instanceidentifier-1

Comment on lines +144 to +152
exactIdentifier: options.instanceIdentifier,
propertiesToReturn: [
'DBInstanceArn',
'Endpoint.Address',
'Endpoint.Port',
'DbiResourceId',
'DBSecurityGroups',
],
} as cxschema.CcApiContextQuery,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Does it also work? Have you tested this in your actual AWS account?

I trust that the unit tests assert that the code holds to some contract... but do we actually know the contract is the right one? 😁

Comment on lines +162 to +166
const securityGroup = ec2.SecurityGroup.fromSecurityGroupId(
scope,
`LSG-${securityGroupId}`,
securityGroupId,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably need to add an option to the properties object, and pass it here to fromSecurityGroupId, on whether the security groups are going to be mutable or immutable by default.

The difference is whether we will try to add rules to them. false makes more sense, but due to historical reasons the default is true... ☹️

Comment on lines +1438 to +1439
// Add the new security group to the existing security groups of the RDS instance
instance.connections.addSecurityGroup(myNewSecurityGroup);
Copy link
Contributor

@rix0rrr rix0rrr Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example of what to do with a freshly imported database instance doesn't make a lot of sense. Adding a security group to it in this way is unlikely to be what you want (and also, this doesn't do anything because the instance itself is not owned).

Is there another example to copy/paste?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

db.grantConnect and db.connections.allowDefaultPortFrom are also popular results from my search. I will use grantConnect instead.

"@jsii/check-node": "1.104.0",
"@smithy/middleware-endpoint": "3.1.4",
"@smithy/middleware-endpoint": "4.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new major version. This is particularly scary! Have you checked for breaking changes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I will try to find a 3.x version that works with client-cloudcontrol.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rico had already bump this to '^4'. I won't need to make this change.

"@smithy/node-http-handler": "3.2.4",
"@smithy/property-provider": "3.1.10",
"@smithy/shared-ini-file-loader": "3.1.8",
"@smithy/types": "3.5.0",
"@smithy/types": "4.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new major version. This is particularly scary! Have you checked for breaking changes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will try to find a 3.x version that works with client-cloudcontrol.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will probably require a change to packages/aws-cdk/lib/context-providers/index.ts as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will double check this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for trying to be a good citizen! It's usually very much appreciated. <3

We are currently trying to keep the CLI a bit more stable. Are any of the version upgrades strictly necessary to achieve your goal - or would adding the new package be enough? If so, I'd rather just add one and keep everything else as it was.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to use "@aws-sdk/client-cloudcontrol": "3.723.0". I had to bump the other ones to the same version, because of a smithy error.

'DbiResourceId',
'DBSecurityGroups',
],
} as cxschema.CcApiContextQuery,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the type casting required here? How do we guarantee type correctness? If so, please add a comment explaining why.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. We should use the keyword satisfies instead of as here.

@aws-cdk-automation
Copy link
Collaborator

This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state.

@aws-cdk-automation
Copy link
Collaborator

This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state.

1 similar comment
@aws-cdk-automation
Copy link
Collaborator

This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state.

@aws-cdk-automation
Copy link
Collaborator

This PR has been in the BUILD FAILING state for 3 weeks, and looks abandoned. To keep this PR from being closed, please continue work on it. If not, it will automatically be closed in a week.

@aws-cdk-automation
Copy link
Collaborator

This PR has been deemed to be abandoned, and will be automatically closed. Please create a new PR for these changes if you think this decision has been made in error.

@aws-cdk-automation aws-cdk-automation added the closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. label Feb 11, 2025
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. p2 pr/needs-cli-test-run This PR needs CLI tests run against it.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants