Skip to content

Commit

Permalink
feat: sync adopter probe node version with gp
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Apr 23, 2024
1 parent 9a94ef3 commit b5abb6f
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 8 deletions.
54 changes: 46 additions & 8 deletions snapshots/collections-schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ fields:
options: null
readonly: false
required: false
sort: 19
sort: 20
special: null
translations: null
validation: null
Expand Down Expand Up @@ -638,7 +638,7 @@ fields:
label: Is Custom City
readonly: false
required: false
sort: 21
sort: 22
special:
- cast-boolean
translations: null
Expand Down Expand Up @@ -717,7 +717,7 @@ fields:
options: null
readonly: false
required: false
sort: 17
sort: 18
special: null
translations: null
validation: null
Expand Down Expand Up @@ -755,7 +755,7 @@ fields:
options: null
readonly: false
required: false
sort: 18
sort: 19
special: null
translations: null
validation: null
Expand Down Expand Up @@ -833,7 +833,7 @@ fields:
options: null
readonly: false
required: false
sort: 20
sort: 21
special: null
translations: null
validation: null
Expand Down Expand Up @@ -871,7 +871,7 @@ fields:
options: null
readonly: false
required: false
sort: 22
sort: 23
special: null
translations: null
validation: null
Expand Down Expand Up @@ -947,7 +947,7 @@ fields:
options: null
readonly: false
required: true
sort: 16
sort: 17
special: null
translations: null
validation: null
Expand Down Expand Up @@ -1145,7 +1145,7 @@ fields:
options: null
readonly: false
required: false
sort: 15
sort: 16
special: null
translations: null
validation: null
Expand All @@ -1167,6 +1167,44 @@ fields:
has_auto_increment: false
foreign_key_table: null
foreign_key_column: null
- collection: gp_adopted_probes
field: nodeVersion
type: string
meta:
collection: gp_adopted_probes
conditions: null
display: null
display_options: null
field: nodeVersion
group: null
hidden: false
interface: input
note: null
options: null
readonly: false
required: false
sort: 15
special: null
translations: null
validation: null
validation_message: null
width: full
schema:
name: nodeVersion
table: gp_adopted_probes
data_type: varchar
default_value: null
max_length: 255
numeric_precision: null
numeric_scale: null
is_nullable: true
is_unique: false
is_primary_key: false
is_generated: false
generation_expression: null
has_auto_increment: false
foreign_key_table: null
foreign_key_column: null
- collection: gp_credits
field: id
type: integer
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/endpoints/adoption-code/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Request = ExpressRequest & {
type SendCodeResponse = {
uuid: string;
version: string;
nodeVersion: string;
hardwareDevice: string | null;
status: string;
city: string;
Expand All @@ -32,6 +33,7 @@ type AdoptedProbe = {
code: string;
uuid: string | null;
version: string | null;
nodeVersion: string | null;
hardwareDevice: string | null;
status: string;
city: string | null;
Expand Down Expand Up @@ -90,6 +92,7 @@ export default defineEndpoint((router, { env, logger, services }) => {
code,
uuid: null,
version: null,
nodeVersion: null,
hardwareDevice: null,
status: 'offline',
city: null,
Expand All @@ -113,6 +116,7 @@ export default defineEndpoint((router, { env, logger, services }) => {
code,
uuid: response.data.uuid,
version: response.data.version,
nodeVersion: response.data.nodeVersion,
hardwareDevice: response.data.hardwareDevice || null,
status: response.data.status,
city: response.data.city,
Expand Down Expand Up @@ -174,6 +178,7 @@ export default defineEndpoint((router, { env, logger, services }) => {
ip: probe.ip,
uuid: probe.uuid,
version: probe.version,
nodeVersion: probe.nodeVersion,
hardwareDevice: probe.hardwareDevice,
status: probe.status,
city: probe.city,
Expand Down
9 changes: 9 additions & 0 deletions src/extensions/endpoints/adoption-code/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('adoption code endpoints', () => {
}).reply(200, {
uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a',
version: '0.26.0',
nodeVersion: '18.17.0',
hardwareDevice: null,
status: 'ready',
city: 'Paris',
Expand Down Expand Up @@ -159,6 +160,7 @@ describe('adoption code endpoints', () => {
}).reply(200, {
uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a',
version: '0.26.0',
nodeVersion: '18.17.0',
hardwareDevice: 'v1',
status: 'ready',
city: 'Paris',
Expand Down Expand Up @@ -197,6 +199,7 @@ describe('adoption code endpoints', () => {
ip: '1.1.1.1',
uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a',
version: '0.26.0',
nodeVersion: '18.17.0',
hardwareDevice: 'v1',
status: 'ready',
city: 'Paris',
Expand Down Expand Up @@ -249,6 +252,7 @@ describe('adoption code endpoints', () => {
ip: '1.1.1.1',
uuid: null,
version: null,
nodeVersion: null,
hardwareDevice: null,
status: 'offline',
city: null,
Expand All @@ -274,6 +278,7 @@ describe('adoption code endpoints', () => {
}).reply(200, {
uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a',
version: '0.26.0',
nodeVersion: '18.17.0',
hardwareDevice: null,
status: 'ready',
city: 'Paris',
Expand Down Expand Up @@ -312,6 +317,7 @@ describe('adoption code endpoints', () => {
ip: '1.1.1.1',
uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a',
version: '0.26.0',
nodeVersion: '18.17.0',
hardwareDevice: null,
status: 'ready',
city: 'Paris',
Expand All @@ -337,6 +343,7 @@ describe('adoption code endpoints', () => {
}).reply(200, {
uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a',
version: '0.26.0',
nodeVersion: '18.17.0',
hardwareDevice: null,
status: 'ready',
city: 'Paris',
Expand Down Expand Up @@ -379,6 +386,7 @@ describe('adoption code endpoints', () => {
}).reply(200, {
uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a',
version: '0.26.0',
nodeVersion: '18.17.0',
hardwareDevice: null,
status: 'ready',
city: 'Paris',
Expand Down Expand Up @@ -422,6 +430,7 @@ describe('adoption code endpoints', () => {
}).reply(200, {
uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a',
version: '0.26.0',
nodeVersion: '18.17.0',
hardwareDevice: null,
status: 'ready',
city: 'Paris',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const DIRECTUS_URL = process.env.DIRECTUS_URL;
const ADMIN_ACCESS_TOKEN = process.env.ADMIN_ACCESS_TOKEN;
const USER_ROLE_NAME = 'User';

const COLLECTION_NAME = 'gp_adopted_probes';
const FIELDS_TO_REMOVE = [];
const FIELDS_TO_ADD = [ 'nodeVersion' ];

async function getUserRoleId () {
const URL = `${DIRECTUS_URL}/roles?filter[name][_eq]=${USER_ROLE_NAME}&access_token=${ADMIN_ACCESS_TOKEN}`;
const response = await fetch(URL).then((response) => {
if (!response.ok) {
throw new Error(`Fetch request failed. Status: ${response.status}`);
}

return response.json();
});
return response.data[0].id;
}

async function getUserPermissions (roleId) {
const URL = `${DIRECTUS_URL}/permissions?filter[collection][_eq]=${COLLECTION_NAME}&filter[role][_eq]=${roleId}&access_token=${ADMIN_ACCESS_TOKEN}`;
const response = await fetch(URL).then((response) => {
if (!response.ok) {
throw new Error(`Fetch request failed. Status: ${response.status}`);
}

return response.json();
});
const permissions = response.data;
const readPermissions = permissions.find(({ action }) => action === 'read');

return { readPermissions };
}

async function patchReadPermissions (readPermissions) {
const URL = `${DIRECTUS_URL}/permissions/${readPermissions.id}?access_token=${ADMIN_ACCESS_TOKEN}`;
const filteredFields = readPermissions.fields.filter(field => !FIELDS_TO_REMOVE.includes(field));

const response = await fetch(URL, {
method: 'PATCH',
body: JSON.stringify({
...readPermissions,
fields: [
...filteredFields,
...FIELDS_TO_ADD,
],
}),
headers: {
'Content-Type': 'application/json',
},
}).then((response) => {
if (!response.ok) {
throw new Error(`Fetch request failed. Status: ${response.status}`);
}

return response.json();
});
return response.data;
}

export async function up () {
const roleId = await getUserRoleId();
const { readPermissions } = await getUserPermissions(roleId);
await patchReadPermissions(readPermissions);
console.log('User permissions patched read adopted probes "nodeVersion" field');
}

export async function down () {
console.log('There is no down operation for that migration.');
}

0 comments on commit b5abb6f

Please sign in to comment.