Skip to content

Commit

Permalink
Fixes #321 - j1-integration document output in alphabetical order by …
Browse files Browse the repository at this point in the history
…_type
  • Loading branch information
austinkelleher committed Aug 30, 2020
1 parent 28baab2 commit 7964752
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,88 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`document alphabetizes "entities" metadata 1`] = `
"# Integration with JupiterOne
## Setup
In this section, please provide details about how to set up the integration with
JupiterOne. This may require provisioning some resources on the provider's side
(perhaps a role, app, or api key) and passing information over to JupiterOne.
<!-- {J1_DOCUMENTATION_MARKER_START} -->
<!--
********************************************************************************
NOTE: ALL OF THE FOLLOWING DOCUMENTATION IS GENERATED USING THE
\\"j1-integration document\\" COMMAND. DO NOT EDIT BY HAND! PLEASE SEE THE DEVELOPER
DOCUMENTATION FOR USAGE INFORMATION:
https://github.com/JupiterOne/sdk/blob/master/docs/integrations/development.md
********************************************************************************
-->
## Data Model
### Entities
The following entities are created:
| Resources | Entity \`_type\` | Entity \`_class\` |
| ----------- | -------------- | --------------- |
| The Group | \`my_group\` | \`Group\` |
| The Group 2 | \`my_group_2\` | \`Group\` |
| The User | \`my_user\` | \`User\` |
| The User 2 | \`my_user_2\` | \`User\` |
<!--
********************************************************************************
END OF GENERATED DOCUMENTATION AFTER BELOW MARKER
********************************************************************************
-->
<!-- {J1_DOCUMENTATION_MARKER_END} -->"
`;

exports[`document alphabetizes "relationships" metadata 1`] = `
"# Integration with JupiterOne
## Setup
In this section, please provide details about how to set up the integration with
JupiterOne. This may require provisioning some resources on the provider's side
(perhaps a role, app, or api key) and passing information over to JupiterOne.
<!-- {J1_DOCUMENTATION_MARKER_START} -->
<!--
********************************************************************************
NOTE: ALL OF THE FOLLOWING DOCUMENTATION IS GENERATED USING THE
\\"j1-integration document\\" COMMAND. DO NOT EDIT BY HAND! PLEASE SEE THE DEVELOPER
DOCUMENTATION FOR USAGE INFORMATION:
https://github.com/JupiterOne/sdk/blob/master/docs/integrations/development.md
********************************************************************************
-->
## Data Model
### Relationships
The following relationships are created/mapped:
| Source Entity \`_type\` | Relationship \`_class\` | Target Entity \`_type\` |
| --------------------- | --------------------- | --------------------- |
| \`the_root\` | **HAS** | \`my_account\` |
| \`the_root\` | **HAS** | \`my_account_1\` |
| \`the_root\` | **HAS** | \`my_account_2\` |
<!--
********************************************************************************
END OF GENERATED DOCUMENTATION AFTER BELOW MARKER
********************************************************************************
-->
<!-- {J1_DOCUMENTATION_MARKER_END} -->"
`;

