@@ -304,3 +304,71 @@ def get_routers(routers=None, count=2):
304304 if routers is None :
305305 routers = FakeRouter .create_routers (count )
306306 return mock .MagicMock (side_effect = routers )
307+
308+
309+ class FakeSubnet (object ):
310+ """Fake one or more subnets."""
311+
312+ @staticmethod
313+ def create_one_subnet (attrs = {}, methods = {}):
314+ """Create a fake subnet.
315+
316+ :param Dictionary attrs:
317+ A dictionary with all attributes
318+ :param Dictionary methods:
319+ A dictionary with all methods
320+ :return:
321+ A FakeResource object faking the subnet
322+ """
323+ # Set default attributes.
324+ subnet_attrs = {
325+ 'id' : 'subnet-id-' + uuid .uuid4 ().hex ,
326+ 'name' : 'subnet-name-' + uuid .uuid4 ().hex ,
327+ 'network_id' : 'network-id-' + uuid .uuid4 ().hex ,
328+ 'cidr' : '10.10.10.0/24' ,
329+ 'tenant_id' : 'project-id-' + uuid .uuid4 ().hex ,
330+ 'enable_dhcp' : True ,
331+ 'dns_nameservers' : [],
332+ 'allocation_pools' : [],
333+ 'host_routes' : [],
334+ 'ip_version' : '4' ,
335+ 'gateway_ip' : '10.10.10.1' ,
336+ }
337+
338+ # Overwrite default attributes.
339+ subnet_attrs .update (attrs )
340+
341+ # Set default methods.
342+ subnet_methods = {
343+ 'keys' : ['id' , 'name' , 'network_id' , 'cidr' , 'enable_dhcp' ,
344+ 'allocation_pools' , 'dns_nameservers' , 'gateway_ip' ,
345+ 'host_routes' , 'ip_version' , 'tenant_id' ]
346+ }
347+
348+ # Overwrite default methods.
349+ subnet_methods .update (methods )
350+
351+ subnet = fakes .FakeResource (info = copy .deepcopy (subnet_attrs ),
352+ methods = copy .deepcopy (subnet_methods ),
353+ loaded = True )
354+
355+ return subnet
356+
357+ @staticmethod
358+ def create_subnets (attrs = {}, methods = {}, count = 2 ):
359+ """Create multiple fake subnets.
360+
361+ :param Dictionary attrs:
362+ A dictionary with all attributes
363+ :param Dictionary methods:
364+ A dictionary with all methods
365+ :param int count:
366+ The number of subnets to fake
367+ :return:
368+ A list of FakeResource objects faking the subnets
369+ """
370+ subnets = []
371+ for i in range (0 , count ):
372+ subnets .append (FakeSubnet .create_one_subnet (attrs , methods ))
373+
374+ return subnets
0 commit comments