3
3
import uuid
4
4
from enum import StrEnum
5
5
6
- from understack_workflows .domain import DefaultDomain
7
- from understack_workflows .domain import domain_id
8
6
from understack_workflows .helpers import credential
9
7
from understack_workflows .helpers import parser_nautobot_args
10
8
from understack_workflows .helpers import setup_logger
@@ -39,11 +37,6 @@ def argument_parser():
39
37
help = "Cloud to load. default: %(default)s" ,
40
38
)
41
39
42
- parser .add_argument (
43
- "--only-domain" ,
44
- type = domain_id ,
45
- help = "Only operate on projects from specified domain" ,
46
- )
47
40
parser .add_argument ("event" , type = Event , choices = [item .value for item in Event ])
48
41
parser .add_argument (
49
42
"object" , type = uuid .UUID , help = "Keystone ID of object the event happened on"
@@ -53,25 +46,6 @@ def argument_parser():
53
46
return parser
54
47
55
48
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
-
75
49
def _create_outside_network (conn : Connection , project_id : uuid .UUID ):
76
50
network = _find_outside_network (conn , project_id .hex )
77
51
if network :
@@ -120,44 +94,60 @@ def _find_outside_network(conn: Connection, project_id: str):
120
94
)
121
95
122
96
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 )
109
+
110
+
123
111
def handle_project_create (
124
112
conn : Connection , nautobot : Nautobot , project_id : uuid .UUID
125
113
) -> 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
114
+ logger .info ("got request to create tenant %s" , project_id .hex )
115
+ tenant_name , tenant_description = _tenant_attrs (conn , project_id )
116
+
117
+ nautobot_tenant_api = nautobot .session .tenancy .tenants
129
118
try :
130
- ten = ten_api .create (
131
- id = str (project_id ), name = project . name , description = project . description
119
+ tenant = nautobot_tenant_api .create (
120
+ id = str (project_id ), name = tenant_name , description = tenant_description
132
121
)
133
122
_create_outside_network (conn , project_id )
134
123
except Exception :
135
124
logger .exception (
136
- "Unable to create project %s / %s" , str (project_id ), project . name
125
+ "Unable to create project %s / %s" , str (project_id ), tenant_name
137
126
)
138
127
return _EXIT_API_ERROR
139
128
140
- logger .info ("tenant %s created %s" , project_id , ten .created ) # type: ignore
129
+ logger .info ("tenant %s created %s" , project_id , tenant .created ) # type: ignore
141
130
return _EXIT_SUCCESS
142
131
143
132
144
133
def handle_project_update (
145
134
conn : Connection , nautobot : Nautobot , project_id : uuid .UUID
146
135
) -> 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
136
+ logger .info ("got request to update tenant %s" , project_id .hex )
137
+ tenant_name , tenant_description = _tenant_attrs (conn , project_id )
150
138
139
+ tenant_api = nautobot .session .tenancy .tenants
151
140
existing_tenant = tenant_api .get (project_id )
152
141
logger .info ("existing_tenant: %s" , existing_tenant )
153
142
try :
154
143
if existing_tenant is None :
155
144
new_tenant = tenant_api .create (
156
- id = str (project_id ), name = project . name , description = project . description
145
+ id = str (project_id ), name = tenant_name , description = tenant_description
157
146
)
158
147
logger .info ("tenant %s created %s" , project_id , new_tenant .created ) # type: ignore
159
148
else :
160
- existing_tenant .description = project .description # type: ignore
149
+ existing_tenant .name = tenant_name # type: ignore
150
+ existing_tenant .description = tenant_description # type: ignore
161
151
existing_tenant .save () # type: ignore
162
152
logger .info (
163
153
"tenant %s last updated %s" ,
@@ -168,7 +158,7 @@ def handle_project_update(
168
158
_create_outside_network (conn , project_id )
169
159
except Exception :
170
160
logger .exception (
171
- "Unable to update project %s / %s" , str (project_id ), project . name
161
+ "Unable to update project %s / %s" , str (project_id ), tenant_name
172
162
)
173
163
return _EXIT_API_ERROR
174
164
return _EXIT_SUCCESS
@@ -194,16 +184,7 @@ def do_action(
194
184
nautobot : Nautobot ,
195
185
event : Event ,
196
186
project_id : uuid .UUID ,
197
- only_domain : uuid .UUID | DefaultDomain | None ,
198
187
) -> 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
-
207
188
match event :
208
189
case Event .ProjectCreate :
209
190
return handle_project_create (conn , nautobot , project_id )
@@ -224,4 +205,4 @@ def main() -> int:
224
205
nb_token = args .nautobot_token or credential ("nb-token" , "token" )
225
206
nautobot = Nautobot (args .nautobot_url , nb_token , logger = logger )
226
207
227
- return do_action (conn , nautobot , args .event , args .object , args . only_domain )
208
+ return do_action (conn , nautobot , args .event , args .object )
0 commit comments