| 1 | package WWW::Hetzner::CLI::Cmd::Firewall::Cmd::Describe; | ||
| 2 | # ABSTRACT: Describe a firewall | ||
| 3 | |||
| 4 | our $VERSION = '0.100'; | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | use MooX::Cmd; | ||
| 8 | use MooX::Options protect_argv => 0, usage_string => 'USAGE: hcloud.pl firewall describe <id>'; | ||
| 9 | use JSON::MaybeXS qw(encode_json); | ||
| 10 | |||
| 11 | sub execute { | ||
| 12 | my ($self, $args, $chain) = @_; | ||
| 13 | my $id = $args->[0] or die "Usage: hcloud.pl firewall describe <id>\n"; | ||
| 14 | |||
| 15 | my $main = $chain->[0]; | ||
| 16 | my $cloud = $main->cloud; | ||
| 17 | |||
| 18 | my $fw = $cloud->firewalls->get($id); | ||
| 19 | |||
| 20 | if ($main->output eq 'json') { | ||
| 21 | print encode_json($fw->data), "\n"; | ||
| 22 | return; | ||
| 23 | } | ||
| 24 | |||
| 25 | printf "ID: %s\n", $fw->id; | ||
| 26 | printf "Name: %s\n", $fw->name; | ||
| 27 | printf "Created: %s\n", $fw->created // '-'; | ||
| 28 | |||
| 29 | my $rules = $fw->rules; | ||
| 30 | if ($rules && @$rules) { | ||
| 31 | print "\nRules:\n"; | ||
| 32 | for my $r (@$rules) { | ||
| 33 | my $port = $r->{port} // 'any'; | ||
| 34 | my $proto = $r->{protocol} // 'any'; | ||
| 35 | my $dir = $r->{direction} // 'in'; | ||
| 36 | my $sources = $r->{source_ips} ? join(', ', @{$r->{source_ips}}) : 'any'; | ||
| 37 | my $dests = $r->{destination_ips} ? join(', ', @{$r->{destination_ips}}) : 'any'; | ||
| 38 | |||
| 39 | if ($dir eq 'in') { | ||
| 40 | printf " - %s %s/%s from %s\n", $dir, $proto, $port, $sources; | ||
| 41 | } else { | ||
| 42 | printf " - %s %s/%s to %s\n", $dir, $proto, $port, $dests; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | my $applied = $fw->applied_to; | ||
| 48 | if ($applied && @$applied) { | ||
| 49 | print "\nApplied to:\n"; | ||
| 50 | for my $a (@$applied) { | ||
| 51 | my $type = $a->{type}; | ||
| 52 | if ($type eq 'server' && $a->{server}) { | ||
| 53 | printf " - Server %d\n", $a->{server}{id}; | ||
| 54 | } elsif ($type eq 'label_selector' && $a->{label_selector}) { | ||
| 55 | printf " - Label: %s\n", $a->{label_selector}{selector}; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | my $labels = $fw->labels; | ||
| 61 | if ($labels && %$labels) { | ||
| 62 | print "\nLabels:\n"; | ||
| 63 | for my $k (sort keys %$labels) { | ||
| 64 | printf " %s: %s\n", $k, $labels->{$k}; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ |