Exemplo n.º 1
0
class TestZone(unittest.TestCase):
    config = config.load_config(
        os.environ.get('SPACEONE_TEST_CONFIG_FILE', './config.yml'))

    identity_v1 = None
    inventory_v1 = None
    domain = None
    domain_owner = None
    owner_id = None
    owner_pw = None
    token = None

    @classmethod
    def setUpClass(cls):
        super(TestZone, cls).setUpClass()
        endpoints = cls.config.get('ENDPOINTS', {})
        cls.identity_v1 = pygrpc.client(endpoint=endpoints.get('identity',
                                                               {}).get('v1'),
                                        version='v1')
        cls.inventory_v1 = pygrpc.client(endpoint=endpoints.get(
            'inventory', {}).get('v1'),
                                         version='v1')

        cls._create_domain()
        cls._create_domain_owner()
        cls._issue_owner_token()

    @classmethod
    def tearDownClass(cls):
        super(TestZone, cls).tearDownClass()
        cls.identity_v1.DomainOwner.delete({
            'domain_id': cls.domain.domain_id,
            'owner_id': cls.owner_id
        })

        if cls.domain:
            cls.identity_v1.Domain.delete({'domain_id': cls.domain.domain_id})

    @classmethod
    def _create_domain(cls):
        name = utils.random_string()
        param = {
            'name': name,
            'tags': {
                utils.random_string(): utils.random_string(),
                utils.random_string(): utils.random_string()
            },
            'config': {
                'aaa': 'bbbb'
            }
        }

        cls.domain = cls.identity_v1.Domain.create(param)
        print(f'domain_id: {cls.domain.domain_id}')
        print(f'domain_name: {cls.domain.name}')

    @classmethod
    def _create_domain_owner(cls):
        cls.owner_id = utils.random_string()[0:10]
        cls.owner_pw = 'qwerty'

        param = {
            'owner_id': cls.owner_id,
            'password': cls.owner_pw,
            'name': 'Steven' + utils.random_string()[0:5],
            'timezone': 'utc+9',
            'email': 'Steven' + utils.random_string()[0:5] + '@mz.co.kr',
            'mobile': '+821026671234',
            'domain_id': cls.domain.domain_id
        }

        owner = cls.identity_v1.DomainOwner.create(param)
        cls.domain_owner = owner
        print(f'owner_id: {cls.owner_id}')
        print(f'owner_pw: {cls.owner_pw}')

    @classmethod
    def _issue_owner_token(cls):
        token_param = {
            'credentials': {
                'user_type': 'DOMAIN_OWNER',
                'user_id': cls.owner_id,
                'password': cls.owner_pw
            },
            'domain_id': cls.domain.domain_id
        }

        issue_token = cls.identity_v1.Token.issue(token_param)
        cls.token = issue_token.access_token
        print(f'token: {cls.token}')

    def setUp(self):
        self.regions = []
        self.region = None
        self.zones = []
        self.zone = None
        self.users = []
        self.user = None

    def tearDown(self):
        for zone in self.zones:
            self.inventory_v1.Zone.delete(
                {
                    'zone_id': zone.zone_id,
                    'domain_id': self.domain.domain_id
                },
                metadata=(('token', self.token), ))

        for region in self.regions:
            self.inventory_v1.Region.delete(
                {
                    'region_id': region.region_id,
                    'domain_id': self.domain.domain_id
                },
                metadata=(('token', self.token), ))

        for user in self.users:
            self.identity_v1.User.delete(
                {
                    'user_id': user.user_id,
                    'domain_id': self.domain.domain_id
                },
                metadata=(('token', self.token), ))

    def _create_user(self, user_id=None):
        lang_code = random.choice(['zh-hans', 'jp', 'ko', 'en', 'es'])
        language = Language.get(lang_code)
        user_id = utils.random_string()[0:10] if user_id is None else user_id

        param = {
            'user_id': user_id,
            'domain_id': self.domain.domain_id,
            'password': '******',
            'name': 'Steven' + utils.random_string()[0:5],
            'language': language.__str__(),
            'timezone': 'Asia/Seoul',
            'tags': {
                'aa': 'bb'
            },
            'email': 'Steven' + utils.random_string()[0:5] + '@mz.co.kr',
            'mobile': '+821026671234',
            'group': 'group-id',
        }

        user = self.identity_v1.User.create(param,
                                            metadata=(('token', self.token), ))
        self.user = user
        self.users.append(user)
        self.assertEqual(self.user.name, param['name'])

    def _create_region(self, name=None):
        """ Create Region
        """

        if not name:
            name = random_string()

        params = {'name': name, 'domain_id': self.domain.domain_id}

        self.region = self.inventory_v1.Region.create(
            params, metadata=(('token', self.token), ))

        self.regions.append(self.region)

    def test_create_zone(self, name=None, region_id=None):
        """ Create Zone
        """
        if region_id is None:
            self._create_region()
            region_id = self.region.region_id

        if not name:
            name = random_string()

        params = {
            'name': name,
            'region_id': region_id,
            'domain_id': self.domain.domain_id
        }
        self.zone = self.inventory_v1.Zone.create(params,
                                                  metadata=(('token',
                                                             self.token), ))

        self.zones.append(self.zone)
        self.assertEqual(self.zone.name, name)

    def test_update_zone_name(self):
        self.test_create_zone()

        name = random_string()
        param = {
            'zone_id': self.zone.zone_id,
            'name': name,
            'domain_id': self.domain.domain_id,
        }
        self.zone = self.inventory_v1.Zone.update(param,
                                                  metadata=(('token',
                                                             self.token), ))
        self.assertEqual(self.zone.name, name)

    def test_update_zone_tags(self):
        self.test_create_zone()

        tags = {
            random_string(): random_string(),
            random_string(): random_string()
        }
        param = {
            'zone_id': self.zone.zone_id,
            'tags': tags,
            'domain_id': self.domain.domain_id,
        }
        self.zone = self.inventory_v1.Zone.update(param,
                                                  metadata=(('token',
                                                             self.token), ))
        self.assertEqual(MessageToDict(self.zone.tags), tags)

    def test_get_zone(self):
        name = 'test-zone'
        self.test_create_zone(name)

        param = {
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }
        self.zone = self.inventory_v1.Zone.get(param,
                                               metadata=(('token',
                                                          self.token), ))
        self.assertEqual(self.zone.name, name)

    def test_add_member_zone(self):
        self.test_create_zone()
        self._create_user()

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        zone_admin = self.inventory_v1.Zone.add_member(
            param, metadata=(('token', self.token), ))
        user_info = MessageToDict(zone_admin.user_info)

        self.assertEqual(user_info.get('user_id'), self.user.user_id)

    def test_add_member_not_exist_user(self):
        self.test_create_zone()
        self._create_user()

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': 'test',
            'domain_id': self.domain.domain_id
        }

        with self.assertRaises(Exception):
            self.inventory_v1.Zone.add_member(param,
                                              metadata=(('token',
                                                         self.token), ))

    def test_add_member_duplicate_user(self):
        self.test_create_zone()
        self._create_user()

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        self.inventory_v1.Zone.add_member(param,
                                          metadata=(('token', self.token), ))

        with self.assertRaises(Exception):
            self.inventory_v1.Zone.add_member(param,
                                              metadata=(('token',
                                                         self.token), ))

    def test_add_member_not_exist_zone(self):
        self.test_create_zone()
        self._create_user()

        param = {
            'zone_id': 'test',
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        with self.assertRaises(Exception):
            self.inventory_v1.Zone.add_member(param,
                                              metadata=(('token',
                                                         self.token), ))

    def test_modify_member_zone(self):
        self.test_create_zone()
        self._create_user()

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        self.inventory_v1.Zone.add_member(param,
                                          metadata=(('token', self.token), ))

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        zone_member = self.inventory_v1.Zone.modify_member(
            param, metadata=(('token', self.token), ))
        user_info = MessageToDict(zone_member.user_info)

        self.assertEqual(user_info.get('user_id'), self.user.user_id)

    def test_modify_member_zone_labels(self):
        self.test_create_zone()
        self._create_user()

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        labels = ['developer', 'operator', 'operator']

        self.inventory_v1.Zone.add_member(param,
                                          metadata=(('token', self.token), ))

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id,
            'labels': labels
        }

        zone_member = self.inventory_v1.Zone.modify_member(
            param, metadata=(('token', self.token), ))

        print(zone_member.labels)
        user_info = MessageToDict(zone_member.user_info)

        self.assertEqual(user_info.get('user_id'), self.user.user_id)

    def test_modify_member_not_exist_user(self):
        self.test_create_zone()
        self._create_user()

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        self.inventory_v1.Zone.add_member(param,
                                          metadata=(('token', self.token), ))

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': 'test',
            'domain_id': self.domain.domain_id,
        }

        with self.assertRaises(Exception):
            self.inventory_v1.Zone.modify_member(param,
                                                 metadata=(('token',
                                                            self.token), ))

    def test_modify_member_not_exist_zone(self):
        self.test_create_zone()
        self._create_user()

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        self.inventory_v1.Zone.add_member(param,
                                          metadata=(('token', self.token), ))

        param = {
            'zone_id': 'test',
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id,
        }

        with self.assertRaises(Exception):
            self.inventory_v1.Zone.modify_member(param,
                                                 metadata=(('token',
                                                            self.token), ))

    def test_remove_member_region(self):
        self.test_create_zone()
        self._create_user()

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        self.inventory_v1.Zone.add_member(param,
                                          metadata=(('token', self.token), ))

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        self.inventory_v1.Zone.remove_member(param,
                                             metadata=(('token',
                                                        self.token), ))

        param = {
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }

        zone_members = self.inventory_v1.Zone.list_members(
            param, metadata=(('token', self.token), ))
        self.assertEqual(0, zone_members.total_count)

    def test_remove_member_not_exist_user(self):
        self.test_create_zone()
        self._create_user()

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        self.inventory_v1.Zone.add_member(param,
                                          metadata=(('token', self.token), ))

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': 'test',
            'domain_id': self.domain.domain_id
        }

        with self.assertRaises(Exception):
            self.inventory_v1.Zone.remove_member(param,
                                                 metadata=(('token',
                                                            self.token), ))

    def test_list_members_zone_id(self):
        self.test_create_zone()
        self._create_user()

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        self.inventory_v1.Zone.add_member(param,
                                          metadata=(('token', self.token), ))

        param = {
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }

        zone_members = self.inventory_v1.Zone.list_members(
            param, metadata=(('token', self.token), ))
        self.assertEqual(1, zone_members.total_count)

    def test_list_members_zone_user_id(self):
        self.test_create_zone()
        self._create_user()

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        self.inventory_v1.Zone.add_member(param,
                                          metadata=(('token', self.token), ))

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        zone_members = self.inventory_v1.Zone.list_members(
            param, metadata=(('token', self.token), ))
        self.assertEqual(1, zone_members.total_count)

    def test_list_members_zone_query(self):
        self.test_create_zone()
        self._create_user()

        param = {
            'zone_id': self.zone.zone_id,
            'user_id': self.user.user_id,
            'domain_id': self.domain.domain_id
        }

        self.inventory_v1.Zone.add_member(param,
                                          metadata=(('token', self.token), ))

        param = {
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id,
            'query': {
                'filter': [{
                    'k': 'user_id',
                    'v': self.user.user_id,
                    'o': 'eq'
                }]
            }
        }

        zone_members = self.inventory_v1.Zone.list_members(
            param, metadata=(('token', self.token), ))
        self.assertEqual(1, zone_members.total_count)

    def test_list_region_id(self):
        self.test_create_zone()
        self.test_create_zone(region_id=self.region.region_id)

        param = {
            'region_id': self.region.region_id,
            'domain_id': self.domain.domain_id
        }

        zones = self.inventory_v1.Zone.list(param,
                                            metadata=(('token', self.token), ))

        self.assertEqual(2, zones.total_count)

    def test_list_zone_id(self):
        self.test_create_zone()
        self.test_create_zone()

        param = {
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }

        zones = self.inventory_v1.Zone.list(param,
                                            metadata=(('token', self.token), ))

        self.assertEqual(1, zones.total_count)

    def test_list_name(self):
        self.test_create_zone()
        self.test_create_zone()

        param = {'name': self.zone.name, 'domain_id': self.domain.domain_id}

        zones = self.inventory_v1.Zone.list(param,
                                            metadata=(('token', self.token), ))

        self.assertEqual(1, zones.total_count)

    def test_list_query(self):
        self.test_create_zone()
        self.test_create_zone()
        self.test_create_zone()

        param = {
            'domain_id': self.domain.domain_id,
            'query': {
                'filter': [{
                    'k':
                    'zone_id',
                    'v':
                    list(map(lambda zone: zone.zone_id, self.zones)),
                    'o':
                    'in'
                }]
            }
        }

        zones = self.inventory_v1.Zone.list(param,
                                            metadata=(('token', self.token), ))
        self.assertEqual(len(self.zones), zones.total_count)

    def test_list_query_2(self):
        self.test_create_zone()
        self.test_create_zone()
        self.test_create_zone()
        self.test_create_zone()
        self.test_create_zone()
        self.test_create_zone()
        self.test_create_zone()

        param = {
            'domain_id': self.domain.domain_id,
            'query': {
                'minimal': True
            }
        }

        zones = self.inventory_v1.Zone.list(param,
                                            metadata=(('token', self.token), ))

        print(zones.results)

        self.assertEqual(len(self.zones), zones.total_count)

    def test_stat_zones(self):
        self.test_list_query()

        params = {
            'domain_id': self.domain.domain_id,
            'query': {
                'aggregate': {
                    'group': {
                        'keys': [{
                            'key': 'zone_id',
                            'name': 'Id'
                        }],
                        'fields': [{
                            'operator': 'count',
                            'name': 'Count'
                        }]
                    }
                },
                'sort': {
                    'name': 'Count',
                    'desc': True
                }
            }
        }

        result = self.inventory_v1.Zone.stat(params,
                                             metadata=(('token',
                                                        self.token), ))

        print(result)
