open-consul/ui/packages/consul-ui/app/models/intention.js
John Cowen 299ecff703
ui: Adds @NullValue attr decorator (#9587)
There are many places in the API where we receive a property set to
`null` which can then lead to defensive code deeper in the app in order
to guard for this type of thing when usually we are expecting an array
or for the property to be undefined using omitempty on the backend.

Previously we had two places where we would deal with this in the
serializer using our 'remove-null' util (KV and Intentions).

This new decorator lets you declaritively define this type of data using
a decorator @NullValue([]) (which would replce a null value with [].

@NullValue in turn uses a more generic @replace helper, which we
currently don't need but would let you replace any value with another,
not just a null value.

An additional benefit here is that the guard/replacement is executed
lazily when we get the property instead of serializing all the values
when they come in via the API. On super large datasets, where we only
visualize part of the dataset (say in our scroll panes), this feels like
a good improvement on the previous approach.
2021-01-27 10:41:24 +00:00

45 lines
1.4 KiB
JavaScript

import Model, { attr } from '@ember-data/model';
import { computed } from '@ember/object';
import { fragmentArray } from 'ember-data-model-fragments/attributes';
import { nullValue } from 'consul-ui/decorators/replace';
export const PRIMARY_KEY = 'uid';
export const SLUG_KEY = 'ID';
export default class Intention extends Model {
@attr('string') uid;
@attr('string') ID;
@attr('string') Datacenter;
@attr('string') Description;
@attr('string', { defaultValue: () => 'default' }) SourceNS;
@attr('string', { defaultValue: () => '*' }) SourceName;
@attr('string', { defaultValue: () => 'default' }) DestinationNS;
@attr('string', { defaultValue: () => '*' }) DestinationName;
@attr('number') Precedence;
@attr('string', { defaultValue: () => 'consul' }) SourceType;
@nullValue(undefined) @attr('string') Action;
@attr('string') LegacyID;
@attr('boolean', { defaultValue: () => true }) Legacy;
@attr('number') SyncTime;
@attr('date') CreatedAt;
@attr('date') UpdatedAt;
@attr('number') CreateIndex;
@attr('number') ModifyIndex;
@attr() Meta; // {}
@fragmentArray('intention-permission') Permissions;
@computed('Meta')
get IsManagedByCRD() {
const meta = Object.entries(this.Meta || {}).find(
([key, value]) => key === 'external-source' && value === 'kubernetes'
);
return typeof meta !== 'undefined';
}
@computed('IsManagedByCRD')
get IsEditable() {
return !this.IsManagedByCRD;
}
}