File: interfaces.py

package info (click to toggle)
python-seamicroclient 0.4.0%2B2016.05.20.git.40ee44c664-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 452 kB
  • sloc: python: 2,093; sh: 134; makefile: 15
file content (113 lines) | stat: -rw-r--r-- 3,833 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
Interfaces object.
"""

from seamicroclient import base


class Interface(base.Resource):
    HUMAN_ID = True

    def shutdown(self, **kwargs):
        return self.manager.shutdown(self, **kwargs)

    def no_shutdown(self, **kwargs):
        return self.manager.no_shutdown(self, **kwargs)

    def add_tagged_vlan(self, vlan_id, **kwargs):
        return self.manager.add_tagged_vlan(self, vlan_id, **kwargs)

    def remove_tagged_vlan(self, vlan_id, **kwargs):
        return self.manager.remove_tagged_vlan(self, vlan_id, **kwargs)

    def add_untagged_vlan(self, vlan_id, **kwargs):
        return self.manager.add_untagged_vlan(self, vlan_id, **kwargs)

    def remove_untagged_vlan(self, vlan_id, **kwargs):
        return self.manager.remove_untagged_vlan(self, vlan_id, **kwargs)


class InterfaceManager(base.ManagerWithFind):
    resource_class = Interface

    def get(self, interface):
        """
        Get a interface.

        :param interface: ID of the :class:`Interface` to get.
        :rtype: :class:`Interface`
        """
        return self._get(base.getid(interface),
                         "/interfaces/%s" % base.getid(interface))

    def list(self, filters=None):
        """
        Get a list of interfaces.

        :rtype: list of :class:`Interface`
        """
        return self._list("/interfaces", filters=filters)

    def shutdown(self, interface, **kwargs):
        """
        Shutdown the specified network Interface
        """
        url = "/interfaces/%s/shutdown" % base.getid(interface)
        body = {'value': True}
        return self.api.client.put(url, body=body)

    def no_shutdown(self, interface, **kwargs):
        """
        Start the specified network Interface
        """
        url = "/interfaces/%s/shutdown" % base.getid(interface)
        body = {'value': False}
        return self.api.client.put(url, body=body)

    def add_tagged_vlan(self, interface, vlan_id, **kwargs):
        """
        Add tagged vlan for the given Interface
        """
        url = '/interfaces/%s/vlans/taggedVlans' % base.getid(interface)
        if isinstance(vlan_id, list):
            vlan_id = map(lambda x: str(x), vlan_id)
            body = {'add': ','.join(vlan_id)}
        else:
            body = {'add': str(vlan_id)}
        return self.api.client.put(url, body=body)

    def remove_tagged_vlan(self, interface, vlan_id, **kwargs):
        """
        Remove tagged vlan for the given Interface
        """
        url = '/interfaces/%s/vlans/taggedVlans' % base.getid(interface)
        body = {'remove': str(vlan_id)}
        return self.api.client.put(url, body=body)

    def add_untagged_vlan(self, interface, vlan_id, **kwargs):
        """
        Add untagged vlan for the given Interface
        """
        url = '/interfaces/%s/vlans/untaggedVlans' % base.getid(interface)
        body = {'add': str(vlan_id)}
        return self.api.client.put(url, body=body)

    def remove_untagged_vlan(self, interface, vlan_id, **kwargs):
        """
        Remove untagged vlan for the given Interface
        """
        url = '/interfaces/%s/vlans/untaggedVlans' % base.getid(interface)
        body = {'remove': str(vlan_id)}
        return self.api.client.put(url, body=body)