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
18 changes: 11 additions & 7 deletions apps/ocp-plugin/src/utils/apiCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getErrorMsgFromApiResponse,
} from '@flightctl/ui-components/src/utils/apiCalls';
import { ORGANIZATION_STORAGE_KEY } from '@flightctl/ui-components/src/utils/organizationStorage';
import { API_VERSION } from '@flightctl/ui-components/src/constants';
import { getApiVersion } from '@flightctl/ui-components/src/constants';

declare global {
interface Window {
Expand All @@ -16,12 +16,16 @@ declare global {
}
}

const addRequiredHeaders = (options: RequestInit, apiVersion?: string): RequestInit => {
type Api = 'flightctl' | 'imagebuilder' | 'alerts';

const addRequiredHeaders = (options: RequestInit, api?: Api): RequestInit => {
const token = getCSRFToken();
const orgId = localStorage.getItem(ORGANIZATION_STORAGE_KEY);

const headers = new Headers(options.headers || {});
headers.set('X-CSRFToken', token);

const apiVersion = api ? getApiVersion(api) : undefined;
if (apiVersion) {
headers.set('Flightctl-API-Version', apiVersion);
}
Expand Down Expand Up @@ -51,7 +55,7 @@ export const fetchUiProxy = async (endpoint: string, requestInit: RequestInit):
return await fetch(`${uiProxy}/api/${endpoint}`, options);
};

const getFullApiUrl = (path: string) => {
const getFullApiUrl = (path: string): { api: Api; url: string } => {
if (path.startsWith('alerts')) {
return { api: 'alerts', url: `${alertsAPI}/api/v2/${path}` };
}
Expand Down Expand Up @@ -109,7 +113,7 @@ const putOrPostData = async <TRequest, TResponse = TRequest>(
};

const { api, url } = getFullApiUrl(kind);
const options = addRequiredHeaders(baseOptions, api === 'flightctl' ? API_VERSION : undefined);
const options = addRequiredHeaders(baseOptions, api);
try {
const response = await fetch(url, options);
return handleApiJSONResponse(response);
Expand All @@ -132,7 +136,7 @@ export const deleteData = async <R>(kind: string, abortSignal?: AbortSignal): Pr
};

const { api, url } = getFullApiUrl(kind);
const options = addRequiredHeaders(baseOptions, api === 'flightctl' ? API_VERSION : undefined);
const options = addRequiredHeaders(baseOptions, api);
try {
const response = await fetch(url, options);
return handleApiJSONResponse(response);
Expand All @@ -153,7 +157,7 @@ export const patchData = async <R>(kind: string, data: PatchRequest, abortSignal
};

const { api, url } = getFullApiUrl(kind);
const options = addRequiredHeaders(baseOptions, api === 'flightctl' ? API_VERSION : undefined);
const options = addRequiredHeaders(baseOptions, api);
try {
const response = await fetch(url, options);
return handleApiJSONResponse(response);
Expand All @@ -171,7 +175,7 @@ export const fetchData = async <R>(path: string, abortSignal?: AbortSignal): Pro
signal: abortSignal,
};

const options = addRequiredHeaders(baseOptions, api === 'flightctl' ? API_VERSION : undefined);
const options = addRequiredHeaders(baseOptions, api);

const response = await fetch(url, options);
if (api === 'alerts') {
Expand Down
12 changes: 5 additions & 7 deletions apps/standalone/src/app/utils/apiCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
getErrorMsgFromAlertsApiResponse,
getErrorMsgFromApiResponse,
} from '@flightctl/ui-components/src/utils/apiCalls';
import { getApiVersion } from '@flightctl/ui-components/src/constants';
import { ORGANIZATION_STORAGE_KEY } from '@flightctl/ui-components/src/utils/organizationStorage';
import { API_VERSION } from '@flightctl/ui-components/src/constants';

import { lastRefresh } from '../context/AuthContext';

Expand Down Expand Up @@ -45,7 +45,7 @@ export const fetchUiProxy = async (endpoint: string, requestInit: RequestInit):
return await fetch(`${uiProxyAPI}/${endpoint}`, options);
};

const getFullApiUrl = (path: string) => {
const getFullApiUrl = (path: string): { api: 'flightctl' | 'imagebuilder' | 'alerts'; url: string } => {
if (path.startsWith('alerts')) {
return { api: 'alerts', url: `${uiProxyAPI}/alerts/api/v2/${path}` };
}
Expand Down Expand Up @@ -106,13 +106,11 @@ const handleAlertsJSONResponse = async <R>(response: Response): Promise<R> => {
const fetchWithRetry = async <R>(path: string, init?: RequestInit): Promise<R> => {
const { api, url } = getFullApiUrl(path);

// Add organization header if available
const options = addOrganizationHeader({ ...init });

// Add version header only for FlightCtl API
if (api === 'flightctl') {
const apiVersion = getApiVersion(api);
if (apiVersion) {
const headers = new Headers(options.headers);
headers.set('Flightctl-API-Version', API_VERSION);
headers.set('Flightctl-API-Version', apiVersion);
options.headers = headers;
}

Expand Down
9 changes: 6 additions & 3 deletions libs/types/scripts/openapi-typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const { rimraf, copyDir, fixImagebuilderCoreReferences } = require('./openapi-ut
const CORE_API = 'core';
const IMAGEBUILDER_API = 'imagebuilder';

const getSwaggerUrl = (api) => `https://raw.githubusercontent.com/flightctl/flightctl/main/api/${api}/v1beta1`;
const getSwaggerUrl = (api) => {
const apiVersion = api === CORE_API ? 'v1beta1' : 'v1alpha1';
return `https://raw.githubusercontent.com/flightctl/flightctl/main/api/${api}/${apiVersion}/openapi.yaml`;
};

const processJsonAPI = (jsonString) => {
const json = YAML.load(jsonString);
Expand All @@ -29,12 +32,12 @@ const processJsonAPI = (jsonString) => {
async function generateTypes(mode) {
const config = {
[CORE_API]: {
swaggerUrl: `${getSwaggerUrl(CORE_API)}/openapi.yaml`,
swaggerUrl: getSwaggerUrl(CORE_API),
output: path.resolve(__dirname, '../tmp-types'),
finalDir: path.resolve(__dirname, '../models'),
},
[IMAGEBUILDER_API]: {
swaggerUrl: `${getSwaggerUrl(IMAGEBUILDER_API)}/openapi.yaml`,
swaggerUrl: getSwaggerUrl(IMAGEBUILDER_API),
output: path.resolve(__dirname, '../tmp-imagebuilder-types'),
finalDir: path.resolve(__dirname, '../imagebuilder/models'),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from './types';
import { validKubernetesDnsSubdomain } from '../../form/validations';
import { DynamicAuthProviderSpec, ProviderType } from '../../../types/extraTypes';
import { API_VERSION } from '../../../constants';
import { CORE_API_VERSION } from '../../../constants';

export const getAssignmentTypeLabel = (
type: AuthOrganizationAssignment['type'] | AuthRoleAssignment['type'] | undefined,
Expand Down Expand Up @@ -317,7 +317,7 @@ export const getAuthProvider = (values: AuthProviderFormValues): AuthProvider =>
}

return {
apiVersion: API_VERSION,
apiVersion: CORE_API_VERSION,
kind: 'AuthProvider',
metadata: {
name: values.name,
Expand Down
4 changes: 2 additions & 2 deletions libs/ui-components/src/components/Fleet/CreateFleet/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Fleet, PatchRequest } from '@flightctl/types';
import { TFunction } from 'i18next';
import * as Yup from 'yup';
import { API_VERSION } from '../../../constants';
import { CORE_API_VERSION } from '../../../constants';
import { toAPILabel } from '../../../utils/labels';
import {
systemdUnitListValidationSchema,
Expand Down Expand Up @@ -178,7 +178,7 @@ export const getFleetResource = (values: FleetFormValues): Fleet => {
},
};
const fleet: Fleet = {
apiVersion: API_VERSION,
apiVersion: CORE_API_VERSION,
kind: 'Fleet',
metadata: {
name: values.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ImageExport,
ResourceKind,
} from '@flightctl/types/imagebuilder';
import { API_VERSION } from '../../../constants';
import { IMAGEBUILDER_API_VERSION } from '../../../constants';
import { ImageBuildFormValues } from './types';
import { ImageBuildWithExports } from '../../../types/extraTypes';

Expand Down Expand Up @@ -162,7 +162,7 @@ export const getImageBuildResource = (values: ImageBuildFormValues): ImageBuild
}

return {
apiVersion: API_VERSION,
apiVersion: IMAGEBUILDER_API_VERSION,
kind: ResourceKind.IMAGE_BUILD,
metadata: {
name,
Expand All @@ -175,7 +175,7 @@ export const getImageExportResource = (imageBuildName: string, format: ExportFor
const exportName = generateExportName(imageBuildName, format);

return {
apiVersion: API_VERSION,
apiVersion: IMAGEBUILDER_API_VERSION,
kind: ResourceKind.IMAGE_EXPORT,
metadata: {
name: exportName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '@flightctl/types';

import { RepositoryFormValues, ResourceSyncFormValue } from './types';
import { API_VERSION } from '../../../constants';
import { CORE_API_VERSION } from '../../../constants';
import { getErrorMessage } from '../../../utils/error';
import { appendJSONPatch } from '../../../utils/patch';
import { MAX_TARGET_REVISION_LENGTH, maxLengthString, validKubernetesDnsSubdomain } from '../../form/validations';
Expand Down Expand Up @@ -779,7 +779,7 @@ export const getRepository = (values: Omit<RepositoryFormValues, 'useResourceSyn
}

return {
apiVersion: API_VERSION,
apiVersion: CORE_API_VERSION,
kind: 'Repository',
metadata: {
name: values.name,
Expand Down Expand Up @@ -877,7 +877,7 @@ export const getRepository = (values: Omit<RepositoryFormValues, 'useResourceSyn
}

return {
apiVersion: API_VERSION,
apiVersion: CORE_API_VERSION,
kind: 'Repository',
metadata: {
name: values.name,
Expand All @@ -888,7 +888,7 @@ export const getRepository = (values: Omit<RepositoryFormValues, 'useResourceSyn

export const getResourceSync = (repositoryId: string, values: ResourceSyncFormValue): ResourceSync => {
return {
apiVersion: API_VERSION,
apiVersion: CORE_API_VERSION,
kind: 'ResourceSync',
metadata: {
name: values.name,
Expand Down
25 changes: 19 additions & 6 deletions libs/ui-components/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
const APP_TITLE = 'Edge Manager';
const API_VERSION = 'v1beta1';
const PAGE_SIZE = 15;
const EVENT_PAGE_SIZE = 200; // It's 500 in OCP console
const CERTIFICATE_VALIDITY_IN_YEARS = 1;
export const APP_TITLE = 'Edge Manager';
export const CORE_API_VERSION = 'v1beta1';
export const IMAGEBUILDER_API_VERSION = 'v1alpha1';

export { APP_TITLE, API_VERSION, PAGE_SIZE, EVENT_PAGE_SIZE, CERTIFICATE_VALIDITY_IN_YEARS };
export const PAGE_SIZE = 15;
export const EVENT_PAGE_SIZE = 200; // It's 500 in OCP console

export const CERTIFICATE_VALIDITY_IN_YEARS = 1;

export const getApiVersion = (api: 'flightctl' | 'imagebuilder' | 'alerts'): string | undefined => {
switch (api) {
case 'flightctl':
return CORE_API_VERSION;
case 'imagebuilder':
return IMAGEBUILDER_API_VERSION;
case 'alerts':
default:
return undefined;
}
};
4 changes: 2 additions & 2 deletions libs/ui-components/src/utils/search.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fuzzy from 'fuzzysearch';
import { API_VERSION } from '../constants';
import { CORE_API_VERSION } from '../constants';

// Must be an even number for "getSearchResultsCount" to work
export const MAX_TOTAL_SEARCH_RESULTS = 10;
Expand All @@ -26,7 +26,7 @@ export const getSearchResultsCount = (labelCount: number, fleetCount: number) =>
};

export const getEmptyFleetSearch = () => ({
apiVersion: API_VERSION,
apiVersion: CORE_API_VERSION,
kind: 'Fleet',
metadata: {},
items: [],
Expand Down