Skip to content

Commit

Permalink
feat: split cluster form in cluster and schema-registry groups
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-grgt committed Feb 5, 2025
1 parent 83aa7b7 commit 7ee331c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
25 changes: 17 additions & 8 deletions ui/pages/create_cluster_page/upsert_cluster_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
if m.formValues.HasSASLAuthMethodSelected() {
m.NextField(3)
}
m.form.NextGroup()
m.srSelectionState = srDisabledSelected
} else if m.formValues.SrEnabled &&
((m.srSelectionState == srNothingSelected) || m.srSelectionState == srDisabledSelected) {
Expand All @@ -153,6 +154,7 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
if m.formValues.HasSASLAuthMethodSelected() {
m.NextField(3)
}
m.form.NextGroup()
m.srSelectionState = srEnabledSelected
}

Expand Down Expand Up @@ -279,8 +281,10 @@ func (m *Model) createForm() *huh.Form {
huh.NewOption("Disabled", false),
huh.NewOption("Enabled", true),
)
var fields []huh.Field
fields = append(fields, name, color, host, auth)

var clusterFields []huh.Field
clusterFields = append(clusterFields, name, color, host, auth)

if m.formValues.HasSASLAuthMethodSelected() {
securityProtocol := huh.NewSelect[config.SecurityProtocol]().
Value(&m.formValues.SecurityProtocol).
Expand All @@ -296,10 +300,11 @@ func (m *Model) createForm() *huh.Form {
Value(&m.formValues.Password).
EchoMode(huh.EchoModePassword).
Title("Password")
fields = append(fields, securityProtocol, username, pwd)
clusterFields = append(clusterFields, securityProtocol, username, pwd)
}

fields = append(fields, srEnabled)
var schemaRegistryFields []huh.Field
schemaRegistryFields = append(schemaRegistryFields, srEnabled)
if m.formValues.SrEnabled {
srUrl := huh.NewInput().
Value(&m.formValues.SrUrl).
Expand All @@ -311,10 +316,14 @@ func (m *Model) createForm() *huh.Form {
Value(&m.formValues.SrPassword).
EchoMode(huh.EchoModePassword).
Title("Schema Registry Password")
fields = append(fields, srUrl, srUsername, srPwd)
schemaRegistryFields = append(schemaRegistryFields, srUrl, srUsername, srPwd)
}

form := huh.NewForm(huh.NewGroup(fields...))
form := huh.NewForm(
huh.NewGroup(clusterFields...).Title("Cluster").Description("de").WithWidth(m.ktx.WindowWidth/2),
huh.NewGroup(schemaRegistryFields...),
)
form.WithLayout(huh.LayoutColumns(2))
form.QuitAfterSubmit = false
form.Init()
return form
Expand All @@ -331,10 +340,10 @@ func NewForm(
connChecker: connChecker,
}

model.ktx = ktx
model.form = model.createForm()
model.mode = newMode
model.clusterRegisterer = registerer
model.ktx = ktx

model.authSelectionState = nothingSelected
if formValues.SrEnabled {
Expand Down Expand Up @@ -381,9 +390,9 @@ func NewEditForm(
preEditedName := formValues.Name
model.preEditName = &preEditedName
}
model.ktx = ktx
model.form = model.createForm()
model.clusterRegisterer = registerer
model.ktx = ktx
model.authSelectionState = nothingSelected
if formValues.SrEnabled {
model.srSelectionState = srEnabledSelected
Expand Down
37 changes: 22 additions & 15 deletions ui/pages/create_cluster_page/upsert_cluster_page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,28 @@ func TestCreateClusterPage(t *testing.T) {
}
keys.UpdateKeys(createEnvPage, "localhost:9091")
cmd = createEnvPage.Update(keys.Key(tea.KeyEnter))
// next field
createEnvPage.Update(cmd())
// and: auth method none is selected
cmd = createEnvPage.Update(keys.Key(tea.KeyEnter))
// next field
cmd = createEnvPage.Update(cmd())
// and: select disabled schema registry
cmd = createEnvPage.Update(keys.Key(tea.KeyEnter))
cmd = createEnvPage.Update(cmd())
cmd = createEnvPage.Update(cmd())
msg := cmd()
// next group
createEnvPage.Update(cmd())
// and: select disabled schema registry and in doing so submitting the form
msgs := keys.Submit(createEnvPage)

// then
assert.IsType(t, &config.Cluster{}, msg)
assert.Len(t, msgs, 1)
assert.IsType(t, &config.Cluster{}, msgs[0])
// and
assert.Equal(t, &config.Cluster{
Name: "prd",
Color: styles.ColorGreen,
Active: false,
BootstrapServers: []string{"localhost:9091"},
SchemaRegistry: nil,
}, msg)
}, msgs[0])
})

t.Run("name still has to be unique", func(t *testing.T) {
Expand Down Expand Up @@ -279,23 +281,24 @@ func TestCreateClusterPage(t *testing.T) {
createEnvPage.Update(cmd())
// and: auth method none is selected
cmd = createEnvPage.Update(keys.Key(tea.KeyEnter))
// next field
cmd = createEnvPage.Update(cmd())
// and: no schema-registry is selected
cmd = createEnvPage.Update(keys.Key(tea.KeyEnter))
cmd = createEnvPage.Update(cmd())
cmd = createEnvPage.Update(cmd())
msg := cmd()
// next group
createEnvPage.Update(cmd())
// and: select disabled schema registry and in doing so submitting the form
msgs := keys.Submit(createEnvPage)

// then
assert.IsType(t, &config.Cluster{}, msg)
assert.Len(t, msgs, 1)
assert.IsType(t, &config.Cluster{}, msgs[0])
// and
assert.Equal(t, &config.Cluster{
Name: "TST",
Color: styles.ColorRed,
Active: false,
BootstrapServers: []string{"localhost:9092"},
SchemaRegistry: nil,
}, msg)
}, msgs[0])
})

t.Run("Selecting SASL_SSL auth method displays username and password fields", func(t *testing.T) {
Expand Down Expand Up @@ -432,6 +435,8 @@ func TestCreateClusterPage(t *testing.T) {
keys.UpdateKeys(createEnvPage, "password")
cmd = createEnvPage.Update(keys.Key(tea.KeyEnter))
// next field
cmd = createEnvPage.Update(cmd())
// next group
createEnvPage.Update(cmd())
// submit
msgs := keys.Submit(createEnvPage)
Expand Down Expand Up @@ -484,7 +489,7 @@ func TestCreateClusterPage(t *testing.T) {
keys.UpdateKeys(createEnvPage, "localhost:9092")
cmd = createEnvPage.Update(keys.Key(tea.KeyEnter))
createEnvPage.Update(cmd())
// and: auth method none is selected
// and: auth method SASL is selected
cmd = createEnvPage.Update(keys.Key(tea.KeyDown))
cmd = createEnvPage.Update(keys.Key(tea.KeyEnter))
// next field
Expand All @@ -500,6 +505,8 @@ func TestCreateClusterPage(t *testing.T) {
keys.UpdateKeys(createEnvPage, "password")
cmd = createEnvPage.Update(keys.Key(tea.KeyEnter))
// next field
cmd = createEnvPage.Update(cmd())
// next group
createEnvPage.Update(cmd())
// select schema-registry enabled
createEnvPage.Update(keys.Key(tea.KeyDown))
Expand Down

0 comments on commit 7ee331c

Please sign in to comment.