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