Beispiel #1
0
def update_client(module, array, client):
    """Update API Client"""
    changed = True
    if not module.check_mode:
        changed = False
        if client.enabled != module.params['enabled']:
            try:
                array.patch_api_clients(names=[module.params['name']],
                                        api_clients=flasharray.ApiClientPatch(
                                            enabled=module.params['enabled']))
                changed = True
            except Exception:
                module.fail_json(msg='Failed to update API Client {0}'.format(
                    module.params['name']))
    module.exit_json(changed=changed)
Beispiel #2
0
def create_client(module, array):
    """Create API Client"""
    changed = True
    if not module.check_mode:
        changed = False
        if not 1 <= module.params["token_ttl"] <= 86400:
            module.fail_json(
                msg="token_ttl parameter is out of range (1 to 86400)")
        else:
            token_ttl = module.params["token_ttl"] * 1000
        if not module.params["issuer"]:
            module.params["issuer"] = module.params["name"]
        try:
            client = flasharray.ApiClientPost(
                max_role=module.params["role"],
                issuer=module.params["issuer"],
                access_token_ttl_in_ms=token_ttl,
                public_key=module.params["public_key"],
            )
            res = array.post_api_clients(names=[module.params["name"]],
                                         api_clients=client)
            if res.status_code != 200:
                module.fail_json(
                    msg="Failed to create API CLient {0}. Error message: {1}".
                    format(module.params["name"], res.errors[0].message))
            if module.params["enabled"]:
                try:
                    array.patch_api_clients(
                        names=[module.params["name"]],
                        api_clients=flasharray.ApiClientPatch(
                            enabled=module.params["enabled"]),
                    )
                except Exception:
                    array.delete_api_clients(names=[module.params["name"]])
                    module.fail_json(msg="Failed to create API Client {0}".
                                     format(module.params["name"]))
            changed = True
        except Exception:
            module.fail_json(msg="Failed to create API Client {0}".format(
                module.params["name"]))
    module.exit_json(changed=changed)