Skip to content

Commit

Permalink
Merge branch 'master' into not-required-to-increment-version
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Sep 8, 2023
2 parents 31a8f07 + 89d1ed6 commit 4b00693
Show file tree
Hide file tree
Showing 21 changed files with 1,162 additions and 32 deletions.
18 changes: 18 additions & 0 deletions cmd/completion/tenant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package completion

import (
"github.com/metal-stack/metal-go/api/client/tenant"
"github.com/spf13/cobra"
)

func (c *Completion) TenantListCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
resp, err := c.client.Tenant().ListTenants(tenant.NewListTenantsParams(), nil)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, p := range resp.Payload {
names = append(names, p.Meta.ID+"\t/"+p.Name)
}
return names, cobra.ShellCompDirectiveNoFileComp
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ metalctl machine list -o template --template "{{ .id }}:{{ .size.id }}"
rootCmd.AddCommand(newMachineCmd(c))
rootCmd.AddCommand(newFirewallCmd(c))
rootCmd.AddCommand(newProjectCmd(c))
rootCmd.AddCommand(newTenantCmd(c))
rootCmd.AddCommand(newSizeCmd(c))
rootCmd.AddCommand(newFilesystemLayoutCmd(c))
rootCmd.AddCommand(newImageCmd(c))
Expand Down
21 changes: 21 additions & 0 deletions cmd/sorters/tenant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package sorters

import (
"github.com/metal-stack/metal-go/api/models"
"github.com/metal-stack/metal-lib/pkg/multisort"
p "github.com/metal-stack/metal-lib/pkg/pointer"
)

func TenantSorter() *multisort.Sorter[*models.V1TenantResponse] {
return multisort.New(multisort.FieldMap[*models.V1TenantResponse]{
"id": func(a, b *models.V1TenantResponse, descending bool) multisort.CompareResult {
return multisort.Compare(p.SafeDeref(a.Meta).ID, p.SafeDeref(b.Meta).ID, descending)
},
"name": func(a, b *models.V1TenantResponse, descending bool) multisort.CompareResult {
return multisort.Compare(a.Name, b.Name, descending)
},
"description": func(a, b *models.V1TenantResponse, descending bool) multisort.CompareResult {
return multisort.Compare(a.Description, b.Description, descending)
},
}, multisort.Keys{{ID: "id"}})
}
40 changes: 28 additions & 12 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ r01leaf01,swp2,44e3a522-5f48-4f3c-9188-41025f9e401e,<b-serial>
switchMachinesCmd.Flags().String("os-version", "", "OS version of this switch.")
switchMachinesCmd.Flags().String("partition", "", "Partition of this switch.")
switchMachinesCmd.Flags().String("rack", "", "Rack of this switch.")
switchMachinesCmd.Flags().String("size", "", "Size of the connectedmachines.")
switchMachinesCmd.Flags().String("size", "", "Size of the connected machines.")
switchMachinesCmd.Flags().String("machine-id", "", "The id of the connected machine, ignores size flag if set.")

must(switchMachinesCmd.RegisterFlagCompletionFunc("id", c.comp.SwitchListCompletion))
must(switchMachinesCmd.RegisterFlagCompletionFunc("name", c.comp.SwitchNameListCompletion))
must(switchMachinesCmd.RegisterFlagCompletionFunc("partition", c.comp.PartitionListCompletion))
must(switchMachinesCmd.RegisterFlagCompletionFunc("rack", c.comp.SwitchRackListCompletion))
must(switchMachinesCmd.RegisterFlagCompletionFunc("size", c.comp.SizeListCompletion))
must(switchMachinesCmd.RegisterFlagCompletionFunc("machine-id", c.comp.MachineListCompletion))

