Skip to content

Commit 5303805

Browse files
committed
make editing work
1 parent 2c3607a commit 5303805

File tree

2 files changed

+73
-42
lines changed

2 files changed

+73
-42
lines changed

app/pages/project/networking/VpcPage/modals/firewall-rules.tsx

Lines changed: 71 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import {
1717
TextFieldError,
1818
TextFieldHint,
1919
} from '@oxide/ui'
20-
import type { ErrorResponse, VpcFirewallRule } from '@oxide/api'
20+
import type {
21+
ErrorResponse,
22+
VpcFirewallRule,
23+
VpcFirewallRuleUpdate,
24+
} from '@oxide/api'
2125
import {
2226
parsePortRange,
2327
useApiMutation,
@@ -365,6 +369,21 @@ const CommonForm = ({ id, error }: FormProps) => {
365369
)
366370
}
367371

372+
const valuesToRuleUpdate = (values: Values): VpcFirewallRuleUpdate => ({
373+
name: values.name,
374+
status: values.enabled ? 'enabled' : 'disabled',
375+
action: values.action,
376+
description: values.description,
377+
direction: values.direction,
378+
filters: {
379+
hosts: values.hosts,
380+
ports: values.ports,
381+
protocols: values.protocols,
382+
},
383+
priority: parseInt(values.priority, 10),
384+
targets: values.targets,
385+
})
386+
368387
type CreateProps = {
369388
isOpen: boolean
370389
onDismiss: () => void
@@ -386,11 +405,11 @@ export function CreateFirewallRuleModal({
386405
const queryClient = useApiQueryClient()
387406

388407
function dismiss() {
389-
createRule.reset()
408+
updateRules.reset()
390409
onDismiss()
391410
}
392411

393-
const createRule = useApiMutation('vpcFirewallRulesPut', {
412+
const updateRules = useApiMutation('vpcFirewallRulesPut', {
394413
onSuccess() {
395414
queryClient.invalidateQueries('vpcFirewallRulesGet', parentIds)
396415
dismiss()
@@ -444,36 +463,19 @@ export function CreateFirewallRuleModal({
444463
.required('Required'),
445464
})}
446465
validateOnBlur
447-
onSubmit={({ name, ...values }) => {
448-
const rules = existingRules
449-
.filter((r) => r.name !== name)
466+
onSubmit={(values) => {
467+
const otherRules = existingRules
468+
.filter((r) => r.name !== values.name)
450469
.map(firewallRuleGetToPut)
451-
452-
createRule.mutate({
470+
updateRules.mutate({
453471
...parentIds,
454472
body: {
455-
rules: [
456-
...rules,
457-
{
458-
name,
459-
status: values.enabled ? 'enabled' : 'disabled',
460-
action: values.action,
461-
description: values.description,
462-
direction: values.direction,
463-
filters: {
464-
hosts: values.hosts,
465-
ports: values.ports,
466-
protocols: values.protocols,
467-
},
468-
priority: parseInt(values.priority, 10),
469-
targets: values.targets,
470-
},
471-
],
473+
rules: [...otherRules, valuesToRuleUpdate(values)],
472474
},
473475
})
474476
}}
475477
>
476-
<CommonForm id={formId} error={createRule.error} />
478+
<CommonForm id={formId} error={updateRules.error} />
477479
</Formik>
478480
<SideModal.Footer>
479481
<Button variant="dim" className="mr-2.5" onClick={dismiss}>
@@ -493,6 +495,7 @@ type EditProps = {
493495
projectName: string
494496
vpcName: string
495497
originalRule: VpcFirewallRule | null
498+
existingRules: VpcFirewallRule[]
496499
}
497500

498501
// TODO: this whole thing. shouldn't take much to fill in the initialValues
@@ -503,16 +506,17 @@ export function EditFirewallRuleModal({
503506
projectName,
504507
vpcName,
505508
originalRule,
509+
existingRules,
506510
}: EditProps) {
507511
const parentIds = { orgName, projectName, vpcName }
508512
const queryClient = useApiQueryClient()
509513

510514
function dismiss() {
511-
updateRule.reset()
515+
updateRules.reset()
512516
onDismiss()
513517
}
514518

515-
const updateRule = useApiMutation('vpcFirewallRulesPut', {
519+
const updateRules = useApiMutation('vpcFirewallRulesPut', {
516520
onSuccess() {
517521
queryClient.invalidateQueries('vpcFirewallRulesGet', parentIds)
518522
dismiss()
@@ -529,21 +533,47 @@ export function EditFirewallRuleModal({
529533
onDismiss={dismiss}
530534
>
531535
<Formik
532-
initialValues={{
533-
name: originalRule.name,
534-
description: originalRule.description,
535-
}}
536-
onSubmit={(_values) => {
537-
// updateRule.mutate({
538-
// ...parentIds,
539-
// body: {
540-
// name,
541-
// description,
542-
// },
543-
// })
536+
initialValues={
537+
{
538+
enabled: originalRule.status === 'enabled',
539+
name: originalRule.name,
540+
description: originalRule.description,
541+
542+
priority: originalRule.priority.toString(),
543+
action: originalRule.action,
544+
direction: originalRule.direction,
545+
546+
protocols: originalRule.filters.protocols || [],
547+
548+
// port subform
549+
ports: originalRule.filters.ports || [],
550+
portRange: '',
551+
552+
// host subform
553+
hosts: originalRule.filters.hosts || [],
554+
hostType: '',
555+
hostValue: '',
556+
557+
// target subform
558+
targets: originalRule.targets,
559+
targetType: '',
560+
targetValue: '',
561+
} as Values // best way to tell formik this type
562+
}
563+
onSubmit={(values) => {
564+
// note different filter logic from create: filter by the *original* name
565+
const otherRules = existingRules
566+
.filter((r) => r.name !== originalRule.name)
567+
.map(firewallRuleGetToPut)
568+
updateRules.mutate({
569+
...parentIds,
570+
body: {
571+
rules: [...otherRules, valuesToRuleUpdate(values)],
572+
},
573+
})
544574
}}
545575
>
546-
<CommonForm id={formId} error={updateRule.error} />
576+
<CommonForm id={formId} error={updateRules.error} />
547577
</Formik>
548578
<SideModal.Footer>
549579
<Button variant="dim" className="mr-2.5" onClick={dismiss}>

app/pages/project/networking/VpcPage/tabs/VpcFirewallRulesTab.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ export const VpcFirewallRulesTab = () => {
102102
/>
103103
<EditFirewallRuleModal
104104
{...vpcParams}
105-
originalRule={editing} // modal is open if this is non-null
106105
onDismiss={() => setEditing(null)}
106+
existingRules={rules}
107+
originalRule={editing} // modal is open if this is non-null
107108
/>
108109
</div>
109110
<Table table={table} />

0 commit comments

Comments
 (0)