Skip to content

adding a mechanism to get the reference counts of properties in the model #140

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

Merged
merged 1 commit into from
Jun 10, 2022
Merged
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
35 changes: 35 additions & 0 deletions webui/src/js/models/wdt-model-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ define(['knockout', 'utils/observable-properties', 'js-yaml', 'utils/validation-
*/
return function (name) {
const ID = '[A-Za-z0-9_.-]*';
const PROPERTY_PATTERN = new RegExp(`@@PROP:(@@ENV:(?<envvar>${ID})@@)?(?<name>${ID})@@`, 'g');
const SECRET_PATTERN = new RegExp(`@@SECRET:(@@ENV:(?<envvar>${ID})@@)?(?<name>${ID}):(?<field>${ID})@@`, 'g');
function WdtModel() {
const defaultDomainName = 'base_domain';
Expand Down Expand Up @@ -102,6 +103,40 @@ define(['knockout', 'utils/observable-properties', 'js-yaml', 'utils/validation-
}
};

this.getModelPropertiesReferenceCounts = () => {
const propertiesMap = new Map();

[...this.modelContent().matchAll(PROPERTY_PATTERN)].forEach(matches => {
const propertyName = matches.groups.name;
const propertyEnvVar = matches.groups.envvar;

// While this key is never used outside this function, we need the key to
// match the resolved property name. For example, if the DOMAIN_UID is mydomain,
// the following two fields should refer to the same property:
//
// field1: '@@PROP:@@ENV:DOMAIN_UID@@-value@@'
// field2: '@@PROP:mydomain-value@@'
//
let propertyKey = propertyName;
if (propertyEnvVar) {
propertyKey = propertyName.startsWith('-') ? `${propertyEnvVar}${propertyName}` : `${propertyEnvVar}-${propertyName}`;
}

let propertyData;
if (propertiesMap.has(propertyKey)) {
propertyData = propertiesMap.get(propertyKey);
propertyData.referenceCount++;
} else {
propertyData ={ name: propertyName, referenceCount: 1 };
if (propertyEnvVar) {
propertyData.envVar = propertyEnvVar;
}
}
propertiesMap.set(propertyKey, propertyData);
});
return [...propertiesMap.values()];
}

// Placeholder for when multiple model files are supported so that the domain page can reliably get all
// secrets in the models.
//
Expand Down