Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow mapping of origins and directors

  • Loading branch information...
commit 9d784a59efef3d729ddce96dda439a39c1e046e8 1 parent c006a5c
@simonwistow simonwistow authored
Showing with 96 additions and 10 deletions.
  1. +96 −10 bin/fastly
View
106 bin/fastly
@@ -90,7 +90,7 @@ my $basecommands = {
cmds => {
service => {
desc => "Show a service",
- doc => "Lists services",
+ doc => "Lists services",
minargs => 1,
maxargs => 3,
args => [\&list_service_complete, sub { ['version'] }, \&list_version],
@@ -145,8 +145,20 @@ my $basecommands = {
args => [\&list_service_complete, sub { ['version'] }, \&list_version, \&service_models, \&name_map_complete, \&model_args],
proc => \&create,
},
+
},
},
+ "add" => {
+ desc => "Add a backend to a director or a director to an origin",
+ cmds => {
+ service => {
+ desc => "The service name",
+ minargs => 1,
+ args => [\&list_service_complete, sub { ['version'] }, \&list_version, sub { ['backend', 'director'] }, \&list_target, sub { ['to'] }, \&list_target_type, \&list_target],
+ proc => \&add,
+ },
+ }
+ },
"delete" => {
desc => "Delete an object",
cmds => {
@@ -703,6 +715,29 @@ sub delete {
return;
}
+sub add {
+ my @args = @_;
+ my $service_name = $args[0];
+ my $version_num = $args[2];
+ my $dest_type = $args[6];
+ my $dest_name = $args[7];
+ my $src_type = $args[3];
+ my $src_name = $args[4];
+ my $service = eval { $fastly->search_services(name => $service_name) };
+ my $src_meth = "get_${src_type}";
+ my $src = $fastly->$src_meth($service->id, $version_num, $src_name);
+ my $dest_meth = "get_${dest_type}";
+ my $dest = $fastly->$dest_meth($service->id, $version_num, $dest_name);
+ my $add_meth = "add_${src_type}";
+
+ my $out = eval { $dest->$add_meth($src) };
+ if ($@) {
+ print_error($@);
+ } else {
+ warn "Added $src_type ".$src->name." to $dest_type ".$dest->name." for service ".$service->name." v${version_num}\n";
+ }
+}
+
sub create {
my @args = @_;
my $name = shift @args;
@@ -768,6 +803,9 @@ sub model_args {
my $commands = $fastly->commands;
my $cmd = $commands->{"$c->{args}->[3].create"};
my @args = @{$c->{args}};
+ use Data::Dumper;
+ warn Dumper $cmd;
+ warn Dumper \@args;
my %seen;
my $i = 0;
@@ -807,6 +845,8 @@ sub model_args {
}
}
my @r;
+ use Data::Dumper;
+ warn Dumper $cmd;
foreach my $c (sort {$a cmp $b} keys %$cmd) {
next if ($c eq 'name' || $c eq 'service' || $c eq 'version');
next if ($seen{$c});
@@ -816,28 +856,36 @@ sub model_args {
return \@r;
}
+our %_mappings;
+sub _mappings {
+ return %_mappings if keys %_mappings;
+ foreach my $class (@Net::Fastly::CLASSES) {
+ my $name = lc $class;
+ $name =~ s/^net::fastly:://;
+ $_mappings{$name} = $class;
+ }
+ %_mappings
+}
sub service_models {
my $t = shift;
my $c = shift;
- my $commands = eval { $fastly->commands };
+ my %mapping = _mappings;
if ($@) {
- my $string = "$@"; #copy becuase RED can reset it
+ my $string = "$@"; #copy becuase RED can reset it
$t->completemsg(sprintf("\n%s%s%s\n", RED, $string, RESET));
return [];
}
my @options;
- foreach my $command (keys %$commands) {
- if ($command =~/(\w+).create/) {
- next if ($1 eq 'service');
- next if ($1 eq 'version');
- push @options, $1;
- }
+ foreach my $name (keys %_mappings) {
+ next if ($name eq 'service');
+ next if ($name eq 'version');
+ push @options, $name;
}
if (defined($c->{str}) && $c->{str} ne "") {
return [ grep { $_ =~ /^$c->{str}/} sort @options ];
}
- return \@options;
+ return [sort @options];
}
@@ -894,11 +942,49 @@ sub list_version {
return [sort map { $_->number } $service->versions];
}
+sub list_target {
+ my $t = shift;
+ my $c = shift;
+ my $service_name = $c->{args}->[0];
+ my $version_num = $c->{args}->[2];
+ my $type = $c->{args}->[6] || $c->{args}->[3];
+ my $version = _get_details($service_name, $version_num);
+ return [] unless $version;
+ my $objs = $version->{"${type}s"} || [];
+ return [ map { $_->{name} } @$objs ];
+}
+
+sub list_target_type {
+ my $t = shift;
+ my $c = shift;
+ my $service_name = $c->{args}->[0];
+ my $version_num = $c->{args}->[2];
+ my $type = $c->{args}->[3];
+ if ($type eq "backend") {
+ return ["director"];
+ } elsif ($type eq "director") {
+ return ["origin"];
+ } else {
+ return [];
+ }
+}
+
+
+
sub present {
my $obj = shift;
Dump($obj);
}
+sub _get_details {
+ my $service_name = shift;
+ my $version_num = shift;
+ my ($service) = $fastly->search_services(name => $service_name);
+ return undef unless $service;
+ my $details = $service->details(version => $version_num);
+ return $details->{version};
+}
+
$term->commands($basecommands);
$term->run(@ARGV);
Please sign in to comment.
Something went wrong with that request. Please try again.