2017-02-18 01:24:43 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# (c) 2017 Apstra Inc, <community@apstra.com>
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2017-02-18 01:24:43 +00:00
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: aos_asn_pool
|
|
|
|
author: Damien Garros (@dgarros)
|
|
|
|
version_added: "2.3"
|
|
|
|
short_description: Manage AOS ASN Pool
|
|
|
|
description:
|
|
|
|
- Apstra AOS ASN Pool module let you manage your ASN Pool easily. You can create
|
|
|
|
and delete ASN Pool by Name, ID or by using a JSON File. This module
|
|
|
|
is idempotent and support the I(check) mode. It's using the AOS REST API.
|
|
|
|
requirements:
|
|
|
|
- "aos-pyez >= 0.6.0"
|
|
|
|
options:
|
|
|
|
session:
|
|
|
|
description:
|
2017-02-20 18:15:40 +00:00
|
|
|
- An existing AOS session as obtained by M(aos_login) module.
|
2017-02-18 01:24:43 +00:00
|
|
|
required: true
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of the ASN Pool to manage.
|
|
|
|
Only one of I(name), I(id) or I(content) can be set.
|
|
|
|
id:
|
|
|
|
description:
|
|
|
|
- AOS Id of the ASN Pool to manage.
|
|
|
|
Only one of I(name), I(id) or I(content) can be set.
|
|
|
|
content:
|
|
|
|
description:
|
|
|
|
- Datastructure of the ASN Pool to manage. The data can be in YAML / JSON or
|
|
|
|
directly a variable. It's the same datastructure that is returned
|
|
|
|
on success in I(value).
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicate what is the expected state of the ASN Pool (present or not).
|
|
|
|
default: present
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
ranges:
|
|
|
|
description:
|
|
|
|
- List of ASNs ranges to add to the ASN Pool. Each range must have 2 values.
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
|
|
|
|
- name: "Create ASN Pool"
|
|
|
|
aos_asn_pool:
|
2017-02-21 14:56:12 +00:00
|
|
|
session: "{{ aos_session }}"
|
2017-02-18 01:24:43 +00:00
|
|
|
name: "my-asn-pool"
|
|
|
|
ranges:
|
|
|
|
- [ 100, 200 ]
|
|
|
|
state: present
|
|
|
|
register: asnpool
|
|
|
|
|
|
|
|
- name: "Save ASN Pool into a file in JSON"
|
|
|
|
copy:
|
|
|
|
content: "{{ asnpool.value | to_nice_json }}"
|
|
|
|
dest: resources/asn_pool_saved.json
|
|
|
|
|
|
|
|
- name: "Save ASN Pool into a file in YAML"
|
|
|
|
copy:
|
|
|
|
content: "{{ asnpool.value | to_nice_yaml }}"
|
|
|
|
dest: resources/asn_pool_saved.yaml
|
|
|
|
|
|
|
|
|
|
|
|
- name: "Delete ASN Pool"
|
|
|
|
aos_asn_pool:
|
2017-02-21 14:56:12 +00:00
|
|
|
session: "{{ aos_session }}"
|
2017-02-18 01:24:43 +00:00
|
|
|
name: "my-asn-pool"
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
- name: "Load ASN Pool from File(JSON)"
|
|
|
|
aos_asn_pool:
|
2017-02-21 14:56:12 +00:00
|
|
|
session: "{{ aos_session }}"
|
2017-02-18 01:24:43 +00:00
|
|
|
content: "{{ lookup('file', 'resources/asn_pool_saved.json') }}"
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: "Delete ASN Pool from File(JSON)"
|
|
|
|
aos_asn_pool:
|
2017-02-21 14:56:12 +00:00
|
|
|
session: "{{ aos_session }}"
|
2017-02-18 01:24:43 +00:00
|
|
|
content: "{{ lookup('file', 'resources/asn_pool_saved.json') }}"
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
- name: "Load ASN Pool from File(Yaml)"
|
|
|
|
aos_asn_pool:
|
2017-02-21 14:56:12 +00:00
|
|
|
session: "{{ aos_session }}"
|
2017-02-18 01:24:43 +00:00
|
|
|
content: "{{ lookup('file', 'resources/asn_pool_saved.yaml') }}"
|
|
|
|
state: present
|
|
|
|
register: test
|
|
|
|
|
|
|
|
- name: "Delete ASN Pool from File(Yaml)"
|
|
|
|
aos_asn_pool:
|
2017-02-21 14:56:12 +00:00
|
|
|
session: "{{ aos_session }}"
|
2017-02-18 01:24:43 +00:00
|
|
|
content: "{{ lookup('file', 'resources/asn_pool_saved.yaml') }}"
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURNS = '''
|
|
|
|
name:
|
|
|
|
description: Name of the ASN Pool
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: Private-ASN-pool
|
|
|
|
|
|
|
|
id:
|
|
|
|
description: AOS unique ID assigned to the ASN Pool
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: fcc4ac1c-e249-4fe7-b458-2138bfb44c06
|
|
|
|
|
|
|
|
value:
|
|
|
|
description: Value of the object as returned by the AOS Server
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
sample: {'...'}
|
|
|
|
'''
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2017-12-03 16:12:30 +00:00
|
|
|
from ansible.module_utils.network.aos.aos import get_aos_session, find_collection_item, do_load_resource, check_aos_version, content_to_dict
|
2017-02-18 01:24:43 +00:00
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2017-02-18 01:24:43 +00:00
|
|
|
def check_ranges_are_valid(module, ranges):
|
|
|
|
|
|
|
|
i = 1
|
|
|
|
for range in ranges:
|
2017-12-07 16:27:06 +00:00
|
|
|
if not isinstance(range, list):
|
2017-02-18 01:24:43 +00:00
|
|
|
module.fail_json(msg="Range (%i) must be a list not %s" % (i, type(range)))
|
|
|
|
elif len(range) != 2:
|
|
|
|
module.fail_json(msg="Range (%i) must be a list of 2 members, not %i" % (i, len(range)))
|
2017-12-07 16:27:06 +00:00
|
|
|
elif not isinstance(range[0], int):
|
|
|
|
module.fail_json(msg="1st element of range (%i) must be integer instead of %s " % (i, type(range[0])))
|
|
|
|
elif not isinstance(range[1], int):
|
|
|
|
module.fail_json(msg="2nd element of range (%i) must be integer instead of %s " % (i, type(range[1])))
|
2017-02-18 01:24:43 +00:00
|
|
|
elif range[1] <= range[0]:
|
|
|
|
module.fail_json(msg="2nd element of range (%i) must be bigger than 1st " % (i))
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2017-02-18 01:24:43 +00:00
|
|
|
def get_list_of_range(asn_pool):
|
|
|
|
ranges = []
|
|
|
|
|
|
|
|
for range in asn_pool.value['ranges']:
|
2017-12-07 16:27:06 +00:00
|
|
|
ranges.append([range['first'], range['last']])
|
2017-02-18 01:24:43 +00:00
|
|
|
|
|
|
|
return ranges
|
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2017-02-18 01:24:43 +00:00
|
|
|
def create_new_asn_pool(asn_pool, name, ranges):
|
|
|
|
|
|
|
|
# Create value
|
|
|
|
datum = dict(display_name=name, ranges=[])
|
|
|
|
for range in ranges:
|
2017-12-07 16:27:06 +00:00
|
|
|
datum['ranges'].append(dict(first=range[0], last=range[1]))
|
2017-02-18 01:24:43 +00:00
|
|
|
|
|
|
|
asn_pool.datum = datum
|
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
# Write to AOS
|
2017-02-18 01:24:43 +00:00
|
|
|
return asn_pool.write()
|
|
|
|
|
|
|
|
|
|
|
|
def asn_pool_absent(module, aos, my_pool):
|
|
|
|
|
|
|
|
margs = module.params
|
|
|
|
|
|
|
|
# If the module do not exist, return directly
|
|
|
|
if my_pool.exists is False:
|
|
|
|
module.exit_json(changed=False, name=margs['name'], id='', value={})
|
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
# Check if object is currently in Use or Not
|
2017-02-18 01:24:43 +00:00
|
|
|
# If in Use, return an error
|
|
|
|
if my_pool.value:
|
|
|
|
if my_pool.value['status'] != 'not_in_use':
|
|
|
|
module.fail_json(msg="Unable to delete ASN Pool '%s' is currently in use" % my_pool.name)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="ASN Pool object has an invalid format, value['status'] must be defined")
|
|
|
|
|
|
|
|
# If not in check mode, delete Ip Pool
|
|
|
|
if not module.check_mode:
|
|
|
|
try:
|
|
|
|
my_pool.delete()
|
|
|
|
except:
|
2017-06-01 09:45:19 +00:00
|
|
|
module.fail_json(msg="An error occurred, while trying to delete the ASN Pool")
|
2017-02-18 01:24:43 +00:00
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
module.exit_json(changed=True,
|
|
|
|
name=my_pool.name,
|
|
|
|
id=my_pool.id,
|
|
|
|
value={})
|
2017-02-18 01:24:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def asn_pool_present(module, aos, my_pool):
|
|
|
|
|
|
|
|
margs = module.params
|
|
|
|
|
|
|
|
# if content is defined, create object from Content
|
|
|
|
if margs['content'] is not None:
|
|
|
|
|
|
|
|
if 'display_name' in module.params['content'].keys():
|
|
|
|
do_load_resource(module, aos.AsnPools, module.params['content']['display_name'])
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Unable to find display_name in 'content', Mandatory")
|
|
|
|
|
|
|
|
# if asn_pool doesn't exist already, create a new one
|
|
|
|
if my_pool.exists is False and 'name' not in margs.keys():
|
|
|
|
module.fail_json(msg="name is mandatory for module that don't exist currently")
|
|
|
|
|
|
|
|
elif my_pool.exists is False:
|
|
|
|
|
|
|
|
if not module.check_mode:
|
|
|
|
try:
|
|
|
|
my_new_pool = create_new_asn_pool(my_pool, margs['name'], margs['ranges'])
|
|
|
|
my_pool = my_new_pool
|
|
|
|
except:
|
2017-06-01 09:45:19 +00:00
|
|
|
module.fail_json(msg="An error occurred while trying to create a new ASN Pool ")
|
2017-02-18 01:24:43 +00:00
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
module.exit_json(changed=True,
|
|
|
|
name=my_pool.name,
|
|
|
|
id=my_pool.id,
|
|
|
|
value=my_pool.value)
|
2017-02-18 01:24:43 +00:00
|
|
|
|
|
|
|
# Currently only check if the pool exist or not
|
|
|
|
# if exist return change false
|
|
|
|
#
|
|
|
|
# Later it would be good to check if the list of ASN are same
|
|
|
|
# if pool already exist, check if list of ASN is the same
|
|
|
|
# if same just return the object and report change false
|
|
|
|
# if set(get_list_of_range(my_pool)) == set(margs['ranges']):
|
2017-12-07 16:27:06 +00:00
|
|
|
module.exit_json(changed=False,
|
|
|
|
name=my_pool.name,
|
|
|
|
id=my_pool.id,
|
|
|
|
value=my_pool.value)
|
2017-02-18 01:24:43 +00:00
|
|
|
|
|
|
|
# ########################################################
|
|
|
|
# Main Function
|
|
|
|
# ########################################################
|
2017-12-07 16:27:06 +00:00
|
|
|
|
|
|
|
|
2017-02-18 01:24:43 +00:00
|
|
|
def asn_pool(module):
|
|
|
|
|
|
|
|
margs = module.params
|
|
|
|
|
|
|
|
try:
|
|
|
|
aos = get_aos_session(module, margs['session'])
|
|
|
|
except:
|
|
|
|
module.fail_json(msg="Unable to login to the AOS server")
|
|
|
|
|
|
|
|
item_name = False
|
|
|
|
item_id = False
|
|
|
|
|
|
|
|
# Check ID / Name and Content
|
|
|
|
if margs['content'] is not None:
|
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
content = content_to_dict(module, margs['content'])
|
2017-02-18 01:24:43 +00:00
|
|
|
|
|
|
|
if 'display_name' in content.keys():
|
|
|
|
item_name = content['display_name']
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Unable to extract 'display_name' from 'content'")
|
|
|
|
|
|
|
|
elif margs['name'] is not None:
|
|
|
|
item_name = margs['name']
|
|
|
|
|
|
|
|
elif margs['id'] is not None:
|
|
|
|
item_id = margs['id']
|
|
|
|
|
|
|
|
# If ranges are provided, check if they are valid
|
|
|
|
if 'ranges' in margs.keys():
|
|
|
|
check_ranges_are_valid(module, margs['ranges'])
|
|
|
|
|
|
|
|
# ----------------------------------------------------
|
|
|
|
# Find Object if available based on ID or Name
|
|
|
|
# ----------------------------------------------------
|
|
|
|
try:
|
|
|
|
my_pool = find_collection_item(aos.AsnPools,
|
2017-12-07 16:27:06 +00:00
|
|
|
item_name=item_name,
|
|
|
|
item_id=item_id)
|
2017-02-18 01:24:43 +00:00
|
|
|
except:
|
|
|
|
module.fail_json(msg="Unable to find the IP Pool based on name or ID, something went wrong")
|
|
|
|
|
|
|
|
# ----------------------------------------------------
|
|
|
|
# Proceed based on State value
|
|
|
|
# ----------------------------------------------------
|
|
|
|
if margs['state'] == 'absent':
|
|
|
|
|
|
|
|
asn_pool_absent(module, aos, my_pool)
|
|
|
|
|
|
|
|
elif margs['state'] == 'present':
|
|
|
|
|
|
|
|
asn_pool_present(module, aos, my_pool)
|
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2017-02-18 01:24:43 +00:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
session=dict(required=True, type="dict"),
|
2017-12-07 16:27:06 +00:00
|
|
|
name=dict(required=False),
|
|
|
|
id=dict(required=False),
|
2017-02-18 01:24:43 +00:00
|
|
|
content=dict(required=False, type="json"),
|
2017-12-07 16:27:06 +00:00
|
|
|
state=dict(required=False,
|
|
|
|
choices=['present', 'absent'],
|
|
|
|
default="present"),
|
2017-02-18 01:24:43 +00:00
|
|
|
ranges=dict(required=False, type="list", default=[])
|
|
|
|
),
|
2017-12-07 16:27:06 +00:00
|
|
|
mutually_exclusive=[('name', 'id', 'content')],
|
2017-02-18 01:24:43 +00:00
|
|
|
required_one_of=[('name', 'id', 'content')],
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check if aos-pyez is present and match the minimum version
|
|
|
|
check_aos_version(module, '0.6.0')
|
|
|
|
|
|
|
|
asn_pool(module)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|