-
-
Save mwolff44/8ab40782ec6447347f1d8e346f0d1113 to your computer and use it in GitHub Desktop.
Add Wazo webrtc lines to users that don't have any an UC and CRM subscription
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Copyright 2020 The Wazo Authors (see the AUTHORS file) | |
# SPDX-License-Identifier: GPL-3.0-or-later | |
from wazo_auth_client import Client as Auth | |
from wazo_confd_client import Client as Confd | |
# Please add a web service user with acl confd.# | |
# in order to use ./add-webrtc-line | |
host = 'localhost' # Fill with your Wazo hostname if running the script remotely | |
username = "api-user" # Fill with your username | |
password = "secret" # Fill with your password | |
extension_start = '1100' # Start of the extension range for selecting users | |
extension_end = '1199' # End of the extension range. | |
tenant_uuid = 'aaa-bbb-ccc' # Fill with your tenant UUID | |
# ############### Do no modify ############## | |
def find_internal_context(tenant_uuid): | |
contexts = confd.contexts.list(tenant_uuid=tenant_uuid)['items'] | |
internal_contexts = [context for context in contexts if context['type'] == 'internal'] | |
if len(internal_contexts) != 1: | |
raise Exception(f'Tenant {tenant_uuid} has {len(internal_contexts)} internal contexts') | |
return internal_contexts.pop() | |
def extension_in_range(extension, start, end): | |
return start <= extension <= end | |
def find_user_extension(user, context): | |
user_extensions = set((extension['id'], extension['exten']) for line in user['lines'] for extension in line['extensions'] if extension['context'] == context) | |
if len(user_extensions) != 1: | |
return None, None | |
return user_extensions.pop() | |
def find_users_in_range(context, extension_start, extension_end): | |
all_users = confd.users.list()['items'] | |
for user in all_users: | |
extension_id, extension = find_user_extension(user, context) | |
if not extension: | |
print(f'Skipping user {user["firstname"]} {user["lastname"]}: user does not have only one extension') | |
continue | |
if extension_in_range(extension, extension_start, extension_end): | |
yield extension_id, user | |
def endpoint_is_webrtc(endpoint): | |
for option_name, option_value in endpoint['options']: | |
if option_name == 'webrtc' and option_value == 'yes': | |
return True | |
return False | |
def find_webrtc_endpoint_ids(): | |
all_endpoints = confd.endpoints_sip.list()['items'] | |
return (endpoint['id'] for endpoint in all_endpoints if endpoint_is_webrtc(endpoint)) | |
def user_has_webrtc_line(user, webrtc_endpoint_ids): | |
endpoint_sip_ids = set(line['endpoint_sip']['id'] for line in user['lines'] if line['endpoint_sip']) | |
return not set(endpoint_sip_ids).isdisjoint(set(webrtc_endpoint_ids)) | |
def add_webrtc_line_to_user(user, extension_id): | |
endpoint_sip_body = { | |
'type': 'friend', | |
'host': 'dynamic', | |
'options': [ | |
('transport', 'transport-wss'), | |
('dtls_auto_generate_cert', 'yes'), | |
('allow', '!all,g722,ulaw,alaw,vp9,vp8,h264'), | |
('webrtc', 'yes') | |
] | |
} | |
line_body = { | |
'context': context['name'] | |
} | |
endpoint_sip = None | |
line = None | |
try: | |
endpoint_sip = confd.endpoints_sip.create(endpoint_sip_body) | |
line = confd.lines.create(line_body) | |
print('Associating line', line['id'], 'with endpoint', endpoint_sip['id'], 'and user', user['uuid']) | |
confd.endpoints_sip.relations(endpoint_sip['id']).associate_line(line['id']) | |
confd.users.relations(user['uuid']).add_line(line['id']) | |
confd.extensions.relations(extension_id).add_line(line['id']) | |
except Exception as e: | |
print('Error while creating WebRTC line for user', user['firstname'], user['lastname']) | |
print(e) | |
try: | |
if endpoint_sip: | |
confd.endpoints_sip.delete(endpoint_sip['id']) | |
if line: | |
confd.line.delete(line['id']) | |
except Exception: | |
print('Error while removing endpoint_sip', endpoint_sip['id'], 'or line', line['id']) | |
print('Creating token...') | |
auth = Auth(host, username=username, password=password, verify_certificate=False) | |
token = auth.token.new('wazo_user', expiration=600)['token'] | |
confd = Confd(host, verify_certificate=False, token=token) | |
print('Done.') | |
print('Finding context') | |
context = find_internal_context(tenant_uuid) | |
print('Found context', context['name']) | |
print('Finding WebRTC endpoints...') | |
webrtc_endpoints = set(find_webrtc_endpoint_ids()) | |
print('Found', len(webrtc_endpoints), 'WebRTC endpoints.') | |
for extension_id, user in find_users_in_range(context['name'], extension_start, extension_end): | |
if user['subscription_type'] in (1,3): | |
if not user_has_webrtc_line(user, webrtc_endpoints): | |
print('Processing user', user['firstname'], user['lastname']) | |
add_webrtc_line_to_user(user, extension_id) | |
else: | |
print('Skipping user', user['firstname'], user['lastname'], ': user already has webrtc line') | |
else: | |
print ('Not UC or CRM subscription', user['firstname'], user['lastname']) | |
auth.token.revoke(token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment