From cd6d18d16483b1d5508ccc7f40d54ea36d0ef138 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Tue, 15 Sep 2020 16:36:40 -0400 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20add=20target=20default?= =?UTF-8?q?=5Fport=20field=20to=20API=20and=20UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../models/fragment-target-attributes.js | 10 +++++++ addons/api/addon/models/target.js | 8 +++++- .../app/models/fragment-target-attributes.js | 1 + .../api/tests/unit/serializers/target-test.js | 5 +++- ui/core/app/components/form/target/index.hbs | 27 +++++++++++++++---- ui/core/mirage/factories/target.js | 14 ++++++++++ ui/core/translations/en-us.yaml | 1 + 7 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 addons/api/addon/models/fragment-target-attributes.js create mode 100644 addons/api/app/models/fragment-target-attributes.js diff --git a/addons/api/addon/models/fragment-target-attributes.js b/addons/api/addon/models/fragment-target-attributes.js new file mode 100644 index 000000000..1ab994d9d --- /dev/null +++ b/addons/api/addon/models/fragment-target-attributes.js @@ -0,0 +1,10 @@ +import Fragment from 'ember-data-model-fragments/fragment'; +import { attr } from '@ember-data/model'; + +export default class FragmentTargetAttributesModel extends Fragment { + + @attr('number', { + description: 'The default port a target should use if present.' + }) default_port; + +} diff --git a/addons/api/addon/models/target.js b/addons/api/addon/models/target.js index 03a3ad5d7..57b87ff29 100644 --- a/addons/api/addon/models/target.js +++ b/addons/api/addon/models/target.js @@ -1,11 +1,17 @@ import GeneratedTargetModel from '../generated/models/target'; -import { fragmentArray } from 'ember-data-model-fragments/attributes'; +import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes'; import { computed } from '@ember/object'; export default class TargetModel extends GeneratedTargetModel { // =attributes + /** + * Attributes of this resource, if any, represented as a JSON fragment. + * @type {FragmentTargetAttributesModel} + */ + @fragment('fragment-target-attributes', { defaultValue: {} }) attributes; + /** * @type {[FragmentHostSetModel]} */ diff --git a/addons/api/app/models/fragment-target-attributes.js b/addons/api/app/models/fragment-target-attributes.js new file mode 100644 index 000000000..41eb7d88f --- /dev/null +++ b/addons/api/app/models/fragment-target-attributes.js @@ -0,0 +1 @@ +export { default } from 'api/models/fragment-target-attributes'; diff --git a/addons/api/tests/unit/serializers/target-test.js b/addons/api/tests/unit/serializers/target-test.js index 94cab05e5..e129331eb 100644 --- a/addons/api/tests/unit/serializers/target-test.js +++ b/addons/api/tests/unit/serializers/target-test.js @@ -30,7 +30,10 @@ module('Unit | Serializer | target', function(hooks) { description: 'Description', version: 1, type: 'tcp', - scope_id: 'org_1' + scope_id: 'org_1', + attributes: { + default_port: null + } }); }); diff --git a/ui/core/app/components/form/target/index.hbs b/ui/core/app/components/form/target/index.hbs index a6e5a204e..5a977d2e4 100644 --- a/ui/core/app/components/form/target/index.hbs +++ b/ui/core/app/components/form/target/index.hbs @@ -14,11 +14,10 @@ {{#if @model.errors.name}} @@ -31,12 +30,10 @@ {{#if @model.errors.description}} @@ -47,6 +44,26 @@ {{/if}} + {{!-- TODO this is a type-specific field --}} + + {{!-- TODO need to figure out how to reference `attributes.errors` here --}} + {{#if @model.errors.default_port}} + + {{#each @model.errors.default_port as |error|}} + {{error.message}} + {{/each}} + + {{/if}} + + Math.random() < chance; const hostSetChance = 0.3; export default factory.extend({ + /** + * Generates attributes fields by type. + */ + afterCreate(target) { + if (target.type === 'tcp') { + target.update({ + attributes: { + default_port: random.number() + } + }); + } + }, + /** * Randomly selects existing host sets to assign to target. */ diff --git a/ui/core/translations/en-us.yaml b/ui/core/translations/en-us.yaml index 394f1e6ec..142c3577f 100644 --- a/ui/core/translations/en-us.yaml +++ b/ui/core/translations/en-us.yaml @@ -140,6 +140,7 @@ form: address: Address protocol: Protocol actions: Actions + default_port: Default Port errors: titles: unauthenticated-error: You are not signed in