Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

EVG-19946: Support Docker on provider settings page #2017

Merged
merged 12 commits into from
Sep 18, 2023
1 change: 1 addition & 0 deletions src/gql/generated/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,7 @@ export type Permissions = {
__typename?: "Permissions";
canCreateDistro: Scalars["Boolean"]["output"];
canCreateProject: Scalars["Boolean"]["output"];
canEditAdminSettings: Scalars["Boolean"]["output"];
distroPermissions: DistroPermissions;
userId: Scalars["String"]["output"];
};
Expand Down
147 changes: 40 additions & 107 deletions src/pages/distroSettings/tabs/ProviderTab/transformerUtils.ts
Original file line number Diff line number Diff line change
@@ -1,119 +1,52 @@
import { BuildType } from "./types";

type FieldGetter = (providerSettings: Record<string, any>) => {
form: Record<string, any>;
gql: Record<string, any>;
};

const getUserData = ((providerSettings) => ({
form: {
import { BuildType, ProviderFormState } from "./types";

interface ProviderSettingsList {
user_data: string;
merge_user_data_parts: boolean;
security_group_ids: string[];
image_url: string;
build_type: string;
docker_registry_user: string;
docker_registry_pw: string;
}

export const formProviderSettings = (
providerSettings: Partial<ProviderSettingsList>
) => ({
staticProviderSettings: {
userData: providerSettings.user_data ?? "",
},
gql: {
user_data: providerSettings.userData,
},
})) satisfies FieldGetter;

const getMergeUserData = ((providerSettings) => ({
form: {
mergeUserData: providerSettings.merge_user_data_parts ?? false,
},
gql: {
merge_user_data_parts: providerSettings.mergeUserData,
},
})) satisfies FieldGetter;

const getSecurityGroups = ((providerSettings) => ({
form: {
securityGroups: providerSettings.security_group_ids ?? [],
},
gql: {
security_group_ids: providerSettings.securityGroups,
},
})) satisfies FieldGetter;

const getImageUrl = ((providerSettings) => ({
form: {
dockerProviderSettings: {
userData: providerSettings.user_data ?? "",
mergeUserData: providerSettings.merge_user_data_parts ?? false,
securityGroups: providerSettings.security_group_ids ?? [],
imageUrl: providerSettings.image_url ?? "",
},
gql: {
image_url: providerSettings.imageUrl,
},
})) satisfies FieldGetter;

const getBuildType = ((providerSettings) => ({
form: {
buildType: (providerSettings.build_type ?? "") as BuildType,
},
gql: {
build_type: providerSettings.buildType,
},
})) satisfies FieldGetter;

const getRegistryUsername = ((providerSettings) => ({
form: {
registryUsername: providerSettings.docker_registry_user ?? "",
registryPassword: providerSettings.docker_registry_pw ?? "",
},
gql: {
docker_registry_user: providerSettings.registryUsername,
},
})) satisfies FieldGetter;
});

const getRegistryPassword = ((providerSettings) => ({
form: {
registryPassword: providerSettings.docker_registry_pw ?? "",
type ProviderSettings = ProviderFormState["staticProviderSettings"] &
ProviderFormState["dockerProviderSettings"];

export const gqlProviderSettings = (
Copy link
Contributor

Choose a reason for hiding this comment

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

This structure makes a lot more sense 😅 gorgeous!

providerSettings: Partial<ProviderSettings>
) => ({
staticProviderSettings: {
user_data: providerSettings.userData,
merge_user_data_parts: providerSettings.mergeUserData,
security_group_ids: providerSettings.securityGroups,
},
gql: {
dockerProviderSettings: {
user_data: providerSettings.userData,
merge_user_data_parts: providerSettings.mergeUserData,
security_group_ids: providerSettings.securityGroups,
image_url: providerSettings.imageUrl,
build_type: providerSettings.buildType,
docker_registry_user: providerSettings.registryUsername,
docker_registry_pw: providerSettings.registryPassword,
},
})) satisfies FieldGetter;

export const staticProviderSettings = ((providerSettings = {}) => {
const userData = getUserData(providerSettings);
const mergeUserData = getMergeUserData(providerSettings);
const securityGroups = getSecurityGroups(providerSettings);

return {
form: {
...userData.form,
...mergeUserData.form,
...securityGroups.form,
},
gql: {
...userData.gql,
...mergeUserData.gql,
...securityGroups.gql,
},
};
}) satisfies FieldGetter;

export const dockerProviderSettings = ((providerSettings = {}) => {
const imageUrl = getImageUrl(providerSettings);
const buildType = getBuildType(providerSettings);
const registryUser = getRegistryUsername(providerSettings);
const registryPassword = getRegistryPassword(providerSettings);
const userData = getUserData(providerSettings);
const mergeUserData = getMergeUserData(providerSettings);
const securityGroups = getSecurityGroups(providerSettings);

return {
form: {
...imageUrl.form,
...buildType.form,
...registryUser.form,
...registryPassword.form,
...userData.form,
...mergeUserData.form,
...securityGroups.form,
},
gql: {
...imageUrl.gql,
...buildType.gql,
...registryUser.gql,
...registryPassword.gql,
...userData.gql,
...mergeUserData.gql,
...securityGroups.gql,
},
};
}) satisfies FieldGetter;
});
15 changes: 7 additions & 8 deletions src/pages/distroSettings/tabs/ProviderTab/transformers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { DistroSettingsTabRoutes } from "constants/routes";
import { Provider } from "gql/generated/types";
import { FormToGqlFunction, GqlToFormFunction } from "../types";
import {
staticProviderSettings,
dockerProviderSettings,
} from "./transformerUtils";
import { gqlProviderSettings, formProviderSettings } from "./transformerUtils";

type Tab = DistroSettingsTabRoutes.Provider;

Expand All @@ -18,10 +15,10 @@ export const gqlToForm = ((data) => {
providerName: provider,
},
staticProviderSettings: {
...staticProviderSettings(providerSettingsList[0]).form,
...formProviderSettings(providerSettingsList[0]).staticProviderSettings,
},
dockerProviderSettings: {
...dockerProviderSettings(providerSettingsList[0]).form,
...formProviderSettings(providerSettingsList[0]).dockerProviderSettings,
containerPoolId: containerPool,
poolMappingInfo: "",
},
Expand All @@ -40,7 +37,8 @@ export const formToGql = ((data, distro) => {
provider: Provider.Static,
providerSettingsList: [
{
...staticProviderSettings(data.staticProviderSettings).gql,
...gqlProviderSettings(data.staticProviderSettings)
.staticProviderSettings,
},
],
containerPool: "",
Expand All @@ -51,7 +49,8 @@ export const formToGql = ((data, distro) => {
provider: Provider.Docker,
providerSettingsList: [
{
...dockerProviderSettings(data.dockerProviderSettings).gql,
...gqlProviderSettings(data.dockerProviderSettings)
.dockerProviderSettings,
},
],
containerPool: data.dockerProviderSettings.containerPoolId,
Expand Down