Add cloudtaf framework
[ta/cloudtaf.git] / libraries / cluster / usergen.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import itertools
16
17
18 class UserGen(object):
19
20     def __init__(self, roles_len):
21         self._roles_len = roles_len
22         self._usergens = {}
23
24     def create_username(self, roles):
25         if not roles:
26             return 'no_roles'
27         if len(roles) == self._roles_len:
28             return 'all_roles'
29         return next(self._get_user_gen(sorted(roles)[0]))
30
31     def _get_user_gen(self, base):
32         if base not in self._usergens:
33             self._usergens[base] = self._user_gen(base)
34         return self._usergens[base]
35
36     @staticmethod
37     def _user_gen(base):
38         for idx in itertools.count():  # pragma: no branch
39             yield '{base}{idx}'.format(base=base, idx=idx)