Skip to content

Commit

Permalink
Implement Firewall resource label selector (#328)
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Kämmerling <[email protected]>
  • Loading branch information
LKaemmerling authored May 28, 2021
1 parent 5ea977e commit 234dd6d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 30 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/golang/mock v1.5.0
github.com/google/go-cmp v0.5.2 // indirect
github.com/guptarohit/asciigraph v0.5.1
github.com/hetznercloud/hcloud-go v1.25.0
github.com/hetznercloud/hcloud-go v1.26.1
github.com/pelletier/go-toml v1.8.1
github.com/rjeczalik/interfaces v0.1.1
github.com/spf13/cobra v1.1.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hetznercloud/hcloud-go v1.25.0 h1:QAaFKtGKWRxjwjKJWBGMxGYUxVEQmIkb35j/WXrsazY=
github.com/hetznercloud/hcloud-go v1.25.0/go.mod h1:2C5uMtBiMoFr3m7lBFPf7wXTdh33CevmZpQIIDPGYJI=
github.com/hetznercloud/hcloud-go v1.26.1 h1:9d9yMmq0e1ePZ8r/CkASGFSHcpnV/aY/FK0XtD101Cw=
github.com/hetznercloud/hcloud-go v1.26.1/go.mod h1:2C5uMtBiMoFr3m7lBFPf7wXTdh33CevmZpQIIDPGYJI=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand Down
32 changes: 19 additions & 13 deletions internal/cmd/firewall/apply_to_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,29 @@ func newApplyToResourceCommand(cli *state.State) *cobra.Command {
RunE: cli.Wrap(runApplyToResource),
}
cmd.Flags().String("type", "", "Resource Type (server) (required)")
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("server"))
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("server", "label_selector"))
cmd.MarkFlagRequired("type")

cmd.Flags().String("server", "", "Server name of ID (required when type is server)")
cmd.RegisterFlagCompletionFunc("server", cmpl.SuggestCandidatesF(cli.ServerNames))

cmd.Flags().StringP("label-selector", "l", "", "Label Selector")
return cmd
}
func validateApplyToResource(cmd *cobra.Command, args []string) error {
resourceType, _ := cmd.Flags().GetString("type")

switch resourceType {
case "server":
case string(hcloud.FirewallResourceTypeServer):
server, _ := cmd.Flags().GetString("server")
if server == "" {
return fmt.Errorf("type %s need a --server specific", resourceType)
}
case string(hcloud.FirewallResourceTypeLabelSelector):
labelSelector, _ := cmd.Flags().GetString("label-selector")
if labelSelector == "" {
return fmt.Errorf("type %s need a --label-selector specific", resourceType)
}
default:
return fmt.Errorf("unknown type %s", resourceType)
}
Expand All @@ -48,7 +54,7 @@ func validateApplyToResource(cmd *cobra.Command, args []string) error {
func runApplyToResource(cli *state.State, cmd *cobra.Command, args []string) error {
resourceType, _ := cmd.Flags().GetString("type")
serverIdOrName, _ := cmd.Flags().GetString("server")

labelSelector, _ := cmd.Flags().GetString("label-selector")
idOrName := args[0]
firewall, _, err := cli.Client().Firewall.Get(cli.Context, idOrName)
if err != nil {
Expand All @@ -57,22 +63,22 @@ func runApplyToResource(cli *state.State, cmd *cobra.Command, args []string) err
if firewall == nil {
return fmt.Errorf("Firewall not found: %v", idOrName)
}

server, _, err := cli.Client().Server.Get(cli.Context, serverIdOrName)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("Server not found: %v", serverIdOrName)
}

opts := hcloud.FirewallResource{Type: hcloud.FirewallResourceType(resourceType)}

switch opts.Type {
case hcloud.FirewallResourceTypeServer:
server, _, err := cli.Client().Server.Get(cli.Context, serverIdOrName)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("Server not found: %v", serverIdOrName)
}
opts.Server = &hcloud.FirewallResourceServer{ID: server.ID}
case hcloud.FirewallResourceTypeLabelSelector:
opts.LabelSelector = &hcloud.FirewallResourceLabelSelector{Selector: labelSelector}
default:
return fmt.Errorf("unkown type %s", resourceType)
return fmt.Errorf("unknown type %s", opts.Type)
}

actions, _, err := cli.Client().Firewall.ApplyResources(cli.Context, firewall, []hcloud.FirewallResource{opts})
Expand Down
16 changes: 15 additions & 1 deletion internal/cmd/firewall/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,21 @@ func describeText(cli *state.State, firewall *hcloud.Firewall) error {
}
}
}

