11import argparse
22import logging
3+ from typing import Any
34import uuid
45from enum import StrEnum
56
6- from understack_workflows .domain import DefaultDomain
7- from understack_workflows .domain import domain_id
87from understack_workflows .helpers import credential
98from understack_workflows .helpers import parser_nautobot_args
109from understack_workflows .helpers import setup_logger
@@ -39,11 +38,6 @@ def argument_parser():
3938 help = "Cloud to load. default: %(default)s" ,
4039 )
4140
42- parser .add_argument (
43- "--only-domain" ,
44- type = domain_id ,
45- help = "Only operate on projects from specified domain" ,
46- )
4741 parser .add_argument ("event" , type = Event , choices = [item .value for item in Event ])
4842 parser .add_argument (
4943 "object" , type = uuid .UUID , help = "Keystone ID of object the event happened on"
@@ -53,25 +47,6 @@ def argument_parser():
5347 return parser
5448
5549
56- def is_valid_domain (
57- conn : Connection ,
58- project_id : uuid .UUID ,
59- only_domain : uuid .UUID | DefaultDomain | None ,
60- ) -> bool :
61- if only_domain is None :
62- return True
63- project = conn .identity .get_project (project_id .hex ) # type: ignore
64- ret = project .domain_id == only_domain .hex
65- if not ret :
66- logger .info (
67- "keystone project %s part of domain %s and not %s" ,
68- project_id ,
69- project .domain_id ,
70- only_domain ,
71- )
72- return ret
73-
74-
7550def _create_outside_network (conn : Connection , project_id : uuid .UUID ):
7651 network = _find_outside_network (conn , project_id .hex )
7752 if network :
@@ -119,45 +94,61 @@ def _find_outside_network(conn: Connection, project_id: str):
11994 name_or_id = OUTSIDE_NETWORK_NAME ,
12095 )
12196
97+ def _tenant_attrs (conn : Connection , project_id : uuid .UUID ) -> tuple [str , str ]:
98+ project = conn .identity .get_project (project_id .hex ) # type: ignore
99+ domain_id = project .domain_id
100+
101+ if domain_id == "default" :
102+ domain_name = "default"
103+ else :
104+ domain = conn .identity .get_project (domain_id ) # type: ignore
105+ domain_name = domain .name
106+
107+ tenant_name = f"{ domain_name } :{ project .name } "
108+ return tenant_name , str (project .description )
122109
123110def handle_project_create (
124111 conn : Connection , nautobot : Nautobot , project_id : uuid .UUID
125112) -> int :
126- logger .info ("got request to create tenant %s" , project_id )
127- project = conn .identity .get_project (project_id .hex ) # type: ignore
128- ten_api = nautobot .session .tenancy .tenants
113+ logger .info ("got request to create tenant %s" , project_id .hex )
114+ tenant_name , tenant_description = _tenant_attrs (conn , project_id )
115+
116+ nautobot_tenant_api = nautobot .session .tenancy .tenants
129117 try :
130- ten = ten_api .create (
131- id = str (project_id ), name = project .name , description = project .description
118+ tenant = nautobot_tenant_api .create (
119+ id = str (project_id ),
120+ name = tenant_name ,
121+ description = tenant_description
132122 )
133123 _create_outside_network (conn , project_id )
134124 except Exception :
135125 logger .exception (
136- "Unable to create project %s / %s" , str (project_id ), project . name
126+ "Unable to create project %s / %s" , str (project_id ), tenant_name
137127 )
138128 return _EXIT_API_ERROR
139129
140- logger .info ("tenant %s created %s" , project_id , ten .created ) # type: ignore
130+ logger .info ("tenant %s created %s" , project_id , tenant .created ) # type: ignore
141131 return _EXIT_SUCCESS
142132
143133
144134def handle_project_update (
145135 conn : Connection , nautobot : Nautobot , project_id : uuid .UUID
146136) -> int :
147- logger .info ("got request to update tenant %s" , project_id )
148- project = conn .identity .get_project (project_id .hex ) # type: ignore
149- tenant_api = nautobot .session .tenancy .tenants
137+ logger .info ("got request to update tenant %s" , project_id .hex )
138+ tenant_name , tenant_description = _tenant_attrs (conn , project_id )
150139
140+ tenant_api = nautobot .session .tenancy .tenants
151141 existing_tenant = tenant_api .get (project_id )
152142 logger .info ("existing_tenant: %s" , existing_tenant )
153143 try :
154144 if existing_tenant is None :
155145 new_tenant = tenant_api .create (
156- id = str (project_id ), name = project . name , description = project . description
146+ id = str (project_id ), name = tenant_name , description = tenant_description
157147 )
158148 logger .info ("tenant %s created %s" , project_id , new_tenant .created ) # type: ignore
159149 else :
160- existing_tenant .description = project .description # type: ignore
150+ existing_tenant .name = tenant_name # type: ignore
151+ existing_tenant .description = tenant_description # type: ignore
161152 existing_tenant .save () # type: ignore
162153 logger .info (
163154 "tenant %s last updated %s" ,
@@ -168,7 +159,7 @@ def handle_project_update(
168159 _create_outside_network (conn , project_id )
169160 except Exception :
170161 logger .exception (
171- "Unable to update project %s / %s" , str (project_id ), project . name
162+ "Unable to update project %s / %s" , str (project_id ), tenant_name
172163 )
173164 return _EXIT_API_ERROR
174165 return _EXIT_SUCCESS
@@ -194,16 +185,7 @@ def do_action(
194185 nautobot : Nautobot ,
195186 event : Event ,
196187 project_id : uuid .UUID ,
197- only_domain : uuid .UUID | DefaultDomain | None ,
198188) -> int :
199- if event in [Event .ProjectCreate , Event .ProjectUpdate ] and not is_valid_domain (
200- conn , project_id , only_domain
201- ):
202- logger .info (
203- "keystone project %s not part of %s, skipping" , project_id , only_domain
204- )
205- return _EXIT_SUCCESS
206-
207189 match event :
208190 case Event .ProjectCreate :
209191 return handle_project_create (conn , nautobot , project_id )
@@ -224,4 +206,4 @@ def main() -> int:
224206 nb_token = args .nautobot_token or credential ("nb-token" , "token" )
225207 nautobot = Nautobot (args .nautobot_url , nb_token , logger = logger )
226208
227- return do_action (conn , nautobot , args .event , args .object , args . only_domain )
209+ return do_action (conn , nautobot , args .event , args .object )
0 commit comments