Exemplo n.º 2
0
class TestNetwork(unittest.TestCase):
    config = config.load_config(
        os.environ.get('SPACEONE_TEST_CONFIG_FILE', './config.yml'))

    identity_v1 = None
    inventory_v1 = None
    domain = None
    domain_owner = None
    owner_id = None
    owner_pw = None
    token = None
    region = None
    zone = None
    api_key_obj = None

    @classmethod
    def setUpClass(cls):
        super(TestNetwork, cls).setUpClass()
        endpoints = cls.config.get('ENDPOINTS', {})
        cls.identity_v1 = pygrpc.client(endpoint=endpoints.get('identity',
                                                               {}).get('v1'),
                                        version='v1')
        cls.inventory_v1 = pygrpc.client(endpoint=endpoints.get(
            'inventory', {}).get('v1'),
                                         version='v1')

        cls._create_domain()
        cls._create_domain_owner()
        cls._issue_owner_token()

    @classmethod
    def tearDownClass(cls):
        super(TestNetwork, cls).tearDownClass()

        cls.identity_v1.DomainOwner.delete({
            'domain_id': cls.domain.domain_id,
            'owner_id': cls.owner_id
        })

        if cls.domain:
            cls.identity_v1.Domain.delete({'domain_id': cls.domain.domain_id})

    @classmethod
    def _create_domain(cls):
        name = utils.random_string()
        param = {
            'name': name,
            'tags': {
                utils.random_string(): utils.random_string(),
                utils.random_string(): utils.random_string()
            },
            'config': {
                'aaa': 'bbbb'
            }
        }

        cls.domain = cls.identity_v1.Domain.create(param)
        print(f'domain_id: {cls.domain.domain_id}')
        print(f'domain_name: {cls.domain.name}')

    @classmethod
    def _create_domain_owner(cls):
        cls.owner_id = utils.random_string()[0:10]
        cls.owner_pw = 'qwerty'

        param = {
            'owner_id': cls.owner_id,
            'password': cls.owner_pw,
            'name': 'Steven' + utils.random_string()[0:5],
            'timezone': 'utc+9',
            'email': 'Steven' + utils.random_string()[0:5] + '@mz.co.kr',
            'mobile': '+821026671234',
            'domain_id': cls.domain.domain_id
        }

        owner = cls.identity_v1.DomainOwner.create(param)
        cls.domain_owner = owner
        print(f'owner_id: {cls.owner_id}')
        print(f'owner_pw: {cls.owner_pw}')

    @classmethod
    def _issue_owner_token(cls):
        token_param = {
            'credentials': {
                'user_type': 'DOMAIN_OWNER',
                'user_id': cls.owner_id,
                'password': cls.owner_pw
            },
            'domain_id': cls.domain.domain_id
        }

        issue_token = cls.identity_v1.Token.issue(token_param)
        cls.token = issue_token.access_token
        print(f'token: {cls.token}')

    def setUp(self):
        self.networks = []
        self.network = None

        self._create_zone()

    def tearDown(self):
        for network in self.networks:
            self.inventory_v1.Network.delete(
                {
                    'network_id': network.network_id,
                    'domain_id': self.domain.domain_id
                },
                metadata=(('token', self.token), ))

        if self.zone:
            self.inventory_v1.Zone.delete(
                {
                    'zone_id': self.zone.zone_id,
                    'domain_id': self.domain.domain_id
                },
                metadata=(('token', self.token), ))

        if self.region:
            self.inventory_v1.Region.delete(
                {
                    'region_id': self.region.region_id,
                    'domain_id': self.domain.domain_id
                },
                metadata=(('token', self.token), ))

    def _create_region(self, name=None):
        if not name:
            name = random_string()

        params = {'name': name, 'domain_id': self.domain.domain_id}

        self.region = self.inventory_v1.Region.create(
            params, metadata=(('token', self.token), ))

    def _create_zone(self, name=None):
        self._create_region()

        if not name:
            name = random_string()

        params = {
            'name': name,
            'region_id': self.region.region_id,
            'domain_id': self.domain.domain_id
        }

        self.zone = self.inventory_v1.Zone.create(params,
                                                  metadata=(('token',
                                                             self.token), ))

    def test_create_network(self, name=None, cidr=None, data=None):
        if not name:
            name = random_string()

        if not cidr:
            cidr = '172.16.0.0/16'

        params = {
            'name': name,
            'cidr': cidr,
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }

        if data is not None:
            params.update({'data': data})

        self.network = self.inventory_v1.Network.create(
            params, metadata=(('token', self.token), ))

        self.networks.append(self.network)
        self.assertEqual(self.network.name, name)

    def test_create_network_data(self):
        data = {'vpc': {'vpc_id': 'vpc-xxxxxxx'}}

        self.test_create_network(data=data)
        self.assertEqual(MessageToDict(self.network.data), data)

    '''
    def test_create_network_duplicate_cidr(self):
        self.test_create_network()

        with self.assertRaises(Exception):
            self.test_create_network()

    def test_create_network_duplicate_cidr_2(self):
        self.test_create_network(cidr='172.16.0.0/16')

        with self.assertRaises(Exception):
            self.test_create_network(cidr='172.16.1.0/24')

    def test_create_network_duplicate_cidr_3(self):
        self.test_create_network(cidr='172.16.1.0/24')

        with self.assertRaises(Exception):
            self.test_create_network(cidr='172.16.0.0/16')
    '''

    def test_create_network_invalid_cidr_1(self):
        with self.assertRaises(Exception):
            self.test_create_network(cidr='172.16.0.0/48')

    def test_create_network_invalid_cidr_2(self):
        self.test_create_network(cidr='172.16.1.1')

        param = {'cidr': '172.16.1.1/32', 'domain_id': self.domain.domain_id}

        network = self.inventory_v1.Network.list(param,
                                                 metadata=(('token',
                                                            self.token), ))

        self.assertEqual(1, network.total_count)

    def test_update_network_name(self):
        self.test_create_network()

        name = random_string()
        param = {
            'network_id': self.network.network_id,
            'name': name,
            'domain_id': self.domain.domain_id,
        }

        self.network = self.inventory_v1.Network.update(
            param, metadata=(('token', self.token), ))
        self.assertEqual(self.network.name, name)

    def test_update_network_tags(self):
        self.test_create_network()

        tags = {
            random_string(): random_string(),
            random_string(): random_string()
        }
        param = {
            'network_id': self.network.network_id,
            'tags': tags,
            'domain_id': self.domain.domain_id,
        }
        self.network = self.inventory_v1.Network.update(
            param, metadata=(('token', self.token), ))
        self.assertEqual(MessageToDict(self.network.tags), tags)

    def test_update_network_data(self):
        data = {'vpc': {'vpc_id': 'vpc-xxxxxxx'}}

        self.test_create_network()

        tags = {
            random_string(): random_string(),
            random_string(): random_string()
        }
        param = {
            'network_id': self.network.network_id,
            'tags': tags,
            'data': data,
            'domain_id': self.domain.domain_id,
        }
        self.network = self.inventory_v1.Network.update(
            param, metadata=(('token', self.token), ))
        self.assertEqual(MessageToDict(self.network.data), data)

    def test_get_network(self):
        name = 'test-network'
        self.test_create_network(name)

        param = {
            'network_id': self.network.network_id,
            'domain_id': self.domain.domain_id
        }
        self.network = self.inventory_v1.Network.get(param,
                                                     metadata=(('token',
                                                                self.token), ))

        self.assertEqual(self.network.name, name)

    def test_list_network_id(self):
        self.test_create_network(cidr='172.16.1.0/24')
        self.test_create_network(cidr='172.16.2.0/24')

        param = {
            'network_id': self.network.network_id,
            'domain_id': self.domain.domain_id
        }

        network = self.inventory_v1.Network.list(param,
                                                 metadata=(('token',
                                                            self.token), ))

        self.assertEqual(1, network.total_count)

    def test_list_name(self):
        self.test_create_network(cidr='172.16.1.0/24')
        self.test_create_network(cidr='172.16.2.0/24')

        param = {'name': self.network.name, 'domain_id': self.domain.domain_id}

        network = self.inventory_v1.Network.list(param,
                                                 metadata=(('token',
                                                            self.token), ))

        self.assertEqual(1, network.total_count)

    def test_list_query(self):
        self.test_create_network(cidr='172.16.1.0/24')
        self.test_create_network(cidr='172.16.2.0/24')
        self.test_create_network(cidr='172.16.3.0/24')

        param = {
            'domain_id': self.domain.domain_id,
            'query': {
                'filter': [{
                    'k':
                    'network_id',
                    'v':
                    list(map(lambda network: network.network_id,
                             self.networks)),
                    'o':
                    'in'
                }]
            }
        }

        networks = self.inventory_v1.Network.list(param,
                                                  metadata=(('token',
                                                             self.token), ))
        self.assertEqual(len(self.networks), networks.total_count)

    def test_stat_network(self):
        self.test_list_query()

        params = {
            'domain_id': self.domain.domain_id,
            'query': {
                'aggregate': {
                    'group': {
                        'keys': [{
                            'key': 'cloud_service_id',
                            'name': 'Id'
                        }],
                        'fields': [{
                            'operator': 'count',
                            'name': 'Count'
                        }]
                    }
                },
                'sort': {
                    'name': 'Count',
                    'desc': True
                }
            }
        }

        result = self.inventory_v1.Network.stat(params,
                                                metadata=(('token',
                                                           self.token), ))

        print(result)
