Skip to content

Commit

Permalink
Add filter options from machine ls to firewall ls. (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Nov 10, 2020
1 parent 6dfff61 commit 4916e21
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
69 changes: 54 additions & 15 deletions cmd/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,31 @@ should be removed with --remove.`,

func init() {
addMachineCreateFlags(firewallCreateCmd, "firewall")
firewallListCmd.Flags().StringP("partition", "", "", "partition to filter [optional]")
firewallListCmd.Flags().StringP("project", "", "", "project to filter [optional]")

firewallListCmd.Flags().StringVarP(&filterOpts.ID, "id", "", "", "ID to filter [optional]")
firewallListCmd.Flags().StringVarP(&filterOpts.Partition, "partition", "", "", "partition to filter [optional]")
firewallListCmd.Flags().StringVarP(&filterOpts.Size, "size", "", "", "size to filter [optional]")
firewallListCmd.Flags().StringVarP(&filterOpts.Name, "name", "", "", "allocation name to filter [optional]")
firewallListCmd.Flags().StringVarP(&filterOpts.Project, "project", "", "", "allocation project to filter [optional]")
firewallListCmd.Flags().StringVarP(&filterOpts.Image, "image", "", "", "allocation image to filter [optional]")
firewallListCmd.Flags().StringVarP(&filterOpts.Hostname, "hostname", "", "", "allocation hostname to filter [optional]")
firewallListCmd.Flags().StringVarP(&filterOpts.Mac, "mac", "", "", "mac to filter [optional]")
firewallListCmd.Flags().StringSliceVar(&filterOpts.Tags, "tags", []string{}, "tags to filter, use it like: --tags \"tag1,tag2\" or --tags \"tag3\".")
firewallListCmd.RegisterFlagCompletionFunc("partition", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return partitionListCompletion(driver)
})
firewallListCmd.RegisterFlagCompletionFunc("size", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return sizeListCompletion(driver)
})
firewallListCmd.RegisterFlagCompletionFunc("project", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return projectListCompletion(driver)
})
firewallListCmd.RegisterFlagCompletionFunc("id", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return machineListCompletion(driver)
})
firewallListCmd.RegisterFlagCompletionFunc("image", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return imageListCompletion(driver)
})

firewallCmd.AddCommand(firewallCreateCmd)
firewallCmd.AddCommand(firewallListCmd)
Expand Down Expand Up @@ -103,20 +126,36 @@ func firewallList(driver *metalgo.Driver) error {
var err error
if atLeastOneViperStringFlagGiven("id", "partition", "size", "name", "project", "image", "hostname") ||
atLeastOneViperStringSliceFlagGiven("tags") {
ffr := &metalgo.FirewallFindRequest{
MachineFindRequest: metalgo.MachineFindRequest{
ID: viperString("id"),
PartitionID: viperString("partition"),
SizeID: viperString("size"),
AllocationName: viperString("name"),
AllocationProject: viperString("project"),
AllocationImageID: viperString("image"),
AllocationHostname: viperString("hostname"),
Tags: viperStringSlice("tags"),
},
ffr := &metalgo.FirewallFindRequest{}
if filterOpts.ID != "" {
ffr.ID = &filterOpts.ID
}
if filterOpts.Partition != "" {
ffr.PartitionID = &filterOpts.Partition
}
if filterOpts.Size != "" {
ffr.SizeID = &filterOpts.Size
}
if filterOpts.Name != "" {
ffr.AllocationName = &filterOpts.Name
}
if filterOpts.Project != "" {
ffr.AllocationProject = &filterOpts.Project
}
if filterOpts.Image != "" {
ffr.AllocationImageID = &filterOpts.Image
}
if filterOpts.Hostname != "" {
ffr.AllocationHostname = &filterOpts.Hostname
}
if filterOpts.Hostname != "" {
ffr.AllocationHostname = &filterOpts.Hostname
}
if filterOpts.Mac != "" {
ffr.NicsMacAddresses = []string{filterOpts.Mac}
}
if atLeastOneViperStringFlagGiven("mac") {
ffr.NicsMacAddresses = []string{*viperString("mac")}
if len(filterOpts.Tags) > 0 {
ffr.Tags = filterOpts.Tags
}
resp, err = driver.FirewallFind(ffr)
} else {
Expand Down
4 changes: 1 addition & 3 deletions cmd/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (
)

var (
filterOpts *FilterOpts
filterOpts = &FilterOpts{}

machineCmd = &cobra.Command{
Use: "machine",
Expand Down Expand Up @@ -266,8 +266,6 @@ func init() {
addMachineCreateFlags(machineCreateCmd, "machine")
machineCmd.AddCommand(machineCreateCmd)

filterOpts = &FilterOpts{}

machineListCmd.Flags().StringVarP(&filterOpts.ID, "id", "", "", "ID to filter [optional]")
machineListCmd.Flags().StringVarP(&filterOpts.Partition, "partition", "", "", "partition to filter [optional]")
machineListCmd.Flags().StringVarP(&filterOpts.Size, "size", "", "", "size to filter [optional]")
Expand Down

0 comments on commit 4916e21

Please sign in to comment.