trafficcontrol-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mitchell...@apache.org
Subject [05/14] incubator-trafficcontrol git commit: Delivery service tenancy - API
Date Mon, 10 Jul 2017 20:49:57 GMT
Delivery service tenancy - API


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/17ff770e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/17ff770e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/17ff770e

Branch: refs/heads/master
Commit: 17ff770ec09e3f66be2710c67e17feac4d675324
Parents: c041291
Author: nir-sopher <nirsopher@gmail.com>
Authored: Mon Mar 20 19:36:31 2017 +0200
Committer: Jeremy Mitchell <mitchell852@gmail.com>
Committed: Mon Jul 10 14:41:42 2017 -0600

----------------------------------------------------------------------
 traffic_ops/app/lib/API/Deliveryservice.pm      |  17 +-
 traffic_ops/app/lib/API/Deliveryservice2.pm     |  13 +
 traffic_ops/app/lib/Fixtures/Deliveryservice.pm |  40 +++
 traffic_ops/app/t/api/1.2/deliveryservice.t     | 116 +++++++-
 .../app/t/api/1.2/deliveryservice_crud.t        | 296 +++++++++++++++++++
 5 files changed, 475 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/17ff770e/traffic_ops/app/lib/API/Deliveryservice.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/API/Deliveryservice.pm b/traffic_ops/app/lib/API/Deliveryservice.pm
