예제 #1
0
def test_init_analysis(network):
    """Ensure bf_init_analysis does not crash."""
    bf_init_analysis("test_analysis", _stable_question_dir)
예제 #2
0
def run_module():
    # define the available arguments/parameters that a user can pass to
    # the module
    module_args = dict(policy_name=dict(type='str',
                                        required=False,
                                        default=None),
                       host=dict(type='str',
                                 required=False,
                                 default='localhost'),
                       name=dict(type='str', required=True),
                       network=dict(type='str', required=True),
                       new=dict(type='bool', required=False, default=False),
                       path=dict(type='str', required=False))

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(changed=False, result='', result_verbose='', summary='')

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True,
        required_if=[["new", True, ["path", "policy_name"]
                      ]  # path and name are required if adding a new policy
                     ])

    if not pybatfish_found:
        module.fail_json(msg='Python module Pybatfish is required')

    if module.check_mode:
        return result

    snapshot_name = module.params['name']
    policy_name = module.params['policy_name']

    try:
        bf_session.coordinatorHost = module.params['host']
        network = bf_set_network(module.params['network'])
    except Exception as e:
        module.fail_json(msg='Failed to set network: {}'.format(e), **result)

    try:
        if module.params['new']:
            bf_init_analysis(policy_name, module.params['path'])
    except Exception as e:
        module.fail_json(msg='Failed to initialize policy: {}'.format(e),
                         **result)

    try:
        if policy_name is not None:
            policy_results = {
                policy_name: _run_policy(policy_name, snapshot_name)
            }
        else:
            policy_results = {
                a: _run_policy(a, snapshot_name)
                for a in bf_list_analyses()
            }
    except Exception as e:
        module.fail_json(msg='Failed to answer policy: {}'.format(e), **result)

    result['result'] = {}
    result['result_verbose'] = {}
    failure = False
    # If a check's summary.numFailed is 0, we assume the check PASSed
    for policy in policy_results:
        policy_result = policy_results[policy]
        result['result'][policy] = {
            k: PASS if policy_result[k]['summary']['numFailed'] == 0 else FAIL
            for k in policy_result
        }
        failure |= FAIL in result['result'][policy].values()

        result['result_verbose'][policy] = {
            k: policy_result[k]['answerElements'][0]['rows']
            if 'rows' in policy_result[k]['answerElements'][0] else []
            for k in policy_result
        }

    result['summary'] = FAIL if failure else PASS

    module.exit_json(**result)