summaryrefslogtreecommitdiff
path: root/network/f5/bigip_vlan.py
blob: 40df948f6c6b1ace40e6d45ad85f607c70ca5763 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2016 F5 Networks Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

ANSIBLE_METADATA = {'status': ['preview'],
                    'supported_by': 'community',
                    'version': '1.0'}

DOCUMENTATION = '''
---
module: bigip_vlan
short_description: Manage VLANs on a BIG-IP system
description:
  - Manage VLANs on a BIG-IP system
version_added: "2.2"
options:
  description:
    description:
      - The description to give to the VLAN.
  tagged_interfaces:
    description:
      - Specifies a list of tagged interfaces and trunks that you want to
        configure for the VLAN. Use tagged interfaces or trunks when
        you want to assign a single interface or trunk to multiple VLANs.
    required: false
    aliases:
      - tagged_interface
  untagged_interfaces:
    description:
      - Specifies a list of untagged interfaces and trunks that you want to
        configure for the VLAN.
    required: false
    aliases:
      - untagged_interface
  name:
    description:
      - The VLAN to manage. If the special VLAN C(ALL) is specified with
        the C(state) value of C(absent) then all VLANs will be removed.
    required: true
  state:
    description:
      - The state of the VLAN on the system. When C(present), guarantees
        that the VLAN exists with the provided attributes. When C(absent),
        removes the VLAN from the system.
    required: false
    default: present
    choices:
      - absent
      - present
  tag:
    description:
      - Tag number for the VLAN. The tag number can be any integer between 1
        and 4094. The system automatically assigns a tag number if you do not
        specify a value.
notes:
  - Requires the f5-sdk Python package on the host. This is as easy as pip
    install f5-sdk.
  - Requires BIG-IP versions >= 12.0.0
extends_documentation_fragment: f5
requirements:
  - f5-sdk
author:
  - Tim Rupp (@caphrim007)
'''

EXAMPLES = '''
- name: Create VLAN
  bigip_vlan:
      name: "net1"
      password: "secret"
      server: "lb.mydomain.com"
      user: "admin"
      validate_certs: "no"
  delegate_to: localhost

- name: Set VLAN tag
  bigip_vlan:
      name: "net1"
      password: "secret"
      server: "lb.mydomain.com"
      tag: "2345"
      user: "admin"
      validate_certs: "no"
  delegate_to: localhost

- name: Add VLAN 2345 as tagged to interface 1.1
  bigip_vlan:
      tagged_interface: 1.1
      name: "net1"
      password: "secret"
      server: "lb.mydomain.com"
      tag: "2345"
      user: "admin"
      validate_certs: "no"
  delegate_to: localhost

- name: Add VLAN 1234 as tagged to interfaces 1.1 and 1.2
  bigip_vlan:
      tagged_interfaces:
          - 1.1
          - 1.2
      name: "net1"
      password: "secret"
      server: "lb.mydomain.com"
      tag: "1234"
      user: "admin"
      validate_certs: "no"
  delegate_to: localhost
'''

RETURN = '''
description:
    description: The description set on the VLAN
    returned: changed
    type: string
    sample: foo VLAN
interfaces:
    description: Interfaces that the VLAN is assigned to
    returned: changed
    type: list
    sample: ['1.1','1.2']
name:
    description: The name of the VLAN
    returned: changed
    type: string
    sample: net1
partition:
    description: The partition that the VLAN was created on
    returned: changed
    type: string
    sample: Common
tag:
    description: The ID of the VLAN
    returned: changed
    type: int
    sample: 2345
'''

try:
    from f5.bigip import ManagementRoot
    from icontrol.session import iControlUnexpectedHTTPError
    HAS_F5SDK = True
except ImportError:
    HAS_F5SDK = False


