Skip to content

Commit

Permalink
refactor: 💡 add helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
DhariniJeeva committed Dec 13, 2024
1 parent 3be6f06 commit af68dfe
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 41 deletions.
2 changes: 1 addition & 1 deletion addons/api/addon/models/host-catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class HostCatalogModel extends GeneratedHostCatalogModel {
}

/**
* True if host catalog plugin type is Azure.
* True if host catalog plugin type is GCP.
* @type {boolean}
*/
get isGCP() {
Expand Down
122 changes: 82 additions & 40 deletions addons/api/addon/serializers/host-catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ export default class HostCatalogSerializer extends ApplicationSerializer {
serialize(snapshot) {
const { credentialType } = snapshot.record;
const serialized = super.serialize(...arguments);

// By default, disable credential rotation for dynamic credentials
if (credentialType === TYPE_CREDENTIAL_DYNAMIC) {
serialized.attributes.disable_credential_rotation = true;
}

return serialized;
}

Expand All @@ -24,50 +22,94 @@ export default class HostCatalogSerializer extends ApplicationSerializer {
const { compositeType, credentialType, isPlugin } = snapshot.record;
const { options } = attribute;

if (
options?.compositeType &&
!options.compositeType.includes(compositeType)
) {
delete json[key];
if (json['attributes'] && Object.keys(json.attributes).length === 0) {
delete json.attributes;
}
// Remove invalid fields based on composite type
if (options?.compositeType) {
this._removeInvalidFields(
json,
key,
options.compositeType,
compositeType,
);
}

// Delete any nested attribute fields that don't belong to the record's compositeType or credential Type
if (isPlugin && options.isNestedAttribute && json.attributes) {
if (
options?.compositeType &&
!options.compositeType.includes(compositeType)
) {
delete json.attributes[key];
} else if (
options?.credentialType &&
options.credentialType !== credentialType
) {
json.attributes[key] = null;
// Handle nested attributes and secrets for plugins
if (isPlugin) {
if (options?.isNestedAttribute && json.attributes) {
this._handleNestedAttributes(
json,
key,
options,
compositeType,
credentialType,
);
}
}
// Delete any secret fields that don't belong to the composite or credential type
if (isPlugin && options.isNestedSecret && json.secrets) {
if (
options?.compositeType &&
!options.compositeType.includes(compositeType)
) {
delete json.secrets[key];
if (options?.isNestedSecret && json.secrets) {
this._handleNestedSecrets(
json,
key,
options,
compositeType,
credentialType,
);
}
}
// Clean up empty attributes and secrets
this._cleanUpEmptyObjects(json);
return value;
}

if (
options?.credentialType &&
options.credentialType !== credentialType
) {
delete json.secrets[key];
}
// This removes invalid fields based on composite type
_removeInvalidFields(json, key, validCompositeTypes, compositeType) {
if (!validCompositeTypes.includes(compositeType)) {
delete json[key];
}
}

if (json['secrets'] && Object.keys(json.secrets).length === 0) {
delete json.secrets;
}
// This handles nested attributes for plugins by comparing the composite types first and then the credential types
_handleNestedAttributes(json, key, options, compositeType, credentialType) {
const {
compositeType: validCompositeTypes,
credentialType: validCredentialType,
} = options;
if (validCompositeTypes && !validCompositeTypes.includes(compositeType)) {
delete json.attributes[key];
} else if (validCredentialType && validCredentialType !== credentialType) {
json.attributes[key] = null;
}
}

// This handles nested secret fields for plugins for composite types and credential types
_handleNestedSecrets(json, key, options, compositeType, credentialType) {
const {
compositeType: validCompositeTypes,
credentialType: validCredentialType,
} = options;
this._removeSecrets(
json.secrets,
key,
validCompositeTypes && !validCompositeTypes.includes(compositeType),
);
this._removeSecrets(
json.secrets,
key,
validCredentialType && validCredentialType !== credentialType,
);
}

// This removes secrets based on a condition
_removeSecrets(json, key, condition) {
if (condition) {
delete json?.[key];
}
}

// Delete empty secrets/attributes
_cleanUpEmptyObjects(json) {
if (json.secrets && Object.keys(json.secrets).length === 0) {
delete json.secrets;
}
if (json.attributes && Object.keys(json.attributes).length === 0) {
delete json.attributes;
}
return value;
}
}
22 changes: 22 additions & 0 deletions addons/api/tests/unit/models/host-catalog-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ module('Unit | Model | host catalog', function (hooks) {
assert.false(modelRandom.isAzure);
});

test('it has isGCP property and return the expected values', async function (assert) {
const store = this.owner.lookup('service:store');
const modelGCP = store.createRecord('host-catalog', {
type: 'plugin',
plugin: { name: 'gcp' },
});
const modelRandom = store.createRecord('host-catalog', {
plugin: { name: 'random' },
});
assert.true(modelGCP.isGCP);
assert.false(modelRandom.isGCP);
});

test('get compositeType returns expected values', async function (assert) {
const store = this.owner.lookup('service:store');
const modelA = store.createRecord('host-catalog', {
Expand All @@ -94,8 +107,13 @@ module('Unit | Model | host catalog', function (hooks) {
type: 'plugin',
plugin: { name: 'no-such-type' },
});
const modelD = store.createRecord('host-catalog', {
type: 'plugin',
plugin: { name: 'gcp' },
});
assert.strictEqual(modelA.compositeType, 'static');
assert.strictEqual(modelB.compositeType, 'aws');
assert.strictEqual(modelD.compositeType, 'gcp');
assert.strictEqual(modelC.compositeType, 'unknown');
});

Expand All @@ -107,8 +125,12 @@ module('Unit | Model | host catalog', function (hooks) {
const modelStatic = store.createRecord('host-catalog', {
compositeType: 'static',
});
const modelGCP = store.createRecord('host-catalog', {
compositeType: 'gcp',
});
assert.strictEqual(modelPlugin.type, 'plugin');
assert.strictEqual(modelPlugin.plugin.name, 'aws');
assert.strictEqual(modelStatic.type, 'static');
assert.strictEqual(modelGCP.plugin.name, 'gcp');
});
});

0 comments on commit af68dfe

Please sign in to comment.