@@ -130,20 +130,22 @@ def find_entity(self, controller, path, params=None):
130130 if len (result ) == 1 :
131131 result = result [0 ]
132132 else :
133- if len (result ) > 1 :
134- error_msg = "too many ({0})" .format (len (result ))
135- else :
136- error_msg = "no"
137- self .fail_json (msg = "Found {0} results while searching for {1} at {2}" .format (error_msg , controller , path ))
133+ self .fail_json (msg = "Found no results while searching for {0} at {1}" .format (controller , path ))
138134
139135 return result
140136
141- def find_subnet (self , subnet , mask ):
137+ def find_subnet (self , subnet , mask , section ):
142138 # lookups for subnets need a separate find method
143139 # We only support cidr format to simplify the task.
144140 # CIDR is valid for ipv4 and ipv6 too.
145141 path = 'cidr/{0}/{1}' .format (subnet , mask )
146- return self .find_entity ('subnets' , path )
142+
143+ lookup_params = {
144+ 'filter_by' : 'sectionId' ,
145+ 'filter_value' : self .find_entity ('sections' , section )['id' ],
146+ }
147+
148+ return self .find_entity ('subnets' , path , params = lookup_params )
147149
148150 def find_address (self , address ):
149151 path = 'search/{0}' .format (address )
@@ -181,7 +183,7 @@ def find_by_key(self, controller, value, key='name'):
181183
182184 def find_current_entity (self ):
183185 if self .controller_name == 'subnet' :
184- entity = self .find_subnet (self .phpipam_params ['subnet' ], self .phpipam_params ['mask' ])
186+ entity = self .find_subnet (self .phpipam_params ['subnet' ], self .phpipam_params ['mask' ], self . phpipam_params [ 'section' ] )
185187 elif self .controller_name == 'address' :
186188 entity = self .find_address (self .phpipam_params ['ipaddress' ])
187189 elif self .controller_name == 'device' :
@@ -206,9 +208,10 @@ def _resolve_entity(self, key):
206208
207209 controller = entity_spec ['controller' ] or self .controller_uri
208210
211+ result = None
209212 if controller == 'subnets' :
210213 subnet , mask = self .phpipam_params [key ].split ('/' )
211- result = self .find_subnet (subnet , mask )
214+ result = self .find_subnet (subnet , mask , self . phpipam_params [ 'section' ] )
212215 elif controller == 'tools/device_types' :
213216 result = self .find_device_type (self .phpipam_params [key ])
214217 elif 'tools' in controller or controller in ['vlan' , 'l2domains' , 'vrf' ]:
@@ -496,7 +499,7 @@ def run(self, **kwargs):
496499 Manage entities. Lookup, ensure, sanitize and manage parameters.
497500 """
498501 if ('parent' in self .phpipam_spec and self .phpipam_spec ['parent' ].get ('type' ) == 'entity'
499- and self .desired_absent and 'parent' in self .phpipam_params and self .loopup_entity ('parent' ) is None ):
502+ and self .desired_absent and 'parent' in self .phpipam_params and self ._resolve_entity ('parent' ) is None ):
500503 return None
501504
502505 desired_entity = self ._auto_resolve_entities ()
0 commit comments