switchReplaceCmd := &cobra.Command{
Use: "replace <switchID>",
Expand Down Expand Up @@ -256,29 +258,43 @@ func (c *switchCmd) switchMachines() error {
return err
}

resp, err := c.client.Machine().FindIPMIMachines(machine.NewFindIPMIMachinesParams().WithBody(&models.V1MachineFindRequest{
PartitionID: viper.GetString("partition"),
Rackid: viper.GetString("rack"),
Sizeid: viper.GetString("size"),
}), nil)
if err != nil {
return err
var machines []*models.V1MachineIPMIResponse

if viper.IsSet("machine-id") {
resp, err := c.client.Machine().FindIPMIMachine(machine.NewFindIPMIMachineParams().WithID(viper.GetString("machine-id")), nil)
if err != nil {
return err
}

machines = append(machines, resp.Payload)
} else {
resp, err := c.client.Machine().FindIPMIMachines(machine.NewFindIPMIMachinesParams().WithBody(&models.V1MachineFindRequest{
ID: viper.GetString("machine-id"),
PartitionID: viper.GetString("partition"),
Rackid: viper.GetString("rack"),
Sizeid: viper.GetString("size"),
}), nil)
if err != nil {
return err
}

machines = append(machines, resp.Payload...)
}

machines := map[string]*models.V1MachineIPMIResponse{}
for _, m := range resp.Payload {
ms := map[string]*models.V1MachineIPMIResponse{}
for _, m := range machines {
m := m

if m.ID == nil {
continue
}

machines[*m.ID] = m
ms[*m.ID] = m
}

return c.listPrinter.Print(&tableprinters.SwitchesWithMachines{
SS: switches,
MS: machines,
MS: ms,
})
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/tableprinters/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (t *TablePrinter) ToHeaderAndRows(data any, wide bool) ([]string, [][]strin
return t.ProjectTable(pointer.WrapInSlice(d), wide)
case []*models.V1ProjectResponse:
return t.ProjectTable(d, wide)
case *models.V1TenantResponse:
return t.TenantTable(pointer.WrapInSlice(d), wide)
case []*models.V1TenantResponse:
return t.TenantTable(d, wide)
case []*models.V1MachineIPMIResponse:
return t.MachineIPMITable(d, wide)
case *models.V1MachineIPMIResponse:
Expand Down
15 changes: 11 additions & 4 deletions cmd/tableprinters/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ func (t *TablePrinter) SwitchWithConnectedMachinesTable(data *SwitchesWithMachin
rows = append(rows, []string{id, "", "", partition, rack})

conns := s.Connections
if viper.IsSet("size") {
conns = []*models.V1SwitchConnection{}
if viper.IsSet("size") || viper.IsSet("machine-id") {
filteredConns := []*models.V1SwitchConnection{}

for _, conn := range s.Connections {
conn := conn

Expand All @@ -137,10 +138,16 @@ func (t *TablePrinter) SwitchWithConnectedMachinesTable(data *SwitchesWithMachin
continue
}

if pointer.SafeDeref(m.Size.ID) == viper.GetString("size") {
conns = append(conns, conn)
if viper.IsSet("machine-id") && pointer.SafeDeref(m.ID) == viper.GetString("machine-id") {
filteredConns = append(filteredConns, conn)
}

if viper.IsSet("size") && pointer.SafeDeref(m.Size.ID) == viper.GetString("size") {
filteredConns = append(filteredConns, conn)
}
}

conns = filteredConns
}

sort.Slice(conns, switchInterfaceNameLessFunc(conns))
Expand Down
66 changes: 66 additions & 0 deletions cmd/tableprinters/tenant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package tableprinters

import (
"strconv"
"strings"

"github.com/metal-stack/metal-go/api/models"
"github.com/metal-stack/metal-lib/pkg/genericcli"
)

func (t *TablePrinter) TenantTable(data []*models.V1TenantResponse, wide bool) ([]string, [][]string, error) {
var (
rows [][]string
)

header := []string{"ID", "Name", "Description", "Labels", "Annotations"}
if wide {
header = []string{"ID", "Name", "Description", "Labels", "Annotations", "Quotas"}
}

for _, pr := range data {
var (
clusterQuota = "∞"
machineQuota = "∞"
ipQuota = "∞"
)

if pr.Quotas != nil {
qs := pr.Quotas
if qs.Cluster != nil {
if qs.Cluster.Quota != 0 {
clusterQuota = strconv.FormatInt(int64(qs.Cluster.Quota), 10)
}
}
if qs.Machine != nil {
if qs.Machine.Quota != 0 {
machineQuota = strconv.FormatInt(int64(qs.Machine.Quota), 10)
}
}
if qs.IP != nil {
if qs.IP.Quota != 0 {
ipQuota = strconv.FormatInt(int64(qs.IP.Quota), 10)
}
}
}

quotas := []string{
clusterQuota + " Cluster(s)",
machineQuota + " Machine(s)",
ipQuota + " IP(s)",
}

labels := strings.Join(pr.Meta.Labels, "\n")

as := genericcli.MapToLabels(pr.Meta.Annotations)
annotations := strings.Join(as, "\n")

if wide {
rows = append(rows, []string{pr.Meta.ID, pr.Name, pr.Description, labels, annotations, strings.Join(quotas, "\n")})
} else {
rows = append(rows, []string{pr.Meta.ID, pr.Name, pr.Description, labels, annotations})
}
}

return header, rows, nil
}
Loading

0 comments on commit 4b00693

Please sign in to comment.