Skip to content

Commit

Permalink
edit dialogue initial values are properly set
Browse files Browse the repository at this point in the history
  • Loading branch information
basakil committed Feb 23, 2023
1 parent f85fbb1 commit 2617bfd
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,55 @@ import { useModal } from "../../../../hooks/useModal";
import { info } from "autoprefixer";

export default function StepAWSProviderConfig({ goToNamedStep, ...props }) {
const [regions, setRegions] = useState(["Yükleniyor..."]);
const [sshKeys, setSshKeys] = useState(["Yükleniyor..."]);

const UITexts = {
Loading : 'Yükleniyor...',
ErrCheckProvider : "Bir hata oluştu! Altyapı sağlayıcısının yapılandırmasını kontrol edin.",
}
Object.freeze(UITexts);

const [regions, setRegions] = useState([UITexts.Loading]);
const [sshKeys, setSshKeys] = useState([UITexts.Loading]);
const [k8sVersions, setK8sVersions] = useState(kubernetesVersions);

const snack = useSnackbar().enqueueSnackbar;
const { handleSubmit, control, setValue } = useForm();
const modal = useModal();
const wizard = useWizard();
const isEdit = wizard.data.clusterInfo ? true : false;
const clusterInfo = wizard.data.clusterInfo;
const isEdit = clusterInfo ? true : false;


const reloadForm = async (region) => {
try {
if (!(await checkAWSCli(goToNamedStep, modal))) return;
// console.log(`wizard.data = \n${JSON.stringify(wizard.data, null, 2)}`);
const info = await checkConfig(
goToNamedStep,
modal,
wizard.manClusterName,
region ?? regions[0]
);
if (!info) return;
_reloadNew(region);

// console.log(`credentials_region = ${info.credentials_region}`);
if (isEdit) {
const info = await checkConfig(
goToNamedStep,
modal,
wizard.manClusterName,
region ?? regions[0]
);

if (!region) {
setRegions(info.regions);
setValue("region", info.credentials_region ?? info.regions[0]);
}
setValue('clusterName', clusterInfo.cluster.metadata.name);
setValue('region', clusterInfo.infrastructure.spec.region);
setValue('sshKeyName', clusterInfo.infrastructure.spec.sshKeyName);

if (! kubernetesVersions.includes(clusterInfo.controlPlane.spec.version)) {
setK8sVersions([clusterInfo.controlPlane.spec.version, ...kubernetesVersions]);
}
setValue('kubVersion', clusterInfo.controlPlane.spec.version);

setSshKeys(info.sshKeys?.map((x) => x.KeyName));
setValue("sshKeyName", info.sshKeys[0]?.KeyName ?? "");
setValue('masterCount', clusterInfo.controlPlane.spec.replicas);
setValue('workerCount', clusterInfo.machineDeployments[0].spec.replicas);
} else {

}
} catch (err) {
logger.error(err.message);
snack(
"Bir hata oluştu! Altyapı sağlayıcısının yapılandırmasını kontrol edin.",
UITexts.ErrCheckProvider,
{
variant: "error",
autoHideDuration: 5000,
Expand All @@ -53,12 +69,34 @@ export default function StepAWSProviderConfig({ goToNamedStep, ...props }) {
}
};

const _reloadNew = async (region) => {
if (!(await checkAWSCli(goToNamedStep, modal))) return;
// console.log(`wizard.data = \n${JSON.stringify(wizard.data, null, 2)}`);
const info = await checkConfig(
goToNamedStep,
modal,
wizard.manClusterName,
region ?? regions[0]
);
if (!info) return;

// console.log(`credentials_region = ${info.credentials_region}`);

if (!region) {
setRegions(info.regions);
setValue("region", info.credentials_region ?? info.regions[0]);
}

setSshKeys(info.sshKeys?.map((x) => x.KeyName));
setValue("sshKeyName", info.sshKeys[0]?.KeyName ?? "");
}

return (
<StepWizardWrapper
onLoad={async () => {
if (
regions[0] === "Yükleniyor..." ||
sshKeys[0] === "Yükleniyor..."
regions[0] === UITexts.Loading ||
sshKeys[0] === UITexts.Loading
)
await reloadForm();
}}
Expand Down Expand Up @@ -100,7 +138,7 @@ export default function StepAWSProviderConfig({ goToNamedStep, ...props }) {
name="kubVersion"
control={control}
label="Kubernetes versiyonu"
items={kubernetesVersions}
items={k8sVersions}
rules={{
required: "Versiyon giriniz",
minLength: {
Expand Down Expand Up @@ -174,7 +212,7 @@ export default function StepAWSProviderConfig({ goToNamedStep, ...props }) {
rules={{
required: "SSH anahtarını seçiniz",
validate: (x) =>
x != "Yükleniyor..."
x != UITexts.Loading
? true
: "SSH anahtarını seçiniz",
}}
Expand Down Expand Up @@ -226,12 +264,12 @@ export default function StepAWSProviderConfig({ goToNamedStep, ...props }) {
rules={{
required: "Bölge giriniz",
onChange: async (e, val = e.target.value) => {
await setSshKeys(["Yükleniyor..."]);
setValue("sshKeyName", "Yükleniyor...");
await setSshKeys([UITexts.Loading]);
setValue("sshKeyName", UITexts.Loading);
await reloadForm(val);
},
validate: (x) =>
x != "Yükleniyor..." ? true : "Bölge giriniz",
x != UITexts.Loading ? true : "Bölge giriniz",
}}
/>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@ export default function StepFetchWorkloadCluster({ goToNamedStep, ...props }) {
};

const baseOnload = async () => {
let cluster, controlPlane, machineDeployments, machineDeployment0;
const clusterInfo = {
'cluster': null,
'controlPlane' : null,
'infrastructure': null,
'machineDeployments' : []
}
try {
// layout.disableBack();
console.log(`props = \n${JSON.stringify(props, null, 2)}`);
let clusterName = props.clusterName;
const clusterName = props.clusterName;
await wizard.updateData("clusterName", clusterName);

setInfo(
Expand All @@ -101,53 +106,53 @@ export default function StepFetchWorkloadCluster({ goToNamedStep, ...props }) {


// fetch cluster
cluster = await getKubeResource('cluster', clusterName); //TODO: namespace
clusterInfo.cluster = await getKubeResource('cluster', clusterName); //TODO: namespace

// fetch controlplane
controlPlane = await getKubeResource(
cluster.spec.controlPlaneRef.kind,
cluster.spec.controlPlaneRef.name,
cluster.spec.controlPlaneRef.namespace);
clusterInfo.controlPlane = await getKubeResource(
clusterInfo.cluster.spec.controlPlaneRef.kind,
clusterInfo.cluster.spec.controlPlaneRef.name,
clusterInfo.cluster.spec.controlPlaneRef.namespace);

setProgress(40);


// fetch machineDeployments:
// TODO: handle multiple machineDeployments
let machineDeployments;
try {
machineDeployments = await kubectl.getMachineDeployments(
config,
{"namespace": cluster?.metadata?.namespace, "cluster_name" : clusterName}
{"namespace": clusterInfo.cluster?.metadata?.namespace, "cluster_name" : clusterName}
);
} catch (err) {
throw {name: 'ClientInvocationError', message: `${UITexts.ClientInvocationError}: ${err.message}`};
}
console.log(`machineDeployments = \n${JSON.stringify(machineDeployments, null, 2)}`);
if (machineDeployments?.length <= 0)
{ throw new Error(`E-MDL ${UITexts.ClientInvocationError}`); }
{ throw new Error(`E-MachineDeploymentList ${UITexts.ClientInvocationError}`); }

setProgress(60);


machineDeployment0 = await getKubeResource(
clusterInfo.machineDeployments.push( await getKubeResource(
'MachineDeployment',
machineDeployments[0],
cluster.spec.controlPlaneRef.namespace
);
clusterInfo.cluster.spec.controlPlaneRef.namespace
) );

setProgress(80);

clusterInfo.infrastructure = await getKubeResource(
clusterInfo.cluster.spec.infrastructureRef.kind,
clusterInfo.cluster.spec.infrastructureRef.name,
clusterInfo.cluster.spec.infrastructureRef.namespace);

let clusterInfo = {
'cluster': cluster,
'controlPlane' : controlPlane,
'machineDeployments' : [machineDeployment0]
}

//done..
wizard.updateData('clusterInfo' , clusterInfo);
setInfo(UITexts.LoadedClusterData);

setProgress(100);
wizard.updateData('clusterInfo' , clusterInfo);

setTimeout(() => {
_goto("AWSProviderConfig");
Expand Down

0 comments on commit 2617bfd

Please sign in to comment.