Merge pull request #96 from hashicorp/refactor-host-catalogs

Refactor host catalogs
This commit is contained in:
Susmitha Girumala authored and GitHub committed 2020-07-29 15:50:47 -04:00
commit e145ea38ba
25 files changed
+467 -63

No files matched your search

@@ -0,0 +1,19 @@
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
export default class ScopesScopeProjectsProjectHostCatalogsController extends Controller {
// =services
@service intl;
// =attributes
/**
* @type {string}
*/
get breadCrumb() {
return this.intl.t('resources.host-catalogs');
}
}
@@ -0,0 +1,14 @@
import Controller from '@ember/controller';
export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogController extends Controller {
// =attributes
/**
* @type {string}
*/
get breadCrumb() {
return this.model.displayName;
}
}
+6 -1
View File
@@ -14,7 +14,12 @@ Router.map(function () {
this.route('method', { path: ':auth_method_id'});
});
this.route('projects', function() {
this.route('project', { path: ':project_id' }, function() {});
this.route('project', { path: ':project_id' }, function() {
this.route('host-catalogs', function() {
this.route('new');
this.route('host-catalog', { path: ':host_catalog_id' }, function() {});
});
});
this.route('new');
});
});
@@ -14,4 +14,19 @@ export default class ScopesScopeProjectsProjectRoute extends Route {
return this.store.findRecord('scope', project_id);
}
/**
* Renders the project-specific sidebar template.
* @override
* @param {object} controller
* @param {object} model
*/
renderTemplate(controller, model) {
this.render('scopes/scope/projects/project/_sidebar', {
into: 'scopes/scope',
outlet: 'sidebar',
model: model
});
super.renderTemplate(...arguments);
}
}
@@ -0,0 +1,73 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
export default class ScopesScopeProjectsProjectHostCatalogsRoute extends Route {
// =services
@service notify;
// =methods
/**
* Loads all scopes under the current scope.
* @return {Promise{[HostCatalogModel]}}
*/
async model() {
const scopeID = this.modelFor('scopes.scope.projects.project').id;
return this.store.findAll('host-catalog', { adapterOptions: { scopeID } });
}
// =actions
/**
* Rollback changes on project.
* @param {HostCatalogModel} hostCatalog
*/
@action
cancel(hostCatalog) {
const { isNew } = hostCatalog;
hostCatalog.rollbackAttributes();
if (isNew) this.transitionTo('scopes.scope.projects.project.host-catalogs');
}
/**
* Handle save.
* @param {HostCatalogModel} hostCatalog
* @param {Event} e
*/
@action
async save(hostCatalog) {
try {
await hostCatalog.save();
this.refresh();
// TODO: replace with translated strings
this.notify.success('Save succeeded.');
this.transitionTo('scopes.scope.projects.project.host-catalogs.host-catalog', hostCatalog);
} catch (error) {
// TODO: replace with translated strings
this.notify.error(error.message, { closeAfter: null });
}
}
/**
* Deletes the host catalog and redirects to index.
* @param {ProjectModel} project
*/
@action
async delete(project) {
try {
await project.destroyRecord();
this.refresh();
this.transitionTo('scopes.scope.projects.project.host-catalogs');
// TODO: replace with translated strings
this.notify.success('Deleted host catalog succesfully.');
} catch (error) {
// TODO: replace with translated strings
this.notify.error(error.message, { closeAfter: null });
}
}
}
@@ -0,0 +1,4 @@
import Route from '@ember/routing/route';
export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogRoute extends Route {
}
@@ -0,0 +1,4 @@
import Route from '@ember/routing/route';
export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogIndexRoute extends Route {
}
@@ -0,0 +1,4 @@
import Route from '@ember/routing/route';
export default class ScopesScopeProjectsProjectHostCatalogsIndexRoute extends Route {
}
@@ -0,0 +1,19 @@
import Route from '@ember/routing/route';
export default class ScopesScopeProjectsProjectHostCatalogsNewRoute extends Route {
// =methods
/**
* Creates a new unsaved host catalog belonging to the current project scope.
* @return {HostCatalogModel}
*/
model() {
const scopeModel = this.modelFor('scopes.scope.projects.project');
return this.store.createRecord('host-catalog', {
type: 'static',
scopeModel
});
}
}
@@ -0,0 +1,5 @@
<Rose::Nav::Sidebar @title={{t "titles.resources"}} as |nav|>
<nav.link @route="scopes.scope.projects.project.host-catalogs">
{{t "resources.host-catalogs"}}
</nav.link>
</Rose::Nav::Sidebar>
@@ -0,0 +1,3 @@
{{title (t "resources.host-catalogs")}}
{{outlet}}
@@ -0,0 +1,3 @@
{{title @model.displayName}}
{{outlet}}
@@ -0,0 +1,35 @@
<Rose::Layout::Page as |page|>
<page.breadcrumbs>
<BreadCrumbs />
</page.breadcrumbs>
<page.header>
<h2>{{t "resources.host-catalog"}}</h2>
</page.header>
<page.navigation>
<Rose::Nav::Tabs as |nav|>
<nav.link @route='scopes.scope.projects.project.host-catalogs.host-catalog'>
{{t 'titles.details'}}
</nav.link>
</Rose::Nav::Tabs>
</page.navigation>
<page.body>
<Form::HostCatalog
@model={{@model}}
@submit={{route-action "save" @model}}
@cancel={{route-action "cancel" @model}}
/>
<Rose::Separator />
<Rose::Button
@style="warning"
@disabled={{@model.canSave}}
{{on "click" (route-action "delete" @model)}}
>
{{t 'actions.delete'}}
</Rose::Button>
</page.body>
</Rose::Layout::Page>
@@ -0,0 +1,71 @@
<Rose::Layout::Page as |page|>
<page.breadcrumbs>
<BreadCrumbs />
</page.breadcrumbs>
<page.header>
<h2>{{t "resources.host-catalogs"}}</h2>
<p>{{t "descriptions.host-catalogs"}}</p>
</page.header>
<page.actions>
{{#if @model}}
<Rose::LinkButton @route="scopes.scope.projects.project.host-catalogs.new" @style="primary">
{{t "titles.new"}}
</Rose::LinkButton>
{{/if}}
</page.actions>
<page.body>
{{#if @model}}
<Rose::Table as |table|>
<table.header as |header|>
<header.row as |row|>
<row.cell>{{t "form.label.id"}}</row.cell>
<row.cell>{{t "form.label.name"}}</row.cell>
<row.cell>{{t "form.label.type"}}</row.cell>
</header.row>
</table.header>
<table.body as |body|>
{{#each @model as |hostCatalog|}}
<body.row as |row|>
<row.cell>
<code>{{hostCatalog.id}}</code>
</row.cell>
<row.cell>
<LinkTo
@route="scopes.scope.projects.project.host-catalogs.host-catalog"
@model={{hostCatalog}}
>
{{hostCatalog.displayName}}
</LinkTo>
<p>{{hostCatalog.description}}</p>
</row.cell>
<row.cell>{{hostCatalog.type}}</row.cell>
</body.row>
{{/each}}
</table.body>
</Rose::Table>
{{else}}
<Rose::Layout::Centered>
<Rose::Message @title={{t "titles.host-catalogs-welcome"}} as |message|>
<message.description>
{{t "descriptions.host-catalogs-welcome"}}
</message.description>
<message.link @route="scopes.scope.projects.project.host-catalogs.new">
<Rose::Icon @name="plus-circle-outline" />
{{t "titles.new"}}
</message.link>
</Rose::Message>
</Rose::Layout::Centered>
{{/if}}
</page.body>
</Rose::Layout::Page>
@@ -0,0 +1,21 @@
{{title (t 'titles.new')}}
<Rose::Layout::Page as |page|>
<page.breadcrumbs>
<BreadCrumbs />
</page.breadcrumbs>
<page.header>
<h2>{{t "titles.new"}}</h2>
</page.header>
<page.body>
<Form::HostCatalog
@model={{@model}}
@submit={{route-action "save" @model}}
@cancel={{route-action "cancel" @model}}
/>
</page.body>
</Rose::Layout::Page>
+6 -15
View File
@@ -3,24 +3,12 @@ export default function(server) {
// Scope resources
const globalScope = server.create('scope', { id: 'global' }); // creates a global scope
const orgScope = server.create('scope', {
const orgScope = server.createList('scope', 3, {
type: 'org',
scope: { id: globalScope.id, type: globalScope.type }
}, 'withChildren');
}, 'withChildren')[0];
const scope = { id: orgScope.id, type: orgScope.type };
// Create additional scopes for authentication organization selection
server.create('scope', {
type: 'org',
scope: { id: globalScope.id, type: globalScope.type }
}, 'withChildren');
server.create('scope', {
type: 'org',
scope: { id: globalScope.id, type: globalScope.type }
}, 'withChildren');
// Auth
// Auth methods exist both at the global scope and the org scope.
@@ -38,6 +26,9 @@ export default function(server) {
// Other resources
server.createList('host-catalog', 6);
server.db.scopes.where({type: 'project'}).forEach(projectScope => {
const scope = { id: projectScope.id, type: projectScope.type };
server.createList('host-catalog', 2, { scope });
});
}
+84 -47
View File
@@ -1,52 +1,93 @@
import { module, test } from 'qunit';
import { visit, currentURL, click, fillIn, find } from '@ember/test-helpers';
import { visit, currentURL, fillIn, click, find } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import a11yAudit from 'ember-a11y-testing/test-support/audit';
import { Response } from 'miragejs';
import {
authenticateSession,
// These are left here intentionally for future reference.
//currentSession,
//invalidateSession,
} from 'ember-simple-auth/test-support';
module('Acceptance | host catalogs', function(hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
const instances = {
scopes: {
global: null,
org: null,
project: null
},
hostCatalog: null
};
const urls = {
orgScope: null,
projects: null,
project: null,
hostCatalogs: null,
newHostCatalog: null,
hostCatalog: null
};
hooks.beforeEach(function () {
this.server.create('org');
// Setup Mirage mock resources for this test
instances.scopes.global = this.server.create('scope', { id: 'global' });
instances.scopes.org = this.server.create('scope', { type: 'org',
scope: { id: instances.scopes.global.id, type: instances.scopes.global.type }
});
instances.scopes.project = this.server.create('scope', { type: 'project',
scope: { id: instances.scopes.org.id, type: instances.scopes.org.type }
});
instances.hostCatalog = this.server.create('host-catalog', {
scope: {
id: instances.scopes.project.id,
type: instances.scopes.project.type
}
});
// Generate route URLs for resources
urls.orgScope = `/scopes/${instances.scopes.org.id}`;
urls.projects = `${urls.orgScope}/projects`;
urls.project = `${urls.projects}/${instances.hostCatalog.scope.id}`;
urls.hostCatalogs = `${urls.project}/host-catalogs`;
urls.newHostCatalog = `${urls.hostCatalogs}/new`;
urls.hostCatalog = `${urls.hostCatalogs}/${instances.hostCatalog.id}`;
});
test('visiting host catalogs', async function (assert) {
assert.expect(1);
this.server.createList('project', 1);
this.server.createList('host-catalog', 1);
await visit('/orgs/1/projects/1/host-catalogs');
authenticateSession();
await visit(urls.hostCatalogs);
await a11yAudit();
assert.equal(currentURL(), '/orgs/1/projects/1/host-catalogs');
assert.equal(currentURL(), urls.hostCatalogs);
});
test('can navigate to host catalog form', async function(assert) {
assert.expect(1);
this.server.createList('project', 1);
this.server.createList('host-catalog', 1);
await visit('/orgs/1/projects/1/host-catalogs');
await click('main .rose-table-cell:first-child a');
authenticateSession({});
await visit(urls.hostCatalogs);
await click('main tbody .rose-table-cell:nth-child(2) a');
await a11yAudit();
assert.equal(currentURL(), '/orgs/1/projects/1/host-catalogs/1');
assert.equal(currentURL(), urls.hostCatalog);
});
test('can delete host catalog', async function(assert) {
assert.expect(2);
this.server.createList('project', 1);
this.server.createList('host-catalog', 1);
assert.equal(this.server.db.hostCatalogs.length, 1);
await visit('/orgs/1/projects/1/host-catalogs/1');
const hostCatalogsCount = this.server.db.hostCatalogs.length;
authenticateSession({});
await visit(urls.hostCatalog);
assert.equal(currentURL(), urls.hostCatalog);
await click('.rose-button-warning');
assert.equal(this.server.db.hostCatalogs.length, 0);
assert.equal(this.server.db.hostCatalogs.length, hostCatalogsCount - 1);
});
test('can update host catalog and save changes', async function(assert) {
assert.expect(1);
this.server.createList('project', 1);
this.server.createList('host-catalog', 1);
await visit('/orgs/1/projects/1/host-catalogs/1');
authenticateSession({});
await visit(urls.hostCatalog);
await fillIn('[name="name"]', 'Test Name');
await click('form [type="submit"]:not(:disabled)');
assert.equal(this.server.db.hostCatalogs[0].name, 'Test Name');
@@ -54,39 +95,36 @@ module('Acceptance | host catalogs', function(hooks) {
test('can update host catalog and cancel changes', async function(assert) {
assert.expect(1);
this.server.createList('project', 1);
this.server.createList('host-catalog', 1);
await visit('/orgs/1/projects/1/host-catalogs/1');
authenticateSession({});
await visit(urls.hostCatalog);
await fillIn('[name="name"]', 'Test Name');
await click('form button:not([type="submit"])');
assert.notEqual(this.server.db.hostCatalogs[0].name, 'Test Name');
});
test('can create host catalog and save changes', async function(assert) {
assert.expect(2);
this.server.createList('project', 1);
await visit('/orgs/1/projects/1/host-catalogs/new');
assert.expect(1);
const hostCatalogsCount = this.server.db.hostCatalogs.length;
authenticateSession({});
await visit(urls.newHostCatalog);
await fillIn('[name="name"]', 'Test Name');
await click('form [type="submit"]:not(:disabled)');
assert.equal(this.server.db.hostCatalogs.length, 1);
assert.equal(currentURL(), '/orgs/1/projects/1/host-catalogs/1');
assert.equal(this.server.db.hostCatalogs.length, hostCatalogsCount + 1);
});
test('can create host catalog and cancel changes', async function(assert) {
assert.expect(2);
this.server.createList('project', 1);
await visit('/orgs/1/projects/1/host-catalogs/new');
assert.expect(1);
const hostCatalogsCount = this.server.db.hostCatalogs.length;
authenticateSession({});
await visit(urls.newHostCatalog);
await fillIn('[name="name"]', 'Test Name');
await click('form button:not([type="submit"])');
assert.equal(this.server.db.hostCatalogs.length, 0);
assert.equal(currentURL(), '/orgs/1/projects/1/host-catalogs');
assert.equal(this.server.db.hostCatalogs.length, hostCatalogsCount);
});
test('errors are displayed when save host catalog fails', async function (assert) {
assert.expect(1);
this.server.createList('project', 1);
this.server.createList('host-catalog', 1);
this.server.patch('/orgs/:org_id/projects/:project_id/host-catalogs/:id', () => {
this.server.patch('/scopes/:scope_id/host-catalogs/:id', () => {
return new Response(
490,
{},
@@ -97,7 +135,8 @@ module('Acceptance | host catalogs', function(hooks) {
}
);
});
await visit('/orgs/1/projects/1/host-catalogs/1');
authenticateSession({});
await visit(urls.hostCatalog);
await fillIn('[name="name"]', 'random string');
await click('form [type="submit"]');
await a11yAudit();
@@ -110,9 +149,7 @@ module('Acceptance | host catalogs', function(hooks) {
test('errors are displayed when delete host catalog fails', async function (assert) {
assert.expect(1);
this.server.createList('project', 1);
this.server.createList('host-catalog', 1);
this.server.del('/orgs/:org_id/projects/:project_id/host-catalogs/:id', () => {
this.server.del('/scopes/:scope_id/host-catalogs/:id', () => {
return new Response(
490,
{},
@@ -123,7 +160,8 @@ module('Acceptance | host catalogs', function(hooks) {
}
);
});
await visit('/orgs/1/projects/1/host-catalogs/1');
authenticateSession({});
await visit(urls.hostCatalog);
await click('.rose-button-warning');
await a11yAudit();
assert.ok(
@@ -135,7 +173,7 @@ module('Acceptance | host catalogs', function(hooks) {
test('saving a new host catalog with invalid fields displays error messages', async function (assert) {
assert.expect(2);
this.server.post('/orgs/:org_id/projects/:project_id/host-catalogs', () => {
this.server.post('/scopes/:scope_id/host-catalogs', () => {
return new Response(
400,
{},
@@ -154,8 +192,8 @@ module('Acceptance | host catalogs', function(hooks) {
}
);
});
this.server.createList('project', 1);
await visit('/orgs/1/projects/1/host-catalogs/new');
authenticateSession({});
await visit(urls.newHostCatalog);
await fillIn('[name="name"]', 'random string');
await click('form [type="submit"]');
await a11yAudit();
@@ -173,9 +211,7 @@ module('Acceptance | host catalogs', function(hooks) {
test('saving an existing host catalog with invalid fields displays error messages', async function (assert) {
assert.expect(2);
this.server.createList('project', 1);
this.server.createList('host-catalog', 1);
this.server.patch('/orgs/:org_id/projects/:project_id/host-catalogs/:id', () => {
this.server.patch('/scopes/:scope_id/host-catalogs/:id', () => {
return new Response(
400,
{},
@@ -194,7 +230,8 @@ module('Acceptance | host catalogs', function(hooks) {
}
);
});
await visit('/orgs/1/projects/1/host-catalogs/1');
authenticateSession({});
await visit(urls.hostCatalog);
await fillIn('[name="name"]', 'random string');
await click('form [type="submit"]');
await a11yAudit();
@@ -0,0 +1,12 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Controller | scopes/scope/projects/project/host-catalogs', function(hooks) {
setupTest(hooks);
// Replace this with your real tests.
test('it exists', function(assert) {
let controller = this.owner.lookup('controller:scopes/scope/projects/project/host-catalogs');
assert.ok(controller);
});
});
@@ -0,0 +1,12 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Controller | scopes/scope/projects/project/host-catalogs/host-catalog', function(hooks) {
setupTest(hooks);
// Replace this with your real tests.
test('it exists', function(assert) {
let controller = this.owner.lookup('controller:scopes/scope/projects/project/host-catalogs/host-catalog');
assert.ok(controller);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/projects/project/host-catalogs', function(hooks) {
setupTest(hooks);
test('it exists', function(assert) {
let route = this.owner.lookup('route:scopes/scope/projects/project/host-catalogs');
assert.ok(route);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/projects/project/host-catalogs/host-catalog', function(hooks) {
setupTest(hooks);
test('it exists', function(assert) {
let route = this.owner.lookup('route:scopes/scope/projects/project/host-catalogs/host-catalog');
assert.ok(route);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/projects/project/host-catalogs/host-catalog/index', function(hooks) {
setupTest(hooks);
test('it exists', function(assert) {
let route = this.owner.lookup('route:scopes/scope/projects/project/host-catalogs/host-catalog/index');
assert.ok(route);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/projects/project/host-catalogs/index', function(hooks) {
setupTest(hooks);
test('it exists', function(assert) {
let route = this.owner.lookup('route:scopes/scope/projects/project/host-catalogs/index');
assert.ok(route);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/projects/project/host-catalogs/new', function(hooks) {
setupTest(hooks);
test('it exists', function(assert) {
let route = this.owner.lookup('route:scopes/scope/projects/project/host-catalogs/new');
assert.ok(route);
});
});
+2
View File
@@ -33,6 +33,7 @@ resources:
titles:
iam: IAM
new: New
resources: Resources
details: Details
general: General
overview: Overview
@@ -58,6 +59,7 @@ states:
error: Error
form:
label:
id: ID
name: Name
description: Description
type: Type