Пример #1
0
def test_check_required_one_of_missing(arguments_terms):
    params = {"state": "present"}
    expected = "one of the following is required: path, owner"

    with pytest.raises(TypeError) as e:
        check_required_one_of(arguments_terms, params)

    assert to_native(e.value) == expected
Пример #2
0
def test_check_required_one_of_context(arguments_terms):
    params = {"state": "present"}
    expected = "one of the following is required: path, owner found in foo_context"
    option_context = ["foo_context"]

    with pytest.raises(TypeError) as e:
        check_required_one_of(arguments_terms, params, option_context)

    assert to_native(e.value) == expected
Пример #3
0
    def _check_required_one_of(self, spec, param=None):
        if spec is None:
            return

        if param is None:
            param = self.params

        try:
            check_required_one_of(spec, param)
        except TypeError as e:
            msg = to_native(e)
            if self._options_context:
                msg += " found in %s" % " -> ".join(self._options_context)
            self.fail_json(msg=msg)
Пример #4
0
def map_params_to_obj(module):
    obj = []
    aggregate = module.params.get("aggregate")
    if aggregate:
        for item in aggregate:
            for key in item:
                if item.get(key) is None:
                    item[key] = module.params[key]

            d = item.copy()

            if not d["vlan_id"]:
                module.fail_json(msg="vlan_id is required")

            d["vlan_id"] = str(d["vlan_id"])
            try:
                check_required_one_of(module.required_one_of, item)
            except TypeError as exc:
                module.fail_json(to_text(exc))

            obj.append(d)
    else:
        obj.append({
            "vlan_id":
            str(module.params["vlan_id"]),
            "name":
            module.params["name"],
            "address":
            module.params["address"],
            "state":
            module.params["state"],
            "interfaces":
            module.params["interfaces"],
            "associated_interfaces":
            module.params["associated_interfaces"],
        })

    return obj
Пример #5
0
def test_check_required_one_of_provided(arguments_terms):
    params = {"state": "present", "path": "/foo"}
    assert check_required_one_of(arguments_terms, params) == []
Пример #6
0
def test_check_required_one_of():
    assert check_required_one_of([], {}) == []
def run_module():
    module_args = dict(cert_not_after=dict(type="str"),
                       cert_not_before=dict(type="str"),
                       force=dict(type="bool"),
                       host=dict(type="bool"),
                       k8ssa_token_path=dict(type="path"),
                       key=dict(type="path"),
                       kid=dict(type="str"),
                       name=dict(aliases=["subject"],
                                 type="str",
                                 required=True),
                       not_after=dict(type="str"),
                       not_before=dict(type="str"),
                       output_file=dict(type="path"),
                       principal=dict(type="list", elements="str"),
                       provisioner=dict(type="str", aliases=["issuer"]),
                       provisioner_password_file=dict(type="path",
                                                      no_log=False),
                       return_token=dict(type="bool"),
                       revoke=dict(type="bool"),
                       renew=dict(type="bool"),
                       rekey=dict(type="bool"),
                       san=dict(type="list", elements="str"),
                       ssh=dict(type="bool"),
                       sshpop_cert=dict(type="str"),
                       sshpop_key=dict(type="path"),
                       x5c_cert=dict(type="str"),
                       x5c_key=dict(type="path"),
                       step_cli_executable=dict(type="path",
                                                default="step-cli"))

    result = dict(changed=False, stdout="", stderr="", msg="")
    module = AnsibleModule(argument_spec={
        **module_args,
        **connection_argspec
    },
                           supports_check_mode=True)

    check_mutually_exclusive(["return_token", "output_file"], module.params)
    check_required_one_of(["return_token", "output_file"], module.params)

    check_step_cli_install(module, module.params["step_cli_executable"],
                           result)

    # Positional Parameters
    params = ["ca", "token", module.params["name"]]
    # Regular args
    args = [
        "cert_not_after", "cert_not_before", "force", "host",
        "k8ssa_token_path", "key", "kid", "not_after", "not_before",
        "output_file", "principal", "provisioner", "provisioner_password_file",
        "revoke", "renew", "rekey", "san", "ssh", "sshpop_cert", "sshpop_key",
        "x5c_cert", "x5c_key"
    ]
    # All parameters can be converted to a mapping by just appending -- and replacing the underscores
    args = {arg: "--{a}".format(a=arg.replace("_", "-")) for arg in args}

    result = run_step_cli_command(module.params["step_cli_executable"], params,
                                  module, result, {
                                      **args,
                                      **connection_run_args
                                  })
    result["changed"] = True
    if module.params["return_token"]:
        result["token"] = result["stdout"]
        result["stdout"] = ""
        result["stdout_lines"] = ""
    module.exit_json(**result)