Skip to content
Draft
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 cmd/admin/v2/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func AddCmds(cmd *cobra.Command, c *config.Config) {
adminCmd.AddCommand(newAuditCmd(c))
adminCmd.AddCommand(newComponentCmd(c))
adminCmd.AddCommand(newImageCmd(c))
adminCmd.AddCommand(newIPCmd(c))
adminCmd.AddCommand(newNetworkCmd(c))
adminCmd.AddCommand(newPartitionCmd(c))
adminCmd.AddCommand(newProjectCmd(c))
Expand Down
132 changes: 132 additions & 0 deletions cmd/admin/v2/ip.go

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to wrap this into an own PR. Makes it much easier to review.

Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package v2

import (
"fmt"

adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/metal-stack/cli/cmd/config"
"github.com/metal-stack/cli/cmd/sorters"
"github.com/metal-stack/cli/pkg/helpers"
"github.com/metal-stack/metal-lib/pkg/genericcli"
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type ip struct {
c *config.Config
}

func newIPCmd(c *config.Config) *cobra.Command {
w := &ip{
c: c,
}

cmdsConfig := &genericcli.CmdsConfig[any, any, *apiv2.IP]{
BinaryName: config.BinaryName,
GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs),
Singular: "ip",
Plural: "ips",
Description: "an ip address of metal-stack.io",
Sorter: sorters.IPSorter(),
DescribePrinter: func() printers.Printer { return c.DescribePrinter },
ListPrinter: func() printers.Printer { return c.ListPrinter },
ListCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().String("ip", "", "ip which should be listed")
cmd.Flags().String("uuid", "", "allocation uuid of ip which should be listed")
cmd.Flags().String("project", "", "project from where ips should be listed")
cmd.Flags().String("name", "", "name from ips which should be listed")
cmd.Flags().String("network", "", "network from where ips should be listed")
cmd.Flags().String("machine", "", "machine where ips are attached to")
cmd.Flags().StringSlice("labels", nil, "lists only ips with the given labels")
cmd.Flags().String("addressfamily", "", "addressfamily of ips which should be listed")
cmd.Flags().String("type", "", "type of ips which should be listed")

genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.Project))
genericcli.Must(cmd.RegisterFlagCompletionFunc("network", c.Completion.Network))
genericcli.Must(cmd.RegisterFlagCompletionFunc("machine", c.Completion.Machine))
genericcli.Must(cmd.RegisterFlagCompletionFunc("addressfamily", c.Completion.AddressFamily))
genericcli.Must(cmd.RegisterFlagCompletionFunc("type", c.Completion.IPType))

},
ValidArgsFn: c.Completion.Ip,
}

return genericcli.NewCmds(cmdsConfig)
}

func (c *ip) Create(_ any) (*apiv2.IP, error) {
panic("unimplemented")
}

func (c *ip) Delete(id string) (*apiv2.IP, error) {
panic("unimplemented")
}

func (c *ip) Get(id string) (*apiv2.IP, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

resp, err := c.c.Client.Adminv2().IP().List(ctx, &adminv2.IPServiceListRequest{
Query: &apiv2.IPQuery{
Ip: &id,
},
})
if err != nil {
return nil, err
}
switch len(resp.Ips) {
case 0:
return nil, fmt.Errorf("no ip found for ip:%s", id)
case 1:
return resp.Ips[0], nil
default:
return nil, fmt.Errorf("more than one ip found for ip:%s", id)
}
}

func (c *ip) List() ([]*apiv2.IP, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

var labels *apiv2.Labels
if labelSlice := viper.GetStringSlice("labels"); len(labelSlice) > 0 {
var err error

labels, err = helpers.LabelsFromSlice(labelSlice)
if err != nil {
return nil, err
}
}

resp, err := c.c.Client.Adminv2().IP().List(ctx, &adminv2.IPServiceListRequest{
Query: &apiv2.IPQuery{
Ip: pointer.PointerOrNil(viper.GetString("ip")),
Uuid: pointer.PointerOrNil(viper.GetString("uuid")),
Network: pointer.PointerOrNil(viper.GetString("network")),
Project: pointer.PointerOrNil(viper.GetString("project")),
Name: pointer.PointerOrNil(viper.GetString("name")),
Machine: pointer.PointerOrNil(viper.GetString("machine")),
ParentPrefixCidr: pointer.PointerOrNil(viper.GetString("parent-prefix")),
Labels: labels,
Type: helpers.IPTypeToType(viper.GetString("type")),
AddressFamily: helpers.IPAddressFamilyToType(viper.GetString("addressfamily")),
Namespace: pointer.PointerOrNil(viper.GetString("namespace")),
},
})
if err != nil {
return nil, err
}

return resp.Ips, nil
}

