Skip to content

Conversation

@lovisaberggren
Copy link
Collaborator

@lovisaberggren lovisaberggren commented Jan 8, 2025

Proposed changes

Adds rule for IPA 113: "Singleton resources must not have a user-provided or system-generated ID".

  • For singleton resources checks the fields in the response for the GET method, if there is a field id or _id it returns a validation error
  • Ignores paginated or array responses

Drive-by:

  • Improvement of the test hook to only create a spectral instance with the specific rule that is tested, reduces the number of rules that spectral will try to validate in one test run

Current offenders:

warning  xgen-IPA-113-singleton-must-not-have-id  Singleton resources must not have a user-provided or system-generated ID. http://go/ipa/113  paths./api/atlas/v2/groups/{groupId}/clusters/{clusterName}/outageSimulation
warning  xgen-IPA-113-singleton-must-not-have-id  Singleton resources must not have a user-provided or system-generated ID. http://go/ipa/113  paths./api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/deployment
warning  xgen-IPA-113-singleton-must-not-have-id  Singleton resources must not have a user-provided or system-generated ID. http://go/ipa/113  paths./api/atlas/v2/orgs/{orgId}/federationSettings

Jira ticket: CLOUDP-287250

"@babel/preset-env": "^7.26.0",
"@eslint/js": "^9.16.0",
"@jest/globals": "^29.7.0",
"@stoplight/types": "^14.1.1",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added as I noticed this one wasn't in the package.json, though we use it in the test files

Comment on lines +28 to +46
async function createSpectral(ruleName) {
const rulesetPath = path.join(__dirname, '../../rulesets', ruleName.slice(5, 12) + '.yaml');
const s = new Spectral({ resolver: httpAndFileResolver });
s.setRuleset(await bundleAndLoadRuleset(rulesetPath, { fs, fetch }));
const ruleset = Object(await bundleAndLoadRuleset(rulesetPath, { fs, fetch })).toJSON();
s.setRuleset(getRulesetForRule(ruleName, ruleset));
return s;
}

function getErrorsForRule(errors, rule) {
return errors.filter((e) => e.code === rule);
/**
* Takes the passed ruleset and returns a ruleset with only the specified rule.
*
* @param ruleName the name of the rule
* @param ruleset the ruleset containing the rule by ruleName and optionally other rules
* @returns {Object} a ruleset with only the rule with name ruleName
*/
function getRulesetForRule(ruleName, ruleset) {
const modifiedRuleset = { rules: {} };
modifiedRuleset.rules[ruleName] = ruleset.rules[ruleName].definition;
return modifiedRuleset;
Copy link
Collaborator Author

@lovisaberggren lovisaberggren Jan 8, 2025

Choose a reason for hiding this comment

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

Drive-by to create a spectral instance with only the specific rule for the test run

@lovisaberggren lovisaberggren marked this pull request as ready for review January 8, 2025 14:30
@lovisaberggren lovisaberggren requested a review from a team as a code owner January 8, 2025 14:30
Copy link
Contributor

@gssbzn gssbzn left a comment

Choose a reason for hiding this comment

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

LGTM

@lovisaberggren lovisaberggren merged commit 4bb7e30 into main Jan 8, 2025
13 checks passed
@lovisaberggren lovisaberggren deleted the CLOUDP-287250 branch January 8, 2025 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants