# Copyright 2013 NTT Data
# All Rights Reserved.
#
#    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.

from tempest.api.compute import base
from tempest.common import utils
from tempest.lib.common.utils import data_utils
from tempest.lib import decorators


class SecurityGroupsTestAdminJSON(base.BaseV2ComputeAdminTest):
    max_microversion = '2.35'

    @classmethod
    def setup_clients(cls):
        super(SecurityGroupsTestAdminJSON, cls).setup_clients()
        cls.adm_client = cls.os_admin.compute_security_groups_client
        cls.client = cls.security_groups_client

    def _delete_security_group(self, securitygroup_id, admin=True):
        if admin:
            self.adm_client.delete_security_group(securitygroup_id)
        else:
            self.client.delete_security_group(securitygroup_id)

    @decorators.idempotent_id('49667619-5af9-4c63-ab5d-2cfdd1c8f7f1')
    @utils.services('network')
    def test_list_security_groups_list_all_tenants_filter(self):
        # Admin can list security groups of all tenants
        # List of all security groups created
        security_group_list = []
        # Create two security groups for a non-admin tenant
        for _ in range(2):
            name = data_utils.rand_name('securitygroup')
            description = data_utils.rand_name('description')
            securitygroup = self.client.create_security_group(
                name=name, description=description)['security_group']
            self.addCleanup(self._delete_security_group,
                            securitygroup['id'], admin=False)
            security_group_list.append(securitygroup)

        client_tenant_id = securitygroup['tenant_id']
        # Create two security groups for admin tenant
        for _ in range(2):
            name = data_utils.rand_name('securitygroup')
            description = data_utils.rand_name('description')
            adm_securitygroup = self.adm_client.create_security_group(
                name=name, description=description)['security_group']
            self.addCleanup(self._delete_security_group,
                            adm_securitygroup['id'])
            security_group_list.append(adm_securitygroup)

        # Fetch all security groups based on 'all_tenants' search filter
        fetched_list = self.adm_client.list_security_groups(
            all_tenants='true')['security_groups']
        sec_group_id_list = [sg['id'] for sg in fetched_list]
        # Now check if all created Security Groups are present in fetched list
        for sec_group in security_group_list:
            self.assertIn(sec_group['id'], sec_group_id_list)

        # Fetch all security groups for non-admin user with 'all_tenants'
        # search filter
        fetched_list = (self.client.list_security_groups(all_tenants='true')
                        ['security_groups'])
        # Now check if all created Security Groups are present in fetched list
        for sec_group in fetched_list:
            self.assertEqual(sec_group['tenant_id'], client_tenant_id,
                             "Failed to get all security groups for "
                             "non admin user.")

@@ -3368,34 +3368,194 @@
         
-# Now check if all
+sec_group_id_list = %5Bsg%5B'id'%5D for sg in fetched_list%5D%0A        # Now check that 'all_tenants='true' filter for non-admin user only%0A        # provide the requested non-admin user's
  created
@@ -3551,33 +3551,33 @@
 r's created 
-S
+s
 ecurity 
 Groups are p
@@ -3568,42 +3568,113 @@
 ity 
-G
+g
 roups
- are present in fetched list
+,%0A        # not all security groups which include security groups created by%0A        # other users.
 %0A   
@@ -3691,31 +3691,38 @@
 ec_group in 
-fetched
+security_group
 _list:%0A     
@@ -3732,25 +3732,11 @@
     
-self.assertEqual(
+if 
 sec_
@@ -3753,17 +3753,19 @@
 ant_id'%5D
-,
+ ==
  client_
@@ -3773,18 +3773,85 @@
 enant_id
+:%0A                self.assertIn(sec_group%5B'id'%5D, sec_group_id_list
 ,%0A
+ 
         
@@ -3941,16 +3941,17 @@
         
+ 
 %22non adm
@@ -3961,8 +3961,226 @@
 user.%22)%0A
+            else:%0A                self.assertNotIn(sec_group%5B'id'%5D, sec_group_id_list,%0A                                 %22Non admin user shouldn't get other user's %22%0A                                 %22security groups.%22)%0A

Fix assert logic in test_list_security_groups_list_all_tenants_filter