func (c *ip) Update(_ any) (*apiv2.IP, error) {
panic("unimplemented")
}

func (c *ip) Convert(r *apiv2.IP) (string, any, any, error) {
panic("unimplemented")
}
17 changes: 3 additions & 14 deletions cmd/api/v2/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func newIPCmd(c *config.Config) *cobra.Command {
cmd.Flags().StringP("addressfamily", "", "", "addressfamily, can be either IPv4|IPv6, defaults to IPv4 (optional)")

genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.Project))
genericcli.Must(cmd.RegisterFlagCompletionFunc("network", c.Completion.Network))
genericcli.Must(cmd.RegisterFlagCompletionFunc("addressfamily", c.Completion.AddressFamily))
},
UpdateCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().StringP("project", "p", "", "project of the ip")
Expand Down Expand Up @@ -88,7 +90,7 @@ func (c *ip) createFromCLI() (*apiv2.IPServiceCreateRequest, error) {
Description: pointer.PointerOrNil(viper.GetString("description")),
Labels: labels,
Type: new(ipStaticToType(viper.GetBool("static"))),
AddressFamily: addressFamilyToType(viper.GetString("addressfamily")),
AddressFamily: helpers.IPAddressFamilyToType(viper.GetString("addressfamily")),
}, nil
}

Expand Down Expand Up @@ -257,16 +259,3 @@ func ipStaticToType(b bool) apiv2.IPType {
}
return apiv2.IPType_IP_TYPE_EPHEMERAL
}

func addressFamilyToType(af string) *apiv2.IPAddressFamily {
switch af {
case "":
return nil
case "ipv4", "IPv4":
return apiv2.IPAddressFamily_IP_ADDRESS_FAMILY_V4.Enum()
case "ipv6", "IPv6":
return apiv2.IPAddressFamily_IP_ADDRESS_FAMILY_V6.Enum()
default:
return apiv2.IPAddressFamily_IP_ADDRESS_FAMILY_UNSPECIFIED.Enum()
}
}
11 changes: 2 additions & 9 deletions cmd/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@ import (
)

type Completion struct {
client client.Client
project string
}

func New(c client.Client, project string) *Completion {
return &Completion{
client: c,
project: project,
}
Client client.Client
Proj string
}

