Skip to content

ENG-2964 GetEnvironment RTKQuery #1343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
@@ -1,10 +1,11 @@
package handler
package v2

import (
"context"
"net/http"
"os"

"github.com/aqueducthq/aqueduct/cmd/server/handler"
"github.com/aqueducthq/aqueduct/lib"
)

Expand All @@ -16,7 +17,9 @@ type getServerEnvironmentResponse struct {
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:
//
Expand All @@ -25,7 +28,7 @@ type getServerEnvironmentResponse struct {
//
// Response: Aqueduct server's environment variables.
type GetServerEnvironmentHandler struct {
GetHandler
handler.GetHandler
}

func (*GetServerEnvironmentHandler) Name() string {
Expand Down
4 changes: 2 additions & 2 deletions src/golang/cmd/server/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
GetServerEnvironmentRoute = "/api/v2/environment"

// V1 routes
GetArtifactVersionsRoute = "/api/artifact/versions"
Expand Down Expand Up @@ -73,6 +74,5 @@ const (
GetWorkflowDagResultRoute = "/api/workflow/{workflowId}/result/{workflowDagResultId}"
GetWorkflowHistoryRoute = "/api/workflow/{workflowId}/history"

GetServerVersionRoute = "/api/version"
Copy link
Contributor

Choose a reason for hiding this comment

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

Is any SDK code still using this route? To be safe, we could keep these routes and map them to the same handler (similar to workflow trigger / delete / edit routes)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the version route does have a usage. I will keep this one in place.

GetServerEnvironmentRoute = "/api/environment"
GetServerVersionRoute = "/api/version"
)
4 changes: 2 additions & 2 deletions src/golang/cmd/server/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func (s *AqServer) Handlers() map[string]handler.Handler {
DAGResultRepo: s.DAGResultRepo,
OperatorRepo: s.OperatorRepo,
},
routes.GetServerEnvironmentRoute: &v2.GetServerEnvironmentHandler{},

// V1 Handlers
// (ENG-2715) Remove deprecated ones
Expand Down Expand Up @@ -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{},
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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';

import {
IntegrationDialogProps,
KubernetesConfig,
} from '../../../utils/integrations';
import { apiAddress } from '../../hooks/useAqueductConsts';
import { IntegrationTextInputField } from './IntegrationTextInputField';

const Placeholders: KubernetesConfig = {
Expand All @@ -18,9 +17,14 @@ const Placeholders: KubernetesConfig = {
use_same_cluster: 'false',
};

export const KubernetesDialog: React.FC<IntegrationDialogProps> = ({
interface KuberentesDialogProps extends IntegrationDialogProps {
inK8sCluster: boolean;
}

export const KubernetesDialog: React.FC<KuberentesDialogProps> = ({
editMode = false,
user,
inK8sCluster = false,
}) => {
const { register, setValue, getValues } = useFormContext();
const use_same_cluster = getValues('use_same_cluster');
Expand All @@ -31,27 +35,6 @@ export const KubernetesDialog: React.FC<IntegrationDialogProps> = ({
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 (
<Box sx={{ mt: 2 }}>
{inK8sCluster && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -42,6 +43,11 @@ export const OnDemandKubernetesDialog: React.FC<IntegrationDialogProps> = ({
loading,
onCloseDialog,
}) => {
const {
data: environment,
error,
isLoading,
} = useEnvironmentGetQuery({ apiKey: user.apiKey });
const { register, setValue } = useFormContext();

const [currentStep, setCurrentStep] = useState('INITIAL');
Expand Down Expand Up @@ -137,14 +143,19 @@ export const OnDemandKubernetesDialog: React.FC<IntegrationDialogProps> = ({
);
};

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<IntegrationDialogProps> = ({
const RegularK8sStepLayout: React.FC<RegularK8sStepLayoutProps> = ({
user,
editMode,
onCloseDialog,
loading,
disabled,
inK8sCluster = false,
}) => {
const methods = useFormContext();
const dispatch: AppDispatch = useDispatch();
Expand All @@ -170,6 +181,7 @@ export const OnDemandKubernetesDialog: React.FC<IntegrationDialogProps> = ({
onCloseDialog={onCloseDialog}
loading={loading}
disabled={disabled}
inK8sCluster={inK8sCluster}
/>
<DialogActionButtons
onCloseDialog={onCloseDialog}
Expand Down Expand Up @@ -376,6 +388,7 @@ export const OnDemandKubernetesDialog: React.FC<IntegrationDialogProps> = ({
loading={loading}
onCloseDialog={onCloseDialog}
editMode={editMode}
inK8sCluster={environment?.inK8sCluster}
/>
);
case 'ONDEMAND_K8S':
Expand Down
19 changes: 8 additions & 11 deletions src/ui/common/src/components/layouts/menuSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -123,22 +123,19 @@ const MenuSidebar: React.FC<{
const [versionNumber, setVersionNumber] = useState('');
const location = useLocation();

const { data } = useEnvironmentGetQuery({ apiKey: user.apiKey } as any, {
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to cast to any and you probably don't need the skip here, as the user is passed by caller

skip: !user?.apiKey,
});

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);
if (data) {
setVersionNumber(data.version);
Copy link
Contributor

Choose a reason for hiding this comment

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

will the user or other things set the version number? Otherwise we can just do const versionNumber = data?.version

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, I will remove this useEffect and state variable.
Vestiges here from the infinite loop that I was facing with the Kubernetes dialog :)

}

fetchVersionNumber();
}, [user.apiKey]);
}, [user.apiKey, data]);

const pathPrefix = getPathPrefix();
return (
Expand Down
12 changes: 12 additions & 0 deletions src/ui/common/src/handlers/AqueductApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import {
DagResultsGetRequest,
DagResultsGetResponse,
} from './v2/DagResultsGet';
import {
environmentGetQuery,
EnvironmentGetRequest,
} from './v2/EnvironmentGet';
import {
integrationOperatorsGetQuery,
IntegrationOperatorsGetRequest,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -246,6 +257,7 @@ export const {
useDagOperatorsGetQuery,
useDagResultGetQuery,
useDagResultsGetQuery,
useEnvironmentGetQuery,
useIntegrationOperatorsGetQuery,
useIntegrationWorkflowsGetQuery,
useIntegrationsWorkflowsGetQuery,
Expand Down
13 changes: 13 additions & 0 deletions src/ui/common/src/handlers/v2/EnvironmentGet.ts
Original file line number Diff line number Diff line change
@@ -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 },
});