Exemplo n.º 3
0
class TestNetworkPolicy(unittest.TestCase):
    config = config.load_config(
        os.environ.get('SPACEONE_TEST_CONFIG_FILE', './config.yml'))

    identity_v1 = None
    inventory_v1 = None
    domain = None
    domain_owner = None
    owner_id = None
    owner_pw = None
    token = None
    region = None
    zone = None

    @classmethod
    def setUpClass(cls):
        super(TestNetworkPolicy, cls).setUpClass()
        endpoints = cls.config.get('ENDPOINTS', {})
        cls.identity_v1 = pygrpc.client(endpoint=endpoints.get('identity',
                                                               {}).get('v1'),
                                        version='v1')
        cls.inventory_v1 = pygrpc.client(endpoint=endpoints.get(
            'inventory', {}).get('v1'),
                                         version='v1')

        cls._create_domain()
        cls._create_domain_owner()
        cls._issue_owner_token()

    @classmethod
    def tearDownClass(cls):
        super(TestNetworkPolicy, cls).tearDownClass()
        cls.identity_v1.DomainOwner.delete({
            'domain_id': cls.domain.domain_id,
            'owner_id': cls.owner_id
        })

        if cls.domain:
            cls.identity_v1.Domain.delete({'domain_id': cls.domain.domain_id})

    @classmethod
    def _create_domain(cls):
        name = utils.random_string()
        param = {
            'name': name,
            'tags': {
                utils.random_string(): utils.random_string(),
                utils.random_string(): utils.random_string()
            },
            'config': {
                'aaa': 'bbbb'
            }
        }

        cls.domain = cls.identity_v1.Domain.create(param)
        print(f'domain_id: {cls.domain.domain_id}')
        print(f'domain_name: {cls.domain.name}')

    @classmethod
    def _create_domain_owner(cls):
        cls.owner_id = utils.random_string()[0:10]
        cls.owner_pw = 'qwerty'

        param = {
            'owner_id': cls.owner_id,
            'password': cls.owner_pw,
            'name': 'Steven' + utils.random_string()[0:5],
            'timezone': 'utc+9',
            'email': 'Steven' + utils.random_string()[0:5] + '@mz.co.kr',
            'mobile': '+821026671234',
            'domain_id': cls.domain.domain_id
        }

        owner = cls.identity_v1.DomainOwner.create(param)
        cls.domain_owner = owner
        print(f'owner_id: {cls.owner_id}')
        print(f'owner_pw: {cls.owner_pw}')

    @classmethod
    def _issue_owner_token(cls):
        token_param = {
            'credentials': {
                'user_type': 'DOMAIN_OWNER',
                'user_id': cls.owner_id,
                'password': cls.owner_pw
            },
            'domain_id': cls.domain.domain_id
        }

        issue_token = cls.identity_v1.Token.issue(token_param)
        cls.token = issue_token.access_token
        print(f'token: {cls.token}')

    def setUp(self):
        self.network_policies = []
        self.network_policy = None

        self._create_zone()

    def tearDown(self):
        for network_policy in self.network_policies:
            self.inventory_v1.NetworkPolicy.delete(
                {
                    'network_policy_id': network_policy.network_policy_id,
                    'domain_id': self.domain.domain_id
                },
                metadata=(('token', self.token), ))

        if self.zone:
            self.inventory_v1.Zone.delete(
                {
                    'zone_id': self.zone.zone_id,
                    'domain_id': self.domain.domain_id
                },
                metadata=(('token', self.token), ))

        if self.region:
            self.inventory_v1.Region.delete(
                {
                    'region_id': self.region.region_id,
                    'domain_id': self.domain.domain_id
                },
                metadata=(('token', self.token), ))

    def _create_region(self, name=None):
        if not name:
            name = random_string()

        params = {'name': name, 'domain_id': self.domain.domain_id}

        self.region = self.inventory_v1.Region.create(
            params, metadata=(('token', self.token), ))

    def _create_zone(self, name=None):
        self._create_region()

        if not name:
            name = random_string()

        params = {
            'name': name,
            'region_id': self.region.region_id,
            'domain_id': self.domain.domain_id
        }

        self.zone = self.inventory_v1.Zone.create(params,
                                                  metadata=(('token',
                                                             self.token), ))

    def test_create_network_policy(self, name=None, data=None):
        if not name:
            name = random_string()

        params = {
            'name': name,
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }

        if data is not None:
            params.update({'data': data})

        self.network_policy = self.inventory_v1.NetworkPolicy.create(
            params, metadata=(('token', self.token), ))

        self.network_policies.append(self.network_policy)
        self.assertEqual(self.network_policy.name, name)

    def test_create_network_policy_with_routing_tables(self):
        name = random_string()

        routing_tables = [{
            'cidr': '172.16.0.0/16',
            'destination': '172.16.0.1'
        }]

        params = {
            'name': name,
            'routing_tables': routing_tables,
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }

        self.network_policy = self.inventory_v1.NetworkPolicy.create(
            params, metadata=(('token', self.token), ))

        for rt in self.network_policy.routing_tables:
            routing_table = MessageToDict(rt)

        self.network_policies.append(self.network_policy)
        self.assertEqual(routing_table, routing_tables[0])

    def test_create_network_policy_with_dns(self):
        name = random_string()

        dns = ['172.16.0.1', '172.16.0.2', '172.16.0.3']

        params = {
            'name': name,
            'dns': dns,
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }

        self.network_policy = self.inventory_v1.NetworkPolicy.create(
            params, metadata=(('token', self.token), ))

        self.network_policies.append(self.network_policy)
        self.assertEqual(self.network_policy.dns, dns)

    def test_create_network_policy_data(self):
        data = {'vpc': {'rtable_id': 'rtable-xxxxxxx'}}

        self.test_create_network_policy(data=data)
        self.assertEqual(MessageToDict(self.network_policy.data), data)

    def test_update_network_policy_name(self):
        self.test_create_network_policy()

        name = random_string()
        param = {
            'network_policy_id': self.network_policy.network_policy_id,
            'name': name,
            'domain_id': self.domain.domain_id,
        }
        self.network_policy = self.inventory_v1.NetworkPolicy.update(
            param, metadata=(('token', self.token), ))
        self.assertEqual(self.network_policy.name, name)

    def test_update_network_policy_data(self):
        data = {'vpc': {'rtable_id': 'rtable-xxxxxxx'}}

        self.test_create_network_policy()

        param = {
            'network_policy_id': self.network_policy.network_policy_id,
            'data': data,
            'domain_id': self.domain.domain_id,
        }
        self.network_policy = self.inventory_v1.NetworkPolicy.update(
            param, metadata=(('token', self.token), ))
        self.assertEqual(MessageToDict(self.network_policy.data), data)

    def test_update_network_policy_tags(self):
        self.test_create_network_policy()

        tags = {
            random_string(): random_string(),
            random_string(): random_string()
        }
        param = {
            'network_policy_id': self.network_policy.network_policy_id,
            'tags': tags,
            'domain_id': self.domain.domain_id,
        }
        self.network_policy = self.inventory_v1.NetworkPolicy.update(
            param, metadata=(('token', self.token), ))
        self.assertEqual(MessageToDict(self.network_policy.tags), tags)

    def test_update_network_policy_with_routing_tables(self):
        name = random_string()

        routing_tables = [{
            'cidr': '172.16.0.0/16',
            'destination': '172.16.0.1'
        }]

        params = {
            'name': name,
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }

        self.network_policy = self.inventory_v1.NetworkPolicy.create(
            params, metadata=(('token', self.token), ))
        self.network_policies.append(self.network_policy)

        params = {
            'network_policy_id': self.network_policy.network_policy_id,
            'routing_tables': routing_tables,
            'domain_id': self.domain.domain_id
        }

        self.network_policy = self.inventory_v1.NetworkPolicy.update(
            params, metadata=(('token', self.token), ))

        for rt in self.network_policy.routing_tables:
            routing_table = MessageToDict(rt)

        self.assertEqual(routing_table, routing_tables[0])

    def test_udpate_network_policy_with_dns(self):
        name = random_string()

        dns = ['172.16.0.1', '172.16.0.2', '172.16.0.3']

        params = {
            'name': name,
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }

        self.network_policy = self.inventory_v1.NetworkPolicy.create(
            params, metadata=(('token', self.token), ))
        self.network_policies.append(self.network_policy)

        params = {
            'network_policy_id': self.network_policy.network_policy_id,
            'dns': dns,
            'domain_id': self.domain.domain_id
        }

        self.network_policy = self.inventory_v1.NetworkPolicy.update(
            params, metadata=(('token', self.token), ))

        self.assertEqual(self.network_policy.dns, dns)

    def test_get_network_policy(self):
        name = 'test-npolicy'
        self.test_create_network_policy(name)

        param = {
            'network_policy_id': self.network_policy.network_policy_id,
            'domain_id': self.domain.domain_id
        }
        self.network_policy = self.inventory_v1.NetworkPolicy.get(
            param, metadata=(('token', self.token), ))
        self.assertEqual(self.network_policy.name, name)

    def test_list_network_policy_id(self):
        self.test_create_network_policy()
        self.test_create_network_policy()

        param = {
            'network_policy_id': self.network_policy.network_policy_id,
            'domain_id': self.domain.domain_id
        }

        network_policies = self.inventory_v1.NetworkPolicy.list(
            param, metadata=(('token', self.token), ))

        self.assertEqual(1, network_policies.total_count)

    def test_list_name(self):
        self.test_create_network_policy()
        self.test_create_network_policy()

        param = {
            'name': self.network_policy.name,
            'domain_id': self.domain.domain_id
        }

        network_policies = self.inventory_v1.NetworkPolicy.list(
            param, metadata=(('token', self.token), ))

        self.assertEqual(1, network_policies.total_count)

    def test_list_query(self):
        self.test_create_network_policy()
        self.test_create_network_policy()
        self.test_create_network_policy()

        param = {
            'domain_id': self.domain.domain_id,
            'query': {
                'filter': [{
                    'k':
                    'network_policy_id',
                    'v':
                    list(
                        map(lambda npolicy: npolicy.network_policy_id,
                            self.network_policies)),
                    'o':
                    'in'
                }]
            }
        }

        npolicies = self.inventory_v1.NetworkPolicy.list(
            param, metadata=(('token', self.token), ))
        self.assertEqual(len(self.network_policies), npolicies.total_count)

    def test_list_query_with_dns(self):
        self.test_create_network_policy()
        self.test_create_network_policy()
        self.test_create_network_policy()

        dns = ['172.16.0.1', '172.16.0.2']

        params = {
            'network_policy_id': self.network_policy.network_policy_id,
            'dns': dns,
            'domain_id': self.domain.domain_id
        }

        self.network_policy = self.inventory_v1.NetworkPolicy.update(
            params, metadata=(('token', self.token), ))

        param = {
            'domain_id': self.domain.domain_id,
            'query': {
                'filter': [{
                    'k': 'dns',
                    'v': '172.16.0.2',
                    'o': 'eq'
                }]
            }
        }

        npolicies = self.inventory_v1.NetworkPolicy.list(
            param, metadata=(('token', self.token), ))
        self.assertEqual(1, npolicies.total_count)

    def test_list_query_with_routing_tables(self):
        self.test_create_network_policy()
        self.test_create_network_policy()
        self.test_create_network_policy()

        routing_tables = [{
            'cidr': '172.16.0.0/16',
            'destination': '172.16.0.1'
        }]

        params = {
            'network_policy_id': self.network_policy.network_policy_id,
            'routing_tables': routing_tables,
            'domain_id': self.domain.domain_id
        }

        self.network_policy = self.inventory_v1.NetworkPolicy.update(
            params, metadata=(('token', self.token), ))

        param = {
            'domain_id': self.domain.domain_id,
            'query': {
                'filter': [{
                    'k': 'routing_tables.cidr',
                    'v': '172.16.0.0/16',
                    'o': 'eq'
                }]
            }
        }

        npolicies = self.inventory_v1.NetworkPolicy.list(
            param, metadata=(('token', self.token), ))
        self.assertEqual(1, npolicies.total_count)

    def test_stat_network_policy(self):
        self.test_list_query()

        params = {
            'domain_id': self.domain.domain_id,
            'query': {
                'aggregate': {
                    'group': {
                        'keys': [{
                            'key': 'network_policy_id',
                            'name': 'Id'
                        }],
                        'fields': [{
                            'operator': 'count',
                            'name': 'Count'
                        }]
                    }
                },
                'sort': {
                    'name': 'Count',
                    'desc': True
                }
            }
        }

        result = self.inventory_v1.NetworkPolicy.stat(
            params, metadata=(('token', self.token), ))

        print(result)
