Skip to content

Commit

Permalink
Enhancement: diff now supports adding children diff recursively.
Browse files Browse the repository at this point in the history
  • Loading branch information
rash805115 committed May 28, 2023
1 parent 62379e3 commit f22bfa5
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/v0/models/app/app.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export class App implements IModel<App> {
region.regionId,
),
);

const regionDiff = region.diffAdd();
if (regionDiff.length !== 0) {
diff.push(...regionDiff);
}
}
}

Expand Down Expand Up @@ -130,6 +135,11 @@ export class App implements IModel<App> {
server.serverKey,
),
);

const serverDiff = server.diffAdd();
if (serverDiff.length !== 0) {
diff.push(...serverDiff);
}
}
}

Expand Down Expand Up @@ -164,6 +174,11 @@ export class App implements IModel<App> {
support.serverKey,
),
);

const supportDiff = support.diffAdd();
if (supportDiff.length !== 0) {
diff.push(...supportDiff);
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/v0/models/deployment/deployment.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ export class Deployment implements IModel<Deployment> {
return [];
}

/**
* Generate a diff adding all children of self.
*/
diffAdd(): Diff[] {
return [];
}

getContext(): string {
return [`deployment=${this.deploymentTag}`, this.context.getContext].join(
return [`deployment=${this.deploymentTag}`, this.context.getContext()].join(
',',
);
}
Expand Down
18 changes: 18 additions & 0 deletions src/v0/models/environment/environment.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ export class Environment implements IModel<Environment> {
return diff;
}

/**
* Generate a diff adding all children of self.
*/
diffAdd(): Diff[] {
const diff: Diff[] = [];

for (const [key, value] of this.environmentVariables) {
diff.push(
new Diff(DiffAction.ADD, this.getContext(), 'environmentVariables', {
key,
value,
}),
);
}

return diff;
}

getContext(): string {
return [
`environment=${this.environmentName}`,
Expand Down
9 changes: 9 additions & 0 deletions src/v0/models/model.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { Diff } from '../utility/diff.utility';

export interface IModel<T> {
/**
* Create a duplicate instance of this model.
*/
clone(): T;

/**
* Generate a diff comparing all children of self with latest instance.
*/
diff(latest: T): Diff[];

/**
* Get a string representation of context.
*/
getContext(): string;
}
24 changes: 23 additions & 1 deletion src/v0/models/region/region.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ describe('Region UT', () => {

it('should capture addition of an environment', () => {
const oldRegion = new AwsRegion(new App('test'), 'aws-us-east-1');

const newRegion = new AwsRegion(new App('test'), 'aws-us-east-1');
newRegion.addEnvironment(new Environment(newRegion, 'qa'));
const newEnvironment = new Environment(newRegion, 'qa');
newEnvironment.environmentVariables.set('key1', 'value 1');
newEnvironment.environmentVariables.set('key2', 'value 2');
newRegion.addEnvironment(newEnvironment);

const diff = oldRegion.diff(newRegion);

Expand All @@ -87,6 +91,24 @@ describe('Region UT', () => {
"field": "environment",
"value": "qa",
},
Diff {
"action": "add",
"context": "environment=qa,region=aws-us-east-1,app=test",
"field": "environmentVariables",
"value": {
"key": "key1",
"value": "value 1",
},
},
Diff {
"action": "add",
"context": "environment=qa,region=aws-us-east-1,app=test",
"field": "environmentVariables",
"value": {
"key": "key2",
"value": "value 2",
},
},
]
`);
});
Expand Down
32 changes: 31 additions & 1 deletion src/v0/models/region/region.model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Diff, DiffAction } from '../../utility/diff.utility';
import { App } from '../app/app.model';
import { AwsRegionId } from './aws/region.model';
import { Environment } from '../environment/environment.model';
import { IModel } from '../model.interface';
import { AwsRegionId } from './aws/region.model';

export type RegionId = AwsRegionId;

Expand Down Expand Up @@ -79,6 +79,36 @@ export class Region implements IModel<Region> {
environment.environmentName,
),
);

const environmentDiff = environment.diffAdd();
if (environmentDiff.length !== 0) {
diff.push(...environmentDiff);
}
}
}

return diff;
}

/**
* Generate a diff adding all children of self.
*/
diffAdd(): Diff[] {
const diff: Diff[] = [];

for (const environment of this.environments) {
diff.push(
new Diff(
DiffAction.ADD,
this.getContext(),
'environment',
environment.environmentName,
),
);

const environmentDiff = environment.diffAdd();
if (environmentDiff.length !== 0) {
diff.push(...environmentDiff);
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/v0/models/server/server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,36 @@ export class Server implements IModel<Server> {
deployment.deploymentTag,
),
);

const deploymentDiff = deployment.diffAdd();
if (deploymentDiff.length !== 0) {
diff.push(...deploymentDiff);
}
}
}

return diff;
}

/**
* Generate a diff adding all children of self.
*/
diffAdd(): Diff[] {
const diff: Diff[] = [];

for (const deployment of this.deployments) {
diff.push(
new Diff(
DiffAction.ADD,
this.getContext(),
'deployment',
deployment.deploymentTag,
),
);

const deploymentDiff = deployment.diffAdd();
if (deploymentDiff.length !== 0) {
diff.push(...deploymentDiff);
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/v0/models/support/support.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,36 @@ export class Support implements IModel<Support> {
deployment.deploymentTag,
),
);

const deploymentDiff = deployment.diffAdd();
if (deploymentDiff.length !== 0) {
diff.push(...deploymentDiff);
}
}
}

return diff;
}

/**
* Generate a diff adding all children of self.
*/
diffAdd(): Diff[] {
const diff: Diff[] = [];

for (const deployment of this.deployments) {
diff.push(
new Diff(
DiffAction.ADD,
this.getContext(),
'deployment',
deployment.deploymentTag,
),
);

const deploymentDiff = deployment.diffAdd();
if (deploymentDiff.length !== 0) {
diff.push(...deploymentDiff);
}
}

Expand Down
58 changes: 54 additions & 4 deletions test/v0/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { App, AwsRegion, Environment, Server, Support } from '../../src/v0';
import {
App,
AwsRegion,
Deployment,
Environment,
Server,
Support,
} from '../../src/v0';

describe('App E2E Test', () => {
it('should generate app diff', () => {
Expand All @@ -17,6 +24,14 @@ describe('App E2E Test', () => {
const newAppRegion = newApp.regions.find(
(r) => r.regionId === 'aws-us-east-1',
);
const newAppBackendServer = newApp.servers.find(
(s) => s.serverKey === 'backend',
);

// Add a deployment to backend server.
newAppBackendServer.addDeployment(
new Deployment(newAppBackendServer, 'v0.0.1'),
);

// Add a new staging environment.
const stagingEnvironment = new Environment(newAppRegion, 'staging');
Expand All @@ -28,9 +43,17 @@ describe('App E2E Test', () => {
.find((e) => e.environmentName === 'qa')
.environmentVariables.set('env', 'qa');

// Add new server and support.
newApp.addServer(new Server(newApp, 'database'));
newApp.addSupport(new Support(newApp, 'nginx'));
// Add new server.
const newAppDatabaseServer = new Server(newApp, 'database');
newAppDatabaseServer.addDeployment(
new Deployment(newAppDatabaseServer, 'v0.0.1'),
);
newApp.addServer(newAppDatabaseServer);

// Add new support.
const newAppNginxSupport = new Support(newApp, 'nginx');
newAppNginxSupport.addDeployment(new Deployment(newAppNginxSupport, 'v1'));
newApp.addSupport(newAppNginxSupport);

expect(app.diff(newApp)).toMatchInlineSnapshot(`
[
Expand All @@ -49,18 +72,45 @@ describe('App E2E Test', () => {
"field": "environment",
"value": "staging",
},
Diff {
"action": "add",
"context": "environment=staging,region=aws-us-east-1,app=test-app",
"field": "environmentVariables",
"value": {
"key": "env",
"value": "staging",
},
},
Diff {
"action": "add",
"context": "server=backend,app=test-app",
"field": "deployment",
"value": "v0.0.1",
},
Diff {
"action": "add",
"context": "app=test-app",
"field": "server",
"value": "database",
},
Diff {
"action": "add",
"context": "server=database,app=test-app",
"field": "deployment",
"value": "v0.0.1",
},
Diff {
"action": "add",
"context": "app=test-app",
"field": "support",
"value": "nginx",
},
Diff {
"action": "add",
"context": "support=nginx,app=test-app",
"field": "deployment",
"value": "v1",
},
]
`);
});
Expand Down

0 comments on commit f22bfa5

Please sign in to comment.