Skip to content

Commit

Permalink
UI: Namespace area fixes (hashicorp#19799)
Browse files Browse the repository at this point in the history
  • Loading branch information
hashishaw committed Apr 5, 2023
1 parent 1fe70f8 commit 44cf5e9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 33 deletions.
3 changes: 3 additions & 0 deletions changelog/19799.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
ui: Fix bad link to namespace when namespace name includes `.`
```
20 changes: 10 additions & 10 deletions ui/app/adapters/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@

import ApplicationAdapter from './application';

export default ApplicationAdapter.extend({
export default class NamespaceAdapter extends ApplicationAdapter {
pathForType() {
return 'namespaces';
},
}
urlForFindAll(modelName, snapshot) {
if (snapshot.adapterOptions && snapshot.adapterOptions.forUser) {
return `/${this.urlPrefix()}/internal/ui/namespaces`;
}
return `/${this.urlPrefix()}/namespaces?list=true`;
},
}

urlForCreateRecord(modelName, snapshot) {
const id = snapshot.attr('path');
return this.buildURL(modelName, id);
},
}

createRecord(store, type, snapshot) {
const id = snapshot.attr('path');
return this._super(...arguments).then(() => {
return super.createRecord(...arguments).then(() => {
return { id };
});
},
}

findAll(store, type, sinceToken, snapshot) {
if (snapshot.adapterOptions && typeof snapshot.adapterOptions.namespace !== 'undefined') {
return this.ajax(this.urlForFindAll('namespace', snapshot), 'GET', {
namespace: snapshot.adapterOptions.namespace,
});
}
return this._super(...arguments);
},
return super.findAll(...arguments);
}
query() {
return this.ajax(`/${this.urlPrefix()}/namespaces?list=true`);
},
});
}
}
9 changes: 6 additions & 3 deletions ui/app/components/namespace-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ export default Component.extend({
//public api
targetNamespace: null,
showLastSegment: false,
// set to true if targetNamespace is passed in unmodified
// otherwise, this assumes it is parsed as in namespace-picker
unparsed: false,

normalizedNamespace: computed('targetNamespace', function () {
const ns = this.targetNamespace;
return (ns || '').replace(/\.+/g, '/').replace(//g, '.');
normalizedNamespace: computed('targetNamespace', 'unparsed', function () {
const ns = this.targetNamespace || '';
return this.unparsed ? ns : ns.replace(/\.+/g, '/').replace(//g, '.');
}),

namespaceDisplay: computed('normalizedNamespace', 'showLastSegment', function () {
Expand Down
28 changes: 14 additions & 14 deletions ui/app/models/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
*/

import Model, { attr } from '@ember-data/model';
import { computed } from '@ember/object';
import { expandAttributeMeta } from 'vault/utils/field-to-attrs';
import { withExpandedAttributes } from 'vault/decorators/model-expanded-attributes';

export default Model.extend({
path: attr('string', {
@withExpandedAttributes()
export default class NamespaceModel extends Model {
@attr('string', {
validationAttr: 'pathIsValid',
invalidMessage: 'You have entered and invalid path please only include letters, numbers, -, ., and _.',
}),
pathIsValid: computed('path', function () {
})
path;

get pathIsValid() {
return this.path && this.path.match(/^[\w\d-.]+$/g);
}),
description: attr('string', {
editType: 'textarea',
}),
fields: computed(function () {
return expandAttributeMeta(this, ['path']);
}),
});
}

get fields() {
return ['path'].map((f) => this.allByKey[f]);
}
}
14 changes: 9 additions & 5 deletions ui/app/serializers/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

import ApplicationSerializer from './application';

export default ApplicationSerializer.extend({
export default class NamespaceSerializer extends ApplicationSerializer {
attrs = {
path: { serialize: false },
};

normalizeList(payload) {
const data = payload.data.keys
? payload.data.keys.map((key) => ({
Expand All @@ -16,14 +20,14 @@ export default ApplicationSerializer.extend({
: payload.data;

return data;
},
}

normalizeResponse(store, primaryModelClass, payload, id, requestType) {
const nullResponses = ['deleteRecord', 'createRecord'];
const cid = (id || payload.id || '').replace(/\/$/, '');
const normalizedPayload = nullResponses.includes(requestType)
? { id: cid, path: cid }
: this.normalizeList(payload);
return this._super(store, primaryModelClass, normalizedPayload, id, requestType);
},
});
return super.normalizeResponse(store, primaryModelClass, normalizedPayload, id, requestType);
}
}
2 changes: 1 addition & 1 deletion ui/app/templates/vault/cluster/access/namespaces/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
{{#let (concat this.currentNamespace (if this.currentNamespace "/") list.item.id) as |targetNamespace|}}
{{#if (includes targetNamespace this.accessibleNamespaces)}}
<li class="action">
<NamespaceLink @targetNamespace={{targetNamespace}} @class="is-block">
<NamespaceLink @targetNamespace={{targetNamespace}} @unparsed={{true}} @class="is-block">
Switch to Namespace
</NamespaceLink>
</li>
Expand Down

0 comments on commit 44cf5e9

Please sign in to comment.