Exemplo n.º 4
0
class TestSubnet(unittest.TestCase):
    config = config.load_config(
        os.environ.get('SPACEONE_TEST_CONFIG_FILE', './config.yml'))

    identity_v1 = None
    inventory_v1 = None
    domain = None
    domain_owner = None
    owner_id = None
    owner_pw = None
    token = None

    @classmethod
    def setUpClass(cls):
        super(TestSubnet, cls).setUpClass()
        endpoints = cls.config.get('ENDPOINTS', {})
        cls.identity_v1 = pygrpc.client(endpoint=endpoints.get('identity', {}).get('v1'), version='v1')
        cls.inventory_v1 = pygrpc.client(endpoint=endpoints.get('inventory', {}).get('v1'), version='v1')

        cls._create_domain()
        cls._create_domain_owner()
        cls._issue_owner_token()

    @classmethod
    def tearDownClass(cls):
        super(TestSubnet, cls).tearDownClass()
        cls.identity_v1.DomainOwner.delete({
            'domain_id': cls.domain.domain_id,
            'owner_id': cls.owner_id
        })

        if cls.domain:
            cls.identity_v1.Domain.delete({'domain_id': cls.domain.domain_id})

    @classmethod
    def _create_domain(cls):
        name = utils.random_string()
        param = {
            'name': name,
            'tags': {utils.random_string(): utils.random_string(), utils.random_string(): utils.random_string()},
            'config': {
                'aaa': 'bbbb'
            }
        }

        cls.domain = cls.identity_v1.Domain.create(param)
        print(f'domain_id: {cls.domain.domain_id}')
        print(f'domain_name: {cls.domain.name}')

    @classmethod
    def _create_domain_owner(cls):
        cls.owner_id = utils.random_string()[0:10]
        cls.owner_pw = 'qwerty'

        param = {
            'owner_id': cls.owner_id,
            'password': cls.owner_pw,
            'name': 'Steven' + utils.random_string()[0:5],
            'timezone': 'utc+9',
            'email': 'Steven' + utils.random_string()[0:5] + '@mz.co.kr',
            'mobile': '+821026671234',
            'domain_id': cls.domain.domain_id
        }

        owner = cls.identity_v1.DomainOwner.create(
            param
        )
        cls.domain_owner = owner
        print(f'owner_id: {cls.owner_id}')
        print(f'owner_pw: {cls.owner_pw}')

    @classmethod
    def _issue_owner_token(cls):
        token_param = {
            'credentials': {
                'user_type': 'DOMAIN_OWNER',
                'user_id': cls.owner_id,
                'password': cls.owner_pw
            },
            'domain_id': cls.domain.domain_id
        }

        issue_token = cls.identity_v1.Token.issue(token_param)
        cls.token = issue_token.access_token
        print(f'token: {cls.token}')

    def setUp(self):
        self.network = None
        self.network_type = None
        self.network_types = []
        self.network_policy = None
        self.network_polcies = []
        self.subnets = []
        self.subnet = None
        self.project_group = None
        self.project = None
        self.projects = []

        self._create_zone()

    def tearDown(self):
        for subnet in self.subnets:
            self.inventory_v1.Subnet.delete(
                {'subnet_id': subnet.subnet_id,
                 'domain_id': self.domain.domain_id},
                metadata=(('token', self.token),)
            )

        if self.network:
            self.inventory_v1.Network.delete(
                {'network_id': self.network.network_id,
                 'domain_id': self.domain.domain_id},
                metadata=(('token', self.token),)
            )

        for ntype in self.network_types:
            self.inventory_v1.NetworkType.delete(
                {'network_type_id': ntype.network_type_id,
                 'domain_id': self.domain.domain_id},
                metadata=(('token', self.token),)
            )

        for npolicy in self.network_polcies:
            self.inventory_v1.NetworkPolicy.delete(
                {'network_policy_id': npolicy.network_policy_id,
                 'domain_id': self.domain.domain_id},
                metadata=(('token', self.token),)
            )

        for project in self.projects:
            self.identity_v1.Project.delete(
                {'project_id': project.project_id,
                 'domain_id': self.domain.domain_id},
                metadata=(('token', self.token),)
            )

        if self.project_group:
            self.identity_v1.ProjectGroup.delete(
                {'project_group_id': self.project_group.project_group_id,
                 'domain_id': self.domain.domain_id},
                metadata=(('token', self.token),)
            )

        if self.zone:
            self.inventory_v1.Zone.delete({'zone_id': self.zone.zone_id,
                                           'domain_id': self.domain.domain_id},
                                          metadata=(('token', self.token),))

        if self.region:
            self.inventory_v1.Region.delete({'region_id': self.region.region_id,
                                             'domain_id': self.domain.domain_id},
                                            metadata=(('token', self.token),))

    def _create_region(self, name=None):
        if not name:
            name = random_string()

        params = {
            'name': name,
            'domain_id': self.domain.domain_id
        }

        self.region = self.inventory_v1.Region.create(params,
                                                      metadata=(('token', self.token),)
                                                      )

    def _create_zone(self, name=None):
        self._create_region()

        if not name:
            name = random_string()

        params = {
            'name': name,
            'region_id': self.region.region_id,
            'domain_id': self.domain.domain_id
        }

        self.zone = self.inventory_v1.Zone.create(params,
                                                  metadata=(('token', self.token),)
                                                  )

    def _create_network_type(self):
        params = {
            'name': random_string(),
            'domain_id': self.domain.domain_id
        }

        self.network_type = self.inventory_v1.NetworkType.create(params,
                                                                 metadata=(('token', self.token),)
                                                                 )

        self.network_types.append(self.network_type)

    def _create_network_policy(self, name=None):
        if not name:
            name = random_string()

        params = {
            'name': name,
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }

        self.network_policy = self.inventory_v1.NetworkPolicy.create(params,
                                                                     metadata=(('token', self.token),)
                                                                     )

        self.network_polcies.append(self.network_policy)

    def _create_project_group(self, name=None):
        if not name:
            name = random_string()

        params = {
            'name': name,
            'domain_id': self.domain.domain_id
        }

        self.project_group = self.identity_v1.ProjectGroup.create(params,
                                                                  metadata=(('token', self.token),)
                                                                  )

    def _create_project(self, name=None, project_group_id=None):
        if not project_group_id:
            self._create_project_group()
            project_group_id = self.project_group.project_group_id

        if not name:
            name = random_string()

        params = {
            'name': name,
            'project_group_id': project_group_id,
            'domain_id': self.domain.domain_id
        }

        self.project = self.identity_v1.Project.create(params,
                                                       metadata=(('token', self.token),)
                                                      )

        self.projects.append(self.project)

    def _create_network(self, name=None, cidr=None):
        if not name:
            name = random_string()

        if not cidr:
            cidr = '172.16.0.0/16'

        params = {
            'name': name,
            'cidr': cidr,
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }

        self.network = self.inventory_v1.Network.create(params,
                                                        metadata=(('token', self.token),)
                                                        )

    def test_create_subnet(self, name=None, cidr=None, ip_ranges=None, network_id=None, network_cidr=None,
                           network_type_id=None, network_policy_id=None, gateway=None, vlan=None, project_id=None, data=None):

        if not name:
            name = random_string()

        if not cidr:
            cidr = '172.16.1.0/24'

        if not network_id:
            self._create_network(cidr=network_cidr)
            network_id = self.network.network_id

        if not network_type_id:
            self._create_network_type()
            network_type_id = self.network_type.network_type_id

        params = {
            'name': name,
            'cidr': cidr,
            'network_id': network_id,
            'network_type_id': network_type_id,
            'domain_id': self.domain.domain_id
        }

        if ip_ranges is not None:
            params.update({
                'ip_ranges': ip_ranges
            })

        if network_policy_id is not None:
            params.update({
                'network_policy_id': network_policy_id
            })

        if gateway is not None:
            params.update({
                'gateway': gateway
            })

        if vlan is not None:
            params.update({
                'vlan': vlan
            })

        if project_id is not None:
            params.update({
                'project_id': project_id
            })

        if data is not None:
            params.update({
                'data': data
            })

        self.subnet = self.inventory_v1.Subnet.create(params,
                                                      metadata=(('token', self.token),))

        self.subnets.append(self.subnet)
        self.assertEqual(self.subnet.cidr, cidr)

    def test_create_subnet_with_cidr(self):
        cidr = '172.16.1.0/25'
        self.test_create_subnet(cidr=cidr)
        self.assertEqual(self.subnet.cidr, cidr)

    def test_create_subnet_with_ip_ranges(self):
        cidr = '172.16.1.0/25'
        ip_ranges = [{'start': '172.16.1.1', 'end': '172.16.1.2'}]

        self.test_create_subnet(cidr=cidr, ip_ranges=ip_ranges)
        self.assertEqual(self.subnet.cidr, cidr)

    def test_create_subnet_with_ip_ranges_2(self):
        cidr = '172.16.1.0/25'
        ip_ranges = [{'start': '172.16.1.1', 'end': '172.16.1.2'},
                     {'start': '172.16.1.10', 'end': '172.16.1.20'}]

        self.test_create_subnet(cidr=cidr, ip_ranges=ip_ranges)
        self.assertEqual(self.subnet.cidr, cidr)

    def test_create_subnet_with_vlan(self):
        vlan = 100

        self.test_create_subnet(vlan=vlan)
        self.assertEqual(self.subnet.vlan, vlan)

    def test_create_subnet_with_gw(self):
        gw = "172.16.1.254"

        self.test_create_subnet(gateway=gw)
        self.assertEqual(self.subnet.gateway, gw)

    def test_create_subnet_with_npolicy(self):
        self._create_network_policy()

        self.test_create_subnet(network_policy_id=self.network_policy.network_policy_id)
        self.assertEqual(self.subnet.network_policy_info.network_policy_id, self.network_policy.network_policy_id)

    def test_create_subnet_with_project(self):
        self._create_project()

        self.test_create_subnet(project_id=self.project.project_id)
        self.assertEqual(self.subnet.project_id, self.project.project_id)

    def test_create_subnet_with_data(self):
        data = {
            'vpc': {
                'vpc_id': 'vpc-xxxxxxx'
            }
        }
        self.test_create_subnet(data=data)
        self.assertEqual(MessageToDict(self.subnet.data), data)

    def test_create_subnet_invalid_cidr(self):
        with self.assertRaises(Exception):
            self.test_create_subnet(cidr='192.168.1.0/24')

    def test_create_subnet_invalid_network(self):
        with self.assertRaises(Exception):
            self.test_create_subnet(network_id='test')

    def test_create_subnet_invalid_ip_range(self):
        ip_ranges = [{'start': '192.168.0.1', 'end': '192.168.0.2'}]
        with self.assertRaises(Exception):
            self.test_create_subnet(ip_ranges=ip_ranges)

    def test_create_subnet_invalid_ip_range_2(self):
        cidr = '172.16.1.0/25'
        ip_ranges = [{'start': '172.16.1.10', 'end': '172.16.1.129'}]
        with self.assertRaises(Exception):
            self.test_create_subnet(cidr=cidr, ip_ranges=ip_ranges)

    def test_create_subnet_invalid_ip_range_3(self):
        cidr = '172.16.1.0/25'
        ip_ranges = [{'start': '172.16.1.1', 'end': '172.16.1.2'},
                     {'start': '172.16.1.11', 'end': '172.16.1.290'}]

        with self.assertRaises(Exception):
            self.test_create_subnet(cidr=cidr, ip_ranges=ip_ranges)

    def test_create_subnet_invalid_ip_range_4(self):
        cidr = '172.16.1.0/25'
        ip_ranges = [{'start': '172.16.1.1', 'end': '172.16.1.2'},
                     {'start': '172.16.1.20', 'end': '172.16.1.9'}]

        with self.assertRaises(Exception):
            self.test_create_subnet(cidr=cidr, ip_ranges=ip_ranges)

    def test_create_subnet_invalid_vlan(self):
        vlan = 20000
        with self.assertRaises(Exception):
            self.test_create_subnet(vlan=vlan)

    def test_create_subnet_invalid_gw(self):
        gw = "172.16.1.22222"
        with self.assertRaises(Exception):
            self.test_create_subnet(gateway=gw)

    def test_create_subnet_invalid_npolicy(self):
        npolicy = 'test'
        with self.assertRaises(Exception):
            self.test_create_subnet(network_policy_id=npolicy)

    def test_create_subnet_invalid_project(self):
        project = 'test'
        with self.assertRaises(Exception):
            self.test_create_subnet(project_id=project)

    def test_create_subnet_duplicate_cidr(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)
        self.test_create_subnet(cidr='172.16.2.0/24', network_id=self.network.network_id)
        self.test_create_subnet(cidr='172.16.3.0/24', network_id=self.network.network_id)

        with self.assertRaises(Exception):
            self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)

    def test_create_subnet_duplicate_cidr_2(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)
        self.test_create_subnet(cidr='172.16.2.0/24', network_id=self.network.network_id)
        self.test_create_subnet(cidr='172.16.3.0/24', network_id=self.network.network_id)

        with self.assertRaises(Exception):
            self.test_create_subnet(cidr='172.16.1.0/25', network_id=self.network.network_id)

    def test_update_subnet_name(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)

        name = 'test-update-subnet'
        param = {
            'subnet_id': self.subnet.subnet_id,
            'name': name,
            'domain_id': self.domain.domain_id
        }
        self.subnet = self.inventory_v1.Subnet.update(param,
                                                      metadata=(('token', self.token),)
                                                      )
        self.assertEqual(self.subnet.name, name)

    def test_update_subnet_ip_ranges(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)

        ip_ranges = [
            {'start': '172.16.1.1', 'end': '172.16.1.10'}
        ]

        param = {
            'subnet_id': self.subnet.subnet_id,
            'ip_ranges': ip_ranges,
            'domain_id': self.domain.domain_id
        }

        self.subnet = self.inventory_v1.Subnet.update(param,
                                                      metadata=(('token', self.token),)

                                                      )

        self.assertEqual(self.subnet.subnet_id, self.subnet.subnet_id)

    def test_update_subnet_tags(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)

        tags = {
            random_string(): random_string(),
            random_string(): random_string()
        }

        param = {'subnet_id': self.subnet.subnet_id,
                 'tags': tags,
                 'domain_id': self.domain.domain_id,
                 }
        self.subnet = self.inventory_v1.Subnet.update(param,
                                                      metadata=(('token', self.token),)
                                                      )
        self.assertEqual(MessageToDict(self.subnet.tags), tags)

    def test_update_subnet_data(self):
        self.test_create_subnet()

        data = {
            'vpc': {
                'vpc_id': 'vpc-xxxxxxx'
            }
        }

        param = {
            'subnet_id': self.subnet.subnet_id,
            'data': data,
            'domain_id': self.domain.domain_id,
        }
        self.subnet = self.inventory_v1.Subnet.update(param,
                                                      metadata=(('token', self.token),)
                                                      )
        self.assertEqual(MessageToDict(self.subnet.data), data)

    def test_update_subnet_gw(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)

        gw = '172.16.1.254'

        param = {
            'subnet_id': self.subnet.subnet_id,
            'gateway': gw,
            'domain_id': self.domain.domain_id
        }

        self.subnet = self.inventory_v1.Subnet.update(param,
                                                      metadata=(('token', self.token),)
                                                      )
        self.assertEqual(self.subnet.gateway, gw)

    def test_update_subnet_vlan(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)

        vlan = 1000

        param = {
            'subnet_id': self.subnet.subnet_id,
            'vlan': vlan,
            'domain_id': self.domain.domain_id
        }

        self.subnet = self.inventory_v1.Subnet.update(param,
                                                      metadata=(('token', self.token),)
                                                      )
        self.assertEqual(self.subnet.vlan, vlan)

    def test_update_subnet_network_type(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)
        self._create_network_type()

        param = {
            'subnet_id': self.subnet.subnet_id,
            'network_type_id': self.network_type.network_type_id,
            'domain_id': self.domain.domain_id
        }

        self.subnet = self.inventory_v1.Subnet.update(param,
                                                      metadata=(('token', self.token),)
                                                      )
        self.assertEqual(self.subnet.network_type_info.network_type_id, self.network_type.network_type_id)

    def test_update_subnet_network_policy(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)
        self._create_network_policy()

        param = {
            'subnet_id': self.subnet.subnet_id,
            'network_policy_id': self.network_policy.network_policy_id,
            'domain_id': self.domain.domain_id
        }

        self.subnet = self.inventory_v1.Subnet.update(param,
                                                      metadata=(('token', self.token),)
                                                      )
        self.assertEqual(self.subnet.network_policy_info.network_policy_id, self.network_policy.network_policy_id)

    def test_update_subnet_project(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)
        self._create_project()

        param = {
            'subnet_id': self.subnet.subnet_id,
            'project_id': self.project.project_id,
            'domain_id': self.domain.domain_id
        }

        self.subnet = self.inventory_v1.Subnet.update(param,
                                                      metadata=(('token', self.token),)
                                                      )
        self.assertEqual(self.subnet.project_id, self.project.project_id)

    def test_update_subnet_release_project(self):
        self._create_project()
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24',
                                network_id=self.network.network_id,
                                project_id=self.project.project_id)

        param = {
            'subnet_id': self.subnet.subnet_id,
            'release_project': True,
            'domain_id': self.domain.domain_id
        }

        self.subnet = self.inventory_v1.Subnet.update(param,
                                                      metadata=(('token', self.token),)
                                                      )
        self.assertEqual(self.subnet.project_id, '')

    def test_get_subnet(self):
        name = 'test-subnet'
        self.test_create_subnet(name=name)

        param = {
            'subnet_id': self.subnet.subnet_id,
            'domain_id': self.domain.domain_id
        }
        self.subnet = self.inventory_v1.Subnet.get(param,
                                                   metadata=(('token', self.token),)
                                                   )

        self.assertEqual(self.subnet.name, name)

    def test_list_subnet_id(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)
        self.test_create_subnet(cidr='172.16.2.0/24', network_id=self.network.network_id)
        self.test_create_subnet(cidr='172.16.3.0/24', network_id=self.network.network_id)

        param = {
            'subnet_id': self.subnet.subnet_id,
            'domain_id': self.domain.domain_id
        }

        subnet = self.inventory_v1.Subnet.list(param, metadata=(('token', self.token),))

        self.assertEqual(1, subnet.total_count)

    def test_list_subnet_name(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)

        name = 'test-subnet'
        self.test_create_subnet(name=name, cidr='172.16.2.0/24', network_id=self.network.network_id)
        self.test_create_subnet(name=name, cidr='172.16.3.0/24', network_id=self.network.network_id)

        param = {
            'name': self.subnet.name,
            'domain_id': self.domain.domain_id
        }

        subnet = self.inventory_v1.Subnet.list(param, metadata=(('token', self.token),))

        self.assertEqual(2, subnet.total_count)

    def test_list_subnet_network_id(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)
        self.test_create_subnet(cidr='172.16.2.0/24', network_id=self.network.network_id)
        self.test_create_subnet(cidr='172.16.3.0/24', network_id=self.network.network_id)

        param = {
            'network_id': self.network.network_id,
            'domain_id': self.domain.domain_id
        }

        subnet = self.inventory_v1.Subnet.list(param, metadata=(('token', self.token),))

        self.assertEqual(3, subnet.total_count)

    def test_list_subnet_zone_id(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)
        self.test_create_subnet(cidr='172.16.2.0/24', network_id=self.network.network_id)
        self.test_create_subnet(cidr='172.16.3.0/24', network_id=self.network.network_id)

        param = {
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }

        subnet = self.inventory_v1.Subnet.list(param, metadata=(('token', self.token),))

        self.assertEqual(3, subnet.total_count)

    def test_list_subnets_query(self):
        self._create_network(cidr='172.16.0.0/16')
        self.test_create_subnet(cidr='172.16.1.0/24', network_id=self.network.network_id)
        self.test_create_subnet(cidr='172.16.2.0/24', network_id=self.network.network_id)
        self.test_create_subnet(cidr='172.16.3.0/24', network_id=self.network.network_id)

        param = {
            'domain_id': self.domain.domain_id,
            'query': {
                'filter': [
                    {
                        'k': 'network_id',
                        'v': self.network.network_id,
                        'o': 'eq'
                    }
                ]
            }
        }

        subnets = self.inventory_v1.Subnet.list(param, metadata=(('token', self.token),))
        self.assertEqual(3, subnets.total_count)

    def test_stat_subnet(self):
        self.test_list_subnets_query()

        params = {
            'domain_id': self.domain.domain_id,
            'query': {
                'aggregate': {
                    'group': {
                        'keys': [{
                            'key': 'subnet_id',
                            'name': 'Id'
                        }],
                        'fields': [{
                            'operator': 'count',
                            'name': 'Count'
                        }]
                    }
                },
                'sort': {
                    'name': 'Count',
                    'desc': True
                }
            }
        }

        result = self.inventory_v1.Subnet.stat(
            params, metadata=(('token', self.token),))

        print(result)
