Skip to content

Commit

Permalink
Consume new machine issues endpoint from metal-api. (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Oct 11, 2023
1 parent 3a8c20e commit f3acb33
Show file tree
Hide file tree
Showing 21 changed files with 714 additions and 1,341 deletions.
115 changes: 115 additions & 0 deletions cmd/completion/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package completion

import (
"github.com/metal-stack/metal-go/api/client/machine"
"github.com/metal-stack/metal-go/api/models"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/spf13/cobra"
)

Expand All @@ -20,3 +22,116 @@ func (c *Completion) MachineListCompletion(cmd *cobra.Command, args []string, to
}
return names, cobra.ShellCompDirectiveNoFileComp
}

func (c *Completion) MachineManufacturerCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
resp, err := c.client.Machine().FindIPMIMachines(machine.NewFindIPMIMachinesParams().WithBody(&models.V1MachineFindRequest{}), nil)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, m := range resp.Payload {
if m == nil || m.Ipmi == nil || m.Ipmi.Fru == nil {
continue
}

names = append(names, m.Ipmi.Fru.ProductManufacturer)
}
return names, cobra.ShellCompDirectiveNoFileComp
}

func (c *Completion) MachineProductPartNumberCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
resp, err := c.client.Machine().FindIPMIMachines(machine.NewFindIPMIMachinesParams().WithBody(&models.V1MachineFindRequest{}), nil)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, m := range resp.Payload {
if m == nil || m.Ipmi == nil || m.Ipmi.Fru == nil {
continue
}

names = append(names, m.Ipmi.Fru.ProductPartNumber)
}
return names, cobra.ShellCompDirectiveNoFileComp
}

func (c *Completion) MachineProductSerialCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
resp, err := c.client.Machine().FindIPMIMachines(machine.NewFindIPMIMachinesParams().WithBody(&models.V1MachineFindRequest{}), nil)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, m := range resp.Payload {
if m == nil || m.Ipmi == nil || m.Ipmi.Fru == nil {
continue
}

names = append(names, m.Ipmi.Fru.ProductSerial)
}
return names, cobra.ShellCompDirectiveNoFileComp
}

func (c *Completion) MachineBoardPartNumberCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
resp, err := c.client.Machine().FindIPMIMachines(machine.NewFindIPMIMachinesParams().WithBody(&models.V1MachineFindRequest{}), nil)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, m := range resp.Payload {
if m == nil || m.Ipmi == nil || m.Ipmi.Fru == nil {
continue
}

names = append(names, m.Ipmi.Fru.BoardPartNumber)
}
return names, cobra.ShellCompDirectiveNoFileComp
}

func (c *Completion) IssueTypeCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
resp, err := c.client.Machine().ListIssues(machine.NewListIssuesParams(), nil)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, issue := range resp.Payload {
issue := issue

if issue.ID == nil {
continue
}

name := *issue.ID
description := pointer.SafeDeref(issue.Description)
if description != "" {
name = name + "\t" + description
}

names = append(names, name)
}
return names, cobra.ShellCompDirectiveNoFileComp
}

func (c *Completion) IssueSeverityCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
resp, err := c.client.Machine().ListIssues(machine.NewListIssuesParams(), nil)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

severities := map[string]bool{}
for _, issue := range resp.Payload {
issue := issue

if issue.Severity == nil {
continue
}

severities[*issue.Severity] = true
}

var names []string
for s := range severities {
names = append(names, s)
}

return names, cobra.ShellCompDirectiveNoFileComp
}
14 changes: 14 additions & 0 deletions cmd/completion/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,17 @@ func (c *Completion) NetworkListCompletion(cmd *cobra.Command, args []string, to
}
return names, cobra.ShellCompDirectiveNoFileComp
}

func (c *Completion) NetworkDestinationPrefixesCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
resp, err := c.client.Network().ListNetworks(network.NewListNetworksParams(), nil)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var prefixes []string
for _, n := range resp.Payload {
for _, prefix := range n.Destinationprefixes {
prefixes = append(prefixes, prefix)
}
}
return prefixes, cobra.ShellCompDirectiveNoFileComp
}
9 changes: 7 additions & 2 deletions cmd/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newFirewallCmd(c *config) *cobra.Command {
cmd.Flags().String("project", "", "allocation project to filter [optional]")
cmd.Flags().String("image", "", "allocation image to filter [optional]")
cmd.Flags().String("hostname", "", "allocation hostname to filter [optional]")
cmd.Flags().StringSlice("mac", []string{}, "mac to filter [optional]")
cmd.Flags().String("mac", "", "mac to filter [optional]")
cmd.Flags().StringSlice("tags", []string{}, "tags to filter, use it like: --tags \"tag1,tag2\" or --tags \"tag3\".")
must(cmd.RegisterFlagCompletionFunc("partition", c.comp.PartitionListCompletion))
must(cmd.RegisterFlagCompletionFunc("size", c.comp.SizeListCompletion))
Expand Down Expand Up @@ -86,6 +86,11 @@ func (c firewallCmd) Get(id string) (*models.V1FirewallResponse, error) {
}

func (c firewallCmd) List() ([]*models.V1FirewallResponse, error) {
var macs []string
if viper.IsSet("mac") {
macs = pointer.WrapInSlice(viper.GetString("mac"))
}

resp, err := c.client.Firewall().FindFirewalls(firewall.NewFindFirewallsParams().WithBody(&models.V1FirewallFindRequest{
ID: viper.GetString("id"),
PartitionID: viper.GetString("partition"),
Expand All @@ -94,7 +99,7 @@ func (c firewallCmd) List() ([]*models.V1FirewallResponse, error) {
AllocationProject: viper.GetString("project"),
AllocationImageID: viper.GetString("image"),
AllocationHostname: viper.GetString("hostname"),
NicsMacAddresses: viper.GetStringSlice("mac"),
NicsMacAddresses: macs,
Tags: viper.GetStringSlice("tags"),
}), nil)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func Test_FirewallCmd_MultiResult(t *testing.T) {
mocks: &client.MetalMockFns{
Firewall: func(mock *mock.Mock) {
mock.On("FindFirewalls", testcommon.MatchIgnoreContext(t, firewall.NewFindFirewallsParams().WithBody(&models.V1FirewallFindRequest{
NicsMacAddresses: []string{},
NicsMacAddresses: nil,
Tags: []string{},
})), nil).Return(&firewall.FindFirewallsOK{
Payload: []*models.V1FirewallResponse{
Expand Down
Loading

0 comments on commit f3acb33

Please sign in to comment.