Skip to content

Commit 58e263e

Browse files
committed
feat: enhance cost module integration by adding costModuleSchema and updating related components
1 parent 235345c commit 58e263e

File tree

4 files changed

+50
-48
lines changed

4 files changed

+50
-48
lines changed

src/Pages/GlobalConfigurations/ClustersAndEnvironments/ClusterForm/ClusterForm.tsx

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ import {
2424
ButtonVariantType,
2525
ClusterCostModuleConfigPayload,
2626
ClusterDetailListType,
27+
ClusterProviderType,
2728
DEFAULT_SECRET_PLACEHOLDER,
2829
Icon,
2930
ModalSidebarPanel,
3031
ModuleNameMap,
3132
ModuleStatus,
3233
noop,
3334
RemoteConnectionType,
35+
SCHEMA_07_VALIDATOR,
3436
SelectPickerOptionType,
3537
showError,
3638
ToastManager,
@@ -83,6 +85,7 @@ const ClusterForm = ({
8385
category,
8486
clusterProvider,
8587
costModuleConfig,
88+
costModuleSchema,
8689
}: ClusterFormProps) => {
8790
const location = useLocation()
8891

@@ -93,12 +96,17 @@ const ClusterForm = ({
9396
)
9497

9598
const [costModuleState, setCostModuleState] = useState<
96-
Pick<ClusterDetailListType['costModuleConfig'], 'enabled'> & { config: string }
99+
Pick<ClusterDetailListType['costModuleConfig'], 'enabled'> & {
100+
config: ClusterDetailListType['costModuleConfig']['config'] & { provider: ClusterProviderType }
101+
}
97102
>({
98103
enabled: costModuleConfig?.enabled || false,
99-
config: costModuleConfig?.config ? JSON.stringify(costModuleConfig.config) : '',
104+
config: {
105+
...costModuleConfig?.config,
106+
// Adding provider here to make the form work, it won't be sent to backend
107+
provider: clusterProvider,
108+
},
100109
})
101-
const [costModuleConfigErrorState, setCostModuleErrorState] = useState<string>('')
102110

103111
const [isAppMetricsEnabled, setIsAppMetricsEnabled] = useState(!!prometheusUrl)
104112
const [prometheusAuthenticationType, setPrometheusAuthenticationType] = useState({
@@ -181,28 +189,20 @@ const ClusterForm = ({
181189
setIsConnectedViaSSHTunnelTemp(false)
182190
}
183191

184-
const validateCostModuleConfig = (requiredConfig: string = costModuleState.config): string => {
185-
try {
186-
if (requiredConfig) {
187-
JSON.parse(requiredConfig)
188-
}
189-
190-
return ''
191-
} catch (e) {
192-
return e.message || 'Invalid JSON'
192+
const validateCostModuleConfig = (): boolean => {
193+
if (!costModuleSchema || !costModuleState.enabled) {
194+
return true
193195
}
194-
}
195196

196-
const getParsedConfigValue = (): ClusterCostModuleConfigPayload['config'] => {
197-
if (costModuleState.config) {
198-
try {
199-
const parsedConfig = JSON.parse(costModuleState.config)
200-
return parsedConfig
201-
} catch {
202-
return {}
203-
}
197+
try {
198+
const validationResult = SCHEMA_07_VALIDATOR.validateFormData(
199+
{ ...costModuleState.config },
200+
costModuleSchema,
201+
)
202+
return !validationResult?.errors?.length
203+
} catch {
204+
return true
204205
}
205-
return null
206206
}
207207

208208
const getCostModulePayload = (): ClusterCostModuleConfigPayload | null => {
@@ -213,9 +213,12 @@ const ClusterForm = ({
213213
}
214214

215215
if (costModuleState.config) {
216+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
217+
const { provider, ...sanitizedConfig } = costModuleState.config
218+
216219
return {
217220
enabled: true,
218-
config: getParsedConfigValue(),
221+
config: sanitizedConfig,
219222
}
220223
}
221224

@@ -250,18 +253,14 @@ const ClusterForm = ({
250253
},
251254
server_url: '',
252255
...(getCategoryPayload ? getCategoryPayload(selectedCategory) : null),
253-
...(clusterProvider ? { costModuleConfig: getCostModulePayload() } : null),
256+
...(costModuleSchema ? { costModuleConfig: getCostModulePayload() } : null),
254257
})
255258

256259
const additionalValidations = (): boolean => {
257260
let hasError = false
258261

259-
if (costModuleState.enabled) {
260-
const costConfigError = validateCostModuleConfig()
261-
if (costConfigError) {
262-
setCostModuleErrorState(costConfigError)
263-
hasError = true
264-
}
262+
if (!validateCostModuleConfig()) {
263+
hasError = true
265264
}
266265

267266
return hasError
@@ -512,14 +511,11 @@ const ClusterForm = ({
512511
}))
513512
}
514513

515-
const handleCostConfigChange = (newConfig: string) => {
514+
const handleCostConfigChange = (newConfig: typeof costModuleState.config) => {
516515
setCostModuleState((prev) => ({
517516
...prev,
518517
config: newConfig,
519518
}))
520-
521-
const error = validateCostModuleConfig(newConfig)
522-
setCostModuleErrorState(error)
523519
}
524520

525521
const renderFooter = () => (
@@ -612,10 +608,9 @@ const ClusterForm = ({
612608
costModuleEnabled={costModuleState.enabled}
613609
toggleCostModule={toggleCostModule}
614610
installationStatus={costModuleConfig.installationStatus}
615-
clusterProvider={clusterProvider}
616611
handleCostConfigChange={handleCostConfigChange}
617-
config={costModuleState.config || ''}
618-
configError={costModuleConfigErrorState}
612+
config={costModuleState.config}
613+
costModuleSchema={costModuleSchema || {}}
619614
/>
620615
</div>
621616
) : null

src/Pages/GlobalConfigurations/ClustersAndEnvironments/EditClusterDrawerContent.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ const EditClusterDrawerContent = ({
5555
enabled: !!clusterId,
5656
})
5757

58-
const { prometheusAuthResult, clusterProvider } = metadata || {
58+
const { prometheusAuthResult, clusterProvider, costModuleSchema } = metadata || {
5959
prometheusAuthResult: null,
60-
cloudProvider: null,
60+
clusterProvider: null,
61+
costModuleSchema: null,
6162
}
6263

6364
return (
@@ -89,6 +90,7 @@ const EditClusterDrawerContent = ({
8990
installationId={installationId}
9091
category={category}
9192
clusterProvider={clusterProvider}
93+
costModuleSchema={costModuleSchema}
9294
costModuleConfig={costModuleConfig}
9395
/>
9496
</APIResponseHandler>

src/Pages/GlobalConfigurations/ClustersAndEnvironments/cluster.service.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ClusterProviderType, get, post, put, trash } from '@devtron-labs/devtron-fe-common-lib'
17+
import { ClusterProviderDetailsType, get, post, put, trash } from '@devtron-labs/devtron-fe-common-lib'
1818

1919
import { importComponentFromFELibrary } from '@Components/common'
2020
import { Routes } from '@Config/constants'
2121

2222
import { DeleteClusterPayload, EditClusterDrawerMetadataType, Environment, EnvironmentDTO } from './cluster.type'
2323

24-
const getCloudProviderForCluster: (clusterId: number) => Promise<ClusterProviderType> = importComponentFromFELibrary(
25-
'getCloudProviderForCluster',
26-
null,
27-
'function',
28-
)
24+
const getCloudProviderForCluster: (clusterId: number) => Promise<ClusterProviderDetailsType> =
25+
importComponentFromFELibrary('getCloudProviderForCluster', null, 'function')
2926

3027
export const getEnvironmentList = async (): Promise<Environment[]> => {
3128
const { result } = await get<EnvironmentDTO[]>(Routes.ENVIRONMENT)
@@ -96,12 +93,17 @@ export function deleteEnvironment(request): Promise<any> {
9693

9794
export const getEditClusterDrawerMetadata = async (clusterId: number): Promise<EditClusterDrawerMetadataType> => {
9895
if (!clusterId) {
99-
return { prometheusAuthResult: null, clusterProvider: null }
96+
return { prometheusAuthResult: null, clusterProvider: null, costModuleSchema: null }
10097
}
10198

102-
const [prometheusAuthResult, clusterProvider] = await Promise.all([
99+
const [prometheusAuthResult, clusterProviderDetails] = await Promise.all([
103100
getCluster(+clusterId),
104101
getCloudProviderForCluster ? getCloudProviderForCluster(+clusterId) : null,
105102
])
106-
return { prometheusAuthResult, clusterProvider }
103+
104+
return {
105+
prometheusAuthResult,
106+
clusterProvider: clusterProviderDetails.clusterProvider,
107+
costModuleSchema: clusterProviderDetails.costModuleSchema,
108+
}
107109
}

src/Pages/GlobalConfigurations/ClustersAndEnvironments/cluster.type.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
FiltersTypeEnum,
2626
OptionType,
2727
ResponseType,
28+
RJSFFormSchema,
2829
SelectPickerOptionType,
2930
TableProps,
3031
UseUrlFiltersReturnType,
@@ -213,6 +214,7 @@ export type EditClusterFormProps = {
213214
isConnectedViaSSHTunnel: boolean
214215
isTlsConnection: boolean
215216
clusterProvider: ClusterProviderType
217+
costModuleSchema: RJSFFormSchema
216218
} & Pick<EditClusterDrawerContentProps, 'costModuleConfig'>
217219

218220
export type ClusterFormProps = { reload: () => void; handleModalClose: () => void } & Pick<
@@ -375,4 +377,5 @@ export interface EditDeleteClusterProps {
375377
export interface EditClusterDrawerMetadataType {
376378
prometheusAuthResult: ResponseType
377379
clusterProvider: ClusterProviderType
380+
costModuleSchema: RJSFFormSchema
378381
}

0 commit comments

Comments
 (0)