exports[`document handles duplicate entity _type ingested in multiple steps 1`] = `
"# Integration with JupiterOne
Expand Down
8 changes: 8 additions & 0 deletions packages/integration-sdk-cli/src/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ describe('document', () => {
await documentCommandSnapshotTest('docsInstanceDuplicateRelationshipTypes');
});

test('alphabetizes "entities" metadata', async () => {
await documentCommandSnapshotTest('docsInstanceEntitiesAlphabetize');
});

test('alphabetizes "relationships" metadata', async () => {
await documentCommandSnapshotTest('docsInstanceRelationshipsAlphabetize');
});

test('should allow passing a file path for the generated documentation', async () => {
loadProjectStructure('docsInstanceCustomDocLoc');

Expand Down
26 changes: 25 additions & 1 deletion packages/integration-sdk-cli/src/commands/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ async function executeDocumentAction(

log.info('\nConfiguration successfully loaded!\n');

const metadata = collectGraphObjectMetadataFromSteps(config.integrationSteps);
const metadata = alphabetizeMetadataProperties(
collectGraphObjectMetadataFromSteps(config.integrationSteps),
);

if (!metadata.entities.length && !metadata.relationships.length) {
log.info(
Expand Down Expand Up @@ -165,6 +167,28 @@ function generateRelationshipTableFromAllStepEntityMetadata(
return generated;
}

function alphabetizeMetadataPropertyByTypeCompareFn(
a: StepEntityMetadata | StepRelationshipMetadata,
b: StepEntityMetadata | StepRelationshipMetadata,
): number {
if (a._type > b._type) return 1;
if (a._type < b._type) return -1;
return 0;
}

function alphabetizeMetadataProperties(
metadata: StepGraphObjectMetadataProperties,
): StepGraphObjectMetadataProperties {
return {
entities: metadata.entities.sort(
alphabetizeMetadataPropertyByTypeCompareFn,
),
relationships: metadata.relationships.sort(
alphabetizeMetadataPropertyByTypeCompareFn,
),
};
}

function collectGraphObjectMetadataFromSteps(
steps: Step<IntegrationStepExecutionContext<object>>[],
): StepGraphObjectMetadataProperties {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.j1-integration
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Integration with JupiterOne

## Setup

In this section, please provide details about how to set up the integration with
JupiterOne. This may require provisioning some resources on the provider's side
(perhaps a role, app, or api key) and passing information over to JupiterOne.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fetchDataSteps from './steps/fetchData';
import {
IntegrationInvocationConfig,
IntegrationInstanceConfig,
} from '@jupiterone/integration-sdk-core';

export const invocationConfig: IntegrationInvocationConfig<IntegrationInstanceConfig> = {
instanceConfigFields: {},
integrationSteps: [fetchDataSteps],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import noop from 'lodash/noop';
import { StepExecutionContext, Step } from '@jupiterone/integration-sdk-core';

const fetchDataSteps: Step<StepExecutionContext> = {
id: 'fetch-data',
name: 'Fetch Data',
entities: [
{
resourceName: 'The User 2',
_type: 'my_user_2',
_class: 'User',
},
{
resourceName: 'The Group',
_type: 'my_group',
_class: 'Group',
},
{
resourceName: 'The Group 2',
_type: 'my_group_2',
_class: 'Group',
},
{
resourceName: 'The User',
_type: 'my_user',
_class: 'User',
},
],
relationships: [],
executionHandler: noop,
};

export default fetchDataSteps;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.j1-integration
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Integration with JupiterOne

## Setup

In this section, please provide details about how to set up the integration with
JupiterOne. This may require provisioning some resources on the provider's side
(perhaps a role, app, or api key) and passing information over to JupiterOne.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fetchAccountsStep from './steps/fetchAccounts';
import {
IntegrationInvocationConfig,
IntegrationInstanceConfig,
} from '@jupiterone/integration-sdk-core';

export const invocationConfig: IntegrationInvocationConfig<IntegrationInstanceConfig> = {
instanceConfigFields: {},
integrationSteps: [fetchAccountsStep],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import noop from 'lodash/noop';
import { StepExecutionContext, Step } from '@jupiterone/integration-sdk-core';
import { RelationshipClass } from '@jupiterone/data-model';

const fetchAccountsStep: Step<StepExecutionContext> = {
id: 'fetch-accounts',
name: 'Fetch Accounts',
entities: [],
relationships: [
{
_type: 'the_root_has_my_account_2',
_class: RelationshipClass.HAS,
sourceType: 'the_root',
targetType: 'my_account_2',
},
{
_type: 'the_root_has_my_account',
_class: RelationshipClass.HAS,
sourceType: 'the_root',
targetType: 'my_account',
},
{
_type: 'the_root_has_my_account_1',
_class: RelationshipClass.HAS,
sourceType: 'the_root',
targetType: 'my_account_1',
},
],
executionHandler: noop,
};

export default fetchAccountsStep;
8 changes: 7 additions & 1 deletion packages/integration-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ and this project adheres to

## Unreleased

### Changed

- [#321](https://github.com/JupiterOne/sdk/issues/321) -
`j1-integration document` output in alphabetical order by `_type`.

## 3.1.0 - 2020-08-26

### Changed
Expand All @@ -19,7 +24,8 @@ and this project adheres to

### Fixed

- [#301](https://github.com/JupiterOne/sdk/issues/301) - Fix test `findEntity` for initialized entities in a `MockJobState`.
- [#301](https://github.com/JupiterOne/sdk/issues/301) - Fix test `findEntity`
for initialized entities in a `MockJobState`.

## 3.0.0 - 2020-08-24

Expand Down

0 comments on commit 7964752

Please sign in to comment.