Skip to content

Commit cf941e7

Browse files
scoopexMarc Schöchlin
andcommitted
Be more tolerant with not needed roles (#26)
* Be more tolerant with not needed roles * bugfix, wrong flag --------- Signed-off-by: Marc Schöchlin <[email protected]> Co-authored-by: Marc Schöchlin <[email protected]> Signed-off-by: Marc Schöchlin <[email protected]>
1 parent 42b7e80 commit cf941e7

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/openstack_workload_generator/__main__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def establish_connection():
170170
)
171171
if args.ansible_inventory:
172172
workload_project.dump_inventory_hosts(args.ansible_inventory)
173-
if args.clouds_yaml:
173+
if args.generate_clouds_yaml:
174174
clouds_yaml_data[
175175
f"{workload_domain.domain_name}-{workload_project.project_name}"
176176
] = workload_project.get_clouds_yaml_data()
@@ -179,10 +179,11 @@ def establish_connection():
179179
args.delete_machines
180180
):
181181
machine_obj.delete_machine()
182-
if args.generate_clouds_yaml:
183-
LOGGER.info(f"Creating a a clouds yaml : {args.generate_clouds_yaml}")
184182

183+
if args.generate_clouds_yaml:
184+
LOGGER.info(f"Creating a clouds yaml : {args.generate_clouds_yaml}")
185185
clouds_yaml_data_new = {"clouds": clouds_yaml_data}
186+
186187
if os.path.exists(args.generate_clouds_yaml):
187188
with open(args.generate_clouds_yaml, "r") as file:
188189
existing_data = yaml.safe_load(file)

src/openstack_workload_generator/entities/project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def create_and_get_project(self) -> Project:
236236
self.adapt_quota()
237237

238238
self.assign_role_to_user_for_project("manager")
239-
self.assign_role_to_user_for_project("load-balancer_member")
239+
self.assign_role_to_user_for_project("load-balancer_member", required=False)
240240
self.assign_role_to_user_for_project("member")
241241

242242
self.workload_network = WorkloadGeneratorNetwork(

src/openstack_workload_generator/entities/user.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ def __init__(self, conn: Connection, user_name: str, domain: Domain):
2020
user_name, query={"domain_id": self.domain.id}
2121
)
2222

23-
def assign_role_to_user(self, role_name: str):
23+
def assign_role_to_user(self, role_name: str, mandatory: bool = True):
24+
25+
role_id = self.get_role_id_by_name(role_name, mandatory)
26+
27+
if role_id is None:
28+
LOGGER.warning(f"Role '{role_name}' not found, not assigning it")
29+
return
30+
2431
self.conn.identity.assign_project_role_to_user(
25-
self.obj.id, self.domain.id, self.get_role_id_by_name(role_name)
32+
self.obj.id, self.domain.id, role_id
2633
)
2734
LOGGER.info(
2835
f"Assigned role '{role_name}' to user '{self.obj.name}' in {DomainCache.ident_by_id(self.domain.id)}"
@@ -55,8 +62,11 @@ def delete_user(self):
5562
LOGGER.warning(f"Deleted user: {self.obj.name} / {self.obj.id}")
5663
self.obj = None
5764

58-
def get_role_id_by_name(self, role_name) -> str:
65+
def get_role_id_by_name(self, role_name, mandatory: bool = True) -> str | None:
5966
for role in self.conn.identity.roles():
6067
if role.name == role_name:
6168
return role.id
62-
raise RuntimeError(f"No such role {role_name}")
69+
if mandatory:
70+
raise RuntimeError(f"No such role {role_name}")
71+
else:
72+
return None

0 commit comments

Comments
 (0)