Exemplo n.º 1
0
def test_check_mutually_exclusive_found(mutually_exclusive_terms):
    params = {
        'string1': 'cat',
        'string2': 'hat',
        'fox': 'red',
        'socks': 'blue',
    }
    expected = "TypeError('parameters are mutually exclusive: string1|string2, box|fox|socks',)"

    with pytest.raises(TypeError) as e:
        check_mutually_exclusive(mutually_exclusive_terms, params)
        assert e.value == expected
Exemplo n.º 2
0
def test_check_mutually_exclusive_none():
    terms = None
    params = {
        'string1': 'cat',
        'fox': 'hat',
    }
    assert check_mutually_exclusive(terms, params) == []
Exemplo n.º 3
0
def test_check_mutually_exclusive_no_params(mutually_exclusive_terms):
    with pytest.raises(TypeError) as te:
        check_mutually_exclusive(mutually_exclusive_terms, None)
        assert "TypeError: 'NoneType' object is not iterable" in to_native(
            te.error)
Exemplo n.º 4
0
def test_check_mutually_exclusive(mutually_exclusive_terms):
    params = {
        'string1': 'cat',
        'fox': 'hat',
    }
    assert check_mutually_exclusive(mutually_exclusive_terms, params) == []
Exemplo n.º 5
0
def main():
    result = {}
    module = AnsibleModule(
        argument_spec=dict(
            category=dict(required=True),
            command=dict(required=True, type='list'),
            baseuri=dict(required=True),
            username=dict(required=True),
            password=dict(required=True, no_log=True),
            manager_attribute_name=dict(default=None),
            manager_attribute_value=dict(default=None),
            manager_attributes=dict(type='dict', default={}),
            timeout=dict(type='int', default=10),
            resource_id=dict()
        ),
        supports_check_mode=False
    )

    category = module.params['category']
    command_list = module.params['command']

    # admin credentials used for authentication
    creds = {'user': module.params['username'],
             'pswd': module.params['password']}

    # timeout
    timeout = module.params['timeout']

    # System, Manager or Chassis ID to modify
    resource_id = module.params['resource_id']

    # Build root URI
    root_uri = "https://" + module.params['baseuri']
    rf_utils = IdracRedfishUtils(creds, root_uri, timeout, module,
                                 resource_id=resource_id, data_modification=True)

    # Check that Category is valid
    if category not in CATEGORY_COMMANDS_ALL:
        module.fail_json(msg=to_native("Invalid Category '%s'. Valid Categories = %s" % (category, CATEGORY_COMMANDS_ALL.keys())))

    # Check that all commands are valid
    for cmd in command_list:
        # Fail if even one command given is invalid
        if cmd not in CATEGORY_COMMANDS_ALL[category]:
            module.fail_json(msg=to_native("Invalid Command '%s'. Valid Commands = %s" % (cmd, CATEGORY_COMMANDS_ALL[category])))

    # check for mutually exclusive commands
    try:
        # check_mutually_exclusive accepts a single list or list of lists that
        # are groups of terms that should be mutually exclusive with one another
        # and checks that against a dictionary
        check_mutually_exclusive(CATEGORY_COMMANDS_MUTUALLY_EXCLUSIVE[category],
                                 dict.fromkeys(command_list, True))

    except TypeError as e:
        module.fail_json(msg=to_native(e))

    # Organize by Categories / Commands

    if category == "Manager":
        # execute only if we find a Manager resource
        result = rf_utils._find_managers_resource()
        if result['ret'] is False:
            module.fail_json(msg=to_native(result['msg']))

        for command in command_list:
            if command in ["SetManagerAttributes", "SetLifecycleControllerAttributes", "SetSystemAttributes"]:
                result = rf_utils.set_manager_attributes(command)

    if any((module.params['manager_attribute_name'], module.params['manager_attribute_value'])):
        module.deprecate(msg='Arguments `manager_attribute_name` and '
                             '`manager_attribute_value` are deprecated. '
                             'Use `manager_attributes` instead for passing in '
                             'the manager attribute name and value pairs',
                             version='2.13')

    # Return data back or fail with proper message
    if result['ret'] is True:
        module.exit_json(changed=result['changed'], msg=to_native(result['msg']))
    else:
        module.fail_json(msg=to_native(result['msg']))