Icu 12933 updates to target model to support aliasing (#2214)

* feat: 🎸 target aliases association API, form creation
This commit is contained in:
Dharini Jeeva authored and GitHub committed 2024-04-05 16:35:23 -07:00
1 parent ac5110d94a
commit c8839d6db1
19 files changed
+197 -60

No files matched your search

@@ -108,4 +108,10 @@ export default class GeneratedTargetModel extends BaseModel {
isNestedAttribute: true,
})
enable_session_recording;
@attr('array', {
description:
'with_aliases specify the aliases that should be created when the target is created. ',
})
with_aliases;
}
+4 -4
View File
@@ -27,7 +27,7 @@ export default class RoleModel extends GeneratedRoleModel {
* see obvious. Instead, the application layer is expected to load referenced
* users and groups as needed.
*/
@attr('principal-array', {
@attr('array', {
readOnly: true,
emptyArrayIfMissing: true,
})
@@ -55,7 +55,7 @@ export default class RoleModel extends GeneratedRoleModel {
get userIDs() {
return this.principals
.filter(({ type }) => type === 'user')
.map(({ principal_id }) => principal_id);
.map(({ id }) => id);
}
/**
@@ -65,7 +65,7 @@ export default class RoleModel extends GeneratedRoleModel {
get groupIDs() {
return this.principals
.filter(({ type }) => type === 'group')
.map(({ principal_id }) => principal_id);
.map(({ id }) => id);
}
/**
@@ -75,7 +75,7 @@ export default class RoleModel extends GeneratedRoleModel {
get managedGroupIDs() {
return this.principals
.filter(({ type }) => type === 'managed group')
.map(({ principal_id }) => principal_id);
.map(({ id }) => id);
}
/**
+8
View File
@@ -44,6 +44,14 @@ export default class TargetSerializer extends ApplicationSerializer {
injectedApplicationCredentialSourceIDs,
);
if (serialized?.with_aliases) {
// API expects scope id along with every alias value
serialized.with_aliases = serialized.with_aliases.map((item) => ({
...item,
scope_id: 'global',
}));
}
// Delete session recording related fields from non-SSH targets
if (type !== TYPE_TARGET_SSH) {
delete serialized?.attributes?.storage_bucket_id;
+19
View File
@@ -0,0 +1,19 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Transform from '@ember-data/serializer/transform';
import { TrackedArray } from 'tracked-built-ins';
export default class ArrayTransform extends Transform {
deserialize(serialized) {
const arr = new TrackedArray(serialized || []);
return arr;
}
serialize(deserialized) {
const arr = deserialized || [];
return arr;
}
}
@@ -1,26 +0,0 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Transform from '@ember-data/serializer/transform';
export default class PrincipalArrayTransform extends Transform {
deserialize(serialized) {
const stringValues = serialized || [];
return stringValues.map(({ id: principal_id, scope_id, type }) => ({
principal_id,
scope_id,
type,
}));
}
serialize(deserialized) {
const strings = deserialized || [];
return strings.map(({ principal_id: id, scope_id, type }) => ({
id,
scope_id,
type,
}));
}
}
@@ -3,4 +3,4 @@
* SPDX-License-Identifier: MPL-2.0
*/
export { default } from 'api/transforms/principal-array';
export { default } from 'api/transforms/array';
@@ -36,6 +36,7 @@ module('Unit | Serializer | target', function (hooks) {
address: '0.0.0.0',
enable_session_recording: false,
storage_bucket_id: null,
with_aliases: [{ value: 'a' }],
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {};
@@ -51,6 +52,7 @@ module('Unit | Serializer | target', function (hooks) {
worker_filter: null,
egress_worker_filter: null,
ingress_worker_filter: null,
with_aliases: [{ value: 'a', scope_id: 'global' }],
attributes: {
default_port: 1234,
default_client_port: 4321,
@@ -85,6 +87,7 @@ module('Unit | Serializer | target', function (hooks) {
enable_session_recording: false,
storage_bucket_id: null,
address: '0.0.0.0',
with_aliases: [{ value: 'www.test.com' }],
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {};
@@ -100,6 +103,7 @@ module('Unit | Serializer | target', function (hooks) {
worker_filter: null,
egress_worker_filter: null,
ingress_worker_filter: null,
with_aliases: [{ value: 'www.test.com', scope_id: 'global' }],
attributes: {
default_port: 1234,
default_client_port: 4321,
@@ -189,6 +193,7 @@ module('Unit | Serializer | target', function (hooks) {
enable_session_recording: true,
storage_bucket_id: 'bucketID',
default_port: 1234,
with_aliases: [],
scope: {
scope_id: 'org_1',
},
@@ -206,6 +211,7 @@ module('Unit | Serializer | target', function (hooks) {
worker_filter: null,
egress_worker_filter: null,
ingress_worker_filter: null,
with_aliases: [],
attributes: {
default_port: 1234,
default_client_port: null,
@@ -226,6 +232,7 @@ module('Unit | Serializer | target', function (hooks) {
type: TYPE_TARGET_SSH,
worker_filter: 'worker',
default_port: 1234,
with_aliases: [],
scope: {
scope_id: 'org_1',
},
@@ -243,6 +250,7 @@ module('Unit | Serializer | target', function (hooks) {
worker_filter: 'worker',
egress_worker_filter: null,
ingress_worker_filter: null,
with_aliases: [],
attributes: {
default_port: 1234,
default_client_port: null,
@@ -263,6 +271,7 @@ module('Unit | Serializer | target', function (hooks) {
type: TYPE_TARGET_SSH,
egress_worker_filter: 'egress worker',
default_port: 1234,
with_aliases: [],
scope: {
scope_id: 'org_1',
},
@@ -280,6 +289,7 @@ module('Unit | Serializer | target', function (hooks) {
worker_filter: null,
egress_worker_filter: 'egress worker',
ingress_worker_filter: null,
with_aliases: [],
attributes: {
default_port: 1234,
default_client_port: null,
@@ -300,6 +310,7 @@ module('Unit | Serializer | target', function (hooks) {
type: TYPE_TARGET_SSH,
ingress_worker_filter: 'ingress worker',
default_port: 1234,
with_aliases: [],
scope: {
scope_id: 'org_1',
},
@@ -317,6 +328,7 @@ module('Unit | Serializer | target', function (hooks) {
worker_filter: null,
egress_worker_filter: null,
ingress_worker_filter: 'ingress worker',
with_aliases: [],
attributes: {
default_port: 1234,
default_client_port: null,
@@ -0,0 +1,45 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Transform | array', function (hooks) {
setupTest(hooks);
test('it serializes a principals array', function (assert) {
const transform = this.owner.lookup('transform:array');
const principal = [
{ id: 2, type: 'user' },
{ id: 3, type: 'group' },
{ id: 3, type: 'managed-group' },
];
const expectedArr = [
{ id: 2, type: 'user' },
{ id: 3, type: 'group' },
{ id: 3, type: 'managed-group' },
];
const emptyArr = [];
assert.deepEqual(transform.serialize(principal), expectedArr);
assert.deepEqual(transform.serialize(emptyArr), []);
});
test('it deserializes a principals array', function (assert) {
const transform = this.owner.lookup('transform:array');
const principal = [
{ id: 2, type: 'user' },
{ id: 3, type: 'group' },
{ id: 3, type: 'managed-group' },
];
const expectedArr = [
{ id: 2, type: 'user' },
{ id: 3, type: 'group' },
{ id: 3, type: 'managed-group' },
];
const emptyArr = [];
assert.deepEqual(transform.deserialize(principal), expectedArr);
assert.deepEqual(transform.deserialize(emptyArr), []);
});
});
@@ -1,17 +0,0 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Transform | principal array', function (hooks) {
setupTest(hooks);
// Replace this with your real tests.
test('it exists', function (assert) {
let transform = this.owner.lookup('transform:principal-array');
assert.ok(transform);
});
});