Files
threshold-ui/ui/admin/app/routes/onboarding/quick-setup/create-resources.js
T

107 lines
3.1 KiB
JavaScript
Raw Normal View History

2022-06-03 13:25:53 -04:00
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { notifyError } from 'core/decorators/notify';
2022-06-13 16:38:18 -04:00
export default class OnboardingQuickSetupCreateResourcesRoute extends Route {
2022-06-03 13:25:53 -04:00
// =services
@service store;
2022-06-03 13:25:53 -04:00
@service router;
@service intl;
// =methods
async model() {
return {
org: this.store.createRecord('scope', {
type: 'org',
scopeID: 'global',
name: 'SecOps',
2022-06-03 13:25:53 -04:00
description: 'Sample org created by quick setup',
}),
project: this.store.createRecord('scope', {
type: 'project',
name: 'AWS',
2022-06-03 13:25:53 -04:00
description: 'Sample project created by quick setup',
}),
hostCatalog: this.store.createRecord('host-catalog', {
2022-06-13 16:38:18 -04:00
type: 'static',
name: 'US-East-1',
2022-06-03 13:25:53 -04:00
description: 'Sample host catalog created by quick setup',
}),
hostSet: this.store.createRecord('host-set', {
name: 'Prod-EC2',
2022-06-03 13:25:53 -04:00
description: 'Sample host set created by quick setup',
}),
host: this.store.createRecord('host', {
name: 'Instance-1',
2022-06-03 13:25:53 -04:00
description: 'Sample host created by quick setup',
}),
target: this.store.createRecord('target', {
2022-06-13 16:38:18 -04:00
type: 'tcp',
name: 'EC2 Instances',
2022-06-03 13:25:53 -04:00
description: 'Sample target created by quick setup',
}),
2022-06-16 18:11:11 -05:00
role: this.store.createRecord('role', {
name: 'ec2_connect_role',
2022-06-16 18:11:11 -05:00
}),
2022-06-03 13:25:53 -04:00
};
}
// =actions
@action
@loading
2022-06-13 16:38:18 -04:00
@notifyError(function () {
return this.intl.t('errors.quick-setup-failed.description');
})
async createResources(hostAddress, targetPort) {
2022-06-03 13:25:53 -04:00
try {
await this.createOnboardingResourcesAndRedirect(hostAddress, targetPort);
} catch (e) {
this.router.replaceWith('scopes.scope', 'global');
2022-06-13 16:38:18 -04:00
throw new Error();
2022-06-03 13:25:53 -04:00
}
}
async createOnboardingResourcesAndRedirect(hostAddress, targetPort) {
2022-06-16 18:11:11 -05:00
const { org, project, hostCatalog, hostSet, host, target, role } =
2022-06-03 13:25:53 -04:00
this.currentModel;
// The Procedure
await org.save();
project.scopeID = org.id;
await project.save();
hostCatalog.scopeID = project.id;
await hostCatalog.save();
hostSet.host_catalog_id = hostCatalog.id;
await hostSet.save();
host.host_catalog_id = hostCatalog.id;
if (hostAddress) {
host.address = hostAddress;
await host.save();
await hostSet.addHosts([host.id]);
target.scopeID = project.id;
target.default_port = targetPort;
await target.save();
await target.addHostSources([hostSet.id]);
2022-06-16 18:11:11 -05:00
role.scopeID = org.id;
role.grant_scope_id = project.id;
await role.save();
await role.saveGrantStrings([
`type=target;actions=list`,
`id=${target.id};actions=authorize-session`,
]);
2022-06-03 13:25:53 -04:00
this.router.transitionTo(
2022-06-13 16:38:18 -04:00
'onboarding.quick-setup.create-resources.success'
2022-06-03 13:25:53 -04:00
);
} else {
this.router.transitionTo(
'scopes.scope.host-catalogs.host-catalog.hosts',
project.id,
2022-06-03 13:25:53 -04:00
hostCatalog.id
);
}
}
}