99# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1010# License for the specific language governing permissions and limitations
1111# under the License.
12+ from openstack import exceptions
1213from openstack import proxy
1314
1415from otcextensions .sdk .vpc .v1 import peering as _peering
1516from otcextensions .sdk .vpc .v1 import route as _route
17+ from otcextensions .sdk .vpc .v1 import subnet as _subnet
1618from otcextensions .sdk .vpc .v1 import vpc as _vpc
1719
1820
@@ -187,15 +189,17 @@ def vpcs(self, **query):
187189 return self ._list (_vpc .Vpc , ** query )
188190
189191 def create_vpc (self , ** attrs ):
190- """ Create a new vpc from attributes
191- :param dict attrs: Keyword arguments which will be used to create
192- a :class:`~otcextensions.sdk.vpc.v1.vpc.Vpc`
192+ """Create a new vpc from attributes
193+
194+ :param dict attrs: Keyword arguments which will be used to create a
195+ :class:`~otcextensions.sdk.vpc.v1.vpc.Vpc`
193196 """
194197 return self ._create (_vpc .Vpc , ** attrs ,
195198 project_id = self .get_project_id ())
196199
197200 def delete_vpc (self , vpc , ignore_missing = True ):
198- """ Delete a vpc
201+ """Delete a vpc
202+
199203 :param vpc: vpc id or an instance of
200204 :class:`~otcextensions.sdk.vpc.v1.vpc.Vpc`
201205
@@ -212,9 +216,10 @@ def delete_vpc(self, vpc, ignore_missing=True):
212216 ignore_missing = ignore_missing )
213217
214218 def get_vpc (self , vpc ):
215- """ Get a vpc by id
219+ """Get a vpc by id
220+
216221 :param vpc: vpc id or an instance of
217- :class:`~otcextensions.sdk.vpc.v1.vpc.Vpc`
222+ :class:`~otcextensions.sdk.vpc.v1.vpc.Vpc`
218223
219224 :returns: One :class:`~otcextensions.sdk.vpc.v1.vpc.Vpc`
220225 """
@@ -224,6 +229,7 @@ def find_vpc(self, name_or_id, ignore_missing=False):
224229 """Find a single vpc
225230
226231 :param name_or_id: The name or ID of a vpc
232+
227233 :param bool ignore_missing: When set to ``False``
228234 :class:`~openstack.exceptions.ResourceNotFound` will be raised
229235 when the vpc does not exist.
@@ -238,7 +244,7 @@ def find_vpc(self, name_or_id, ignore_missing=False):
238244 project_id = self .get_project_id ())
239245
240246 def update_vpc (self , vpc , ** attrs ):
241- """ Update vpc
247+ """Update vpc
242248
243249 :param vpc: vpc id or an instance of
244250 :class:`~otcextensions.sdk.vpc.v1.vpc.Vpc`
@@ -249,6 +255,124 @@ def update_vpc(self, vpc, **attrs):
249255 attrs ['project_id' ] = self .get_project_id ()
250256 return self ._update (_vpc .Vpc , vpc , ** attrs )
251257
258+ # ========== Subnet ==========
259+ def subnets (self , ** query ):
260+ """Return a generator of subnets
261+
262+ :param dict query: Optional query parameters to be sent to limit
263+ the resources being returned.
264+
265+ :returns: A generator of subnet objects
266+
267+ :rtype: :class:`~otcextensions.sdk.vpc.v1.subnet.Subnet`
268+ """
269+ query ['project_id' ] = self .get_project_id ()
270+ return self ._list (_subnet .Subnet , ** query )
271+
272+ def create_subnet (self , ** attrs ):
273+ """Create a new subnet from attributes
274+
275+ :param dict attrs: Keyword arguments which will be used to create
276+ a :class:`~otcextensions.sdk.vpc.v1.subnet.Subnet`
277+ """
278+ attrs ['project_id' ] = self .get_project_id ()
279+ return self ._create (_subnet .Subnet , ** attrs )
280+
281+ def get_subnet (self , subnet ):
282+ """Get a subnet by id
283+
284+ :param subnet: subnet id or an instance of
285+ :class:`~otcextensions.sdk.vpc.v1.subnet.Subnet`
286+
287+ :returns: One :class:`~otcextensions.sdk.vpc.v1.subnet.Subnet`
288+ """
289+ return self ._get (_subnet .Subnet , subnet ,
290+ project_id = self .get_project_id ())
291+
292+ def find_subnet (self , name_or_id , ignore_missing = False ):
293+ """Find a single subnet
294+
295+ :param name_or_id: The name or ID of a subnet
296+
297+ :param bool ignore_missing: When set to ``False``
298+ :class:`~openstack.exceptions.ResourceNotFound` will be raised
299+ when the subnet does not exist.
300+ When set to ``True``, no exception will be set when attempting
301+ to delete a nonexistent peering.
302+
303+ :returns: One :class:`~otcextensions.sdk.vpc.v1.subnet.Subnet`
304+ """
305+ return self ._find (
306+ _subnet .Subnet , name_or_id ,
307+ ignore_missing = ignore_missing ,
308+ project_id = self .get_project_id ())
309+
310+ def update_subnet (self , subnet , ** attrs ):
311+ """Update subnet
312+
313+ :param subnet: subnet id or an instance of
314+ :class:`~otcextensions.sdk.vpc.v1.subnet.Subnet`
315+
316+ :param dict attrs: The attributes to update on the subnet
317+ represented by ``subnet``.
318+ """
319+ attrs ['project_id' ] = self .get_project_id ()
320+
321+ rs = self ._get_resource (_subnet .Subnet , subnet )
322+ if rs .vpc_id is None and 'vpc_id' not in attrs :
323+ raise AttributeError ('Updating subnet requires VPC ID' )
324+ vpc_id = attrs .pop ('vpc_id' , rs .vpc_id ) # vpc_id can't be changed
325+
326+ attrs .pop ('base_path' , None )
327+ base_path = _subnet .vpc_subnet_base_path (vpc_id )
328+
329+ return self ._update (_subnet .Subnet ,
330+ subnet ,
331+ base_path = base_path ,
332+ ** attrs )
333+
334+ def _delete (self , resource_type , value , ignore_missing = True , ** attrs ):
335+ """Override of ``_delete`` with support of ``base_path``"""
336+ base_path = attrs .pop ('base_path' , None )
337+
338+ res = self ._get_resource (resource_type , value , ** attrs )
339+
340+ try :
341+ rv = res .delete (self , base_path = base_path )
342+ except exceptions .ResourceNotFound :
343+ if ignore_missing :
344+ return None
345+ raise
346+
347+ return rv
348+
349+ def delete_subnet (self , subnet , vpc_id = None , ignore_missing = True ):
350+ """Delete a subnet
351+
352+ :param subnet: subnet id or an instance of
353+ :class:`~otcextensions.sdk.vpc.v1.subnet.Subnet`
354+
355+ :param vpc_id: VPC id. By default, taken from ``subnet``, if provided.
356+
357+ :param bool ignore_missing: When set to ``False``
358+ :class:`~openstack.exceptions.ResourceNotFound` will be raised when
359+ the subnet route does not exist.
360+ When set to ``True``, no exception will be set when attempting to
361+ delete a nonexistent route.
362+
363+ :returns: none
364+ """
365+ sn_res = self ._get_resource (_subnet .Subnet , subnet )
366+ sn_res .vpc_id = vpc_id or sn_res .vpc_id
367+ if sn_res .vpc_id is None :
368+ raise AttributeError ('Deleting subnet requires VPC ID' )
369+
370+ base_path = _subnet .vpc_subnet_base_path (sn_res .vpc_id )
371+
372+ return self ._delete (_subnet .Subnet , subnet ,
373+ ignore_missing = ignore_missing ,
374+ base_path = base_path )
375+
252376 # ========== Project cleanup ==========
253377 def _get_cleanup_dependencies (self ):
254378 return {
0 commit comments