Skip to content

Commit 89e1792

Browse files
committed
make editing work
1 parent 2c3607a commit 89e1792

File tree

2 files changed

+73
-44
lines changed

2 files changed

+73
-44
lines changed

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

Lines changed: 71 additions & 43 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,26 +495,26 @@ type EditProps = {
493495
projectName: string
494496
vpcName: string
495497
originalRule: VpcFirewallRule | null
498+
existingRules: VpcFirewallRule[]
496499
}
497500

498-
// TODO: this whole thing. shouldn't take much to fill in the initialValues
499-
// based on the rule being edited
500501
export function EditFirewallRuleModal({
501502
onDismiss,
502503
orgName,
503504
projectName,
504505
vpcName,
505506
originalRule,
507+
existingRules,
506508
}: EditProps) {
507509
const parentIds = { orgName, projectName, vpcName }
508510
const queryClient = useApiQueryClient()
509511

510512
function dismiss() {
511-
updateRule.reset()
513+
updateRules.reset()
512514
onDismiss()
513515
}
514516

515-
const updateRule = useApiMutation('vpcFirewallRulesPut', {
517+
const updateRules = useApiMutation('vpcFirewallRulesPut', {
516518
onSuccess() {
517519
queryClient.invalidateQueries('vpcFirewallRulesGet', parentIds)
518520
dismiss()
@@ -529,21 +531,47 @@ export function EditFirewallRuleModal({
529531
onDismiss={dismiss}
530532
>
531533
<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-
// })
534+
initialValues={
535+
{
536+
enabled: originalRule.status === 'enabled',
537+
name: originalRule.name,
538+
description: originalRule.description,
539+
540+
priority: originalRule.priority.toString(),
541+
action: originalRule.action,
542+
direction: originalRule.direction,
543+
544+
protocols: originalRule.filters.protocols || [],
545+
546+
// port subform
547+
ports: originalRule.filters.ports || [],
548+
portRange: '',
549+
550+
// host subform
551+
hosts: originalRule.filters.hosts || [],
552+
hostType: '',
553+
hostValue: '',
554+
555+
// target subform
556+
targets: originalRule.targets,
557+
targetType: '',
558+
targetValue: '',
559+
} as Values // best way to tell formik this type
560+
}
561+
onSubmit={(values) => {
562+
// note different filter logic from create: filter by the *original* name
563+
const otherRules = existingRules
564+
.filter((r) => r.name !== originalRule.name)
565+
.map(firewallRuleGetToPut)
566+
updateRules.mutate({
567+
...parentIds,
568+
body: {
569+
rules: [...otherRules, valuesToRuleUpdate(values)],
570+
},
571+
})
544572
}}
545573
>
546-
<CommonForm id={formId} error={updateRule.error} />
574+
<CommonForm id={formId} error={updateRules.error} />
547575
</Formik>
548576
<SideModal.Footer>
549577
<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)