fmt.Print("Applied To:\n")
if len(firewall.AppliedTo) == 0 {
fmt.Print(" Not applied\n")
} else {
for _, resource := range firewall.AppliedTo {
fmt.Printf(" - Type:\t\t%s\n", resource.Type)
switch resource.Type {
case hcloud.FirewallResourceTypeServer:
fmt.Printf(" Server ID:\t\t%d\n", resource.Server.ID)
fmt.Printf(" Server Name:\t%s\n", cli.ServerName(resource.Server.ID))
case hcloud.FirewallResourceTypeLabelSelector:
fmt.Printf(" Label Selector:\t%s\n", resource.LabelSelector.Selector)
}
}
}
return nil
}

Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/firewall/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func runList(cli *state.State, cmd *cobra.Command, args []string) error {
switch AppliedTo.Type {
case hcloud.FirewallResourceTypeServer:
s.Server = &schema.FirewallResourceServer{ID: AppliedTo.Server.ID}
case hcloud.FirewallResourceTypeLabelSelector:
s.LabelSelector = &schema.FirewallResourceLabelSelector{Selector: AppliedTo.LabelSelector.Selector}
}

firewallSchema.AppliedTo = append(firewallSchema.AppliedTo, s)
}

Expand Down
32 changes: 19 additions & 13 deletions internal/cmd/firewall/remove_from_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,29 @@ func newRemoveFromResourceCommand(cli *state.State) *cobra.Command {
RunE: cli.Wrap(runRemoveFromResource),
}
cmd.Flags().String("type", "", "Resource Type (server) (required)")
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("server"))
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("server", "label_selector"))
cmd.MarkFlagRequired("type")

cmd.Flags().String("server", "", "Server name of ID (required when type is server)")
cmd.RegisterFlagCompletionFunc("server", cmpl.SuggestCandidatesF(cli.ServerNames))

cmd.Flags().StringP("label-selector", "l", "", "Label Selector")
return cmd
}
func validateRemoveFromResource(cmd *cobra.Command, args []string) error {
resourceType, _ := cmd.Flags().GetString("type")

switch resourceType {
case "server":
case string(hcloud.FirewallResourceTypeServer):
server, _ := cmd.Flags().GetString("server")
if server == "" {
return fmt.Errorf("type %s need a --server specific", resourceType)
}
case string(hcloud.FirewallResourceTypeLabelSelector):
labelSelector, _ := cmd.Flags().GetString("label-selector")
if labelSelector == "" {
return fmt.Errorf("type %s need a --label-selector specific", resourceType)
}
default:
return fmt.Errorf("unknown type %s", resourceType)
}
Expand All @@ -48,6 +54,7 @@ func validateRemoveFromResource(cmd *cobra.Command, args []string) error {
func runRemoveFromResource(cli *state.State, cmd *cobra.Command, args []string) error {
resourceType, _ := cmd.Flags().GetString("type")
serverIdOrName, _ := cmd.Flags().GetString("server")
labelSelector, _ := cmd.Flags().GetString("label-selector")

idOrName := args[0]
firewall, _, err := cli.Client().Firewall.Get(cli.Context, idOrName)
Expand All @@ -57,24 +64,23 @@ func runRemoveFromResource(cli *state.State, cmd *cobra.Command, args []string)
if firewall == nil {
return fmt.Errorf("Firewall not found: %v", idOrName)
}

server, _, err := cli.Client().Server.Get(cli.Context, serverIdOrName)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("Server not found: %v", serverIdOrName)
}

opts := hcloud.FirewallResource{Type: hcloud.FirewallResourceType(resourceType)}

switch opts.Type {
case hcloud.FirewallResourceTypeServer:
server, _, err := cli.Client().Server.Get(cli.Context, serverIdOrName)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("Server not found: %v", serverIdOrName)
}
opts.Server = &hcloud.FirewallResourceServer{ID: server.ID}
case hcloud.FirewallResourceTypeLabelSelector:
opts.LabelSelector = &hcloud.FirewallResourceLabelSelector{Selector: labelSelector}
default:
return fmt.Errorf("unkown type %s", resourceType)
return fmt.Errorf("unknown type %s", opts.Type)
}

actions, _, err := cli.Client().Firewall.RemoveResources(cli.Context, firewall, []hcloud.FirewallResource{opts})
if err != nil {
return err
Expand Down

0 comments on commit 234dd6d

Please sign in to comment.