class BigIpVlan(object):
    def __init__(self, *args, **kwargs):
        if not HAS_F5SDK:
            raise F5ModuleError("The python f5-sdk module is required")

        # The params that change in the module
        self.cparams = dict()

        # Stores the params that are sent to the module
        self.params = kwargs
        self.api = ManagementRoot(kwargs['server'],
                                  kwargs['user'],
                                  kwargs['password'],
                                  port=kwargs['server_port'])

    def present(self):
        if self.exists():
            return self.update()
        else:
            return self.create()

    def absent(self):
        changed = False

        if self.exists():
            changed = self.delete()

        return changed

    def read(self):
        """Read information and transform it

        The values that are returned by BIG-IP in the f5-sdk can have encoding
        attached to them as well as be completely missing in some cases.

        Therefore, this method will transform the data from the BIG-IP into a
        format that is more easily consumable by the rest of the class and the
        parameters that are supported by the module.
        """
        p = dict()
        name = self.params['name']
        partition = self.params['partition']
        r = self.api.tm.net.vlans.vlan.load(
            name=name,
            partition=partition
        )
        ifcs = r.interfaces_s.get_collection()
        if hasattr(r, 'tag'):
            p['tag'] = int(r.tag)
        if hasattr(r, 'description'):
            p['description'] = str(r.description)
        if len(ifcs) is not 0:
            untagged = []
            tagged = []
            for x in ifcs:
                if hasattr(x, 'tagged'):
                    tagged.append(str(x.name))
                elif hasattr(x, 'untagged'):
                    untagged.append(str(x.name))
            if untagged:
                p['untagged_interfaces'] = list(set(untagged))
            if tagged:
                p['tagged_interfaces'] = list(set(tagged))
        p['name'] = name
        return p

    def create(self):
        params = dict()

        check_mode = self.params['check_mode']
        description = self.params['description']
        name = self.params['name']
        untagged_interfaces = self.params['untagged_interfaces']
        tagged_interfaces = self.params['tagged_interfaces']
        partition = self.params['partition']
        tag = self.params['tag']

        if tag is not None:
            params['tag'] = tag

        if untagged_interfaces is not None or tagged_interfaces is not None:
            tmp = []
            ifcs = self.api.tm.net.interfaces.get_collection()
            ifcs = [str(x.name) for x in ifcs]

            if len(ifcs) is 0:
                raise F5ModuleError(
                    'No interfaces were found'
                )

            pinterfaces = []
            if untagged_interfaces:
                interfaces = untagged_interfaces
            elif tagged_interfaces:
                interfaces = tagged_interfaces

            for ifc in interfaces:
                ifc = str(ifc)
                if ifc in ifcs:
                    pinterfaces.append(ifc)

            if tagged_interfaces:
                tmp = [dict(name=x, tagged=True) for x in pinterfaces]
            elif untagged_interfaces:
                tmp = [dict(name=x, untagged=True) for x in pinterfaces]

            if tmp:
                params['interfaces'] = tmp

        if description is not None:
            params['description'] = self.params['description']

        params['name'] = name
        params['partition'] = partition

        self.cparams = camel_dict_to_snake_dict(params)
        if check_mode:
            return True

        d = self.api.tm.net.vlans.vlan
        d.create(**params)

        if self.exists():
            return True
        else:
            raise F5ModuleError("Failed to create the VLAN")

    def update(self):
        changed = False
        params = dict()
        current = self.read()

        check_mode = self.params['check_mode']
        description = self.params['description']
        name = self.params['name']
        tag = self.params['tag']
        partition = self.params['partition']
        tagged_interfaces = self.params['tagged_interfaces']
        untagged_interfaces = self.params['untagged_interfaces']

        if untagged_interfaces is not None or tagged_interfaces is not None:
            ifcs = self.api.tm.net.interfaces.get_collection()
            ifcs = [str(x.name) for x in ifcs]

            if len(ifcs) is 0:
                raise F5ModuleError(
                    'No interfaces were found'
                )

            pinterfaces = []
            if untagged_interfaces:
                interfaces = untagged_interfaces
            elif tagged_interfaces:
                interfaces = tagged_interfaces

            for ifc in interfaces:
                ifc = str(ifc)
                if ifc in ifcs:
                    pinterfaces.append(ifc)
                else:
                    raise F5ModuleError(
                        'The specified interface "%s" was not found' % (ifc)
                    )

            if tagged_interfaces:
                tmp = [dict(name=x, tagged=True) for x in pinterfaces]
                if 'tagged_interfaces' in current:
                    if pinterfaces != current['tagged_interfaces']:
                        params['interfaces'] = tmp
                else:
                    params['interfaces'] = tmp
            elif untagged_interfaces:
                tmp = [dict(name=x, untagged=True) for x in pinterfaces]
                if 'untagged_interfaces' in current:
                    if pinterfaces != current['untagged_interfaces']:
                        params['interfaces'] = tmp
                else:
                    params['interfaces'] = tmp

        if description is not None:
            if 'description' in current:
                if description != current['description']:
                    params['description'] = description
            else:
                params['description'] = description

        if tag is not None:
            if 'tag' in current:
                if tag != current['tag']:
                    params['tag'] = tag
            else:
                params['tag'] = tag

        if params:
            changed = True
            params['name'] = name
            params['partition'] = partition
            if check_mode:
                return changed
            self.cparams = camel_dict_to_snake_dict(params)
        else:
            return changed

        r = self.api.tm.net.vlans.vlan.load(
            name=name,
            partition=partition
        )
        r.update(**params)
        r.refresh()

        return True

    def delete(self):
        params = dict()
        check_mode = self.params['check_mode']

        params['name'] = self.params['name']
        params['partition'] = self.params['partition']

        self.cparams = camel_dict_to_snake_dict(params)
        if check_mode:
            return True

        dc = self.api.tm.net.vlans.vlan.load(**params)
        dc.delete()

        if self.exists():
            raise F5ModuleError("Failed to delete the VLAN")
        return True

    def exists(self):
        name = self.params['name']
        partition = self.params['partition']
        return self.api.tm.net.vlans.vlan.exists(
            name=name,
            partition=partition
        )

    def flush(self):
        result = dict()
        state = self.params['state']

        try:
            if state == "present":
                changed = self.present()
            elif state == "absent":
                changed = self.absent()
        except iControlUnexpectedHTTPError as e:
            raise F5ModuleError(str(e))

        result.update(**self.cparams)
        result.update(dict(changed=changed))
        return result


def main():
    argument_spec = f5_argument_spec()

    meta_args = dict(
        description=dict(required=False, default=None),
        tagged_interfaces=dict(required=False, default=None, type='list', aliases=['tagged_interface']),
        untagged_interfaces=dict(required=False, default=None, type='list', aliases=['untagged_interface']),
        name=dict(required=True),
        tag=dict(required=False, default=None, type='int')
    )
    argument_spec.update(meta_args)

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        mutually_exclusive=[
            ['tagged_interfaces', 'untagged_interfaces']
        ]
    )

    try:
        obj = BigIpVlan(check_mode=module.check_mode, **module.params)
        result = obj.flush()

        module.exit_json(**result)
    except F5ModuleError as e:
        module.fail_json(msg=str(e))

from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import camel_dict_to_snake_dict
from ansible.module_utils.f5 import *

if __name__ == '__main__':
    main()