Exemplo n.º 5
0
class TestNetworkType(unittest.TestCase):
    config = config.load_config(
        os.environ.get('SPACEONE_TEST_CONFIG_FILE', './config.yml'))

    identity_v1 = None
    inventory_v1 = None
    domain = None
    domain_owner = None
    owner_id = None
    owner_pw = None
    token = None

    @classmethod
    def setUpClass(cls):
        super(TestNetworkType, cls).setUpClass()
        endpoints = cls.config.get('ENDPOINTS', {})
        cls.identity_v1 = pygrpc.client(endpoint=endpoints.get('identity', {}).get('v1'), version='v1')
        cls.inventory_v1 = pygrpc.client(endpoint=endpoints.get('inventory', {}).get('v1'), version='v1')

        cls._create_domain()
        cls._create_domain_owner()
        cls._issue_owner_token()

    @classmethod
    def tearDownClass(cls):
        super(TestNetworkType, cls).tearDownClass()
        cls.identity_v1.DomainOwner.delete({
            'domain_id': cls.domain.domain_id,
            'owner_id': cls.owner_id
        })

        if cls.domain:
            cls.identity_v1.Domain.delete({'domain_id': cls.domain.domain_id})

    @classmethod
    def _create_domain(cls):
        name = utils.random_string()
        param = {
            'name': name,
            'tags': {utils.random_string(): utils.random_string(), utils.random_string(): utils.random_string()},
            'config': {
                'aaa': 'bbbb'
            }
        }

        cls.domain = cls.identity_v1.Domain.create(param)
        print(f'domain_id: {cls.domain.domain_id}')
        print(f'domain_name: {cls.domain.name}')

    @classmethod
    def _create_domain_owner(cls):
        cls.owner_id = utils.random_string()[0:10]
        cls.owner_pw = 'qwerty'

        param = {
            'owner_id': cls.owner_id,
            'password': cls.owner_pw,
            'name': 'Steven' + utils.random_string()[0:5],
            'timezone': 'utc+9',
            'email': 'Steven' + utils.random_string()[0:5] + '@mz.co.kr',
            'mobile': '+821026671234',
            'domain_id': cls.domain.domain_id
        }

        owner = cls.identity_v1.DomainOwner.create(
            param
        )
        cls.domain_owner = owner
        print(f'owner_id: {cls.owner_id}')
        print(f'owner_pw: {cls.owner_pw}')

    @classmethod
    def _issue_owner_token(cls):
        token_param = {
            'credentials': {
                'user_type': 'DOMAIN_OWNER',
                'user_id': cls.owner_id,
                'password': cls.owner_pw
            },
            'domain_id': cls.domain.domain_id
        }

        issue_token = cls.identity_v1.Token.issue(token_param)
        cls.token = issue_token.access_token
        print(f'token: {cls.token}')

    def setUp(self):
        self.network_types = []
        self.network_type = None

    def tearDown(self):
        for network_type in self.network_types:
            self.inventory_v1.NetworkType.delete(
                {'network_type_id': network_type.network_type_id,
                 'domain_id': self.domain.domain_id},
                metadata=(('token', self.token),)
            )

    def test_create_network_type(self, name=None):
        if not name:
            name = random_string()

        params = {
            'name': name,
            'domain_id': self.domain.domain_id
        }

        self.network_type = self.inventory_v1.NetworkType.create(params,
                                                          metadata=(('token', self.token),)
                                                          )

        self.network_types.append(self.network_type)
        self.assertEqual(self.network_type.name, name)

    def test_update_network_type_name(self):
        self.test_create_network_type()

        name = random_string()
        param = { 'network_type_id': self.network_type.network_type_id,
                  'name': name,
                  'domain_id': self.domain.domain_id,
                }
        self.network_type = self.inventory_v1.NetworkType.update(param,
                                                                 metadata=(('token', self.token),)
                                                                 )
        self.assertEqual(self.network_type.name, name)

    def test_update_network_type_tags(self):
        self.test_create_network_type()

        tags = {
            random_string(): random_string(),
            random_string(): random_string()
        }
        param = { 'network_type_id': self.network_type.network_type_id,
                  'tags': tags,
                  'domain_id': self.domain.domain_id,
                }
        self.network_type = self.inventory_v1.NetworkType.update(param,
                                                                 metadata=(('token', self.token),)
                                                                 )
        self.assertEqual(MessageToDict(self.network_type.tags), tags)

    def test_get_network_type(self):
        name = 'test-ntype'
        self.test_create_network_type(name)

        param = {
            'network_type_id': self.network_type.network_type_id,
            'domain_id': self.domain.domain_id
        }
        self.network_type = self.inventory_v1.NetworkType.get(param,
                                                              metadata=(('token', self.token),)
                                                              )
        self.assertEqual(self.network_type.name, name)

    def test_list_network_type_id(self):
        self.test_create_network_type()
        self.test_create_network_type()

        param = {
            'network_type_id': self.network_type.network_type_id,
            'domain_id': self.domain.domain_id
        }

        network_types = self.inventory_v1.NetworkType.list(param, metadata=(('token', self.token),))

        self.assertEqual(1, network_types.total_count)

    def test_list_name(self):
        self.test_create_network_type()
        self.test_create_network_type()

        param = {
            'name': self.network_type.name,
            'domain_id': self.domain.domain_id
        }

        network_types = self.inventory_v1.NetworkType.list(param, metadata=(('token', self.token),))

        self.assertEqual(1, network_types.total_count)

    def test_list_query(self):
        self.test_create_network_type()
        self.test_create_network_type()
        self.test_create_network_type()

        param = {
            'domain_id': self.domain.domain_id,
            'query': {
                'filter': [
                    {
                        'k': 'network_type_id',
                        'v': list(map(lambda ntype: ntype.network_type_id, self.network_types)),
                        'o': 'in'
                    }
                ]
            }
        }

        ntypes = self.inventory_v1.NetworkType.list(param, metadata=(('token', self.token),))
        self.assertEqual(len(self.network_types), ntypes.total_count)

    def test_stat_network_type(self):
        self.test_list_query()

        params = {
            'domain_id': self.domain.domain_id,
            'query': {
                'aggregate': {
                    'group': {
                        'keys': [{
                            'key': 'network_type_id',
                            'name': 'Id'
                        }],
                        'fields': [{
                            'operator': 'count',
                            'name': 'Count'
                        }]
                    }
                },
                'sort': {
                    'name': 'Count',
                    'desc': True
                }
            }
        }

        result = self.inventory_v1.NetworkType.stat(
            params, metadata=(('token', self.token),))

        print(result)
