コード例 #1
0
def main():
    """
    Main function
    :returns: Static Route Information
    """
    module = AnsibleModule(
        argument_spec=dict(
            auth=dict(type='dict'),
            region=dict(default='na', type='str'),
            datacenter=dict(required=True, type='str'),
            network_domain=dict(required=True, type='str'),
            name=dict(default=None, required=False, type='str'),
            cidr=dict(default=None, required=False, type='str'),
            next_hop=dict(default=None, required=False, type='str'),
            version=dict(default=4, required=False, type='int', choices=[4, 6])
        ),
        supports_check_mode=True
    )
    try:
        credentials = get_credentials(module)
    except ImportError as e:
        module.fail_json(msg='{0}'.format(e))
    network_domain_name = module.params.get('network_domain')
    datacenter = module.params.get('datacenter')
    name = module.params.get('name')
    routes = []
    route = network_cidr = None

    # Check Imports
    if not HAS_IPADDRESS:
        module.fail_json(msg='Missing Python module: ipaddress')

    # Check the region supplied is valid
    regions = get_regions()
    if module.params.get('region') not in regions:
        module.fail_json(msg='Invalid region. Regions must be one of {0}'.format(regions))

    if credentials is False:
        module.fail_json(msg='Could not load the user credentials')

    client = NTTMCPClient(credentials, module.params.get('region'))

    # Check to see the CIDR provided is valid
    if module.params.get('cidr'):
        try:
            network_cidr = ip_net(unicode(module.params.get('cidr')))
        except (AddressValueError, ValueError) as e:
            module.fail_json(msg='Invalid network CIDR format {0}: {1}'.format(module.params.get('cidr'), e))

    # Get the CND
    try:
        network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter)
        network_domain_id = network.get('id')
    except (KeyError, IndexError, NTTMCPAPIException):
        module.fail_json(msg='Could not find the Cloud Network Domain: {0}'.format(network_domain_name))

    # Check if a route already exists for this name
    try:
        if name:
            routes = client.list_static_routes(network_domain_id=network_domain_id, name=name)
        if not routes and module.params.get('cidr'):
            # If no matching routes were found for the name check to see if the supplied
            # network parameters match a rule with a different name
            routes = client.list_static_routes(network_domain_id=network_domain_id,
                                               name=None,
                                               network=str(network_cidr.network_address),
                                               prefix=network_cidr.prefixlen,
                                               next_hop=module.params.get('next_hop'))
            if len(routes) == 1:
                route = routes[0]
        elif len(routes) == 1:
            route = routes[0]
    except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e:
        module.fail_json(msg='Failed to get a list of existing Static Routes - {0}'.format(e))

    if route:
        return_data = return_object('route')
        return_data = return_object('route')
        return_data['count'] = len(return_data.get('route'))
        module.exit_json(data=route)
    list_static_routes(module, client, network_domain_id, network_cidr)
コード例 #2
0
def main():
    """
    Main function

    :returns: Static route Information
    """
    module = AnsibleModule(
        argument_spec=dict(
            auth=dict(type='dict'),
            region=dict(default='na', type='str'),
            datacenter=dict(required=True, type='str'),
            network_domain=dict(required=True, type='str'),
            name=dict(default=None, required=False, type='str'),
            description=dict(default=None, required=False, type='str'),
            cidr=dict(default=None, required=False, type='str'),
            next_hop=dict(default=None, required=False, type='str'),
            state=dict(default='present', choices=['present', 'absent', 'restore'])
        ),
        supports_check_mode=True
    )
    try:
        credentials = get_credentials(module)
    except ImportError as e:
        module.fail_json(msg='{0}'.format(e))
    name = module.params.get('name')
    network_domain_name = module.params.get('network_domain')
    datacenter = module.params.get('datacenter')
    state = module.params.get('state')
    route = network_cidr = None
    routes = []

    # Check Imports
    if not HAS_IPADDRESS:
        module.fail_json(msg='Missing Python module: ipaddress')

    # Check the region supplied is valid
    regions = get_regions()
    if module.params.get('region') not in regions:
        module.fail_json(msg='Invalid region. Regions must be one of {0}'.format(regions))

    if credentials is False:
        module.fail_json(msg='Error: Could not load the user credentials')

    try:
        client = NTTMCPClient(credentials, module.params.get('region'))
    except NTTMCPAPIException as e:
        module.fail_json(msg=e.msg)

    # Check to see the CIDR provided is valid
    if module.params.get('cidr'):
        try:
            network_cidr = ip_net(unicode(module.params.get('cidr')))
        except (AddressValueError, ValueError) as e:
            module.fail_json(msg='Invalid network CIDR format {0}: {1}'.format(module.params.get('cidr'), e))

    # Get a list of existing CNDs and check if the name already exists
    try:
        network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter)
        network_domain_id = network.get('id')
    except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e:
        module.fail_json(msg='Failed to get a list of Cloud Network Domains - {0}'.format(e))

    # Check if a route already exists for this name
    if state != 'restore':
        try:
            if name:
                routes = client.list_static_routes(network_domain_id=network_domain_id, name=name)
            if not routes and network_cidr:
                # If no matching routes were found for the name check to see if the supplied
                # network parameters match a rule with a different name
                routes = client.list_static_routes(network_domain_id=network_domain_id,
                                                   name=None,
                                                   network=str(network_cidr.network_address),
                                                   prefix=network_cidr.prefixlen,
                                                   next_hop=module.params.get('next_hop'))
                if len(routes) == 1:
                    route = routes[0]
            elif len(routes) == 1:
                route = routes[0]
        except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e:
            module.fail_json(msg='Failed to get a list of existing Static Routes - {0}'.format(e))

    try:
        if state == 'present':
            if not route:
                # Implement Check Mode
                if module.check_mode:
                    module.exit_json(msg='A new static route will be created for {0}'.format(str(network_cidr)))
                create_static_route(module, client, network_domain_id, network_cidr)
            else:
                # Static Routes cannot be updated. The old one must be removed and a new one created with the new parameters
                if compare_static_route(module, network_cidr, route):
                    delete_static_route(module, client, route.get('id'))
                    create_static_route(module, client, network_domain_id, network_cidr)
                module.exit_json(data=route)
        elif state == 'restore':
            # Implement Check Mode
            if module.check_mode:
                module.exit_json(msg='The routes for Network Domain {0} will be restored to defaults'.format(
                    module.params.get('network_domain')))
            client.restore_static_routes(network_domain_id)
            module.exit_json(changed=True, msg='Successfully restored the default Static Routes')
        elif state == 'absent':
            if route:
                # Implement Check Mode
                if module.check_mode:
                    module.exit_json(msg='An existing static route was found for {0} and will be removed'.format(
                        route.get('name')))
                delete_static_route(module, client, route.get('id'))
                module.exit_json(changed=True, msg='Successfully deleted the Static Route rule')
            else:
                module.exit_json(msg='No existing static route was matched')
    except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e:
        module.fail_json(msg='Could not update the static route - {0}'.format(e))