def add_pritunl_organization(module): result = {} org_name = module.params.get("name") org_obj_list = list_pritunl_organizations(**dict_merge( get_pritunl_settings(module), {"filters": { "name": org_name }}, )) # If the organization already exists if len(org_obj_list) > 0: result["changed"] = False result["response"] = org_obj_list[0] else: # Otherwise create it response = post_pritunl_organization(**dict_merge( get_pritunl_settings(module), {"organization_name": org_name}, )) result["changed"] = True result["response"] = response module.exit_json(**result)
def remove_pritunl_user(module): result = {} org_name = module.params.get("organization") user_name = module.params.get("user_name") 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 remove user '%s' from a non existing organization '%s'" % (user_name, org_name)) org_id = org_obj_list[0]["id"] # Grab existing users from this org users = list_pritunl_users(**dict_merge( get_pritunl_settings(module), { "organization_id": org_id, "filters": { "name": user_name }, }, )) # Check if the pritunl user exists, if not, do nothing if len(users) == 0: result["changed"] = False result["response"] = {} # Otherwise remove the org from Pritunl else: response = delete_pritunl_user(**dict_merge( get_pritunl_settings(module), { "organization_id": org_id, "user_id": users[0]["id"], }, )) result["changed"] = True result["response"] = response module.exit_json(**result)
def test_list_filtered_pritunl_organization( self, pritunl_settings, get_pritunl_organization_mock, org_filters, org_expected, ): api._get_pritunl_organizations = get_pritunl_organization_mock() response = api.list_pritunl_organizations( **dict_merge(pritunl_settings, {"filters": org_filters})) assert len(response) == 1 assert response[0]["name"] == org_expected
def test_list_all_pritunl_organization( self, pritunl_settings, get_pritunl_organization_mock, org_id, org_user_count, ): api._get_pritunl_organizations = get_pritunl_organization_mock() response = api.list_pritunl_organizations(**pritunl_settings) assert len(response) == 3 for org in response: if org["id"] == org_id: org["user_count"] == org_user_count
def remove_pritunl_organization(module): result = {} org_name = module.params.get("name") force = module.params.get("force") org_obj_list = [] org_obj_list = list_pritunl_organizations( **dict_merge( get_pritunl_settings(module), { "filters": {"name": org_name}, }, ) ) # No organization found if len(org_obj_list) == 0: result["changed"] = False result["response"] = {} else: # Otherwise attempt to delete it org = org_obj_list[0] # Only accept deletion under specific conditions if force or org["user_count"] == 0: response = delete_pritunl_organization( **dict_merge( get_pritunl_settings(module), {"organization_id": org["id"]}, ) ) result["changed"] = True result["response"] = response else: module.fail_json( msg=( "Can not remove organization '%s' with %d attached users. " "Either set 'force' option to true or remove active users " "from the organization" ) % (org_name, org["user_count"]) ) module.exit_json(**result)
def get_pritunl_organizations(module): org_name = module.params.get("organization") organizations = [] organizations = list_pritunl_organizations(**dict_merge( get_pritunl_settings(module), {"filters": { "name": org_name } if org_name else None}, )) if org_name and len(organizations) == 0: # When an org_name is provided but no organization match return an error module.fail_json(msg="Organization '%s' does not exist" % org_name) result = {} result["changed"] = False result["organizations"] = organizations module.exit_json(**result)
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 add_or_update_pritunl_user(module): result = {} org_name = module.params.get("organization") user_name = module.params.get("user_name") user_params = { "name": user_name, "email": module.params.get("user_email"), "groups": module.params.get("user_groups"), "disabled": module.params.get("user_disabled"), "gravatar": module.params.get("user_gravatar"), "mac_addresses": module.params.get("user_mac_addresses"), "type": module.params.get("user_type"), } 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 add user to organization '%s' which does not exist" % org_name) org_id = org_obj_list[0]["id"] # Grab existing users from this org users = list_pritunl_users(**dict_merge( get_pritunl_settings(module), { "organization_id": org_id, "filters": { "name": user_name }, }, )) # Check if the pritunl user already exists if len(users) > 0: # Compare remote user params with local user_params and trigger update if needed user_params_changed = False for key in user_params.keys(): # When a param is not specified grab existing ones to prevent from changing it with the PUT request if user_params[key] is None: user_params[key] = users[0][key] # 'groups' and 'mac_addresses' are list comparison if key == "groups" or key == "mac_addresses": if set(users[0][key]) != set(user_params[key]): user_params_changed = True # otherwise it is either a boolean or a string else: if users[0][key] != user_params[key]: user_params_changed = True # Trigger a PUT on the API to update the current user if settings have changed if user_params_changed: response = post_pritunl_user(**dict_merge( get_pritunl_settings(module), { "organization_id": org_id, "user_id": users[0]["id"], "user_data": user_params, }, )) result["changed"] = True result["response"] = response else: result["changed"] = False result["response"] = users else: response = post_pritunl_user(**dict_merge( get_pritunl_settings(module), { "organization_id": org_id, "user_data": user_params, }, )) result["changed"] = True result["response"] = response module.exit_json(**result)
def test_pritunl_error(self, pritunl_settings, get_pritunl_error_mock): api.pritunl_auth_request = get_pritunl_error_mock() with pytest.raises(api.PritunlException): response = api.list_pritunl_organizations(**pritunl_settings)