Exemplo n.º 6
0
class TestIP(unittest.TestCase):
    config = config.load_config(
        os.environ.get('SPACEONE_TEST_CONFIG_FILE', './config.yml'))

    identity_v1 = None
    inventory_v1 = None
    domain = None
    domain_owner = None
    owner_id = None
    owner_pw = None
    token = None
    region = None
    zone = None
    api_key_obj = None

    @classmethod
    def setUpClass(cls):
        super(TestIP, cls).setUpClass()
        endpoints = cls.config.get('ENDPOINTS', {})
        cls.identity_v1 = pygrpc.client(endpoint=endpoints.get('identity', {}).get('v1'), version='v1')
        cls.inventory_v1 = pygrpc.client(endpoint=endpoints.get('inventory', {}).get('v1'), version='v1')

        cls._create_domain()
        cls._create_domain_owner()
        cls._issue_owner_token()

    @classmethod
    def tearDownClass(cls):
        super(TestIP, cls).tearDownClass()

        cls.identity_v1.DomainOwner.delete({
            'domain_id': cls.domain.domain_id,
            'owner_id': cls.owner_id
        })

        if cls.domain:
            cls.identity_v1.Domain.delete({'domain_id': cls.domain.domain_id})

    @classmethod
    def _create_domain(cls):
        name = utils.random_string()
        param = {
            'name': name,
            'tags': {utils.random_string(): utils.random_string(), utils.random_string(): utils.random_string()},
            'config': {
                'aaa': 'bbbb'
            }
        }

        cls.domain = cls.identity_v1.Domain.create(param)
        print(f'domain_id: {cls.domain.domain_id}')
        print(f'domain_name: {cls.domain.name}')

    @classmethod
    def _create_domain_owner(cls):
        cls.owner_id = utils.random_string()[0:10]
        cls.owner_pw = 'qwerty'

        param = {
            'owner_id': cls.owner_id,
            'password': cls.owner_pw,
            'name': 'Steven' + utils.random_string()[0:5],
            'timezone': 'utc+9',
            'email': 'Steven' + utils.random_string()[0:5] + '@mz.co.kr',
            'mobile': '+821026671234',
            'domain_id': cls.domain.domain_id
        }

        owner = cls.identity_v1.DomainOwner.create(
            param
        )
        cls.domain_owner = owner
        print(f'owner_id: {cls.owner_id}')
        print(f'owner_pw: {cls.owner_pw}')

    @classmethod
    def _issue_owner_token(cls):
        token_param = {
            'credentials': {
                'user_type': 'DOMAIN_OWNER',
                'user_id': cls.owner_id,
                'password': cls.owner_pw
            },
            'domain_id': cls.domain.domain_id
        }

        issue_token = cls.identity_v1.Token.issue(token_param)
        cls.token = issue_token.access_token
        print(f'token: {cls.token}')

    def setUp(self):
        self.network = None
        self.network_type = None
        self.subnet = None
        self.ip = None
        self.ips = []

        self._create_zone()

    def tearDown(self):
        for ip in self.ips:
            self.inventory_v1.IPAddress.release(
                {'ip_address': ip.ip_address,
                 'subnet_id': self.subnet.subnet_id,
                 'domain_id': self.domain.domain_id},
                metadata=(('token', self.token),)
            )

        self.inventory_v1.Subnet.delete(
            {'subnet_id': self.subnet.subnet_id,
             'domain_id': self.domain.domain_id},
            metadata=(('token', self.token),)
            )

        self.inventory_v1.Network.delete({'network_id': self.network.network_id,
                                          'domain_id': self.domain.domain_id},
                                         metadata=(('token', self.token),))

        self.inventory_v1.NetworkType.delete({'network_type_id': self.network_type.network_type_id,
                                              'domain_id': self.domain.domain_id},
                                             metadata=(('token', self.token),))

        if self.zone:
            self.inventory_v1.Zone.delete({'zone_id': self.zone.zone_id,
                                           'domain_id': self.domain.domain_id},
                                          metadata=(('token', self.token),))

        if self.region:
            self.inventory_v1.Region.delete({'region_id': self.region.region_id,
                                             'domain_id': self.domain.domain_id},
                                            metadata=(('token', self.token),))

    def _create_region(self, name=None):
        if not name:
            name = random_string()

        params = {
            'name': name,
            'domain_id': self.domain.domain_id
        }

        self.region = self.inventory_v1.Region.create(params,
                                                      metadata=(('token', self.token),)
                                                      )

    def _create_zone(self, name=None):
        self._create_region()

        if not name:
            name = random_string()

        params = {
            'name': name,
            'region_id': self.region.region_id,
            'domain_id': self.domain.domain_id
        }

        self.zone = self.inventory_v1.Zone.create(params,
                                                  metadata=(('token', self.token),)
                                                  )

    def _create_network_type(self):
        params = {
            'name': random_string(),
            'domain_id': self.domain.domain_id
        }

        self.network_type = self.inventory_v1.NetworkType.create(params,
                                                                 metadata=(('token', self.token),)
                                                                 )

    def _create_network(self, name=None, cidr=None):
        if not name:
            name = random_string()

        if not cidr:
            cidr = '172.16.0.0/16'

        params = {
            'name': name,
            'cidr': cidr,
            'zone_id': self.zone.zone_id,
            'domain_id': self.domain.domain_id
        }

        self.network = self.inventory_v1.Network.create(params,
                                                        metadata=(('token', self.token),)
                                                        )

    def _create_subnet(self, name=None, cidr=None, ip_ranges=None, network_id=None, network_cidr=None,
                       network_type_id=None, gateway=None, vlan=None, data=None):

        if not name:
            name = random_string()

        if not cidr:
            cidr = '172.16.1.0/24'

        if not network_type_id:
            self._create_network_type()
            network_type_id = self.network_type.network_type_id

        if not network_id:
            self._create_network(cidr=network_cidr)
            network_id = self.network.network_id

        params = {
            'name': name,
            'cidr': cidr,
            'network_id': network_id,
            'network_type_id': network_type_id,
            'domain_id': self.domain.domain_id
        }

        if ip_ranges is not None:
            params.update({
                'ip_ranges': ip_ranges
            })

        if gateway is not None:
            params.update({
                'gateway': gateway
            })

        if vlan is not None:
            params.update({
                'vlan': vlan
            })

        if data is not None:
            params.update({
                'data': data
            })

        self.subnet = self.inventory_v1.Subnet.create(params,
                                                      metadata=(('token', self.token),))

    def test_allocate_ip(self, subnet_id=None, ip=None):
        if subnet_id is None:
            self._create_subnet()
            subnet_id = self.subnet.subnet_id

        params = {
            'subnet_id': subnet_id,
            'domain_id': self.domain.domain_id

        }

        if ip is not None:
            params.update({
                'ip_address': ip
            })

        self.ip = self.inventory_v1.IPAddress.allocate(params,
                                                       metadata=(('token', self.token),))

        self.ips.append(self.ip)

        print(self.ip)
        self.assertEqual(self.ip, self.ip)

    def test_allocate_ip_multiple(self, num=10):
        self._create_subnet()

        params = {
            'subnet_id': self.subnet.subnet_id,
            'domain_id': self.domain.domain_id

        }

        for i in range(num):
            self.ip = self.inventory_v1.IPAddress.allocate(params,
                                                           metadata=(('token', self.token),))
            print(self.ip.ip_address)
            self.ips.append(self.ip)

        self.assertEqual(self.ip, self.ip)

    def test_allocate_ip_2(self):
        self.test_allocate_ip_multiple(num=5)
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.7')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.9')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.10')

        self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.6
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        ip = self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.8
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        ip = self.test_allocate_ip(subnet_id=self.subnet.subnet_id) # 172.16.1.11
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        self.assertEqual(self.ip, self.ip)

    def test_allocate_ip_3(self):
        # Allocate
        self.test_allocate_ip_multiple(num=5)
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.7')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.9')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.10')

        # Reserve
        self.test_reserve_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.12')

        self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.6
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        ip = self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.8
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        ip = self.test_allocate_ip(subnet_id=self.subnet.subnet_id) # 172.16.1.11
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        ip = self.test_allocate_ip(subnet_id=self.subnet.subnet_id) # 172.16.1.13
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        self.assertEqual(self.ip, self.ip)

    def test_allocate_ip_4(self):
        # Allocate
        ip_ranges = [
            {'start': '172.16.1.10',
             'end': '172.16.1.20'}
        ]

        self._create_subnet(ip_ranges=ip_ranges)
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.10
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.11
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.12
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.13
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.14
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        self.assertEqual(self.ip, self.ip)

    def test_allocate_ip_full(self):
        # Allocate
        ip_ranges = [
            {'start': '172.16.1.10',
             'end': '172.16.1.13'}
        ]

        self._create_subnet(ip_ranges=ip_ranges)
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.10
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.11
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.12
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.13
        print("------------")
        print(self.ip.ip_address)
        print("------------")

        with self.assertRaises(Exception):
            self.test_allocate_ip(subnet_id=self.subnet.subnet_id)  # 172.16.1.14

    def test_allocate_ip_resource(self):
        self._create_subnet()

        resource = {
            'type': 'server',
            'id': 'server-xxxxx'
        }

        params = {
            'subnet_id': self.subnet.subnet_id,
            'domain_id': self.domain.domain_id,
            'resource': resource
        }
        self.ip = self.inventory_v1.IPAddress.allocate(params,
                                                       metadata=(('token', self.token),))

        self.ips.append(self.ip)
        self.assertEqual(MessageToDict(self.ip.resource), resource)

    def test_allocate_ip_static(self):
        self._create_subnet()

        ip = '172.16.1.10'
        params = {
            'ip_address': ip,
            'subnet_id': self.subnet.subnet_id,
            'domain_id': self.domain.domain_id

        }
        self.ip = self.inventory_v1.IPAddress.allocate(params,
                                                       metadata=(('token', self.token),))

        self.ips.append(self.ip)
        self.assertEqual(self.ip.ip_address, ip)

    def test_allocate_ip_static_duplicate(self):
        self._create_subnet()

        ip = '172.16.1.10'
        params = {
            'ip_address': ip,
            'subnet_id': self.subnet.subnet_id,
            'domain_id': self.domain.domain_id

        }
        self.ip = self.inventory_v1.IPAddress.allocate(params,
                                                       metadata=(('token', self.token),))

        self.ips.append(self.ip)

        with self.assertRaises(Exception):
            self.inventory_v1.IPAddress.allocate(params,
                                       metadata=(('token', self.token),))

    def test_allocate_ip_reserve_duplicate(self):
        self._create_subnet()

        ip = '172.16.1.11'
        params = {
            'ip_address': ip,
            'subnet_id': self.subnet.subnet_id,
            'domain_id': self.domain.domain_id

        }
        self.ip = self.inventory_v1.IPAddress.reserve(params, metadata=(('token', self.token),))
        self.ip = self.inventory_v1.IPAddress.allocate(params, metadata=(('token', self.token),))
        self.ips.append(self.ip)

        self.assertEqual(self.ip.state, 1)

    def test_allocate_ip_data(self):
        self._create_subnet()

        data = {
            'ec2': {
                'instance_id': 'i-xxxxxxxx'
            }
        }

        params = {
            'subnet_id': self.subnet.subnet_id,
            'domain_id': self.domain.domain_id,
            'data': data

        }
        self.ip = self.inventory_v1.IPAddress.allocate(params,
                                                       metadata=(('token', self.token),))

        self.ips.append(self.ip)
        self.assertEqual(MessageToDict(self.ip.data), data)

    def test_allocate_ip_duplicate(self):
        self.test_allocate_ip()

        ip = self.ip.ip_address
        params = {
            'ip_address': ip,
            'subnet_id': self.subnet.subnet_id,
            'domain_id': self.domain.domain_id

        }

        with self.assertRaises(Exception):
            self.inventory_v1.IPAddress.allocate(params, metadata=(('token', self.token),))

    def test_reserve_ip(self, subnet_id=None, ip=None):
        if subnet_id is None:
            self._create_subnet()
            subnet_id = self.subnet.subnet_id

        if ip is None:
            ip = '172.16.1.1'

        params = {
            'ip_address': ip,
            'subnet_id': subnet_id,
            'domain_id': self.domain.domain_id

        }

        self.ip = self.inventory_v1.IPAddress.reserve(params,
                                                      metadata=(('token', self.token),))

        self.ips.append(self.ip)
        self.assertEqual(self.ip.state, 2)

    def test_update_ip_resource(self):
        self._create_subnet()

        ip = '172.16.1.10'
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip=ip)

        resource = {
            'type': 'server',
            'id': 'server-xxxxx'
        }

        params = {
            'ip_address': ip,
            'subnet_id': self.subnet.subnet_id,
            'domain_id': self.domain.domain_id,
            'resource': resource
        }
        self.ip = self.inventory_v1.IPAddress.update(params,
                                                     metadata=(('token', self.token),))

        self.assertEqual(MessageToDict(self.ip.resource), resource)

    def test_update_ip_data(self):
        self._create_subnet()

        ip = '172.16.1.10'
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip=ip)

        data = {
            'xxxxx': 'bbbb',
            'yyyyyy': 'zzzz',
            'aaaaa': {
                'bbbbb': 'cccccc'
            }
        }

        params = {
            'ip_address': ip,
            'subnet_id': self.subnet.subnet_id,
            'domain_id': self.domain.domain_id,
            'data': data
        }
        self.ip = self.inventory_v1.IPAddress.update(params,
                                                     metadata=(('token', self.token),))

        self.assertEqual(MessageToDict(self.ip.data), data)

    def test_get_ip(self):
        self._create_subnet()
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.10')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.11')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.12')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.13')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.14')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.15')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.16')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.17')

        param = {
            'ip_address': '172.16.1.13',
            'subnet_id': self.subnet.subnet_id,
            'domain_id': self.domain.domain_id
        }
        ip = self.inventory_v1.IPAddress.get(param,
                                             metadata=(('token', self.token),))

        self.assertEqual(ip.ip_address, '172.16.1.13')

    def test_list_ips(self):
        self._create_subnet()
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.10')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.11')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.12')
        self.test_allocate_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.13')
        self.test_reserve_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.14')
        self.test_reserve_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.15')
        self.test_reserve_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.16')
        self.test_reserve_ip(subnet_id=self.subnet.subnet_id, ip='172.16.1.17')

        param = {
            'state': 'ALLOCATED',
            'subnet_id': self.subnet.subnet_id,
            'domain_id': self.domain.domain_id
        }
        ips = self.inventory_v1.IPAddress.list(param, metadata=(('token', self.token),))

        self.assertEqual(ips.total_count, 4)

    def test_stat_ip(self):
        self.test_list_ips()

        params = {
            'domain_id': self.domain.domain_id,
            'query': {
                'aggregate': {
                    'group': {
                        'keys': [{
                            'key': 'ip_address',
                            'name': 'Id'
                        }],
                        'fields': [{
                            'operator': 'count',
                            'name': 'Count'
                        }]
                    }
                },
                'sort': {
                    'name': 'Count',
                    'desc': True
                }
            }
        }

        result = self.inventory_v1.IPAddress.stat(
            params, metadata=(('token', self.token),))

        print(result)
