2021-03-21 11:30:16 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2021, Florian Dambrine <android.florian@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2021-03-21 11:30:16 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
|
|
|
module: pritunl_user_info
|
|
|
|
author: "Florian Dambrine (@Lowess)"
|
|
|
|
version_added: 2.3.0
|
|
|
|
short_description: List Pritunl Users using the Pritunl API
|
|
|
|
description:
|
|
|
|
- A module to list Pritunl users using the Pritunl API.
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.pritunl
|
2022-11-06 21:16:43 +01:00
|
|
|
- community.general.attributes
|
|
|
|
- community.general.attributes.info_module
|
2021-03-21 11:30:16 +01:00
|
|
|
options:
|
|
|
|
organization:
|
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
aliases:
|
|
|
|
- org
|
|
|
|
description:
|
|
|
|
- The name of the organization the user is part of.
|
|
|
|
|
|
|
|
user_name:
|
|
|
|
type: str
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- Name of the user to filter on Pritunl.
|
|
|
|
|
|
|
|
user_type:
|
|
|
|
type: str
|
|
|
|
required: false
|
|
|
|
default: client
|
|
|
|
choices:
|
|
|
|
- client
|
|
|
|
- server
|
|
|
|
description:
|
2023-06-15 15:48:17 +02:00
|
|
|
- Type of the user O(user_name).
|
2021-03-21 11:30:16 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: List all existing users part of the organization MyOrg
|
|
|
|
community.general.pritunl_user_info:
|
|
|
|
state: list
|
|
|
|
organization: MyOrg
|
|
|
|
|
|
|
|
- name: Search for the user named Florian part of the organization MyOrg
|
|
|
|
community.general.pritunl_user_info:
|
|
|
|
state: list
|
|
|
|
organization: MyOrg
|
|
|
|
user_name: Florian
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
users:
|
|
|
|
description: List of Pritunl users.
|
|
|
|
returned: success
|
|
|
|
type: list
|
|
|
|
elements: dict
|
|
|
|
sample:
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"audit": false,
|
|
|
|
"auth_type": "google",
|
|
|
|
"bypass_secondary": false,
|
|
|
|
"client_to_client": false,
|
|
|
|
"disabled": false,
|
|
|
|
"dns_mapping": null,
|
|
|
|
"dns_servers": null,
|
|
|
|
"dns_suffix": null,
|
|
|
|
"email": "foo@bar.com",
|
|
|
|
"gravatar": true,
|
|
|
|
"groups": [
|
|
|
|
"foo", "bar"
|
|
|
|
],
|
|
|
|
"id": "5d070dafe63q3b2e6s472c3b",
|
|
|
|
"name": "foo@acme.com",
|
|
|
|
"network_links": [],
|
|
|
|
"organization": "58070daee6sf342e6e4s2c36",
|
|
|
|
"organization_name": "Acme",
|
|
|
|
"otp_auth": true,
|
|
|
|
"otp_secret": "35H5EJA3XB2$4CWG",
|
|
|
|
"pin": false,
|
|
|
|
"port_forwarding": [],
|
|
|
|
"servers": [],
|
|
|
|
}
|
|
|
|
]
|
|
|
|
"""
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2021-03-21 11:30:16 +01:00
|
|
|
from ansible.module_utils.common.dict_transformations import dict_merge
|
|
|
|
from ansible_collections.community.general.plugins.module_utils.net_tools.pritunl.api import (
|
|
|
|
PritunlException,
|
|
|
|
get_pritunl_settings,
|
|
|
|
list_pritunl_organizations,
|
|
|
|
list_pritunl_users,
|
|
|
|
pritunl_argument_spec,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_pritunl_user(module):
|
|
|
|
user_name = module.params.get("user_name")
|
|
|
|
user_type = module.params.get("user_type")
|
|
|
|
org_name = module.params.get("organization")
|
|
|
|
|
|
|
|
org_obj_list = []
|
|
|
|
|
|
|
|
org_obj_list = list_pritunl_organizations(
|
|
|
|
**dict_merge(get_pritunl_settings(module), {"filters": {"name": org_name}})
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(org_obj_list) == 0:
|
|
|
|
module.fail_json(
|
|
|
|
msg="Can not list users from the organization '%s' which does not exist"
|
|
|
|
% org_name
|
|
|
|
)
|
|
|
|
|
|
|
|
org_id = org_obj_list[0]["id"]
|
|
|
|
|
|
|
|
users = list_pritunl_users(
|
|
|
|
**dict_merge(
|
|
|
|
get_pritunl_settings(module),
|
|
|
|
{
|
|
|
|
"organization_id": org_id,
|
|
|
|
"filters": (
|
|
|
|
{"type": user_type}
|
|
|
|
if user_name is None
|
|
|
|
else {"name": user_name, "type": user_type}
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
result["changed"] = False
|
|
|
|
result["users"] = users
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = pritunl_argument_spec()
|
|
|
|
|
|
|
|
argument_spec.update(
|
|
|
|
dict(
|
|
|
|
organization=dict(required=True, type="str", aliases=["org"]),
|
|
|
|
user_name=dict(required=False, type="str", default=None),
|
|
|
|
user_type=dict(
|
|
|
|
required=False,
|
|
|
|
choices=["client", "server"],
|
|
|
|
default="client",
|
|
|
|
),
|
|
|
|
)
|
2023-02-04 17:05:08 +01:00
|
|
|
)
|
2021-03-21 11:30:16 +01:00
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
|
|
|
|
|
|
|
try:
|
|
|
|
get_pritunl_user(module)
|
|
|
|
except PritunlException as e:
|
|
|
|
module.fail_json(msg=to_native(e))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|