diff --git a/src/golang/cmd/server/handler/get_server_environment.go b/src/golang/cmd/server/handler/v2/get_server_environment.go similarity index 52% rename from src/golang/cmd/server/handler/get_server_environment.go rename to src/golang/cmd/server/handler/v2/get_server_environment.go index ab6473506..703d92733 100644 --- a/src/golang/cmd/server/handler/get_server_environment.go +++ b/src/golang/cmd/server/handler/v2/get_server_environment.go @@ -1,22 +1,26 @@ -package handler +package v2 import ( "context" "net/http" "os" + "github.com/aqueducthq/aqueduct/cmd/server/handler" "github.com/aqueducthq/aqueduct/lib" ) const inK8sClusterEnvVarName = "AQUEDUCT_IN_K8S_CLUSTER" -type getServerEnvironmentResponse struct { +type getEnvironmentResponse struct { // Whether the server is running within a k8s cluster. InK8sCluster bool `json:"inK8sCluster"` Version string `json:"version"` } -// Route: /api/environment +// Route: /api/v2/environment +// This file should map directly to +// src/ui/common/src/handlers/v2/EnvironmentGet.tsx +// // Method: GET // Request: // @@ -24,25 +28,25 @@ type getServerEnvironmentResponse struct { // `api-key`: user's API Key // // Response: Aqueduct server's environment variables. -type GetServerEnvironmentHandler struct { - GetHandler +type EnvironmentHandler struct { + handler.GetHandler } -func (*GetServerEnvironmentHandler) Name() string { - return "GetServerEnvironment" +func (*EnvironmentHandler) Name() string { + return "GetEnvironment" } -func (*GetServerEnvironmentHandler) Prepare(r *http.Request) (interface{}, int, error) { +func (*EnvironmentHandler) Prepare(r *http.Request) (interface{}, int, error) { return nil, http.StatusOK, nil } -func (h *GetServerEnvironmentHandler) Perform(ctx context.Context, interfaceArgs interface{}) (interface{}, int, error) { +func (h *EnvironmentHandler) Perform(ctx context.Context, interfaceArgs interface{}) (interface{}, int, error) { inCluster := false if os.Getenv(inK8sClusterEnvVarName) == "1" { inCluster = true } - return getServerEnvironmentResponse{ + return getEnvironmentResponse{ InK8sCluster: inCluster, Version: lib.ServerVersionNumber, }, http.StatusOK, nil diff --git a/src/golang/cmd/server/routes/routes.go b/src/golang/cmd/server/routes/routes.go index e0e44d6b7..a28901b67 100644 --- a/src/golang/cmd/server/routes/routes.go +++ b/src/golang/cmd/server/routes/routes.go @@ -24,6 +24,7 @@ const ( NodeDagOperatorsRoute = "/api/v2/workflow/{workflowID}/dag/{dagID}/node/operators" NodeOperatorContentRoute = "/api/v2/workflow/{workflowID}/dag/{dagID}/node/operator/{nodeID}/content" NodesResultsRoute = "/api/v2/workflow/{workflowID}/result/{dagResultID}/nodes/results" + EnvironmentRoute = "/api/v2/environment" // V1 routes GetArtifactVersionsRoute = "/api/artifact/versions" @@ -73,6 +74,5 @@ const ( GetWorkflowDagResultRoute = "/api/workflow/{workflowId}/result/{workflowDagResultId}" GetWorkflowHistoryRoute = "/api/workflow/{workflowId}/history" - GetServerVersionRoute = "/api/version" - GetServerEnvironmentRoute = "/api/environment" + GetServerVersionRoute = "/api/version" ) diff --git a/src/golang/cmd/server/server/handlers.go b/src/golang/cmd/server/server/handlers.go index 748842fcd..228add7cd 100644 --- a/src/golang/cmd/server/server/handlers.go +++ b/src/golang/cmd/server/server/handlers.go @@ -132,6 +132,7 @@ func (s *AqServer) Handlers() map[string]handler.Handler { DAGResultRepo: s.DAGResultRepo, OperatorRepo: s.OperatorRepo, }, + routes.EnvironmentRoute: &v2.EnvironmentHandler{}, // V1 Handlers // (ENG-2715) Remove deprecated ones @@ -425,7 +426,6 @@ func (s *AqServer) Handlers() map[string]handler.Handler { IntegrationRepo: s.IntegrationRepo, }, - routes.GetServerVersionRoute: &handler.GetServerVersionHandler{}, - routes.GetServerEnvironmentRoute: &handler.GetServerEnvironmentHandler{}, + routes.GetServerVersionRoute: &handler.GetServerVersionHandler{}, } } diff --git a/src/ui/common/src/components/integrations/dialogs/kubernetesDialog.tsx b/src/ui/common/src/components/integrations/dialogs/kubernetesDialog.tsx index 0ee5d0036..e01029a77 100644 --- a/src/ui/common/src/components/integrations/dialogs/kubernetesDialog.tsx +++ b/src/ui/common/src/components/integrations/dialogs/kubernetesDialog.tsx @@ -1,7 +1,7 @@ import { Checkbox, FormControlLabel } from '@mui/material'; import Box from '@mui/material/Box'; import React from 'react'; -import { useEffect, useState } from 'react'; +import { useEffect } from 'react'; import { useFormContext } from 'react-hook-form'; import * as Yup from 'yup'; @@ -9,7 +9,6 @@ import { IntegrationDialogProps, KubernetesConfig, } from '../../../utils/integrations'; -import { apiAddress } from '../../hooks/useAqueductConsts'; import { IntegrationTextInputField } from './IntegrationTextInputField'; const Placeholders: KubernetesConfig = { @@ -18,9 +17,14 @@ const Placeholders: KubernetesConfig = { use_same_cluster: 'false', }; -export const KubernetesDialog: React.FC = ({ +interface KuberentesDialogProps extends IntegrationDialogProps { + inK8sCluster: boolean; +} + +export const KubernetesDialog: React.FC = ({ editMode = false, user, + inK8sCluster = false, }) => { const { register, setValue, getValues } = useFormContext(); const use_same_cluster = getValues('use_same_cluster'); @@ -31,27 +35,6 @@ export const KubernetesDialog: React.FC = ({ setValue('use_same_cluster', 'false'); }, []); - const [inK8sCluster, setInK8sCluster] = useState(false); - - // TODO: https://linear.app/aqueducthq/issue/ENG-2964/move-k8s-use-same-cluster-request-to-rtkquery - useEffect(() => { - const fetchEnvironment = async () => { - const environmentResponse = await fetch(`${apiAddress}/api/environment`, { - method: 'GET', - headers: { - 'api-key': user.apiKey, - }, - }); - - const responseBody = await environmentResponse.json(); - setInK8sCluster(responseBody['inK8sCluster']); - }; - - if (user) { - fetchEnvironment().catch(console.error); - } - }, [user]); - return ( {inK8sCluster && ( diff --git a/src/ui/common/src/components/integrations/dialogs/onDemandKubernetesDialog.tsx b/src/ui/common/src/components/integrations/dialogs/onDemandKubernetesDialog.tsx index fab29eee7..ed9ec666c 100644 --- a/src/ui/common/src/components/integrations/dialogs/onDemandKubernetesDialog.tsx +++ b/src/ui/common/src/components/integrations/dialogs/onDemandKubernetesDialog.tsx @@ -9,6 +9,7 @@ import { useFormContext } from 'react-hook-form'; import { useDispatch } from 'react-redux'; import * as Yup from 'yup'; +import { useEnvironmentGetQuery } from '../../../handlers/AqueductApi'; import { handleConnectToNewIntegration } from '../../../reducers/integration'; import { AppDispatch } from '../../../stores/store'; import { IntegrationDialogProps } from '../../../utils/integrations'; @@ -42,6 +43,11 @@ export const OnDemandKubernetesDialog: React.FC = ({ loading, onCloseDialog, }) => { + const { + data: environment, + error, + isLoading, + } = useEnvironmentGetQuery({ apiKey: user.apiKey }); const { register, setValue } = useFormContext(); const [currentStep, setCurrentStep] = useState('INITIAL'); @@ -137,14 +143,19 @@ export const OnDemandKubernetesDialog: React.FC = ({ ); }; + interface RegularK8sStepLayoutProps extends IntegrationDialogProps { + inK8sCluster?: boolean; + } + // We're going to need to share some more info with the dialogs, as they're not all just forms that we can // register anymore in the case of this layout. - const RegularK8sStepLayout: React.FC = ({ + const RegularK8sStepLayout: React.FC = ({ user, editMode, onCloseDialog, loading, disabled, + inK8sCluster = false, }) => { const methods = useFormContext(); const dispatch: AppDispatch = useDispatch(); @@ -170,6 +181,7 @@ export const OnDemandKubernetesDialog: React.FC = ({ onCloseDialog={onCloseDialog} loading={loading} disabled={disabled} + inK8sCluster={inK8sCluster} /> = ({ loading={loading} onCloseDialog={onCloseDialog} editMode={editMode} + inK8sCluster={environment?.inK8sCluster} /> ); case 'ONDEMAND_K8S': diff --git a/src/ui/common/src/components/layouts/menuSidebar.tsx b/src/ui/common/src/components/layouts/menuSidebar.tsx index d0d083d5d..c4ced5f2c 100644 --- a/src/ui/common/src/components/layouts/menuSidebar.tsx +++ b/src/ui/common/src/components/layouts/menuSidebar.tsx @@ -15,9 +15,9 @@ import { useDispatch } from 'react-redux'; import { Link as RouterLink, useLocation } from 'react-router-dom'; import UserProfile from 'src/utils/auth'; +import { useEnvironmentGetQuery } from '../../handlers/AqueductApi'; import { AppDispatch } from '../../stores/store'; import { getPathPrefix } from '../../utils/getPathPrefix'; -import { apiAddress } from '../hooks/useAqueductConsts'; import { menuSidebar, menuSidebarContent, @@ -120,26 +120,21 @@ const MenuSidebar: React.FC<{ }> = ({ onSidebarItemClicked, user }) => { const dispatch: AppDispatch = useDispatch(); const [currentPage, setCurrentPage] = useState(undefined); - const [versionNumber, setVersionNumber] = useState(''); const location = useLocation(); + const { data } = useEnvironmentGetQuery( + { apiKey: user.apiKey }, + { + skip: !user?.apiKey, + } + ); + + console.log('data: ', data); + useEffect(() => { setCurrentPage(location.pathname); }, [dispatch, location.pathname]); - useEffect(() => { - async function fetchVersionNumber() { - const res = await fetch(`${apiAddress}/api/version`, { - method: 'GET', - headers: { 'api-key': user.apiKey }, - }); - const versionNumberResponse = await res.json(); - setVersionNumber(versionNumberResponse.version); - } - - fetchVersionNumber(); - }, [user.apiKey]); - const pathPrefix = getPathPrefix(); return ( @@ -270,7 +265,7 @@ const MenuSidebar: React.FC<{ - {versionNumber.length > 0 ? `v${versionNumber}` : ''} + {data?.version ? `v${data.version}` : ''} diff --git a/src/ui/common/src/handlers/AqueductApi.ts b/src/ui/common/src/handlers/AqueductApi.ts index bc3c6972d..a9481bd8f 100644 --- a/src/ui/common/src/handlers/AqueductApi.ts +++ b/src/ui/common/src/handlers/AqueductApi.ts @@ -18,6 +18,10 @@ import { DagResultsGetRequest, DagResultsGetResponse, } from './v2/DagResultsGet'; +import { + environmentGetQuery, + EnvironmentGetRequest, +} from './v2/EnvironmentGet'; import { integrationOperatorsGetQuery, IntegrationOperatorsGetRequest, @@ -134,6 +138,13 @@ export const aqueductApi = createApi({ query: (req) => dagResultsGetQuery(req), transformErrorResponse, }), + environmentGet: builder.query< + EnvironmentGetResponse, + EnvironmentGetRequest + >({ + query: (req) => environmentGetQuery(req), + transformErrorResponse, + }), integrationOperatorsGet: builder.query< IntegrationOperatorsGetResponse, IntegrationOperatorsGetRequest @@ -246,6 +257,7 @@ export const { useDagOperatorsGetQuery, useDagResultGetQuery, useDagResultsGetQuery, + useEnvironmentGetQuery, useIntegrationOperatorsGetQuery, useIntegrationWorkflowsGetQuery, useIntegrationsWorkflowsGetQuery, diff --git a/src/ui/common/src/handlers/v2/EnvironmentGet.ts b/src/ui/common/src/handlers/v2/EnvironmentGet.ts new file mode 100644 index 000000000..c00591704 --- /dev/null +++ b/src/ui/common/src/handlers/v2/EnvironmentGet.ts @@ -0,0 +1,13 @@ +import { APIKeyParameter } from '../parameters/Header'; + +export type EnvironmentGetRequest = APIKeyParameter; + +export type EnvironmentGetResponse = { + inK8sCluster: boolean; + version: string; +}; + +export const environmentGetQuery = (req: EnvironmentGetRequest) => ({ + url: 'environment', + headers: { 'api-key': req.apiKey }, +});