19
19
_EXIT_API_ERROR = 1
20
20
_EXIT_EVENT_UNKNOWN = 2
21
21
22
+ OUTSIDE_NETWORK_NAME = "OUTSIDE"
23
+
22
24
23
25
class Event (StrEnum ):
24
26
ProjectCreate = "identity.project.created"
@@ -70,6 +72,41 @@ def is_valid_domain(
70
72
return ret
71
73
72
74
75
+ def _create_outside_network (conn : Connection , project_id : uuid .UUID ):
76
+ payload = _outside_network_payload (project_id )
77
+ network = conn .network .find_network (** payload )
78
+ if network :
79
+ logger .info (
80
+ "%s Network %s already exists for this tenant" ,
81
+ OUTSIDE_NETWORK_NAME ,
82
+ network .id ,
83
+ )
84
+ else :
85
+ payload .update (name = payload .pop ("name_or_id" ))
86
+ network = conn .network .create_network (** payload )
87
+ logger .info (
88
+ "Created %s Network %s for tenant" , OUTSIDE_NETWORK_NAME , network .id
89
+ )
90
+
91
+
92
+ def _delete_outside_network (conn : Connection , project_id : uuid .UUID ):
93
+ payload = _outside_network_payload (project_id )
94
+ network = conn .network .find_network (** payload )
95
+ if network :
96
+ conn .delete_network (network .id )
97
+ logger .info (
98
+ "Deleted %s Network %s for this tenant" , OUTSIDE_NETWORK_NAME , network .id
99
+ )
100
+
101
+
102
+ def _outside_network_payload (project_id : uuid .UUID ) -> dict :
103
+ return {
104
+ "project_id" : project_id ,
105
+ "name_or_id" : OUTSIDE_NETWORK_NAME ,
106
+ "router:external" : True ,
107
+ }
108
+
109
+
73
110
def handle_project_create (
74
111
conn : Connection , nautobot : Nautobot , project_id : uuid .UUID
75
112
) -> int :
@@ -80,6 +117,7 @@ def handle_project_create(
80
117
ten = ten_api .create (
81
118
id = str (project_id ), name = project .name , description = project .description
82
119
)
120
+ _create_outside_network (conn , project_id )
83
121
except Exception :
84
122
logger .exception (
85
123
"Unable to create project %s / %s" , str (project_id ), project .name
@@ -113,6 +151,8 @@ def handle_project_update(
113
151
project_id ,
114
152
existing_tenant .last_updated , # type: ignore
115
153
)
154
+
155
+ _create_outside_network (conn , project_id )
116
156
except Exception :
117
157
logger .exception (
118
158
"Unable to update project %s / %s" , str (project_id ), project .name
@@ -129,6 +169,8 @@ def handle_project_delete(
129
169
if not ten :
130
170
logger .warning ("tenant %s does not exist, nothing to delete" , project_id )
131
171
return _EXIT_SUCCESS
172
+
173
+ _delete_outside_network (conn , project_id )
132
174
ten .delete () # type: ignore
133
175
logger .info ("deleted tenant %s" , project_id )
134
176
return _EXIT_SUCCESS
0 commit comments