Exemplo n.º 7
0
class TestCloudServiceType(unittest.TestCase):
    config = config.load_config(
        os.environ.get('SPACEONE_TEST_CONFIG_FILE', './config.yml'))

    pp = pprint.PrettyPrinter(indent=4)
    identity_v1 = None
    inventory_v1 = None
    domain = None
    domain_owner = None
    owner_id = None
    owner_pw = None
    token = None

    @classmethod
    def setUpClass(cls):
        super(TestCloudServiceType, cls).setUpClass()
        endpoints = cls.config.get('ENDPOINTS', {})
        cls.identity_v1 = pygrpc.client(endpoint=endpoints.get('identity',
                                                               {}).get('v1'),
                                        version='v1')
        cls.inventory_v1 = pygrpc.client(endpoint=endpoints.get(
            'inventory', {}).get('v1'),
                                         version='v1')

        cls._create_domain()
        cls._create_domain_owner()
        cls._issue_owner_token()

    @classmethod
    def tearDownClass(cls):
        super(TestCloudServiceType, cls).tearDownClass()
        cls.identity_v1.DomainOwner.delete({
            'domain_id': cls.domain.domain_id,
            'owner_id': cls.owner_id
        })

        if cls.domain:
            cls.identity_v1.Domain.delete({'domain_id': cls.domain.domain_id})

    @classmethod
    def _create_domain(cls):
        name = utils.random_string()
        param = {
            'name': name,
            'tags': {
                utils.random_string(): utils.random_string(),
                utils.random_string(): utils.random_string()
            },
            'config': {
                'aaa': 'bbbb'
            }
        }

        cls.domain = cls.identity_v1.Domain.create(param)
        print(f'domain_id: {cls.domain.domain_id}')
        print(f'domain_name: {cls.domain.name}')

    @classmethod
    def _create_domain_owner(cls):
        cls.owner_id = utils.random_string()[0:10]
        cls.owner_pw = 'qwerty'

        param = {
            'owner_id': cls.owner_id,
            'password': cls.owner_pw,
            'name': 'Steven' + utils.random_string()[0:5],
            'timezone': 'utc+9',
            'email': 'Steven' + utils.random_string()[0:5] + '@mz.co.kr',
            'mobile': '+821026671234',
            'domain_id': cls.domain.domain_id
        }

        owner = cls.identity_v1.DomainOwner.create(param)
        cls.domain_owner = owner
        print(f'owner_id: {cls.owner_id}')
        print(f'owner_pw: {cls.owner_pw}')

    @classmethod
    def _issue_owner_token(cls):
        token_param = {
            'credentials': {
                'user_type': 'DOMAIN_OWNER',
                'user_id': cls.owner_id,
                'password': cls.owner_pw
            },
            'domain_id': cls.domain.domain_id
        }

        issue_token = cls.identity_v1.Token.issue(token_param)
        cls.token = issue_token.access_token
        print(f'token: {cls.token}')

    def setUp(self):
        self.cloud_service_type = None
        self.cloud_service_types = []

    def tearDown(self):
        for cloud_svc_type in self.cloud_service_types:
            self.inventory_v1.CloudServiceType.delete(
                {
                    'cloud_service_type_id':
                    cloud_svc_type.cloud_service_type_id,
                    'domain_id': self.domain.domain_id
                },
                metadata=(('token', self.token), ))

    def _print_data(self, message, description=None):
        print()
        if description:
            print(f'[ {description} ]')

        self.pp.pprint(MessageToDict(message,
                                     preserving_proto_field_name=True))

    def test_create_cloud_service_type(self,
                                       name=None,
                                       provider=None,
                                       group=None):
        """ Create Cloud Service Type
        """

        if name is None:
            name = random_string()

        if provider is None:
            provider = random_string()

        if group is None:
            group = random_string()

        params = {
            'name': name,
            'provider': provider,
            'group': group,
            'domain_id': self.domain.domain_id
        }

        self.cloud_service_type = self.inventory_v1.CloudServiceType.create(
            params, metadata=(('token', self.token), ))
        self.cloud_service_types.append(self.cloud_service_type)
        self.assertEqual(self.cloud_service_type.name, name)

    def test_create_cloud_service_type_metadata(self,
                                                name=None,
                                                provider=None,
                                                group=None):
        """ Create Cloud Service Type with data source
        """

        if name is None:
            name = random_string()

        if provider is None:
            provider = random_string()

        if group is None:
            group = random_string()

        params = {
            'name': name,
            'provider': provider,
            'group': group,
            'domain_id': self.domain.domain_id
        }

        self.cloud_service_type = self.inventory_v1.CloudServiceType.create(
            params, metadata=(('token', self.token), ))
        self.cloud_service_types.append(self.cloud_service_type)
        self.assertEqual(self.cloud_service_type.name, name)

    def test_create_cloud_service_type_group(self, name=None, provider=None):
        """ Create Cloud Service Type with group
        """

        if name is None:
            name = random_string()

        if provider is None:
            provider = random_string()

        group = random_string()

        params = {
            'name': name,
            'provider': provider,
            'group': group,
            'domain_id': self.domain.domain_id
        }

        self.cloud_service_type = self.inventory_v1.CloudServiceType.create(
            params, metadata=(('token', self.token), ))
        self.cloud_service_types.append(self.cloud_service_type)
        self.assertEqual(self.cloud_service_type.group, group)

    def test_create_cloud_service_type_labels(self,
                                              name=None,
                                              provider=None,
                                              group=None):
        """ Create Cloud Service Type with group
        """

        if name is None:
            name = random_string()

        if provider is None:
            provider = random_string()

        if group is None:
            group = random_string()

        labels = [random_string(), random_string(), random_string()]

        params = {
            'name': name,
            'provider': provider,
            'group': group,
            'labels': labels,
            'domain_id': self.domain.domain_id
        }

        self.cloud_service_type = self.inventory_v1.CloudServiceType.create(
            params, metadata=(('token', self.token), ))
        self.cloud_service_types.append(self.cloud_service_type)
        self.assertEqual(self.cloud_service_type.labels, labels)

    def test_create_duplicate_cloud_service_type(self):
        name = random_string()
        provider = random_string()
        group = random_string()

        self.test_create_cloud_service_type(name=name,
                                            provider=provider,
                                            group=group)

        with self.assertRaises(Exception):
            self.test_create_cloud_service_type(name=name,
                                                provider=provider,
                                                group=group)

    def test_update_cloud_service_type_label(self):
        self.test_create_cloud_service_type()

        labels = [random_string(), random_string(), random_string()]

        param = {
            'cloud_service_type_id':
            self.cloud_service_type.cloud_service_type_id,
            'labels': labels,
            'domain_id': self.domain.domain_id,
        }

        self.cloud_service_type = self.inventory_v1.CloudServiceType.update(
            param, metadata=(('token', self.token), ))

        for _label in self.cloud_service_type.labels:
            self.assertEqual(_label, labels[0])
            break

    def test_update_cloud_service_type_tags(self):
        self.test_create_cloud_service_type()

        tags = {
            random_string(): random_string(),
            random_string(): random_string()
        }
        param = {
            'cloud_service_type_id':
            self.cloud_service_type.cloud_service_type_id,
            'tags': tags,
            'domain_id': self.domain.domain_id,
        }
        self.cloud_service_type = self.inventory_v1.CloudServiceType.update(
            param, metadata=(('token', self.token), ))
        self.assertEqual(MessageToDict(self.cloud_service_type.tags), tags)

    def test_get_cloud_service_type(self):
        name = 'test-cst'
        self.test_create_cloud_service_type(name=name)

        param = {
            'cloud_service_type_id':
            self.cloud_service_type.cloud_service_type_id,
            'domain_id': self.domain.domain_id
        }
        self.cloud_service_type = self.inventory_v1.CloudServiceType.get(
            param, metadata=(('token', self.token), ))
        self.assertEqual(self.cloud_service_type.name, name)

    def test_list_cloud_service_types(self):
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()

        param = {
            'cloud_service_type_id':
            self.cloud_service_type.cloud_service_type_id,
            'domain_id': self.domain.domain_id
        }

        cloud_service_types = self.inventory_v1.CloudServiceType.list(
            param, metadata=(('token', self.token), ))

        self.assertEqual(1, cloud_service_types.total_count)

    def test_list_cloud_service_types_name(self):
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()

        param = {
            'name': self.cloud_service_type.name,
            'domain_id': self.domain.domain_id
        }

        cloud_svc_types = self.inventory_v1.CloudServiceType.list(
            param, metadata=(('token', self.token), ))

        self.assertEqual(1, cloud_svc_types.total_count)

    def test_list_cloud_service_types_group(self):
        group = random_string()

        self.test_create_cloud_service_type(group=group)
        self.test_create_cloud_service_type(group=group)
        self.test_create_cloud_service_type(group=group)
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()

        param = {'group': group, 'domain_id': self.domain.domain_id}

        cloud_svc_types = self.inventory_v1.CloudServiceType.list(
            param, metadata=(('token', self.token), ))

        self.assertEqual(3, cloud_svc_types.total_count)

    def test_list_query(self):
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()
        self.test_create_cloud_service_type()

        param = {
            'domain_id': self.domain.domain_id,
            'query': {
                'filter': [{
                    'k':
                    'cloud_service_type_id',
                    'v':
                    list(
                        map(
                            lambda cloud_service_type: cloud_service_type.
                            cloud_service_type_id, self.cloud_service_types)),
                    'o':
                    'in'
                }]
            },
            'include_cloud_service_count': True
        }

        cloud_service_types = self.inventory_v1.CloudServiceType.list(
            param, metadata=(('token', self.token), ))

        print(cloud_service_types)
        self.assertEqual(len(self.cloud_service_types),
                         cloud_service_types.total_count)

    def test_stat_cloud_service_type(self):
        self.test_list_query()

        params = {
            'domain_id': self.domain.domain_id,
            'query': {
                'aggregate': {
                    'group': {
                        'keys': [{
                            'key': 'cloud_service_type_id',
                            'name': 'Id'
                        }],
                        'fields': [{
                            'operator': 'count',
                            'name': 'Count'
                        }]
                    }
                },
                'sort': {
                    'name': 'Id',
                    'desc': True
                }
            }
        }

        result = self.inventory_v1.CloudServiceType.stat(
            params, metadata=(('token', self.token), ))

        self._print_data(result, 'test_stat_cloud_service_type')