index ee68788..0b5fa5e 100644
--- a/traffic_ops/app/lib/API/Deliveryservice.pm
+++ b/traffic_ops/app/lib/API/Deliveryservice.pm
@@ -133,6 +133,7 @@ sub index {
 				"remapText"            => $row->remap_text,
 				"signed"               => \$row->signed,
 				"sslKeyVersion"        => $row->ssl_key_version,
+				"tenantId"		   => $row->tenant_id,
 				"trRequestHeaders"     => $row->tr_request_headers,
 				"trResponseHeaders"    => $row->tr_response_headers,
 				"type"                 => $row->type->name,
@@ -248,6 +249,7 @@ sub show {
 				"remapText"            => $row->remap_text,
 				"signed"               => \$row->signed,
 				"sslKeyVersion"        => $row->ssl_key_version,
+				"tenantId"             => $row->tenant_id,
 				"trRequestHeaders"     => $row->tr_request_headers,
 				"trResponseHeaders"    => $row->tr_response_headers,
 				"type"                 => $row->type->name,
@@ -285,6 +287,9 @@ sub update {
 			return $self->alert( "A deliveryservice with xmlId " . $xml_id . " already exists."
);
 		}
 	}
+	
+	#setting tenant_id to undef if tenant is not set. TODO(nirs): remove when tenancy is no
longer optional in the API
+	my $tenant_id = exists($params->{tenantId}) ? $params->{tenantId} :  undef; 
 
 	my $values = {
 		active                 => $params->{active},
@@ -329,6 +334,7 @@ sub update {
 		remap_text             => $params->{remapText},
 		signed                 => $params->{signed},
 		ssl_key_version        => $params->{sslKeyVersion},
+		tenant_id              => $tenant_id,
 		tr_request_headers     => $params->{trRequestHeaders},
 		tr_response_headers    => $params->{trResponseHeaders},
 		type                   => $params->{typeId},
@@ -415,6 +421,7 @@ sub update {
 				"remapText"                => $rs->remap_text,
 				"signed"                   => $rs->signed,
 				"sslKeyVersion"            => $rs->ssl_key_version,
+				"tenantId"                 => $rs->tenant_id,
 				"trRequestHeaders"         => $rs->tr_request_headers,
 				"trResponseHeaders"        => $rs->tr_response_headers,
 				"type"                     => $rs->type->name,
@@ -446,11 +453,15 @@ sub create {
 		return $self->alert($result);
 	}
 
+
 	my $xml_id = $params->{xmlId};
 	my $existing = $self->db->resultset('Deliveryservice')->find( { xml_id => $xml_id
} );
 	if ($existing) {
 		return $self->alert( "A deliveryservice with xmlId " . $xml_id . " already exists."
);
 	}
+	
+	#setting tenant_id to the user id if tenant is not set. TODO(nirs): remove when tenancy
is no longer optional in the API
+	my $tenant_id = exists($params->{tenantId}) ? $params->{tenantId} :  $self->current_user_tenant();
 
 	my $values = {
 		active                 => $params->{active},
@@ -495,6 +506,7 @@ sub create {
 		remap_text             => $params->{remapText},
 		signed                 => $params->{signed},
 		ssl_key_version        => $params->{sslKeyVersion},
+		tenant_id              => $tenant_id,
 		tr_request_headers     => $params->{trRequestHeaders},
 		tr_response_headers    => $params->{trResponseHeaders},
 		type                   => $params->{typeId},
@@ -594,6 +606,7 @@ sub create {
 				"remapText"                => $insert->remap_text,
 				"signed"                   => $insert->signed,
 				"sslKeyVersion"            => $insert->ssl_key_version,
+				"tenantId"                 => $insert->tenant_id,
 				"trRequestHeaders"         => $insert->tr_request_headers,
 				"trResponseHeaders"        => $insert->tr_response_headers,
 				"type"                     => $insert->type->name,
@@ -703,6 +716,7 @@ sub get_deliveryservices_by_serverId {
 					"remapText"            => $row->remap_text,
 					"signed"               => \$row->signed,
 					"sslKeyVersion"        => $row->ssl_key_version,
+					"tenantId"             => $row->tenant_id,
 					"trRequestHeaders"     => $row->tr_request_headers,
 					"trResponseHeaders"    => $row->tr_response_headers,
 					"type"                 => $row->type->name,
@@ -777,6 +791,7 @@ sub get_deliveryservices_by_userId {
 					"remapText"            => $row->remap_text,
 					"signed"               => \$row->signed,
 					"sslKeyVersion"        => $row->ssl_key_version,
+					"tenantId"             => $row->tenant_id,
 					"trRequestHeaders"     => $row->tr_request_headers,
 					"trResponseHeaders"    => $row->tr_response_headers,
 					"type"                 => $row->type->name,
@@ -1073,7 +1088,7 @@ sub is_deliveryservice_valid {
 
 	my $rules = {
 		fields => [
-			qw/active cacheurl ccrDnsTtl cdnId checkPath displayName dnsBypassCname dnsBypassIp dnsBypassIp6
dnsBypassTtl dscp edgeHeaderRewrite geoLimitRedirectURL geoLimit geoLimitCountries geoProvider
globalMaxMbps globalMaxTps httpBypassFqdn infoUrl initialDispersion ipv6RoutingEnabled logsEnabled
longDesc longDesc1 longDesc2 maxDnsAnswers midHeaderRewrite missLat missLong multiSiteOrigin
multiSiteOriginAlgorithm orgServerFqdn originShield profileId protocol qstringIgnore rangeRequestHandling
regexRemap regionalGeoBlocking remapText signed sslKeyVersion trRequestHeaders trResponseHeaders
typeId xmlId/
+			qw/active cacheurl ccrDnsTtl cdnId checkPath displayName dnsBypassCname dnsBypassIp dnsBypassIp6
dnsBypassTtl dscp edgeHeaderRewrite geoLimitRedirectURL geoLimit geoLimitCountries geoProvider
globalMaxMbps globalMaxTps httpBypassFqdn infoUrl initialDispersion ipv6RoutingEnabled logsEnabled
longDesc longDesc1 longDesc2 maxDnsAnswers midHeaderRewrite missLat missLong multiSiteOrigin
multiSiteOriginAlgorithm orgServerFqdn originShield profileId protocol qstringIgnore rangeRequestHandling
regexRemap regionalGeoBlocking remapText signed sslKeyVersion tenantId trRequestHeaders trResponseHeaders
typeId xmlId/
 		],
 
 		# Validation checks to perform

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/17ff770e/traffic_ops/app/lib/API/Deliveryservice2.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/API/Deliveryservice2.pm b/traffic_ops/app/lib/API/Deliveryservice2.pm
index de443f7..8b3846e 100644
--- a/traffic_ops/app/lib/API/Deliveryservice2.pm
+++ b/traffic_ops/app/lib/API/Deliveryservice2.pm
@@ -130,6 +130,7 @@ sub delivery_services {
 					"remapText"                => $row->remap_text,
 					"signed"                   => \$row->signed,
 					"sslKeyVersion"            => $row->ssl_key_version,
+					"tenantId"		   => $row->tenant_id,
 					"trRequestHeaders"         => $row->tr_request_headers,
 					"trResponseHeaders"        => $row->tr_response_headers,
 					"type"                     => $row->type->name,
@@ -448,6 +449,16 @@ sub _check_params {
 		$transformed_params->{logsEnabled} = 0;
 	}
 
+	if (defined($ds_id)) {
+		#update
+		#setting tenant_id to undef if tenant is not set. TODO(nirs): remove when tenancy is no
longer optional in the API
+		$transformed_params->{tenantId} = exists($params->{tenantId}) ? $params->{tenantId}
 :  undef;
+	} else {
+		#create	
+		#setting tenant_id to the runing user if tenant is not set. TODO(nirs): remove when tenancy
is no longer optional in the API
+		$transformed_params->{tenantId} = exists($params->{tenantId}) ? $params->{tenantId}
 :  $self->current_user_tenant();
+	}
+
 	return ($transformed_params, undef);
 }
 
@@ -503,6 +514,7 @@ sub new_value {
 			tr_request_headers     => $params->{trRequestHeaders},
 			tr_response_headers    => $params->{trResponseHeaders},
 			logs_enabled           => $transformed_params->{logsEnabled},
+			tenant_id              => $transformed_params->{tenantId},
 		};
 
 	return $value;
@@ -562,6 +574,7 @@ sub get_response {
 		$response->{initialDispersion}      = $rs->initial_dispersion;
 		$response->{dnsBypassCname}         = $rs->dns_bypass_cname;
 		$response->{regionalGeoBlocking}    = $rs->regional_geo_blocking;
+		$response->{tenantId}               = $rs->tenant_id;
 		$response->{trRequestHeaders}       = $rs->tr_request_headers;
 		$response->{logsEnabled}            = $rs->logs_enabled==1 ? "true" : "false";
 	}

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/17ff770e/traffic_ops/app/lib/Fixtures/Deliveryservice.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/Fixtures/Deliveryservice.pm b/traffic_ops/app/lib/Fixtures/Deliveryservice.pm
index 0f91ede..a340540 100644
--- a/traffic_ops/app/lib/Fixtures/Deliveryservice.pm
+++ b/traffic_ops/app/lib/Fixtures/Deliveryservice.pm
@@ -512,6 +512,46 @@ my %definition_for = (
 			regional_geo_blocking => 1,
 		},
 	},
+	ds_cdn1_root => {
+		new   => 'Deliveryservice',
+		using => {
+			id                    => 2100,
+			xml_id                => 'test-ds1-root',
+			active                => 1,
+			tenant_id             => 10**9,
+			dscp                  => 40,
+			signed                => 0,
+			qstring_ignore        => 0,
+			geo_limit             => 0,
+			http_bypass_fqdn      => '',
+			dns_bypass_ip         => '',
+			dns_bypass_ip6        => '',
+			dns_bypass_ttl        => -1,
+			ccr_dns_ttl           => 3600,
+			global_max_mbps       => 0,
+			global_max_tps        => 0,
+			long_desc             => 'test-ds1-root long_desc',
+			long_desc_1           => 'test-ds1-root long_desc_1',
+			long_desc_2           => 'test-ds1-root long_desc_2',
+			max_dns_answers       => 0,
+			protocol              => 0,
+			org_server_fqdn       => 'http://test-ds1-root.edge',
+			info_url              => 'http://test-ds1-root.edge/info_url.html',
+			miss_lat              => '41.881944',
+			miss_long             => '-87.627778',
+			check_path            => '/crossdomain.xml',
+			type                  => 21,
+			profile               => 100,
+			cdn_id                => 100,#TODO(nirs) on merge - change to 300
+			ipv6_routing_enabled  => 1,
+			protocol              => 1,
+			display_name          => 'test-ds1-root-displayname',
+			initial_dispersion    => 1,
+			regional_geo_blocking => 1,
+			logs_enabled          => 1,
+		},
+	},
+	
 );
 
 sub get_definition {

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/17ff770e/traffic_ops/app/t/api/1.2/deliveryservice.t
----------------------------------------------------------------------
diff --git a/traffic_ops/app/t/api/1.2/deliveryservice.t b/traffic_ops/app/t/api/1.2/deliveryservice.t
index 58134af..7aee053 100644
--- a/traffic_ops/app/t/api/1.2/deliveryservice.t
+++ b/traffic_ops/app/t/api/1.2/deliveryservice.t
@@ -30,14 +30,21 @@ use Test::TestHelper;
 
 BEGIN { $ENV{MOJO_MODE} = "test" }
 
-my $schema = Schema->connect_to_database;
-my $dbh    = Schema->database_handle;
-my $t      = Test::Mojo->new('TrafficOps');
+
+sub run_ut {
+
+my $t = shift;
+my $schema = shift;
+my $login_user = shift;
+my $login_password = shift;
 
 Test::TestHelper->unload_core_data($schema);
 Test::TestHelper->load_core_data($schema);
 
-ok $t->post_ok( '/login', => form => { u => Test::TestHelper::ADMIN_USER, p =>
Test::TestHelper::ADMIN_USER_PASSWORD } )->status_is(302)
+my $tenant_id = undef;#user does not have tenancy currently
+my $tenant_name = defined ($tenant_id) ? $schema->resultset('Tenant')->find( { id =>
$tenant_id } )->get_column('name') : "null";
+
+ok $t->post_ok( '/login', => form => { u => $login_user, p => $login_password
} )->status_is(302)
 	->or( sub { diag $t->tx->res->content->asset->{content}; } ), 'Should
login?';
 
 # Count the 'response number'
@@ -81,7 +88,9 @@ ok $t->get_ok("/api/1.2/deliveryservices/list?logsEnabled=true")->status_is(200)
 		->json_is( "/response/0/xmlId", "test-ds1" )
 		->json_is( "/response/0/logsEnabled", 1 )
 		->json_is( "/response/0/ipv6RoutingEnabled", 1 )
-		->json_is( "/response/1/xmlId", "test-ds4" );
+		->json_is( "/response/1/xmlId", "test-ds1-root" )
+		->json_is( "/response/1/tenantId", 10**9 )
+		->json_is( "/response/2/xmlId", "test-ds4" );
 
 # It creates new delivery services
 ok $t->post_ok('/api/1.2/deliveryservices/create' => {Accept => 'application/json'}
=> json => {
@@ -90,6 +99,7 @@ ok $t->post_ok('/api/1.2/deliveryservices/create' => {Accept =>
'application/jso
         "protocol" => "1",
         "orgServerFqdn" => "http://10.75.168.91",
         "cdnName" => "cdn1",
+        "tenantId" => $tenant_id,
         "profileName" => "CCR1",
         "type" => "HTTP",
         "multiSiteOrigin" => "0",
@@ -112,6 +122,7 @@ ok $t->post_ok('/api/1.2/deliveryservices/create' => {Accept =>
'application/jso
     ->json_is( "/response/displayName" => "ds_displayname_1")
     ->json_is( "/response/orgServerFqdn" => "http://10.75.168.91")
     ->json_is( "/response/cdnName" => "cdn1")
+    ->json_is( "/response/tenantId" => $tenant_id)
     ->json_is( "/response/profileName" => "CCR1")
     ->json_is( "/response/protocol" => "1")
     ->json_is( "/response/type" => "HTTP")
@@ -154,6 +165,7 @@ ok $t->post_ok('/api/1.2/deliveryservices/create' => {Accept =>
'application/jso
     ->json_is( "/response/displayName" => "ds_displayname_2")
     ->json_is( "/response/orgServerFqdn" => "http://10.75.168.91")
     ->json_is( "/response/cdnName" => "cdn1")
+    ->json_is( "/response/tenantId" => $tenant_id)#tenant id is derived from the current
tenant
     ->json_is( "/response/profileName" => "CCR1")
     ->json_is( "/response/protocol" => "1")
     ->json_is( "/response/type" => "HTTP")
@@ -176,6 +188,7 @@ ok $t->post_ok('/api/1.2/deliveryservices/create' => {Accept =>
'application/jso
         "orgServerFqdn" => "http://10.75.168.91",
         "cdnName" => "cdn2",
         "profileName" => "CCR2",
+        "tenantId" => $tenant_id,
         "type" => "HTTP",
         "multiSiteOrigin" => "0",
         "regionalGeoBlocking" => "1",
@@ -198,6 +211,7 @@ ok $t->post_ok('/api/1.2/deliveryservices/create' => {Accept =>
'application/jso
     ->json_is( "/response/orgServerFqdn" => "http://10.75.168.91")
     ->json_is( "/response/cdnName" => "cdn2")
     ->json_is( "/response/profileName" => "CCR2")
+    ->json_is( "/response/tenantId" => $tenant_id)
     ->json_is( "/response/protocol" => "1")
     ->json_is( "/response/type" => "HTTP")
     ->json_is( "/response/multiSiteOrigin" => "0")
@@ -219,6 +233,7 @@ ok $t->put_ok('/api/1.2/deliveryservices/' . $ds_id . '/update'  =>
{Accept => '
         "protocol" => "2",
         "orgServerFqdn" => "http://10.75.168.91",
         "cdnName" => "cdn1",
+        "tenantId" => $tenant_id,
         "profileName" => "CCR1",
         "type" => "HTTP",
         "multiSiteOrigin" => "0",
@@ -236,6 +251,83 @@ ok $t->put_ok('/api/1.2/deliveryservices/' . $ds_id . '/update'  =>
{Accept => '
     ->json_is( "/response/displayName" => "ds_displayname_11")
     ->json_is( "/response/orgServerFqdn" => "http://10.75.168.91")
     ->json_is( "/response/cdnName" => "cdn1")
+    ->json_is( "/response/tenantId" => $tenant_id)
+    ->json_is( "/response/profileName" => "CCR1")
+    ->json_is( "/response/protocol" => "2")
+    ->json_is( "/response/type" => "HTTP")
+    ->json_is( "/response/multiSiteOrigin" => "0")
+    ->json_is( "/response/regionalGeoBlocking" => "0")
+    ->json_is( "/response/active" => "true")
+    ->json_is( "/response/matchList/0/type" => "HOST_REGEXP")
+    ->json_is( "/response/matchList/0/setNumber" => "0")
+    ->json_is( "/response/matchList/0/pattern" => ".*\\.my_vod1\\..*")
+            , 'Does the deliveryservice details return?';
+
+
+
+#removing tenancy when no set
+ok $t->put_ok('/api/1.2/deliveryservices/' . $ds_id . '/update'  => {Accept => 'application/json'}
=> json => {
+        "xmlId" => "ds_1",
+        "displayName" => "ds_displayname_11",
+        "protocol" => "2",
+        "orgServerFqdn" => "http://10.75.168.91",
+        "cdnName" => "cdn1",
+        "profileName" => "CCR1",
+        "type" => "HTTP",
+        "multiSiteOrigin" => "0",
+        "regionalGeoBlocking" => "0",
+        "active" => "true",
+        "matchList" => [
+            {
+                "type" =>  "HOST_REGEXP",
+                "setNumber" =>  "0",
+                "pattern" => ".*\\.my_vod1\\..*"
+            }
+        ]})
+    ->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};
} )
+    ->json_is( "/response/xmlId" => "ds_1")
+    ->json_is( "/response/displayName" => "ds_displayname_11")
+    ->json_is( "/response/orgServerFqdn" => "http://10.75.168.91")
+    ->json_is( "/response/cdnName" => "cdn1")
+    ->json_is( "/response/tenantId" => undef)
+    ->json_is( "/response/profileName" => "CCR1")
+    ->json_is( "/response/protocol" => "2")
+    ->json_is( "/response/type" => "HTTP")
+    ->json_is( "/response/multiSiteOrigin" => "0")
+    ->json_is( "/response/regionalGeoBlocking" => "0")
+    ->json_is( "/response/active" => "true")
+    ->json_is( "/response/matchList/0/type" => "HOST_REGEXP")
+    ->json_is( "/response/matchList/0/setNumber" => "0")
+    ->json_is( "/response/matchList/0/pattern" => ".*\\.my_vod1\\..*")
+            , 'Does the deliveryservice details return?';
+
+
+#putting tenancy back
+ok $t->put_ok('/api/1.2/deliveryservices/' . $ds_id . '/update'  => {Accept => 'application/json'}
=> json => {
+        "xmlId" => "ds_1",
+        "displayName" => "ds_displayname_11",
+        "protocol" => "2",
+        "orgServerFqdn" => "http://10.75.168.91",
+        "cdnName" => "cdn1",
+        "tenantId" => $tenant_id,
+        "profileName" => "CCR1",
+        "type" => "HTTP",
+        "multiSiteOrigin" => "0",
+        "regionalGeoBlocking" => "0",
+        "active" => "true",
+        "matchList" => [
+            {
+                "type" =>  "HOST_REGEXP",
+                "setNumber" =>  "0",
+                "pattern" => ".*\\.my_vod1\\..*"
+            }
+        ]})
+    ->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};
} )
+    ->json_is( "/response/xmlId" => "ds_1")
+    ->json_is( "/response/displayName" => "ds_displayname_11")
+    ->json_is( "/response/orgServerFqdn" => "http://10.75.168.91")
+    ->json_is( "/response/cdnName" => "cdn1")
+    ->json_is( "/response/tenantId" => $tenant_id)
     ->json_is( "/response/profileName" => "CCR1")
     ->json_is( "/response/protocol" => "2")
     ->json_is( "/response/type" => "HTTP")
@@ -258,6 +350,7 @@ ok $t->put_ok('/api/1.2/deliveryservices/' . $ds_id . '/update' =>
{Accept => 'a
 		"protocol" => "1",
 		"orgServerFqdn" => "http://10.75.168.91",
 		"cdnName" => "cdn2",
+	        "tenantId" => $tenant_id,
 		"profileName" => "CCR2",
 		"type" => "HTTP",
 		"multiSiteOrigin" => "0",
@@ -281,6 +374,7 @@ ok $t->put_ok('/api/1.2/deliveryservices/' . $ds_id . '/update' =>
{Accept => 'a
 		->json_is( "/response/displayName" => "ds_displayname_1")
 		->json_is( "/response/orgServerFqdn" => "http://10.75.168.91")
 		->json_is( "/response/cdnName" => "cdn2")
+		->json_is( "/response/tenantId" => $tenant_id)
 		->json_is( "/response/profileName" => "CCR2")
 		->json_is( "/response/protocol" => "1")
 		->json_is( "/response/type" => "HTTP")
@@ -303,6 +397,7 @@ ok $t->put_ok('/api/1.2/deliveryservices/' . $ds_id . '/update'  =>
{Accept => '
 			"protocol" => "1",
 			"orgServerFqdn" => "http://10.75.168.91",
 			"cdnName" => "cdn1",
+		        "tenantId" => $tenant_id,
 			"profileName" => "CCR1",
 			"type" => "HTTP",
 			"multiSiteOrigin" => "0",
@@ -342,12 +437,21 @@ ok $t->get_ok("/api/1.2/deliveryservices.json")->status_is(200)->or(
sub { diag
 	->json_is( "/response/0/xmlId", "ds_2" )->json_is( "/response/0/logsEnabled", 0 )->json_is(
"/response/0/ipv6RoutingEnabled", 0 )
 	->json_is( "/response/1/xmlId", "ds_3" );
 
-$t->get_ok('/api/1.2/deliveryservices/list?logsEnabled=true')->status_is(200)->$count_response(2)
+$t->get_ok('/api/1.2/deliveryservices/list?logsEnabled=true')->status_is(200)->$count_response(3)
 	->or( sub { diag $t->tx->res->content->asset->{content}; } );
 
 ok $t->put_ok('/api/1.2/snapshot/cdn1')->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};
} );
 
 ok $t->get_ok('/logout')->status_is(302)->or( sub { diag $t->tx->res->content->asset->{content};
} );
+
+}
+
+my $schema = Schema->connect_to_database;
+my $dbh    = Schema->database_handle;
+my $t      = Test::Mojo->new('TrafficOps');
+
+run_ut($t, $schema, Test::TestHelper::ADMIN_USER,  Test::TestHelper::ADMIN_USER_PASSWORD);
+
 $dbh->disconnect();
 done_testing();
 

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/17ff770e/traffic_ops/app/t/api/1.2/deliveryservice_crud.t
----------------------------------------------------------------------
diff --git a/traffic_ops/app/t/api/1.2/deliveryservice_crud.t b/traffic_ops/app/t/api/1.2/deliveryservice_crud.t
new file mode 100644
index 0000000..145177c
--- /dev/null
+++ b/traffic_ops/app/t/api/1.2/deliveryservice_crud.t
@@ -0,0 +1,296 @@
+package main;
+#
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use Mojo::Base -strict;
+use Test::More;
+use Test::Mojo;
+use Data::Dumper;
+use DBI;
+use JSON;
+use strict;
+use warnings;
+no warnings 'once';
+use warnings 'all';
+use Test::TestHelper;
+
+#no_transactions=>1 ==> keep fixtures after every execution, beware of duplicate data!
+#no_transactions=>0 ==> delete fixtures after every execution
+
+BEGIN { $ENV{MOJO_MODE} = "test" }
+
+
+sub run_ut {
+
+	my $t = shift;
+	my $schema = shift;
+	my $login_user = shift;
+	my $login_password = shift;
+
+	Test::TestHelper->unload_core_data($schema);
+	Test::TestHelper->load_core_data($schema);
+
+	my $tenant_id = undef;#TODO(nirs) change when users tenancy is merged
+	my $tenant_name = defined ($tenant_id) ? $schema->resultset('Tenant')->find( { id
=> $tenant_id } )->get_column('name') : "null";
+
+	ok $t->post_ok( '/login', => form => { u => $login_user, p => $login_password
} )->status_is(302)
+		->or( sub { diag $t->tx->res->content->asset->{content}; } ), 'Should
login?';
+
+	# It gets existing delivery services
+	ok $t->get_ok("/api/1.2/deliveryservices")->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}
} )
+			->json_is( "/response/0/xmlId", "steering-ds1" )
+			->json_is( "/response/0/logsEnabled", 0 )
+			->json_is( "/response/0/ipv6RoutingEnabled", 1 )
+			->json_is( "/response/1/xmlId", "steering-ds2" );
+
+	# It creates new delivery services
+	ok $t->post_ok('/api/1.2/deliveryservices' => {Accept => 'application/json'} =>
json => {
+        	"xmlId" => "ds_1",
+        	"displayName" => "ds_displayname_1",
+        	"protocol" => "1",
+        	"orgServerFqdn" => "http://10.75.168.91",
+        	"cdnName" => "cdn1",
+        	"tenantId" => $tenant_id,
+        	"profileId" => 300,
+        	"typeId" => "36",
+        	"multiSiteOrigin" => "0",
+        	"regionalGeoBlocking" => "1",
+        	"active" => "false",
+        	"dscp" => 0,
+        	"ipv6RoutingEnabled" => "true",
+        	"logsEnabled" => "true",
+        	"initialDispersion" => 0,
+        	"cdnId" => 100,
+        	"signed" => "false",
+        	"rangeRequestHandling" => 0,
+        	"geoLimit" => 0,
+        	"geoProvider" => 0,
+        	"qstringIgnore" => 0,
+        	})
+	    ->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};
} )
+	    ->json_is( "/response/0/xmlId" => "ds_1")->or( sub { diag $t->tx->res->content->asset->{content};
} )
+	    ->json_is( "/response/0/displayName" => "ds_displayname_1")
+	    ->json_is( "/response/0/orgServerFqdn" => "http://10.75.168.91")
+	    ->json_is( "/response/0/cdnId" => 100)
+	    ->json_is( "/response/0/tenantId" => $tenant_id)
+	    ->json_is( "/response/0/profileId" => 300)
+	    ->json_is( "/response/0/protocol" => "1")
+	    ->json_is( "/response/0/typeId" => 36)
+	    ->json_is( "/response/0/multiSiteOrigin" => "0")
+	    ->json_is( "/response/0/regionalGeoBlocking" => "1")
+	    ->json_is( "/response/0/active" => "false")
+	            , 'Was the DS properly added and reported?';
+
+
+	my $ds_id = &get_ds_id('ds_1');
+
+	ok $t->get_ok("/api/1.2/deliveryservices/$ds_id")->status_is(200)->or( sub { diag
$t->tx->res->content->asset->{content} } )
+	    ->json_is( "/response/0/xmlId" => "ds_1")->or( sub { diag $t->tx->res->content->asset->{content};
} )
+	    ->json_is( "/response/0/displayName" => "ds_displayname_1")
+	    ->json_is( "/response/0/orgServerFqdn" => "http://10.75.168.91")
+	    ->json_is( "/response/0/cdnId" => 100)
+	    ->json_is( "/response/0/tenantId" => $tenant_id)
+	    ->json_is( "/response/0/profileId" => 300)
+	    ->json_is( "/response/0/protocol" => "1")
+	    ->json_is( "/response/0/typeId" => 36)
+	    ->json_is( "/response/0/multiSiteOrigin" => "0")
+	    ->json_is( "/response/0/regionalGeoBlocking" => "1")
+	    ->json_is( "/response/0/active" => "0")
+	            , 'Does the deliveryservice details return?';
+
+	# A minor change
+	ok $t->put_ok('/api/1.2/deliveryservices/'.$ds_id => {Accept => 'application/json'}
=> json => {
+	        "xmlId" => "ds_1",
+	        "displayName" => "ds_displayname_1",
+	        "protocol" => "1",
+	        "orgServerFqdn" => "http://10.75.168.92",
+	        "cdnName" => "cdn1",
+	        "tenantId" => $tenant_id,
+	        "profileId" => 300,
+	        "typeId" => "36",
+	        "multiSiteOrigin" => "0",
+	        "regionalGeoBlocking" => "1",
+	        "active" => "false",
+	        "dscp" => 0,
+	        "ipv6RoutingEnabled" => "true",
+	        "logsEnabled" => "true",
+	        "initialDispersion" => 0,
+	        "cdnId" => 100,
+	        "signed" => "false",
+	        "rangeRequestHandling" => 0,
+	        "geoLimit" => 0,
+	        "geoProvider" => 0,
+	        "qstringIgnore" => 0,
+	        })
+	    ->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};
} )
+	    ->json_is( "/response/0/xmlId" => "ds_1")->or( sub { diag $t->tx->res->content->asset->{content};
} )
+	    ->json_is( "/response/0/displayName" => "ds_displayname_1")
+	    ->json_is( "/response/0/orgServerFqdn" => "http://10.75.168.92")
+	    ->json_is( "/response/0/cdnId" => 100)
+	    ->json_is( "/response/0/tenantId" => $tenant_id)
+	    ->json_is( "/response/0/profileId" => 300)
+	    ->json_is( "/response/0/protocol" => "1")
+	    ->json_is( "/response/0/typeId" => 36)
+	    ->json_is( "/response/0/multiSiteOrigin" => "0")
+	    ->json_is( "/response/0/regionalGeoBlocking" => "1")
+	    ->json_is( "/response/0/active" => "false")
+	            , 'A minor change';
+
+
+	# Removing tenancy by not putting it in the put
+	ok $t->put_ok('/api/1.2/deliveryservices/'.$ds_id => {Accept => 'application/json'}
=> json => {
+	        "xmlId" => "ds_1",
+	        "displayName" => "ds_displayname_1",
+	        "protocol" => "1",
+	        "orgServerFqdn" => "http://10.75.168.92",
+	        "cdnName" => "cdn1",
+	        "profileId" => 300,
+	        "typeId" => "36",
+	        "multiSiteOrigin" => "0",
+	        "regionalGeoBlocking" => "1",
+	        "active" => "false",
+	        "dscp" => 0,
+	        "ipv6RoutingEnabled" => "true",
+	        "logsEnabled" => "true",
+	        "initialDispersion" => 0,
+	        "cdnId" => 100,
+	        "signed" => "false",
+	        "rangeRequestHandling" => 0,
+	        "geoLimit" => 0,
+	        "geoProvider" => 0,
+	        "qstringIgnore" => 0,
+	        })
+	    ->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};
} )
+	    ->json_is( "/response/0/xmlId" => "ds_1")->or( sub { diag $t->tx->res->content->asset->{content};
} )
+	    ->json_is( "/response/0/displayName" => "ds_displayname_1")
+	    ->json_is( "/response/0/tenantId" => undef)
+	            , 'Was tenant id removed?';
+
+	# Putting tenant id back
+	ok $t->put_ok('/api/1.2/deliveryservices/'.$ds_id => {Accept => 'application/json'}
=> json => {
+	        "xmlId" => "ds_1",
+	        "displayName" => "ds_displayname_1",
+	        "protocol" => "1",
+	        "orgServerFqdn" => "http://10.75.168.92",
+	        "cdnName" => "cdn1",
+	        "tenantId" => $tenant_id,
+	        "profileId" => 300,
+	        "typeId" => "36",
+	        "multiSiteOrigin" => "0",
+	        "regionalGeoBlocking" => "1",
+	        "active" => "false",
+	        "dscp" => 0,
+	        "ipv6RoutingEnabled" => "true",
+	        "logsEnabled" => "true",
+	        "initialDispersion" => 0,
+	        "cdnId" => 100,
+	        "signed" => "false",
+	        "rangeRequestHandling" => 0,
+	        "geoLimit" => 0,
+	        "geoProvider" => 0,
+	        "qstringIgnore" => 0,
+	        })
+	    ->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};
} )
+	    ->json_is( "/response/0/xmlId" => "ds_1")->or( sub { diag $t->tx->res->content->asset->{content};
} )
+	    ->json_is( "/response/0/displayName" => "ds_displayname_1")
+	    ->json_is( "/response/0/tenantId" => $tenant_id)
+	            , 'Was the tenant ID set again?';
+
+
+	# removing tenant id
+	ok $t->put_ok('/api/1.2/deliveryservices/'.$ds_id => {Accept => 'application/json'}
=> json => {
+	        "xmlId" => "ds_1",
+	        "displayName" => "ds_displayname_1",
+	        "protocol" => "1",
+	        "orgServerFqdn" => "http://10.75.168.92",
+	        "cdnName" => "cdn1",
+	        "tenantId" => undef,
+	        "profileId" => 300,
+	        "typeId" => "36",
+	        "multiSiteOrigin" => "0",
+	        "regionalGeoBlocking" => "1",
+	        "active" => "false",
+	        "dscp" => 0,
+	        "ipv6RoutingEnabled" => "true",
+	        "logsEnabled" => "true",
+	        "initialDispersion" => 0,
+	        "cdnId" => 100,
+	        "signed" => "false",
+	        "rangeRequestHandling" => 0,
+	        "geoLimit" => 0,
+	        "geoProvider" => 0,
+	        "qstringIgnore" => 0,
+	        })
+	    ->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};
} )
+	    ->json_is( "/response/0/xmlId" => "ds_1")->or( sub { diag $t->tx->res->content->asset->{content};
} )
+	    ->json_is( "/response/0/displayName" => "ds_displayname_1")
+	    ->json_is( "/response/0/tenantId" => undef)
+	            , 'Was the tenant ID set again?';
+
+	ok $t->delete_ok('/api/1.2/deliveryservices/' . $ds_id)->status_is(200)->or( sub
{ diag $t->tx->res->content->asset->{content}; } );
+
+	# It creates new delivery services, tenant id derived from user
+	ok $t->post_ok('/api/1.2/deliveryservices' => {Accept => 'application/json'} =>
json => {
+	        "xmlId" => "ds_1",
+	        "displayName" => "ds_displayname_1",
+	        "protocol" => "1",
+	        "orgServerFqdn" => "http://10.75.168.91",
+	        "cdnName" => "cdn1",
+	        "profileId" => 300,
+	        "typeId" => "36",
+	        "multiSiteOrigin" => "0",
+	        "regionalGeoBlocking" => "1",
+	        "active" => "false",
+	        "dscp" => 0,
+	        "ipv6RoutingEnabled" => "true",
+	        "logsEnabled" => "true",
+	        "initialDispersion" => 0,
+	        "cdnId" => 100,
+	        "signed" => "false",
+	        "rangeRequestHandling" => 0,
+	        "geoLimit" => 0,
+	        "geoProvider" => 0,
+	        "qstringIgnore" => 0,
+	        })
+	    ->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};
} )
+	    ->json_is( "/response/0/xmlId" => "ds_1")->or( sub { diag $t->tx->res->content->asset->{content};
} )
+	    ->json_is( "/response/0/tenantId" => $tenant_id)
+	            , 'Was the tenant id dervied from the creating user?';
+
+	my $ds_id = &get_ds_id('ds_1');
+	ok $t->delete_ok('/api/1.2/deliveryservices/' . $ds_id)->status_is(200)->or( sub
{ diag $t->tx->res->content->asset->{content}; } );
+
+	ok $t->get_ok('/logout')->status_is(302)->or( sub { diag $t->tx->res->content->asset->{content};
} );
+
+}
+
+my $schema = Schema->connect_to_database;
+my $dbh    = Schema->database_handle;
+my $t      = Test::Mojo->new('TrafficOps');
+
+run_ut($t, $schema, Test::TestHelper::ADMIN_USER,  Test::TestHelper::ADMIN_USER_PASSWORD);
+
+$dbh->disconnect();
+done_testing();
+
+sub get_ds_id {
+    my $xml_id = shift;
+    my $q      = "select id from deliveryservice where xml_id = \'$xml_id\'";
+    my $get_svr = $dbh->prepare($q);
+    $get_svr->execute();
+    my $p = $get_svr->fetchall_arrayref( {} );
+    $get_svr->finish();
+    my $id = $p->[0]->{id};
+    return $id;
+}


Mime
View raw message