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

Add tests and debug logging for ownerTag mapping #1772

Merged
merged 3 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/angry-moles-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@roadiehq/catalog-backend-module-aws': patch
---

Add test, debug logging and RDS support for ownerTag mapping
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
* limitations under the License.
*/

import { Lambda, ListFunctionsCommand } from '@aws-sdk/client-lambda';
import {
Lambda,
ListFunctionsCommand,
ListTagsCommand,
} from '@aws-sdk/client-lambda';
import { STS, GetCallerIdentityCommand } from '@aws-sdk/client-sts';

import { mockClient } from 'aws-sdk-client-mock';
Expand All @@ -31,6 +35,7 @@ const logger = createLogger({
});

describe('AWSLambdaFunctionProvider', () => {
const ownerTag = 'Owner-test';
const config = new ConfigReader({
accountId: '123456789012',
roleName: 'arn:aws:iam::123456789012:role/role1',
Expand Down Expand Up @@ -64,6 +69,7 @@ describe('AWSLambdaFunctionProvider', () => {
});

describe('where there are is a function', () => {
const owner = 'engineering';
beforeEach(() => {
lambda.on(ListFunctionsCommand).resolves({
Functions: [
Expand All @@ -86,6 +92,12 @@ describe('AWSLambdaFunctionProvider', () => {
},
],
});
lambda.on(ListTagsCommand).resolves({
Tags: {
[ownerTag]: owner,
owner: 'wrong',
},
});
});

it('creates aws functions', async () => {
Expand Down Expand Up @@ -130,6 +142,32 @@ describe('AWSLambdaFunctionProvider', () => {
});
});

it('maps owner from custom tag mapping', async () => {
const entityProviderConnection: EntityProviderConnection = {
applyMutation: jest.fn(),
refresh: jest.fn(),
};
const provider = AWSLambdaFunctionProvider.fromConfig(config, {
logger,
ownerTag,
});
provider.connect(entityProviderConnection);
await provider.run();
expect(entityProviderConnection.applyMutation).toHaveBeenCalledWith({
type: 'full',
entities: [
expect.objectContaining({
entity: expect.objectContaining({
kind: 'Resource',
spec: expect.objectContaining({
owner: owner,
}),
}),
}),
],
});
});

it('is able to use dynamic config', async () => {
const entityProviderConnection: EntityProviderConnection = {
applyMutation: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ export class AWSLambdaFunctionProvider extends AWSEntityProvider {
if (lambdaFunction.Role) {
annotations[ANNOTATION_AWS_IAM_ROLE_ARN] = lambdaFunction.Role;
}
const owner = ownerFromTags(tags, this.getOwnerTag(), groups);
const relationships = relationshipsFromTags(tags);
this.logger.debug(
`Setting Lambda owner from tags as ${owner} and relationships of ${JSON.stringify(
relationships,
)}`,
);

lambdaComponents.push({
kind: 'Resource',
apiVersion: 'backstage.io/v1beta1',
Expand All @@ -144,8 +152,8 @@ export class AWSLambdaFunctionProvider extends AWSEntityProvider {
labels: this.labelsFromTags(tags),
},
spec: {
owner: ownerFromTags(tags, this.getOwnerTag(), groups),
...relationshipsFromTags(tags),
owner: owner,
...relationships,
type: 'lambda-function',
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class AWSRDSProvider extends AWSEntityProvider {
providerId?: string;
useTemporaryCredentials?: boolean;
labelValueMapper?: LabelValueMapper;
ownerTag?: string;
},
) {
const accountId = config.getString('accountId');
Expand Down
Loading