Exemplo n.º 8
0
class TestCloudService(unittest.TestCase):
    config = config.load_config(
        os.environ.get('SPACEONE_TEST_CONFIG_FILE', './config.yml'))

    pp = pprint.PrettyPrinter(indent=4)
    identity_v1 = None
    inventory_v1 = None
    domain = None
    domain_owner = None
    owner_id = None
    owner_pw = None
    token = None

    @classmethod
    def setUpClass(cls):
        super(TestCloudService, cls).setUpClass()
        endpoints = cls.config.get('ENDPOINTS', {})
        cls.identity_v1 = pygrpc.client(endpoint=endpoints.get('identity', {}).get('v1'), version='v1')
        cls.inventory_v1 = pygrpc.client(endpoint=endpoints.get('inventory', {}).get('v1'), version='v1')

        cls._create_domain()
        cls._create_domain_owner()
        cls._issue_owner_token()

    @classmethod
    def tearDownClass(cls):
        super(TestCloudService, cls).tearDownClass()
        cls.identity_v1.DomainOwner.delete({
            'domain_id': cls.domain.domain_id,
            'owner_id': cls.owner_id
        })

        if cls.domain:
            cls.identity_v1.Domain.delete({'domain_id': cls.domain.domain_id})

    @classmethod
    def _create_domain(cls):
        name = utils.random_string()
        param = {
            'name': name,
            'tags': {utils.random_string(): utils.random_string(), utils.random_string(): utils.random_string()},
            'config': {
                'aaa': 'bbbb'
            }
        }

        cls.domain = cls.identity_v1.Domain.create(param)
        print(f'domain_id: {cls.domain.domain_id}')
        print(f'domain_name: {cls.domain.name}')

    @classmethod
    def _create_domain_owner(cls):
        cls.owner_id = utils.random_string()[0:10]
        cls.owner_pw = 'qwerty'

        param = {
            'owner_id': cls.owner_id,
            'password': cls.owner_pw,
            'name': 'Steven' + utils.random_string()[0:5],
            'timezone': 'utc+9',
            'email': 'Steven' + utils.random_string()[0:5] + '@mz.co.kr',
            'mobile': '+821026671234',
            'domain_id': cls.domain.domain_id
        }

        owner = cls.identity_v1.DomainOwner.create(
            param
        )
        cls.domain_owner = owner
        print(f'owner_id: {cls.owner_id}')
        print(f'owner_pw: {cls.owner_pw}')

    @classmethod
    def _issue_owner_token(cls):
        token_param = {
            'credentials': {
                'user_type': 'DOMAIN_OWNER',
                'user_id': cls.owner_id,
                'password': cls.owner_pw
            },
            'domain_id': cls.domain.domain_id
        }

        issue_token = cls.identity_v1.Token.issue(token_param)
        cls.token = issue_token.access_token
        print(f'token: {cls.token}')

    def setUp(self):
        self.region = None
        self.project_group = None
        self.project = None
        self.cloud_service = None
        self.cloud_services = []

    def tearDown(self):
        for cloud_svc in self.cloud_services:
            self.inventory_v1.CloudService.delete(
                {'cloud_service_id': cloud_svc.cloud_service_id,
                 'domain_id': self.domain.domain_id},
                metadata=(('token', self.token),)
            )

        if self.region is not None:
            self.inventory_v1.Region.delete(
                {'region_id': self.region.region_id,
                 'domain_id': self.domain.domain_id},
                metadata=(('token', self.token),)
            )

        if self.project is not None:
            self.identity_v1.Project.delete(
                {'project_id': self.project.project_id,
                 'domain_id': self.domain.domain_id},
                metadata=(('token', self.token),)
            )

        if self.project_group is not None:
            self.identity_v1.ProjectGroup.delete(
                {'project_group_id': self.project_group.project_group_id,
                 'domain_id': self.domain.domain_id},
                metadata=(('token', self.token),)
            )

    def _create_region(self, name=None):
        """ Create Region
        """

        if not name:
            name = random_string()

        params = {
            'name': name,
            'domain_id': self.domain.domain_id
        }

        self.region = self.inventory_v1.Region.create(params, metadata=(('token', self.token),))

    def _create_project_group(self, name=None):
        """ Create Project Group
        """

        if not name:
            name = random_string()

        params = {
            'name': name,
            'domain_id': self.domain.domain_id
        }

        self.project_group = self.identity_v1.ProjectGroup.create(params, metadata=(('token', self.token),))

    def _create_project(self, name=None, project_group=None):
        """ Create Project
        """

        if not name:
            name = random_string()

        if not project_group:
            self._create_project_group()
            project_group = self.project_group

        params = {
            'name': name,
            'project_group_id': project_group.project_group_id,
            'domain_id': self.domain.domain_id
        }

        self.project = self.identity_v1.Project.create(params, metadata=(('token', self.token),))

    def _print_data(self, message, description=None):
        print()
        if description:
            print(f'[ {description} ]')

        self.pp.pprint(MessageToDict(message, preserving_proto_field_name=True))

    def test_create_cloud_service(self, cloud_service_type=None, provider=None, data=None, group=None,
                                  metadata=None):
        """ Create Cloud Service
        """

        if cloud_service_type is None:
            cloud_service_type = random_string()

        if provider is None:
            provider = random_string()

        if group is None:
            group = random_string()

        if data is None:
            data = {
                random_string(): random_string(),
                random_string(): random_string(),
                random_string(): random_string()
            }

        if metadata is None:
            metadata = {
                'view': {
                    'sub_data': {
                        "layouts": [{
                            "name": "AWS EC2",
                            "type": "item",
                            "options": {
                                "fields": [{
                                    "name": "Cloud Service ID",
                                    "key": "cloud_service_id"
                                }]
                            }
                        }]
                    }
                }
            }

        params = {
            'cloud_service_type': cloud_service_type,
            'provider': provider,
            'cloud_service_group': group,
            'domain_id': self.domain.domain_id,
            'data': data,
            'metadata': metadata,
            "reference": {
                "resource_id": "resource-xxxx",
                "external_link": "https://aaa.bbb.ccc/"
            },
        }

        self.cloud_service = self.inventory_v1.CloudService.create(params, metadata=(('token', self.token),))
        self._print_data(self.cloud_service, 'test_create_cloud_service')

        self.cloud_services.append(self.cloud_service)
        self.assertEqual(self.cloud_service.provider, provider)

    def test_create_cloud_service_group(self, cloud_service_type=None, provider=None):
        """ Create Cloud Service with cloud service group
        """

        if cloud_service_type is None:
            cloud_service_type = random_string()

        if provider is None:
            provider = random_string()

        group = random_string()

        params = {
            'provider': provider,
            'cloud_service_type': cloud_service_type,
            'cloud_service_group': group,
            'data': {
                random_string(): random_string(),
                random_string(): random_string(),
                random_string(): random_string()
            },
            'domain_id': self.domain.domain_id
        }

        self.cloud_service = self.inventory_v1.CloudService.create(params, metadata=(('token', self.token),))
        self.cloud_services.append(self.cloud_service)
        self.assertEqual(self.cloud_service.cloud_service_group, group)

    def test_create_cloud_service_region(self, cloud_service_type=None, provider=None):
        """ Create Cloud Service with region
        """

        if cloud_service_type is None:
            cloud_service_type = random_string()

        if provider is None:
            provider = random_string()

        self._create_region()

        params = {
            'provider': provider,
            'cloud_service_type': cloud_service_type,
            'data': {
                random_string(): random_string(),
                random_string(): random_string(),
                random_string(): random_string()
            },
            'region_id': self.region.region_id,
            'domain_id': self.domain.domain_id
        }

        self.cloud_service = self.inventory_v1.CloudService.create(params, metadata=(('token', self.token),))
        self.cloud_services.append(self.cloud_service)
        self.assertEqual(self.cloud_service.region_info.region_id, self.region.region_id)

    def test_update_cloud_service_project_id(self):
        self._create_project()
        self.test_create_cloud_service()

        param = {
            'cloud_service_id': self.cloud_service.cloud_service_id,
            'project_id': self.project.project_id,
            'domain_id': self.domain.domain_id
        }

        self.cloud_service = self.inventory_v1.CloudService.update(param, metadata=(('token', self.token),))
        self._print_data(self.cloud_service, 'test_update_cloud_service_project_id_1')
        self.assertEqual(self.cloud_service.project_id, self.project.project_id)

        self._create_project()
        param = {
            'cloud_service_id': self.cloud_service.cloud_service_id,
            'project_id': self.project.project_id,
            'domain_id': self.domain.domain_id
        }

        self.cloud_service = self.inventory_v1.CloudService.update(param, metadata=(('token', self.token),))
        self._print_data(self.cloud_service, 'test_update_cloud_service_project_id_2')
        self.assertEqual(self.cloud_service.project_id, self.project.project_id)

    def test_update_cloud_service_region(self):
        self._create_region()
        self.test_create_cloud_service()

        param = {
            'cloud_service_id': self.cloud_service.cloud_service_id,
            'region_id': self.region.region_id,
            'domain_id': self.domain.domain_id,
        }

        self.cloud_service = self.inventory_v1.CloudService.update(param, metadata=(('token', self.token),))
        self.assertEqual(self.cloud_service.region_info.region_id, self.region.region_id)

    def test_update_cloud_service_release_project(self):
        self._create_project()
        self.test_create_cloud_service()

        param = {
            'cloud_service_id': self.cloud_service.cloud_service_id,
            'project_id': self.project.project_id,
            'domain_id': self.domain.domain_id
        }

        self.cloud_service = self.inventory_v1.CloudService.update(param, metadata=(('token', self.token),))
        self._print_data(self.cloud_service, 'test_update_cloud_service_release_project_1')

        param = {
            'cloud_service_id': self.cloud_service.cloud_service_id,
            'release_project': True,
            'domain_id': self.domain.domain_id
        }

        self.cloud_service = self.inventory_v1.CloudService.update(param, metadata=(('token', self.token),))
        self._print_data(self.cloud_service, 'test_update_cloud_service_release_project_2')

        self.assertEqual(self.cloud_service.project_id, '')

    def test_update_cloud_service_release_region(self):
        self._create_region()
        self.test_create_cloud_service()

        param = {
            'cloud_service_id': self.cloud_service.cloud_service_id,
            'region_id': self.region.region_id,
            'domain_id': self.domain.domain_id
        }

        self.cloud_service = self.inventory_v1.CloudService.update(param, metadata=(('token', self.token),))

        param = {
            'cloud_service_id': self.cloud_service.cloud_service_id,
            'release_region': True,
            'domain_id': self.domain.domain_id
        }

        self.cloud_service = self.inventory_v1.CloudService.update(param, metadata=(('token', self.token),))

        self.assertEqual(self.cloud_service.region_info.region_id, '')

    def test_update_cloud_service_data(self):
        old_data = {
            'a': 'b',
            'c': 'd',
            'x': 'y'
        }

        old_metadata = {
            'view': {
                'sub_data': {
                    "layouts": [{
                        "name": "AWS EC2",
                        "type": "item",
                        "options": {
                            "fields": [{
                                "name": "Cloud Service ID",
                                "key": "cloud_service_id"
                            }]
                        }
                    }]
                }
            }
        }

        self.test_create_cloud_service(data=old_data, metadata=old_metadata)

        data = {
            'a': 'xxx',
            'e': 'f'
        }

        metadata = {
            'view': {
                'sub_data': {
                    "layouts": [{
                        "name": "AWS EC2",
                        "type": "item",
                        "options": {
                            "fields": [{
                                "name": "New Cloud Service ID",
                                "key": "cloud_service_id"
                            }]
                        }
                    }]
                }
            }
        }

        param = {
            'cloud_service_id': self.cloud_service.cloud_service_id,
            'data': data,
            'metadata': metadata,
            'reference': {
                'resource_id': 'resource-yyyy',
                'external_link': 'https://ddd.eee.fff/'
            },
            'domain_id': self.domain.domain_id
        }

        self.cloud_service = self.inventory_v1.CloudService.update(param, metadata=(('token', self.token),))

        self._print_data(self.cloud_service, 'test_update_cloud_service_data')

        result_data = {
            'a': 'xxx',
            'c': 'd',
            'x': 'y',
            'e': 'f'
        }

        self.assertEqual(MessageToDict(self.cloud_service.data), result_data)

    def test_update_cloud_service_tags(self):
        self.test_create_cloud_service()

        tags = {
            random_string(): random_string(),
            random_string(): random_string()
        }
        param = { 'cloud_service_id': self.cloud_service.cloud_service_id,
                  'tags': tags,
                  'domain_id': self.domain.domain_id,
                }
        self.cloud_service = self.inventory_v1.CloudService.update(param, metadata=(('token', self.token),))
        self.assertEqual(MessageToDict(self.cloud_service.tags), tags)

    def test_get_cloud_service(self):
        cloud_service_type = 's3'
        provider = 'aws'
        self.test_create_cloud_service(cloud_service_type=cloud_service_type, provider=provider)

        param = {
            'cloud_service_id': self.cloud_service.cloud_service_id,
            'domain_id': self.domain.domain_id
        }
        self.cloud_service = self.inventory_v1.CloudService.get(param, metadata=(('token', self.token),))
        self.assertEqual(self.cloud_service.provider, provider)

    def test_list_cloud_service_types(self):
        self.test_create_cloud_service()
        self.test_create_cloud_service()
        self.test_create_cloud_service()

        param = {
            'cloud_service_id': self.cloud_service.cloud_service_id,
            'domain_id': self.domain.domain_id
        }

        cloud_services = self.inventory_v1.CloudService.list(param, metadata=(('token', self.token),))

        self.assertEqual(1, cloud_services.total_count)

    def test_list_cloud_services_cloud_service_type(self):
        self.test_create_cloud_service()
        self.test_create_cloud_service()
        self.test_create_cloud_service()
        self.test_create_cloud_service()
        self.test_create_cloud_service()
        self.test_create_cloud_service()

        param = {
            'cloud_service_type': self.cloud_service.cloud_service_type,
            'domain_id': self.domain.domain_id
        }

        cloud_svcs = self.inventory_v1.CloudService.list(param, metadata=(('token', self.token),))

        self.assertEqual(1, cloud_svcs.total_count)

    def test_list_query(self):
        self.test_create_cloud_service()
        self.test_create_cloud_service()
        self.test_create_cloud_service()
        self.test_create_cloud_service()
        self.test_create_cloud_service()
        self.test_create_cloud_service()

        param = {
            'domain_id': self.domain.domain_id,
            'query': {
                'filter': [
                    {
                        'k': 'cloud_service_id',
                        'v': list(map(lambda cloud_service: cloud_service.cloud_service_id, self.cloud_services)),
                        'o': 'in'
                    }
                ]
            }
        }

        cloud_services = self.inventory_v1.CloudService.list(param, metadata=(('token', self.token),))
        self.assertEqual(len(self.cloud_services), cloud_services.total_count)

    def test_list_query_2(self):
        group = random_string()

        self.test_create_cloud_service(group=group)
        self.test_create_cloud_service(group=group)
        self.test_create_cloud_service(group=group)
        self.test_create_cloud_service(group=group)
        self.test_create_cloud_service()
        self.test_create_cloud_service()

        param = {
            'domain_id': self.domain.domain_id,
            'query': {},
            'cloud_service_group': group
        }

        cloud_services = self.inventory_v1.CloudService.list(param, metadata=(('token', self.token),))
        self.assertEqual(4, cloud_services.total_count)

    def test_list_query_minimal(self):
        group = random_string()

        self.test_create_cloud_service(group=group)
        self.test_create_cloud_service(group=group)
        self.test_create_cloud_service(group=group)
        self.test_create_cloud_service(group=group)
        self.test_create_cloud_service()
        self.test_create_cloud_service()

        param = {
            'domain_id': self.domain.domain_id,
            'query': {
                'minimal': True
            }
        }

        response = self.inventory_v1.CloudService.list(param, metadata=(('token', self.token),))
        self.assertEqual(len(response.results), response.total_count)

    def test_stat_cloud_service(self):
        self.test_list_query()

        params = {
            'domain_id': self.domain.domain_id,
            'query': {
                'aggregate': {
                    'group': {
                        'keys': [{
                            'key': 'network_id',
                            'name': 'Id'
                        }],
                        'fields': [{
                            'operator': 'count',
                            'name': 'Count'
                        }]
                    }
                },
                'sort': {
                    'name': 'Count',
                    'desc': True
                }
            }
        }

        result = self.inventory_v1.CloudService.stat(
            params, metadata=(('token', self.token),))

        self._print_data(result, 'test_stat_cloud_service')

    def test_stat_cloud_service_distinct(self):
        self.test_list_query()

        params = {
            'domain_id': self.domain.domain_id,
            'query': {
                'distinct': 'cloud_service_id',
                'page': {
                    'start': 1,
                    'limit': 3
                }
            }
        }

        result = self.inventory_v1.CloudService.stat(
            params, metadata=(('token', self.token),))

        self._print_data(result, 'test_stat_cloud_service_distinct')