Skip to content

Commit

Permalink
add postgres unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sundersc committed Mar 22, 2024
1 parent 7dbbdc3 commit 025c462
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,40 @@ export const schema = a.schema({
"
`;

exports[`Type name conversions postgres schema with database config secret and vpc should generate typescript data schema with configure 1`] = `
"/* eslint-disable */
import { a } from \\"@aws-amplify/data-schema\\";
import { configure } from \\"@aws-amplify/data-schema/internals\\";
import { secret } from \\"@aws-amplify/backend\\";
export const schema = configure({
database: {
engine: \\"postgresql\\",
connectionUri: secret(\\"CONN_STR\\"),
vpc: {
vpcId: \\"123\\",
securityGroupIds: [
\\"sb1\\"
],
subnetAvailabilityZoneConfig: [
{
subnetId: \\"sb1\\",
availabilityZone: \\"az1\\"
}
]
}
}
}).schema({
\\"User\\": a.model({
id: a.string().required(),
name: a.string()
}).identifier([
\\"id\\"
])
});
"
`;

exports[`Type name conversions schema with database config secret and vpc should generate typescript data schema with configure 1`] = `
"/* eslint-disable */
import { a } from \\"@aws-amplify/data-schema\\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,32 @@ describe('Type name conversions', () => {
expect(graphqlSchema).toMatchSnapshot();
});

it('postgres schema with database config secret and vpc should generate typescript data schema with configure', () => {
const dbschema = new Schema(new Engine('Postgres'));
const model = new Model('User');
model.addField(new Field('id', { kind: 'NonNull', type: { kind: 'Scalar', name: 'String' } }));
model.addField(new Field('name', { kind: 'Scalar', name: 'String' }));
model.setPrimaryKey(['id']);
dbschema.addModel(model);

const config: DatasourceConfig = {
secretName: 'CONN_STR',
vpcConfig: {
vpcId: '123',
securityGroupIds: ['sb1'],
subnetAvailabilityZoneConfig: [
{
subnetId: 'sb1',
availabilityZone: 'az1'
},
],
},
};

const graphqlSchema = generateTypescriptDataSchema(dbschema, config);
expect(graphqlSchema).toMatchSnapshot();
});

it('schema with database config without vpc should generate typescript data schema with configure', () => {
const dbschema = new Schema(new Engine('MySQL'));
let model = new Model('User');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export type TypescriptDataSchemaGeneratorConfig = {
username: string;
password: string;
connectionUriSecretName: string;
region?: string;
outputFile?: string;
};

Expand All @@ -22,7 +21,7 @@ export class TypescriptDataSchemaGenerator {
const schema = await TypescriptDataSchemaGenerator.buildSchema(config);
return generateTypescriptDataSchema(schema, {
secretName: config.connectionUriSecretName,
vpcConfig: config.region ? await getHostVpc(config.host, config.region) : undefined,
vpcConfig: await getHostVpc(config.host),
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { VpcConfig, SubnetAvailabilityZone } from '@aws-amplify/graphql-transfor
import { DB_ENGINES } from './supported-db-engines';
import { filterSubnetAvailabilityZones } from './filter-subnet-availability-zones';

export const checkHostInDBClusters = async (hostname: string, region: string): Promise<VpcConfig | undefined> => {
// When region is not provided, it will use the region configured in the AWS profile.
export const checkHostInDBClusters = async (hostname: string, region?: string): Promise<VpcConfig | undefined> => {
const client = new RDSClient({ region });
const params: DescribeDBClustersCommandInput = {
Filters: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { SubnetAvailabilityZone, VpcConfig } from '@aws-amplify/graphql-transfor
import { DB_ENGINES } from './supported-db-engines';
import { filterSubnetAvailabilityZones } from './filter-subnet-availability-zones';

export const checkHostInDBInstances = async (hostname: string, region: string): Promise<VpcConfig | undefined> => {
// When region is not provided, it will use the region configured in the AWS profile.
export const checkHostInDBInstances = async (hostname: string, region?: string): Promise<VpcConfig | undefined> => {
const client = new RDSClient({ region });
const params: DescribeDBInstancesCommandInput = {
Filters: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { VpcConfig, SubnetAvailabilityZone } from '@aws-amplify/graphql-transfor
import { DB_ENGINES } from './supported-db-engines';
import { filterSubnetAvailabilityZones } from './filter-subnet-availability-zones';

export const checkHostInDBProxies = async (hostname: string, region: string): Promise<VpcConfig | undefined> => {
// When region is not provided, it will use the region configured in the AWS profile.
export const checkHostInDBProxies = async (hostname: string, region?: string): Promise<VpcConfig | undefined> => {
const client = new RDSClient({ region });
const params: DescribeDBProxiesCommandInput = {
Filters: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ const spinner = ora('');
/**
* Searches for the host in DB Proxies, then DB Clusters, and finally DB Instances. Returns the VPC configuration if found. Note that some
* inspections may require additional API calls to derive subnet and availability zone configurations.
*
* When region is not provided, we will use the region configured in the AWS profile.
* @param hostname Hostname of the database.
* @param region AWS region.
* @returns the VpcConfig for the database or undefined if not found.
*/
export const getHostVpc = async (hostname: string, region: string): Promise<VpcConfig | undefined> => {
export const getHostVpc = async (hostname: string, region?: string): Promise<VpcConfig | undefined> => {
const proxyResult = await checkHostInDBProxies(hostname, region);
if (proxyResult) {
return proxyResult;
Expand Down

0 comments on commit 025c462

Please sign in to comment.