2017-12-15 21:39:18 +00:00
|
|
|
import { get, computed } from '@ember/object';
|
2021-02-17 21:01:44 +00:00
|
|
|
import { attr } from '@ember-data/model';
|
2017-09-19 14:47:10 +00:00
|
|
|
import Fragment from 'ember-data-model-fragments/fragment';
|
2018-07-06 20:38:57 +00:00
|
|
|
import flat from 'flat';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
const { unflatten } = flat;
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
export default class NodeAttributes extends Fragment {
|
|
|
|
@attr() nodeAttributes;
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('nodeAttributes')
|
|
|
|
get attributesStructured() {
|
2019-10-08 18:44:19 +00:00
|
|
|
const original = this.nodeAttributes;
|
2018-02-07 02:52:16 +00:00
|
|
|
|
|
|
|
if (!original) {
|
2020-06-10 14:07:16 +00:00
|
|
|
return undefined;
|
2018-02-07 02:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// `unflatten` doesn't sort keys before unflattening, so manual preprocessing is necessary.
|
2017-12-15 21:39:18 +00:00
|
|
|
const attrs = Object.keys(original)
|
|
|
|
.sort()
|
|
|
|
.reduce((obj, key) => {
|
|
|
|
obj[key] = original[key];
|
|
|
|
return obj;
|
|
|
|
}, {});
|
2017-09-19 14:47:10 +00:00
|
|
|
return unflatten(attrs, { overwrite: true });
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
unknownProperty(key) {
|
|
|
|
// Returns the exact value in index 0 and the subtree in index 1
|
|
|
|
//
|
|
|
|
// ex: nodeAttrs.get('driver.docker')
|
|
|
|
// [ "1", { version: "17.05.0-ce", volumes: { enabled: "1" } } ]
|
2019-10-08 18:44:19 +00:00
|
|
|
if (this.attributesStructured) {
|
|
|
|
return get(this.attributesStructured, key);
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
}
|