Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
SEED_OPENOPS_AUTO_INSTANCES_SHUTDOWN_TABLE_NAME,
} from '../../openops-tables/template-tables/create-auto-instances-shutdown-table';
import { databaseConnection } from '../database-connection';
import { getDefaultProjectTablesDatabaseToken } from '../get-default-user-db-token';
import { getAdminTablesContext } from './get-admin-token-and-database';

const AUTO_INSTANCES_SHUTDOWN_TABLE_SEED = 'AUTOINSTANCESSHUTDOWN';
Expand Down Expand Up @@ -36,13 +35,13 @@ export const seedAutoInstancesShutdownTable = async (): Promise<void> => {
return;
}

const tablesContext = await getAdminTablesContext();
const table = await getTableByName(
SEED_OPENOPS_AUTO_INSTANCES_SHUTDOWN_TABLE_NAME,
await getDefaultProjectTablesDatabaseToken(),
tablesContext,
);

if (!table) {
const tablesContext = await getAdminTablesContext();
await createAutoInstancesShutdownTable(tablesContext);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TablesServerContext } from '@openops/common';
import { AppSystemProp, system } from '@openops/server-shared';
import { Project } from '@openops/shared';
import { authenticateAdminUserInOpenOpsTables } from '../../openops-tables/auth-admin-tables';
import { TablesContext } from '../../openops-tables/template-tables/types';
import { projectService } from '../../project/project-service';
import { userService } from '../../user/user-service';

Expand All @@ -25,8 +24,8 @@ const getAdminUserProject = async (): Promise<Project> => {
return project;
};

export const getAdminTablesContext = async (): Promise<TablesContext> => {
const { token } = await authenticateAdminUserInOpenOpsTables();
const { tablesDatabaseId } = await getAdminUserProject();
return { token, tablesDatabaseId };
export const getAdminTablesContext = async (): Promise<TablesServerContext> => {
const { tablesDatabaseId, tablesDatabaseToken } = await getAdminUserProject();

return { tablesDatabaseToken, tablesDatabaseId };
};
Comment on lines +27 to 31
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a future PR I will change this so that I don't use the admin account.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
SEED_TABLE_NAME,
} from '../../openops-tables/template-tables/create-aggregated-costs-table';
import { databaseConnection } from '../database-connection';
import { getDefaultProjectTablesDatabaseToken } from '../get-default-user-db-token';
import { getAdminTablesContext } from './get-admin-token-and-database';

const AGGREGATED_TABLE_SEED = 'AGGREGATEDCOSTS';
Expand Down Expand Up @@ -35,13 +34,10 @@ export const seedFocusDataAggregationTemplateTable =
return;
}

const table = await getTableByName(
SEED_TABLE_NAME,
await getDefaultProjectTablesDatabaseToken(),
);
const tablesContext = await getAdminTablesContext();
const table = await getTableByName(SEED_TABLE_NAME, tablesContext);

if (!table) {
const tablesContext = await getAdminTablesContext();
await createAggregatedCostsTable(tablesContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
SEED_OPENOPS_KNOWN_COST_TYPES_BY_APPLICATION_TABLE_NAME,
} from '../../openops-tables/template-tables/create-known-cost-types-by-application-table';
import { databaseConnection } from '../database-connection';
import { getDefaultProjectTablesDatabaseToken } from '../get-default-user-db-token';
import { getAdminTablesContext } from './get-admin-token-and-database';

const KNOWN_COST_TYPES_BY_APPLICATION = 'KNOWNCOSTTYPES';
Expand Down Expand Up @@ -36,13 +35,13 @@ export const seedKnownCostTypesByApplicationTable = async (): Promise<void> => {
return;
}

const tablesContext = await getAdminTablesContext();
const table = await getTableByName(
SEED_OPENOPS_KNOWN_COST_TYPES_BY_APPLICATION_TABLE_NAME,
await getDefaultProjectTablesDatabaseToken(),
tablesContext,
);

if (!table) {
const tablesContext = await getAdminTablesContext();
await createKnownCostTypesByApplicationTable(tablesContext);
}
Comment thread
MarceloRGonc marked this conversation as resolved.

Expand Down
8 changes: 2 additions & 6 deletions packages/server/api/src/app/helper/system-jobs/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TablesServerContext } from '@openops/common';
import { ProjectId } from '@openops/shared';
import { Dayjs } from 'dayjs';

Expand All @@ -23,11 +24,6 @@ type IssuesReminderSystemJobData = {
organizationId: string;
};

export type CreateTemplateTablesSystemJobData = {
tablesDatabaseId: number;
userId: string;
};