func OutputFormat(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
33 changes: 31 additions & 2 deletions cmd/completion/ip.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package completion

import (
"github.com/metal-stack/api/go/enum"
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/spf13/cobra"
)

func (c *Completion) Ip(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
req := &apiv2.IPServiceListRequest{
Project: c.project,
Project: c.Proj,
}
resp, err := c.client.Apiv2().IP().List(cmd.Context(), req)
resp, err := c.Client.Apiv2().IP().List(cmd.Context(), req)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand All @@ -19,3 +20,31 @@ func (c *Completion) Ip(cmd *cobra.Command, args []string, toComplete string) ([
}
return names, cobra.ShellCompDirectiveNoFileComp
}
func (c *Completion) AddressFamily(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var afs []string
for _, af := range []apiv2.IPAddressFamily{
apiv2.IPAddressFamily_IP_ADDRESS_FAMILY_V4,
apiv2.IPAddressFamily_IP_ADDRESS_FAMILY_V6} {
stringValue, err := enum.GetStringValue(af)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
afs = append(afs, *stringValue)
}

return afs, cobra.ShellCompDirectiveNoFileComp
}
func (c *Completion) IPType(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var afs []string
for _, af := range []apiv2.IPType{
apiv2.IPType_IP_TYPE_STATIC,
apiv2.IPType_IP_TYPE_EPHEMERAL} {
stringValue, err := enum.GetStringValue(af)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
afs = append(afs, *stringValue)
}

return afs, cobra.ShellCompDirectiveNoFileComp
}
25 changes: 25 additions & 0 deletions cmd/completion/machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package completion

import (
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
"github.com/spf13/cobra"
)

func (c *Completion) Machine(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
req := &adminv2.MachineServiceListRequest{}
resp, err := c.Client.Adminv2().Machine().List(cmd.Context(), req)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, m := range resp.Machines {
var hostname string
if m.Allocation != nil {
hostname = m.Allocation.Hostname
names = append(names, m.Uuid+"\t"+hostname)
} else {
names = append(names, m.Uuid)
}
}
return names, cobra.ShellCompDirectiveNoFileComp
}
10 changes: 5 additions & 5 deletions cmd/completion/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
)

func (c *Completion) Network(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
ownNetworks, err := c.client.Apiv2().Network().List(cmd.Context(), &apiv2.NetworkServiceListRequest{
Project: c.project,
ownNetworks, err := c.Client.Apiv2().Network().List(cmd.Context(), &apiv2.NetworkServiceListRequest{
Project: c.Proj,
})
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

baseNetworks, err := c.client.Apiv2().Network().ListBaseNetworks(cmd.Context(), &apiv2.NetworkServiceListBaseNetworksRequest{
Project: c.project,
baseNetworks, err := c.Client.Apiv2().Network().ListBaseNetworks(cmd.Context(), &apiv2.NetworkServiceListBaseNetworksRequest{
Project: c.Proj,
})
if err != nil {
return nil, cobra.ShellCompDirectiveError
Expand Down Expand Up @@ -71,7 +71,7 @@ func (c *Completion) NetworkAddressFamily(cmd *cobra.Command, args []string, toC
}

func (c *Completion) NetworkAdmin(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
networks, err := c.client.Adminv2().Network().List(cmd.Context(), &adminv2.NetworkServiceListRequest{})
networks, err := c.Client.Adminv2().Network().List(cmd.Context(), &adminv2.NetworkServiceListRequest{})
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/completion/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func (c *Completion) Partition(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
req := &apiv2.PartitionServiceListRequest{}
resp, err := c.client.Apiv2().Partition().List(cmd.Context(), req)
resp, err := c.Client.Apiv2().Partition().List(cmd.Context(), req)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/completion/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func (c *Completion) Project(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
req := &apiv2.ProjectServiceListRequest{}
resp, err := c.client.Apiv2().Project().List(cmd.Context(), req)
resp, err := c.Client.Apiv2().Project().List(cmd.Context(), req)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand Down Expand Up @@ -36,8 +36,8 @@ func (c *Completion) ProjectRole(cmd *cobra.Command, args []string, toComplete s
}

func (c *Completion) ProjectInvite(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
resp, err := c.client.Apiv2().Project().InvitesList(cmd.Context(), &apiv2.ProjectServiceInvitesListRequest{
Project: c.project,
resp, err := c.Client.Apiv2().Project().InvitesList(cmd.Context(), &apiv2.ProjectServiceInvitesListRequest{
Project: c.Proj,
})
if err != nil {
return nil, cobra.ShellCompDirectiveError
Expand All @@ -53,8 +53,8 @@ func (c *Completion) ProjectInvite(cmd *cobra.Command, args []string, toComplete
}

func (c *Completion) ProjectMember(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
resp, err := c.client.Apiv2().Project().Get(cmd.Context(), &apiv2.ProjectServiceGetRequest{
Project: c.project,
resp, err := c.Client.Apiv2().Project().Get(cmd.Context(), &apiv2.ProjectServiceGetRequest{
Project: c.Proj,
})
if err != nil {
return nil, cobra.ShellCompDirectiveError
Expand Down
2 changes: 1 addition & 1 deletion cmd/completion/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func (c *Completion) Size(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
req := &apiv2.SizeServiceListRequest{}
resp, err := c.client.Apiv2().Size().List(cmd.Context(), req)
resp, err := c.Client.Apiv2().Size().List(cmd.Context(), req)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand Down
Loading
Loading