yakumo.neutron.v2.port

Package yakumo :: Package neutron :: Package v2 :: Module port |
[hide private]
[frames] | no frames]

# Source Code for Module yakumo.neutron.v2.port

      1  # Copyright 2014-2017 by Akira Yoshiyama <akirayoshiyama@gmail.com>.
      2  # All Rights Reserved.
      3  #
      4  #    Licensed under the Apache License, Version 2.0 (the "License"); you may
      5  #    not use this file except in compliance with the License. You may obtain
      6  #    a copy of the License at
      7  #
      8  #         http://www.apache.org/licenses/LICENSE-2.0
      9  #
     10  #    Unless required by applicable law or agreed to in writing, software
     11  #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     12  #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  #    License for the specific language governing permissions and limitations
     14  #    under the License.
     15
     16  """
     17  Resource class and its manager for ports in Networking V2 API
     18  """
     19
     20  from yakumo import base
     21  from yakumo.constant import UNDEF
     22  from yakumo import mapper
     23
     24
     25  FIXED_IP_MAPPING = [
     26      ('ip_address', 'ip_address', mapper.Noop),
     27      ('subnet', 'subnet_id', mapper.Resource('neutron.subnet')),
     28  ]
     29
     30
     31  ATTRIBUTE_MAPPING = [
     32      ('id', 'id', mapper.Noop),
     33      ('name', 'name', mapper.Noop),
     34      ('status', 'status', mapper.Noop),
     35      ('network', 'network_id', mapper.Resource('neutron.network')),
     36      ('project', 'tenant_id', mapper.Resource('project')),
     37      ('device', 'device_id', mapper.Noop),
     38      ('device_owner', 'device_owner', mapper.Noop),
     39      ('allowed_address_pairs', 'allowed_address_pairs', mapper.Noop),
     40      ('mac_address', 'mac_address', mapper.Noop),
     41      ('fixed_ips', 'fixed_ips', mapper.List(mapper.Dict(FIXED_IP_MAPPING))),
     42      ('security_groups', 'security_groups', mapper.Noop),
     43      ('is_enabled', 'admin_state_up', mapper.Noop),
     44  ]
     45
     46
     47 -class Resource(base.Resource):
     48      """Resource class for ports in Networking V2 API"""
     49
     50 -    def update(self, name=UNDEF, network=UNDEF, project=UNDEF, device=UNDEF,
     51                 device_owner=UNDEF, allowed_address_pairs=UNDEF,
     52                 mac_address=UNDEF, fixed_ips=UNDEF, security_groups=UNDEF,
     53                 is_enabled=UNDEF):
     54          """
     55          update properties of a port
     56
     57          @keyword name: Port name
     58          @type name: str
     59          @keyword network: Network
     60          @type network: yakumo.neutron.v2.network.Resource
     61          @keyword project: Project
     62          @type project: yakumo.project.Resource
     63          @keyword device: Attached device (server, router, floating IP, ...)
     64          @type device: Resource object
     65          @keyword device_owner: Device owner
     66          @type device_owner: str
     67          @keyword allowed_address_pairs: Allowed address pairs
     68          @type allowed_address_pairs: str
     69          @keyword mac_address: MAC address
     70          @type mac_address: str
     71          @keyword fixed_ips: List of dictionary with fixed IP and subnet
     72          @type fixed_ips: [{'ip_address': str,
     73                             'subnet': yakumo.subnet.Resource}]
     74          @keyword security_groups: Security groups
     75          @type security_groups: str
     76          @keyword is_enabled: Whether the port is enabled
     77          @type is_enabled: bool
     78          @rtype: None
     79          """
     80          super(Resource, self).update(
     81              name=name,
     82              network=network,
     83              project=project,
     84              device=device,
     85              device_owner=device_owner,
     86              allowed_address_pairs=allowed_address_pairs,
     87              mac_address=mac_address,
     88              fixed_ips=fixed_ips,
     89              security_groups=security_groups,
     90              is_enabled=is_enabled)
     91
     92
     93 -class Manager(base.Manager):
     94      """Manager class for ports in Networking V2 API"""
     95
     96      resource_class = Resource
     97      service_type = 'network'
     98      _attr_mapping = ATTRIBUTE_MAPPING
     99      _json_resource_key = 'port'
    100      _json_resources_key = 'ports'
    101      _url_resource_path = '/v2.0/ports'
    102
    103 -    def _attr2json(self, attrs):
    104          if isinstance(attrs['device'], base.Resource):
    105              attrs['device'] = attrs['device'].get_id()
    106          return super(Manager, self)._attr2json(attrs)
    107
    108 -    def _json2attr(self, json_params):
    109          ret = super(Manager, self)._json2attr(json_params)
    110          owner = json_params.get('device_owner')
    111          device = ret['device']
    112          if owner in ('compute:nova', 'compute:None'):
    113              ret['device'] = self._client.server.get_empty(device)
    114          elif owner == 'network:router_interface':
    115              ret['device'] = self._client.router.get_empty(device)
    116          return ret
    117
    118 -    def create(self, name=UNDEF, network=UNDEF, project=UNDEF, device=UNDEF,
    119                 device_owner=UNDEF, allowed_address_pairs=UNDEF,
    120                 mac_address=UNDEF, fixed_ips=UNDEF, security_groups=UNDEF,
    121                 is_enabled=UNDEF):
    122          """
    123          Create a port
    124
    125          @keyword name: Port name
    126          @type name: str
    127          @keyword network: Network
    128          @type network: yakumo.neutron.v2.network.Resource
    129          @keyword project: Project
    130          @type project: yakumo.project.Resource
    131          @keyword device: Attached device (server, router, floating IP, ...)
    132          @type device: Resource object
    133          @keyword device_owner: Device owner
    134          @type device_owner: str
    135          @keyword allowed_address_pairs: Allowed address pairs
    136          @type allowed_address_pairs: str
    137          @keyword mac_address: MAC address
    138          @type mac_address: str
    139          @keyword fixed_ips: Fixed IP addresses
    140          @type fixed_ips: str
    141          @keyword security_groups: Security groups
    142          @type security_groups: str
    143          @keyword is_enabled: Whether the port is enabled
    144          @type is_enabled: bool
    145          @return: Created port
    146          @rtype: yakumo.neutron.v2.port.Resource
    147          """
    148          return super(Manager, self).create(
    149              name=name,
    150              network=network,
    151              project=project,
    152              device=device,
    153              device_owner=device_owner,
    154              allowed_address_pairs=allowed_address_pairs,
    155              mac_address=mac_address,
    156              fixed_ips=fixed_ips,
    157              security_groups=security_groups,
    158              is_enabled=is_enabled)
    159