From 9f495abf98e407f990e950b292d75888b84fab5e Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Tue, 29 Oct 2019 14:20:14 +0100 Subject: [PATCH] TT#68300 Move data generation for queries close to their requests We do also an early check and die instead of doing unnecessary work. Change-Id: I36591a8050b795729280914815dde701cbbb9bd7 --- bin/ngcp-create-subscriber | 20 ++++++++++---------- bin/ngcp-delete-domain | 24 ++++++++++++------------ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/bin/ngcp-create-subscriber b/bin/ngcp-create-subscriber index 51a86af..051c99f 100755 --- a/bin/ngcp-create-subscriber +++ b/bin/ngcp-create-subscriber @@ -52,22 +52,22 @@ sub main { my $client = NGCP::API::Client->new(verbose => $opts->{verbose}); + # domain_id + my $dom = $client->request('GET', '/api/domains/?domain=' . $opts->{domain}); + if ($dom->as_hash->{total_count} != 1) { + die "Domain $opts->{domain} not found\n"; + } + my $uri = '/api/subscribers/'; my %data = map { $_ => $opts->{$_} } qw(customer_id username password webpassword); $data{primary_number} = { map { $_ => $opts->{$_} } qw(cc ac sn) }; $data{administrative} = $opts->{admin}; - # domain_id - my $dom = $client->request("GET", "/api/domains/?domain=".$opts->{domain}); - if ($dom->as_hash->{total_count} == 1) { - my $tmp = $dom->as_hash->{_embedded}->{'ngcp:domains'}; - if (ref $tmp eq 'ARRAY') { - $data{domain_id} = @{$tmp}[0]->{id}; - } else { - $data{domain_id} = $tmp->{id}; - } + my $tmp = $dom->as_hash->{_embedded}->{'ngcp:domains'}; + if (ref $tmp eq 'ARRAY') { + $data{domain_id} = @{$tmp}[0]->{id}; } else { - die "Domain $opts->{domain} not found\n"; + $data{domain_id} = $tmp->{id}; } my $res = $client->request("POST", $uri, \%data); print $res->result."\n"; diff --git a/bin/ngcp-delete-domain b/bin/ngcp-delete-domain index 34c3df6..379d25f 100755 --- a/bin/ngcp-delete-domain +++ b/bin/ngcp-delete-domain @@ -48,23 +48,23 @@ sub main { my $client = NGCP::API::Client->new(verbose => $opts->{verbose}); - my $uri = '/api/domains/'; - my %data = map { "_".$_."_reload" => $opts->{$_} } qw(skip_xmpp skip_sip); # domain_id my $dom = $client->request("GET", "/api/domains/?domain=".$opts->{domain}); + if ($dom->as_hash->{total_count} != 1) { + die "Domain $opts->{domain} not found\n"; + } + + my $uri = '/api/domains/'; + my %data = map { "_".$_."_reload" => $opts->{$_} } qw(skip_xmpp skip_sip); my $dom_id; - if ($dom->as_hash->{total_count} == 1) { - my $tmp = $dom->as_hash->{_embedded}->{'ngcp:domains'}; - if (ref $tmp eq 'ARRAY') { - $dom_id = @{$tmp}[0]->{id}; - } else { - $dom_id = $tmp->{id}; - } - die "Domain $opts->{domain} not found\n" unless $dom_id; - $uri .= $dom_id; + my $tmp = $dom->as_hash->{_embedded}->{'ngcp:domains'}; + if (ref $tmp eq 'ARRAY') { + $dom_id = @{$tmp}[0]->{id}; } else { - die "Domain $opts->{domain} not found\n"; + $dom_id = $tmp->{id}; } + die "Domain $opts->{domain} not found\n" unless $dom_id; + $uri .= $dom_id; my $res = $client->request("DELETE", $uri, \%data); print $res->result."\n";