feat: 🎸 Add new target field for storage bucket (#133)

✅ Closes: ICU-8077
This commit is contained in:
Zhihe Li authored and GitHub committed 2023-03-22 10:52:33 -04:00
1 parent 1e7268ff54
commit 31b0a57639
5 files changed
+214

No files matched your search

@@ -83,4 +83,9 @@ export default class GeneratedTargetModel extends BaseModel {
'An IP address or DNS name for the session to connect to. Cannot be used in conjunction with host sources.',
})
address;
@attr('string', {
description: 'The storage bucket associated with this target.',
})
storage_bucket_id;
}
+39
View File
@@ -323,6 +323,45 @@ export default class TargetModel extends GeneratedTargetModel {
options
);
}
/**
* @param {object} options
* @param {object} options.adapterOptions
* @returns {Promise}
*/
setStorageBucket(options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
method: 'set-storage-bucket',
};
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
/**
* @param {object} options
* @param {object} options.adapterOptions
* @returns {Promise}
*/
removeStorageBucket(options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
method: 'remove-storage-bucket',
};
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
/**
* True if the target type is tcp.
* @type {boolean}
+23
View File
@@ -41,6 +41,16 @@ export default class TargetSerializer extends ApplicationSerializer {
snapshot,
injectedApplicationCredentialSourceIDs
);
if (snapshot?.adapterOptions?.method === 'set-storage-bucket') {
serialized = this.serializeWithStorageBucket(snapshot);
}
if (snapshot?.adapterOptions?.method === 'remove-storage-bucket') {
return {
version: snapshot.attr('version'),
};
}
return serialized;
}
@@ -91,4 +101,17 @@ export default class TargetSerializer extends ApplicationSerializer {
injected_application_credential_source_ids,
};
}
/**
* Returns a payload containing only version and storage bucket id,
* rather than existing instances on the model.
* @param {Snapshot} snapshot
* @returns {object}
*/
serializeWithStorageBucket(snapshot) {
return {
version: snapshot.attr('version'),
storage_bucket_id: snapshot?.attr('storage_bucket_id'),
};
}
}
@@ -404,4 +404,71 @@ module('Unit | Model | target', function (hooks) {
assert.true(modelTCP.isTCP);
assert.false(modelTCP.isSSH);
});
test('it has a `setStorageBucket` method that targets a specific POST API endpoint and serialization', async function (assert) {
assert.expect(1);
this.server.post(
'/v1/targets/123abc:set-storage-bucket',
(schema, request) => {
const body = JSON.parse(request.requestBody);
assert.deepEqual(body, {
storage_bucket_id: 'bucketID',
version: 1,
});
return { id: '123abc' };
}
);
const store = this.owner.lookup('service:store');
store.push({
data: {
id: '123abc',
type: 'target',
attributes: {
name: 'Target',
description: 'Description',
storage_bucket_id: 'bucketID',
version: 1,
scope: {
scope_id: 'o_1',
type: 'scope',
},
},
},
});
const model = store.peekRecord('target', '123abc');
await model.setStorageBucket();
});
test('it has a `removeStorageBucket` method that targets a specific POST API endpoint and serialization', async function (assert) {
assert.expect(1);
this.server.post(
'/v1/targets/123abc:remove-storage-bucket',
(schema, request) => {
const body = JSON.parse(request.requestBody);
assert.deepEqual(body, {
version: 1,
});
return { id: '123abc' };
}
);
const store = this.owner.lookup('service:store');
store.push({
data: {
id: '123abc',
type: 'target',
attributes: {
name: 'Target',
description: 'Description',
storage_bucket_id: null,
version: 1,
scope: {
scope_id: 'o_1',
type: 'scope',
},
},
},
});
const model = store.peekRecord('target', '123abc');
await model.removeStorageBucket();
});
});
@@ -49,6 +49,7 @@ module('Unit | Serializer | target', function (hooks) {
worker_filter: null,
egress_worker_filter: null,
ingress_worker_filter: null,
storage_bucket_id: null,
attributes: {
default_port: 1234,
},
@@ -127,6 +128,49 @@ module('Unit | Serializer | target', function (hooks) {
});
});
test('it serializes only storage_bucket_id and version when an `adapterOptions.method` is passed for set storage bucket', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('target');
const record = store.createRecord('target', {
name: 'User',
description: 'Description',
storage_bucket_id: 'bucketID',
version: 1,
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {
method: 'set-storage-bucket',
};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
storage_bucket_id: 'bucketID',
version: 1,
});
});
test('it serializes only version when an `adapterOptions.method` is passed for remove storage bucket', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('target');
const record = store.createRecord('target', {
name: 'User',
description: 'Description',
storage_bucket_id: 'bucketID',
version: 1,
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {
method: 'remove-storage-bucket',
};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
version: 1,
});
});
test('it serializes the worker_filter attribute if present', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
@@ -155,6 +199,7 @@ module('Unit | Serializer | target', function (hooks) {
worker_filter: 'worker',
egress_worker_filter: null,
ingress_worker_filter: null,
storage_bucket_id: null,
attributes: {
default_port: 1234,
},
@@ -190,6 +235,7 @@ module('Unit | Serializer | target', function (hooks) {
worker_filter: null,
egress_worker_filter: 'egress worker',
ingress_worker_filter: null,
storage_bucket_id: null,
attributes: {
default_port: 1234,
},
@@ -225,6 +271,7 @@ module('Unit | Serializer | target', function (hooks) {
worker_filter: null,
egress_worker_filter: null,
ingress_worker_filter: 'ingress worker',
storage_bucket_id: null,
attributes: {
default_port: 1234,
},
@@ -438,4 +485,37 @@ module('Unit | Serializer | target', function (hooks) {
},
});
});
test('it normalizes the storage_bucket_id attribute if present', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('target');
const target = store.createRecord('target').constructor;
const payload = {
id: '1',
name: 'Target 1',
storage_bucket_id: 'bucketID',
};
const normalized = serializer.normalizeSingleResponse(
store,
target,
payload
);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'target',
attributes: {
authorized_actions: [],
name: 'Target 1',
host_sources: [],
brokered_credential_source_ids: [],
injected_application_credential_source_ids: [],
storage_bucket_id: 'bucketID',
},
relationships: {},
},
});
});
});