type SystemJobDataMap = {
[SystemJobName.HARD_DELETE_PROJECT]: HardDeleteProjectSystemJobData;
[SystemJobName.ISSUES_REMINDER]: IssuesReminderSystemJobData;
Expand All @@ -38,7 +34,7 @@ type SystemJobDataMap = {
[SystemJobName.TRIAL_TRACKER]: Record<string, never>;
[SystemJobName.TRIGGER_DATA_CLEANER]: Record<string, never>;
[SystemJobName.LOGS_CLEANUP_TRIGGER]: Record<string, never>;
[SystemJobName.CREATE_TEMPLATE_TABLES]: CreateTemplateTablesSystemJobData;
[SystemJobName.CREATE_TEMPLATE_TABLES]: TablesServerContext;
};

export type SystemJobData<T extends SystemJobName = SystemJobName> =
Expand Down
15 changes: 10 additions & 5 deletions packages/server/api/src/app/openops-tables/create-table.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { createAxiosHeaders, makeOpenOpsTablesPost } from '@openops/common';
import {
createAxiosHeaders,
makeOpenOpsTablesPost,
resolveTokenProvider,
TablesServerContext,
} from '@openops/common';

export type Table = {
id: number;
Expand All @@ -8,20 +13,20 @@ export type Table = {
};

export async function createTable(
databaseId: number,
context: TablesServerContext,
tableName: string,
tableColumns: string[][],
token: string,
): Promise<Table> {
const requestBody = {
name: tableName,
data: tableColumns,
first_row_header: true,
};

const tokenOrResolver = await resolveTokenProvider(context);
return makeOpenOpsTablesPost<Table>(
`api/database/tables/database/${databaseId}/`,
`api/database/tables/database/${context.tablesDatabaseId}/`,
requestBody,
createAxiosHeaders(token),
createAxiosHeaders(tokenOrResolver),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@ import {
getPrimaryKeyFieldFromFields,
makeOpenOpsTablesPatch,
makeOpenOpsTablesPost,
resolveTokenProvider,
TablesServerContext,
TokenOrResolver,
} from '@openops/common';
import { logger } from '@openops/server-shared';
import { openopsTables } from '../index';
import { TablesContext } from './types';

export const SEED_TABLE_NAME = 'Aggregated Costs';
const SEED_LOG_HEADER = `[Seeding ${SEED_TABLE_NAME} table]`;

export async function createAggregatedCostsTable({
token,
tablesDatabaseId,
}: TablesContext): Promise<{ tableId: number }> {
export async function createAggregatedCostsTable(
tablesContext: TablesServerContext,
): Promise<{ tableId: number }> {
logger.debug(`${SEED_LOG_HEADER} Start`);

const table = await openopsTables.createTable(
tablesDatabaseId,
tablesContext,
SEED_TABLE_NAME,
[['Group Key']],
token,
);

await addFields(token, table.id);
const tokenOrResolver = await resolveTokenProvider(tablesContext);
await addFields(tokenOrResolver, table.id);

logger.debug(`${SEED_LOG_HEADER} Done`);

Expand All @@ -34,8 +35,11 @@ export async function createAggregatedCostsTable({
};
}

export async function addFields(token: string, tableId: number) {
const fields = await getFields(tableId, token);
export async function addFields(
tokenOrResolver: TokenOrResolver,
tableId: number,
): Promise<void> {
const fields = await getFields(tableId, tokenOrResolver);
const primaryField = getPrimaryKeyFieldFromFields(fields);

logger.debug(
Expand All @@ -47,7 +51,7 @@ export async function addFields(token: string, tableId: number) {
name: 'Group Key',
type: 'text',
},
createAxiosHeaders(token),
createAxiosHeaders(tokenOrResolver),
);
logger.debug(
`${SEED_LOG_HEADER} After adding primary field ${primaryField.name} with id: ${primaryField.id}`,
Expand All @@ -66,12 +70,12 @@ export async function addFields(token: string, tableId: number) {
];

for (const field of fieldsToAdd) {
await addField(token, tableId, field);
await addField(tokenOrResolver, tableId, field);
}
}

async function addField(
token: string,
tokenOrResolver: TokenOrResolver,
tableId: number,
fieldBody: Record<string, unknown>,
): Promise<{ id: number }> {
Expand All @@ -82,7 +86,7 @@ async function addField(
const field = await makeOpenOpsTablesPost<{ id: number }>(
createFieldEndpoint,
fieldBody,
createAxiosHeaders(token),
createAxiosHeaders(tokenOrResolver),
);

logger.debug(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { TablesServerContext } from '@openops/common';
import { logger } from '@openops/server-shared';
import { createAggregatedCostsTable } from './create-aggregated-costs-table';
import { createAutoInstancesShutdownTable } from './create-auto-instances-shutdown-table';
import { createBaseTemplateTables } from './create-base-template-tables';
import { createKnownCostTypesByApplicationTable } from './create-known-cost-types-by-application-table';
import { createOpportunitiesTable } from './create-opportunities-table';
import { TablesContext } from './types';

async function additionalTemplateTables(
tablesContext: TablesContext,
tablesContext: TablesServerContext,
): Promise<void> {
await createOpportunitiesTable(tablesContext);
await createAggregatedCostsTable(tablesContext);
Expand All @@ -17,7 +17,7 @@ async function additionalTemplateTables(
}

export async function createAllTemplateTables(
tablesContext: TablesContext,
tablesContext: TablesServerContext,
): Promise<void> {
await createBaseTemplateTables(tablesContext);
await additionalTemplateTables(tablesContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,31 @@ import {
getPrimaryKeyFieldFromFields,
makeOpenOpsTablesPatch,
makeOpenOpsTablesPost,
resolveTokenProvider,
TablesServerContext,
TokenOrResolver,
} from '@openops/common';
import { logger } from '@openops/server-shared';
import { createTable } from '../create-table';
import { TablesContext } from './types';

export const SEED_OPENOPS_AUTO_INSTANCES_SHUTDOWN_TABLE_NAME =
'Auto instances shutdown';

export async function createAutoInstancesShutdownTable({
token,
tablesDatabaseId,
}: TablesContext): Promise<void> {
export async function createAutoInstancesShutdownTable(
tablesContext: TablesServerContext,
): Promise<void> {
logger.debug(
`[Seeding ${SEED_OPENOPS_AUTO_INSTANCES_SHUTDOWN_TABLE_NAME} table] Start`,
);

const table = await createTable(
tablesDatabaseId,
tablesContext,
SEED_OPENOPS_AUTO_INSTANCES_SHUTDOWN_TABLE_NAME,
[['Resource ID']],
token,
);

const fields = await getFields(table.id, token);
const tokenOrResolver = await resolveTokenProvider(tablesContext);
const fields = await getFields(table.id, tokenOrResolver);
const primaryField = getPrimaryKeyFieldFromFields(fields);

logger.debug(
Expand All @@ -39,13 +40,13 @@ export async function createAutoInstancesShutdownTable({
name: 'Resource ID',
type: 'text',
},
createAxiosHeaders(token),
createAxiosHeaders(tokenOrResolver),
);
logger.debug(
`[Seeding ${SEED_OPENOPS_AUTO_INSTANCES_SHUTDOWN_TABLE_NAME} table] After adding primary field ID with id: ${primaryField.id}`,
);

await addField(token, table.id, {
await addField(tokenOrResolver, table.id, {
name: 'Shutdown time',
type: 'date',
date_format: 'ISO',
Expand All @@ -55,7 +56,10 @@ export async function createAutoInstancesShutdownTable({
const fieldNames = ['Cloud provider', 'Workflow', 'Status'];

for (const fieldName of fieldNames) {
await addField(token, table.id, { name: fieldName, type: 'text' });
await addField(tokenOrResolver, table.id, {
name: fieldName,
type: 'text',
});
}

logger.debug(
Expand All @@ -64,7 +68,7 @@ export async function createAutoInstancesShutdownTable({
}

async function addField(
token: string,
tokenOrResolver: TokenOrResolver,
tableId: number,
fieldBody: Record<string, unknown>,
): Promise<{ id: number }> {
Expand All @@ -77,7 +81,7 @@ async function addField(
const field = await makeOpenOpsTablesPost<{ id: number }>(
createFieldEndpoint,
fieldBody,
createAxiosHeaders(token),
createAxiosHeaders(tokenOrResolver),
);

logger.debug(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import { TablesServerContext } from '@openops/common';
import { logger } from '@openops/server-shared';
import { createBusinessUnitsTable } from './create-business-units-table';
import { createIdleEbsVolumesToDeleteTable } from './create-idle-ebs-volumes-to-delete-table';
import { createResourceBuTagAssignmentTable } from './create-resource-bu-tag-assignment-table';
import { createTagOwnerMappingTable } from './create-tag-owner-mapping-table';
import { TablesContext } from './types';

export async function createBaseTemplateTables({
token,
tablesDatabaseId,
}: TablesContext): Promise<void> {
const buTable = await createBusinessUnitsTable(tablesDatabaseId, token);
await createTagOwnerMappingTable(tablesDatabaseId, token, buTable.tableId);
await createIdleEbsVolumesToDeleteTable(tablesDatabaseId, token);
await createResourceBuTagAssignmentTable(
tablesDatabaseId,
token,
buTable.tableId,
);
export async function createBaseTemplateTables(
tablesContext: TablesServerContext,
): Promise<void> {
const buTable = await createBusinessUnitsTable(tablesContext);
await createTagOwnerMappingTable(tablesContext, buTable.tableId);
await createIdleEbsVolumesToDeleteTable(tablesContext);
await createResourceBuTagAssignmentTable(tablesContext, buTable.tableId);

logger.info('[Seeding template tables] Done');
}
Loading
Loading