2121_EXIT_API_ERROR = 1
2222_EXIT_EVENT_UNKNOWN = 2
2323
24- OUTSIDE_NETWORK_NAME = "OUTSIDE"
25-
2624
2725class Event (StrEnum ):
2826 ProjectCreate = "identity.project.created"
@@ -50,55 +48,7 @@ def argument_parser():
5048 return parser
5149
5250
53- def _create_outside_network (conn : Connection , project_id : uuid .UUID ):
54- network = _find_outside_network (conn , project_id .hex )
55- if network :
56- logger .info (
57- "%s Network %s already exists for this tenant" ,
58- OUTSIDE_NETWORK_NAME ,
59- network .id ,
60- )
61- else :
62- payload = {
63- "project_id" : project_id .hex ,
64- "name" : OUTSIDE_NETWORK_NAME ,
65- "router:external" : False ,
66- }
67- network = conn .network .create_network (** payload ) # type: ignore
68- logger .info (
69- "Created %s Network %s for tenant" , OUTSIDE_NETWORK_NAME , network .id
70- )
71- conn .network .create_rbac_policy ( # type: ignore
72- object_type = "network" ,
73- object_id = network .id ,
74- action = "access_as_external" ,
75- target_project_id = project_id .hex ,
76- )
77- conn .network .create_rbac_policy ( # type: ignore
78- object_type = "network" ,
79- object_id = network .id ,
80- action = "access_as_shared" ,
81- target_project_id = project_id .hex ,
82- )
83-
84-
85- def _delete_outside_network (conn : Connection , project_id : uuid .UUID ):
86- network = _find_outside_network (conn , project_id .hex )
87- if network :
88- conn .delete_network (network .id )
89- logger .info (
90- "Deleted %s Network %s for this tenant" , OUTSIDE_NETWORK_NAME , network .id
91- )
92-
93-
94- def _find_outside_network (conn : Connection , project_id : str ):
95- return conn .network .find_network ( # type: ignore
96- project_id = project_id ,
97- name_or_id = OUTSIDE_NETWORK_NAME ,
98- )
99-
100-
101- def _tenant_attrs (conn : Connection , project_id : uuid .UUID ) -> tuple [str , str , bool ]:
51+ def _tenant_attrs (conn : Connection , project_id : uuid .UUID ) -> tuple [str , str ]:
10252 project = conn .identity .get_project (project_id .hex ) # type: ignore
10353 domain_id = project .domain_id
10454 is_default_domain = domain_id == "default"
@@ -110,7 +60,7 @@ def _tenant_attrs(conn: Connection, project_id: uuid.UUID) -> tuple[str, str, bo
11060 domain_name = domain .name
11161
11262 tenant_name = f"{ domain_name } :{ project .name } "
113- return tenant_name , str (project .description ), is_default_domain
63+ return tenant_name , str (project .description )
11464
11565
11666def _unmap_tenant_from_devices (
@@ -127,14 +77,12 @@ def handle_project_create(
12777 conn : Connection , nautobot : pynautobot .api , project_id : uuid .UUID
12878) -> int :
12979 logger .info ("got request to create tenant %s" , project_id .hex )
130- tenant_name , tenant_description , is_default_domain = _tenant_attrs (conn , project_id )
80+ tenant_name , tenant_description = _tenant_attrs (conn , project_id )
13181
13282 try :
13383 tenant = nautobot .tenancy .tenants .create (
13484 id = str (project_id ), name = tenant_name , description = tenant_description
13585 )
136- if is_default_domain :
137- _create_outside_network (conn , project_id )
13886 except Exception :
13987 logger .exception (
14088 "Unable to create project %s / %s" , str (project_id ), tenant_name
@@ -149,7 +97,7 @@ def handle_project_update(
14997 conn : Connection , nautobot : pynautobot .api , project_id : uuid .UUID
15098) -> int :
15199 logger .info ("got request to update tenant %s" , project_id .hex )
152- tenant_name , tenant_description , is_default_domain = _tenant_attrs (conn , project_id )
100+ tenant_name , tenant_description = _tenant_attrs (conn , project_id )
153101
154102 existing_tenant = nautobot .tenancy .tenants .get (id = project_id )
155103 logger .info ("existing_tenant: %s" , existing_tenant )
@@ -170,8 +118,6 @@ def handle_project_update(
170118 existing_tenant .last_updated , # type: ignore
171119 )
172120
173- if is_default_domain :
174- _create_outside_network (conn , project_id )
175121 except Exception :
176122 logger .exception (
177123 "Unable to update project %s / %s" , str (project_id ), tenant_name
@@ -189,7 +135,6 @@ def handle_project_delete(
189135 logger .warning ("tenant %s does not exist, nothing to delete" , project_id )
190136 return _EXIT_SUCCESS
191137
192- _delete_outside_network (conn , project_id )
193138 _unmap_tenant_from_devices (tenant_id = project_id , nautobot = nautobot )
194139
195140 tenant = cast (Record , tenant )
0 commit comments