From ee77fa8490f6f8f86e99cf1fbad27daec590fbc2 Mon Sep 17 00:00:00 2001 From: Devops Date: Mon, 3 Jul 2017 17:02:58 +0100 Subject: [PATCH] bin/ovh-api --- bin/ovh-api | 63 +++++++++++------------------------------------------ 1 file changed, 13 insertions(+), 50 deletions(-) diff --git a/bin/ovh-api b/bin/ovh-api index 14a76dfa..3be6d512 100755 --- a/bin/ovh-api +++ b/bin/ovh-api @@ -70,7 +70,7 @@ if ($Opt{'ip-is-routed'}) { # Query help() unless @ARGV; -query(@ARGV); +url_query(@ARGV); # END exit 0; @@ -87,7 +87,7 @@ sub hostname { sub ip_routed { my $ip = shift @_; - my $rec = _query("/ip/$ip") or exit 1; + my $rec = query("/ip/$ip") or exit 1; my @routed; if ($rec->{type} =~ /^(failover|dedicated)$/) { @@ -95,18 +95,18 @@ sub ip_routed { } elsif ($rec->{type} eq 'loadBalancing') { - my $frontends = _query("/ipLoadbalancing/ip-$ip/frontend") or exit 1; + my $frontends = query("/ipLoadbalancing/ip-$ip/frontend") or exit 1; for my $frontend_id (@$frontends) { - my $frontend = _query("/ipLoadbalancing/ip-$ip/frontend/$frontend_id") or exit 1; + my $frontend = query("/ipLoadbalancing/ip-$ip/frontend/$frontend_id") or exit 1; next unless $frontend->{defaultBackendId}; - my $farms = _query("/ipLoadbalancing/ip-$ip/".$frontend->{type}."/farm") or exit 1; + my $farms = query("/ipLoadbalancing/ip-$ip/".$frontend->{type}."/farm") or exit 1; for my $farm_id (@$farms) { - my $servers = _query("/ipLoadbalancing/ip-$ip/".$frontend->{type}."/farm/$farm_id/server") or exit 1; + my $servers = query("/ipLoadbalancing/ip-$ip/".$frontend->{type}."/farm/$farm_id/server") or exit 1; for my $id_server (@$servers) { - my $server = _query("/ipLoadbalancing/ip-$ip/".$frontend->{type}."/farm/$farm_id/server/$id_server") or exit 1; + my $server = query("/ipLoadbalancing/ip-$ip/".$frontend->{type}."/farm/$farm_id/server/$id_server") or exit 1; push @routed,ip_routed($server->{address}); } @@ -132,7 +132,7 @@ sub ip_move { my $localhost = hostname(); - my $rec = _query("/ip/$ip") or exit 1; + my $rec = query("/ip/$ip") or exit 1; my $type = $rec->{type}; $type eq 'failover' or die "$ip $type is not an ip failover!\n"; @@ -143,12 +143,12 @@ sub ip_move { require Data::Dumper; die Data::Dumper::Dumper([$localhost,$routed,$rec]); - local $_ = _query("/ip/$ip/move",'POST',{'to' => $localhost}); + local $_ = query("/ip/$ip/move",'POST',{'to' => $localhost}); print JSON::encode_json($_); exit; } -sub _query { +sub query { my ($url,$method,$body) = @_; exists $ALIASES{$url} and $url = $ALIASES{$url}; @@ -192,7 +192,7 @@ sub _query { return @rows; } -sub query { +sub url_query { my ($url,@body) = @_; @@ -218,12 +218,11 @@ sub query { my $response; my $method = $Opt{method}; -if (1) { if ($Opt{rows}) { - local @_ = _query($url,$method,$body); + local @_ = query($url,$method,$body); print JSON::encode_json(\@_); } else { - local $_ = _query($url,$method,$body); + local $_ = query($url,$method,$body); print ref($_) eq '' ? $_ : JSON::encode_json($_); #print JSON::encode_json(_query($url,$method,$body)); } @@ -231,42 +230,6 @@ if (1) { exit; } - my %aliases = ( - '/server' => '/dedicated/server', - '/dns' => '/domain/zone', - ); - exists $aliases{$url} and $url = $aliases{$url}; - - $response = $Api->rawCall(path=>$url,method=>$method,body=>$body); - die "$NAME: ERR: ".$response->error() if $response->isFailure(); - $content = $response->content; - - return unless defined $content; - - if ($Opt{rows}) { - print "[\n" if @$content>0; - my $count = 0; - for my $id (@$content) { - $id = check_row($url,$id); - next unless defined $id; - - $response = $Api->rawCall(path=>$url.'/'.$id,method=>$Opt{method},body=>{}); - die "$NAME: ERR: ".$response->error() if $response->isFailure(); - - - print ',' if $count > 0; - print JSON::encode_json($response->content)."\n"; - $count++; - } - print "]\n" if $count>0; - return - } - - $content = JSON::encode_json($content) if $content and ref($content) ne ''; - #use Data::Dumper; die Dumper($content); - print "$content\n"; -} - sub sql { my $preff = 'ovh_'; my @tbs = ( -- 2.47.3