| 1 | package App::Ordo::Command::Cluster::Update; | ||
| 2 | use Moo; | ||
| 3 | use feature qw(say); | ||
| 4 | extends 'App::Ordo::Command::Base'; | ||
| 5 | |||
| 6 | use App::Ordo qw($CURRENT_PATH); | ||
| 7 | use Term::ANSIColor qw(colored); | ||
| 8 | |||
| 9 | sub name { "cluster update" } | ||
| 10 | sub summary { "Update an existing cluster" } | ||
| 11 | sub usage { "<path> [options]" } | ||
| 12 | |||
| 13 | sub option_spec { | ||
| 14 | return { | ||
| 15 | 'description=s' => 'Change description', | ||
| 16 | 'needs=s@' => 'Replace AND dependencies', | ||
| 17 | 'needs_any=s@' => 'Replace OR dependencies', | ||
| 18 | 'remove_needs' => 'Remove all dependencies (boolean)', | ||
| 19 | 'cal=s' => 'Attach/detach calendar (name or empty)', | ||
| 20 | 'loops=i' => 'Change loop count', | ||
| 21 | 'clonable=i' => 'Change clonable flag', | ||
| 22 | 'on_fail=s' => 'Change fail alarm cluster', | ||
| 23 | }; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub execute { | ||
| 27 | my ($self, $opt, $path) = @_; | ||
| 28 | |||
| 29 | unless ($path) { | ||
| 30 | say colored(["bold red"], "Usage: cluster update <path> [options]"); | ||
| 31 | return; | ||
| 32 | } | ||
| 33 | |||
| 34 | my $payload = { name => $path }; | ||
| 35 | |||
| 36 | if ($opt->{needs}) { | ||
| 37 | my @expanded; | ||
| 38 | for my $item (@{$opt->{needs}}) { | ||
| 39 | push @expanded, split /,/, $item; | ||
| 40 | } | ||
| 41 | $opt->{needs} = \@expanded; | ||
| 42 | } | ||
| 43 | |||
| 44 | if ($opt->{needs_any}) { | ||
| 45 | my @expanded; | ||
| 46 | for my $item (@{$opt->{needs_any}}) { | ||
| 47 | push @expanded, split /,/, $item; | ||
| 48 | } | ||
| 49 | $opt->{needs_any} = \@expanded; | ||
| 50 | } | ||
| 51 | |||
| 52 | # Only include provided options | ||
| 53 | $payload->{description} = $opt->{description} if $opt->{description}; | ||
| 54 | $payload->{needs} = $opt->{needs} if $opt->{needs}; | ||
| 55 | $payload->{needs_any} = $opt->{needs_any} if $opt->{needs_any}; | ||
| 56 | $payload->{remove_needs} = $opt->{remove_needs} if $opt->{remove_needs}; | ||
| 57 | $payload->{cal} = $opt->{cal} if exists $opt->{cal}; | ||
| 58 | $payload->{loops} = $opt->{loops} if defined $opt->{loops}; | ||
| 59 | $payload->{clonable} = $opt->{clonable} if defined $opt->{clonable}; | ||
| 60 | $payload->{on_fail} = $opt->{on_fail} if $opt->{on_fail}; | ||
| 61 | |||
| 62 | unless (keys %$payload > 1) { | ||
| 63 | say colored(["bold yellow"], "No changes specified"); | ||
| 64 | return; | ||
| 65 | } | ||
| 66 | |||
| 67 | my $res = $self->api->call('update_cluster', $payload); | ||
| 68 | |||
| 69 | if ($res->{success}) { | ||
| 70 | say colored(["bold green"], "Cluster '$path' updated"); | ||
| 71 | } else { | ||
| 72 | say colored(["bold red"], "Failed: " . ($res->{message} || 'unknown error')); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; |