Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .idea/dictionaries/project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions internal/resource/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ type NavigateFilteredMsg struct {
Value string
}

type NavigateFilteredMultiMsg struct {
Resource string
Field string
Values []string
}

type DetailsMsg struct {
ID string
Content any
Expand All @@ -21,3 +27,7 @@ type NavigateScopedMsg struct {
Resource string
Scope map[string]string
}

type ErrorMsg struct {
Err error
}
50 changes: 50 additions & 0 deletions internal/resources/compute/servers/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/akyriako/o7k/internal/resource"
tea "github.com/charmbracelet/bubbletea"
computeservers "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/ports"
)

func (r *Resource) image(row resource.Row) tea.Cmd {
Expand All @@ -33,6 +34,55 @@ func (r *Resource) flavor(row resource.Row) tea.Cmd {
}
}

// securityGroups violates the "no N+1 calls" rule, but this is a special case because we
// have to first load the ports and then find out the corresponding security group ids.
// this method should not be considered as the norm of jumping to resources
func (r *Resource) securityGroups(row resource.Row) tea.Cmd {
return func() tea.Msg {

client, err := r.context.NetworkV2()
if err != nil {
return resource.ErrorMsg{Err: err}
}

pages, err := ports.List(client, ports.ListOpts{
DeviceID: row.ID,
}).AllPages(context.Background())
if err != nil {
return resource.ErrorMsg{
Err: fmt.Errorf("listing ports for server %q: %w", row.ID, err),
}
}

items, err := ports.ExtractPorts(pages)
if err != nil {
return resource.ErrorMsg{
Err: fmt.Errorf("extracting ports for server %q: %w", row.ID, err),
}
}

uniqueSecurityGroups := make(map[string]struct{})
securityGroupIDs := make([]string, 0)

for _, port := range items {
for _, securityGroupID := range port.SecurityGroups {
if _, exists := uniqueSecurityGroups[securityGroupID]; exists {
continue
}

uniqueSecurityGroups[securityGroupID] = struct{}{}
securityGroupIDs = append(securityGroupIDs, securityGroupID)
}
}

return resource.NavigateFilteredMultiMsg{
Resource: "securitygroups",
Field: "id",
Values: securityGroupIDs,
}
}
}

func (r *Resource) show(id string) tea.Cmd {
return func() tea.Msg {
client, err := r.context.ComputeV2()
Expand Down
12 changes: 6 additions & 6 deletions internal/resources/compute/servers/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func (r *Resource) Columns() []resource.Column {
{Key: "name", Title: "NAME", MinWidth: 24, Flex: 1},
{Key: "status", Title: "STATUS", MinWidth: 12, Flex: 0},
{Key: "flavor", Title: "FLAVOR", MinWidth: 15, Flex: 1},
{Key: "image", Title: "IMAGE", MinWidth: 40, Flex: 0},
{Key: "addresses", Title: "ADDRESSES", MinWidth: 40, Flex: 2},
{Key: "addresses", Title: "ADDRESSES", MinWidth: 40, Flex: 1},
}
}

Expand All @@ -51,6 +50,7 @@ func (r *Resource) Commands() []resource.Command {
{Key: "s", Description: "Show", Default: true},
{Key: "shift-i", Description: "Image"},
{Key: "shift-f", Description: "Flavor"},
{Key: "shift-g", Description: "Security Groups"},
}
}

Expand Down Expand Up @@ -96,9 +96,7 @@ func (r *Resource) List(ctx context.Context) ([]resource.Row, error) {
flavorName = flavorID
}

//fmt.Printf("SERVER IMAGE: server=%q image=%#v\n", server.Name, server.Image)
imageID, _ := server.Image["id"].(string)

image := imageID
if name, ok := server.Image["name"].(string); ok && name != "" {
image = name
Expand All @@ -124,7 +122,7 @@ func (r *Resource) List(ctx context.Context) ([]resource.Row, error) {
func serverAddresses(addresses map[string]any) string {
values := make([]string, 0)

for network, raw := range addresses {
for _, raw := range addresses {
items, ok := raw.([]any)
if !ok {
continue
Expand All @@ -141,7 +139,7 @@ func serverAddresses(addresses map[string]any) string {
continue
}

values = append(values, network+"="+ip)
values = append(values, ip)
}
}

Expand All @@ -156,6 +154,8 @@ func (r *Resource) Execute(command resource.Command, row resource.Row) tea.Cmd {
return r.image(row)
case "shift-f":
return r.flavor(row)
case "shift-g":
return r.securityGroups(row)
}

return nil
Expand Down
31 changes: 31 additions & 0 deletions internal/ui/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
slog.Error(msg.err.Error(), "cloud", m.context.Cloud)
return m, nil

case resource.ErrorMsg:
m.autoRefreshPaused = true
m.loading = false
m.showLoading = false
m.loadingLabel = ""

m.err = msg.Err

slog.Error(msg.Err.Error(), "cloud", m.context.Cloud)
return m, nil

case clipboardResultMsg:
m.status = "copied to clipboard"
return m, clearStatus(m.status)
Expand Down Expand Up @@ -480,6 +491,26 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

return m, m.navigateScopedResource(msg.Resource)

case resource.NavigateFilteredMultiMsg:
cursor := m.table.Cursor()

if cursor >= 0 && cursor < len(m.resourceRows) {
m.navigation = append(m.navigation, navigationEntry{
resource: m.resource.Kind(),
id: m.resourceRows[cursor].ID,
filter: m.filter,
scope: m.scope,
})
}

m.navigateID = ""
m.filter = &resourceFilter{
field: msg.Field,
values: msg.Values,
}

return m, m.navigateFilteredResource(msg.Resource)

case resource.DetailsMsg:
m.showLoading = false
m.loadingLabel = ""
Expand Down
18 changes: 15 additions & 3 deletions internal/ui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ui
import (
"context"
"fmt"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -43,8 +44,9 @@ type navigationEntry struct {
}

type resourceFilter struct {
field string
value string
field string
value string
values []string
}

type autoRefreshMsg struct{}
Expand Down Expand Up @@ -230,7 +232,17 @@ func filterResourceRows(rows []resource.Row, filter *resourceFilter) []resource.
filtered := make([]resource.Row, 0, len(rows))

for _, row := range rows {
if row.Fields[filter.field] == filter.value {
value := row.Fields[filter.field]

if len(filter.values) > 0 {
if slices.Contains(filter.values, value) {
filtered = append(filtered, row)
}

continue
}

if value == filter.value {
filtered = append(filtered, row)
}
}
Expand Down
Loading