Ejemplo n.º 1
0
    def authenticate(self, **kwargs):
        '''
        kwargs will receive the python dict that may contain
        {username, password, project-id}  to authenticate
        '''
        if all(k in kwargs for k in ('username', 'password', 'project_id')):
            username = kwargs['username']
            password = kwargs['password']

            client = Client()
            result = client.auth(kwargs)

            if 'error' in result and result['error'] is True:
                raise OSMAuthException(result['data'])
            else:

                try:
                    user = OsmUser.objects.get(username=username)
                    user.psw = password
                    user.token = result['data']['id']
                    user.project_id = result['data']['project_id']
                    user.token_expires = result['data']['expires']
                    user.is_admin = bool(result['data']['admin'])
                    user.save()
                except OsmUser.DoesNotExist:
                    user = OsmUser(username=username, psw=password, token=result['data']['id'],
                                   project_id=result['data']['project_id'],
                                   token_expires=result['data']['expires'], is_admin=result['data']['admin'])
                    user.save()

                return user

        return None
Ejemplo n.º 2
0
def get(request, role_id=None):
    user = osmutils.get_user(request)
    try:
        client = Client()
        get_res = client.role_get(user.get_token(), role_id)
    except Exception as e:
        log.exception(e)
        get_res = {'error': True, 'data': str(e)}
    if get_res['error']:
        return __response_handler(request,
                                  get_res['data'],
                                  url=None,
                                  status=get_res['data']['status']
                                  if 'status' in get_res['data'] else 500)
    else:
        role = get_res['data']
        result = {
            '_id': role['_id'],
            'name': role['name'],
            'permissions': {
                key: value
                for key, value in role['permissions'].items()
                if key not in ['_id', 'name', 'root', '_admin']
            }
        }
        return __response_handler(request, result, url=None, status=200)
Ejemplo n.º 3
0
 def get_projects(self):
     client = Client()
     result = client.get_user_info(self.get_token(), self.username)
     if 'error' in result and result['error'] is True:
         return []
     else:
         return result['data']['projects']
Ejemplo n.º 4
0
def show_topology(request, instance_id=None, type=None):
    user = osmutils.get_user(request)
    project_id = user.project_id
    raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
    if 'application/json' in raw_content_types:
        client = Client()
        nsr_object = {'nsr': {}, 'vnfr': {}, 'vnfd': {}}
        if type == 'ns':

            nsr_resp = client.ns_get(user.get_token(), instance_id)
            nsr_object['nsr'] = nsr_resp['data']
            if 'constituent-vnfr-ref' in nsr_object['nsr'] :
                for vnfr_id in nsr_object['nsr']['constituent-vnfr-ref']:
                    vnfr_resp = client.vnf_get(user.get_token(), vnfr_id)
                    vnfr = vnfr_resp['data']
                    nsr_object['vnfr'][vnfr['id']] = vnfr
                    if vnfr['vnfd-id'] not in nsr_object['vnfd']:
                        vnfd_resp = client.vnfd_get(user.get_token(), vnfr['vnfd-id'])
                        nsr_object['vnfd'][vnfr['vnfd-id']] = vnfd_resp['vnfd:vnfd-catalog']['vnfd'][0]

        test = OsmParser()

        result = test.nsr_to_graph(nsr_object)
        return __response_handler(request, result)
    else:
        result = {'type': type, 'project_id': project_id, 'instance_id': instance_id}
        return __response_handler(request, result, 'instance_topology_view.html')
Ejemplo n.º 5
0
def show(request, k8sr_id=None):
    user = osmutils.get_user(request)
    project_id = user.project_id
    client = Client()
    result_client = client.k8sr_get(user.get_token(), k8sr_id)

    return __response_handler(request, result_client)
Ejemplo n.º 6
0
def create_alarm(request, instance_id=None, type=None):
    metric_data = request.POST.dict()
    print metric_data
    user = osmutils.get_user(request)
    project_id = user.project_id
    client = Client()

    keys = ["threshold_value",
            "vnf_member_index",
            "metric_name",
            "vdu_name",
            "alarm_name",
            "correlation_id",
            "statistic",
            "operation",
            "severity"]
    metric_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, metric_data.items()))

    result = client.ns_alarm_create(user.get_token(), instance_id, metric_data)
    if result['error']:
        print result
        return __response_handler(request, result['data'], url=None,
                                  status=result['data']['status'] if 'status' in result['data'] else 500)
    else:
        return __response_handler(request, {}, url=None, status=200)
Ejemplo n.º 7
0
def create(request):
    user = osmutils.get_user(request)
    project_id = user.project_id
    result = {'project_id': project_id}
    client = Client()
    try:
        new_k8sr = {
            "name": request.POST.get('name'),
            "type": request.POST.get('type'),
            "url": request.POST.get('url'),
            "description": request.POST.get('description'),
        }
    except Exception as e:
        return __response_handler(request, {
            'status': 400,
            'code': 'BAD_REQUEST',
            'detail': e.message
        },
                                  url=None,
                                  status=400)
    result = client.k8sr_create(user.get_token(), new_k8sr)
    if result['error']:
        return __response_handler(request,
                                  result['data'],
                                  url=None,
                                  status=result['data']['status']
                                  if 'status' in result['data'] else 500)
    else:
        return __response_handler(request,
                                  result,
                                  'k8sr:list',
                                  to_redirect=True)
Ejemplo n.º 8
0
def clone_package(request, package_type=None, package_id=None):
    user = osmutils.get_user(request)
    try:
        client = Client()
        if package_type == 'ns':
            result = client.nsd_clone(user.get_token(), package_id)
        elif package_type == 'vnf':
            result = client.vnfd_clone(user.get_token(), package_id)
        else:
            log.debug('Update descriptor: Unknown data type')
            result = {
                'error': True,
                'data': 'Update descriptor: Unknown data type'
            }
    except Exception as e:
        log.exception(e)
        result = {'error': True, 'data': str(e)}
    if result['error'] == True:
        return __response_handler(request,
                                  result['data'],
                                  url=None,
                                  status=result['data']['status']
                                  if 'status' in result['data'] else 500)
    else:
        return __response_handler(request, {}, url=None, status=200)
Ejemplo n.º 9
0
def user_projects(request):
    user = osmutils.get_user(request)
    client = Client()
    result = client.project_list(user.get_token())
    return __response_handler(request, {
        'projects': result['data'] if result and result['error'] is False else [],
    },'projectlist.html')
Ejemplo n.º 10
0
def show_packages(request, package_type=None):
    user = osmutils.get_user(request)
    project_id = user.project_id
    client = Client()
    filter = request.GET.get('type')
    try:
        if package_type == 'ns':
            descriptors = client.nsd_list(user.get_token(), filter)
        elif package_type == 'vnf':
            descriptors = client.vnfd_list(user.get_token(), filter)
    except Exception as e:
        log.exception(e)
        descriptors = []

    url = 'package_list.html'
    return __response_handler(
        request, {
            'descriptors':
            descriptors['data']
            if descriptors and descriptors['error'] is False else [],
            'project_id':
            project_id,
            'project_type':
            'osm',
            'package_type':
            package_type
        }, url)
Ejemplo n.º 11
0
def create_package_empty(request, package_type=None):
    user = osmutils.get_user(request)
    pkg_name = request.POST.get('name', '')
    try:
        client = Client()
        if package_type == 'ns':
            result = client.nsd_create_pkg_base(user.get_token(), pkg_name)
        elif package_type == 'vnf':
            result = client.vnfd_create_pkg_base(user.get_token(), pkg_name)
        else:
            log.debug('Update descriptor: Unknown data type')
            result = {
                'error': True,
                'data': 'Update descriptor: Unknown data type'
            }
    except Exception as e:
        log.exception(e)
        result = {'error': True, 'data': str(e)}

    if result['error'] == True:
        return __response_handler(request,
                                  result['data'],
                                  url=None,
                                  status=result['data']['status']
                                  if 'status' in result['data'] else 500)
    else:
        result['data']['type'] = package_type
        return __response_handler(request, result, url=None, status=200)
Ejemplo n.º 12
0
def create(request):
    user = osmutils.get_user(request)
    project_id = user.project_id
    result = {'project_id': project_id}
    if request.method == 'GET':
        return __response_handler(request, result, 'sdn_create.html')
    else:
        new_sdn_dict = request.POST.dict()
        client = Client()
        keys = [
            "name", "type", "version", "dpid", "ip", "port", "user", "password"
        ]
        sdn_data = dict(
            filter(lambda i: i[0] in keys and len(i[1]) > 0,
                   new_sdn_dict.items()))
        sdn_data['port'] = int(sdn_data['port'])

        result = client.sdn_create(user.get_token(), sdn_data)

        return __response_handler(
            request,
            result,
            'sdns:list',
            to_redirect=True,
        )
Ejemplo n.º 13
0
 def get_projects(self):
     client = Client()
     user_info = client.get_user_info(self.get_token(), self.username)
     if 'error' in user_info and user_info['error'] is True:
         return []
     else:
         return user_info['data']['project_role_mappings']
Ejemplo n.º 14
0
def update(request, role_id=None):
    user = osmutils.get_user(request)
    client = Client()
    payload = {'name': request.POST['name'], 'permissions': {}}
    try:
        if 'permissions' in request.POST and request.POST.get(
                'permissions') != '':
            role_permissions = yaml.load(request.POST.get('permissions'))

            if not isinstance(role_permissions, dict):
                raise ValueError(
                    'Role permissions should be provided in a key-value fashion'
                )
            for key, value in role_permissions.items():
                if not isinstance(value, bool):
                    raise ValueError(
                        'Value in a role permissions should be boolean')
                payload['permissions'][key] = value
    except Exception as e:
        return __response_handler(request, {
            'status': 400,
            'code': 'BAD_REQUEST',
            'detail': e.message
        },
                                  url=None,
                                  status=400)
    result = client.role_update(user.get_token(), role_id, payload)
    if result['error']:
        return __response_handler(request,
                                  result['data'],
                                  url=None,
                                  status=result['data']['status']
                                  if 'status' in result['data'] else 500)
    else:
        return __response_handler(request, {}, url=None, status=200)
Ejemplo n.º 15
0
def update(request, user_id=None):
    user = osmutils.get_user(request)
    try:
        client = Client()
        payload = {}

        if request.POST.get('password') and request.POST.get(
                'password') is not '':
            payload["password"] = request.POST.get('password')

        if request.POST.getlist('map_project_name') and request.POST.getlist(
                'map_role_name'):
            project_param_name = request.POST.getlist('map_project_name')
            role_param_ip = request.POST.getlist('map_role_name')
            payload["project_role_mappings"] = []
            for i, project in enumerate(project_param_name):
                payload["project_role_mappings"].append({
                    'project':
                    project,
                    'role':
                    role_param_ip[i],
                })

        update_res = client.user_update(user.get_token(), user_id, payload)
    except Exception as e:
        log.exception(e)
        update_res = {'error': True, 'data': str(e)}
    if update_res['error']:
        return __response_handler(request,
                                  update_res['data'],
                                  url=None,
                                  status=update_res['data']['status']
                                  if 'status' in update_res['data'] else 500)
    else:
        return __response_handler(request, {}, url=None, status=200)
Ejemplo n.º 16
0
def user_list(request):
    user = osmutils.get_user(request)
    client = Client()
    result = client.user_list(user.get_token())
    result = {
        'users': result['data'] if result and result['error'] is False else []
    }
    return __response_handler(request, result, 'user_list.html')
Ejemplo n.º 17
0
def delete(request, vim_id=None):
    user = osmutils.get_user(request)
    try:
        client = Client()
        del_res = client.vim_delete(user.get_token(), vim_id)
    except Exception as e:
        log.exception(e)
    return __response_handler(request, del_res, 'vims:list', to_redirect=True)
Ejemplo n.º 18
0
def details(request, template_id=None):
    user = osmutils.get_user(request)
    client = Client()
    result = client.nst_details(user.get_token(), template_id)
    if result['error']:
        return __response_handler(request, result['data'], url=None, status=result['data']['status'] if 'status' in result['data'] else 500)
    else:
        return __response_handler(request, result, url=None, status=200)
Ejemplo n.º 19
0
def update(request, k8sr_id=None):
    user = osmutils.get_user(request)
    try:
        update_k8sr_dict = request.POST.dict()
        client = Client()
        res = client.k8sr_update(user.get_token(), k8sr_id, update_k8sr_dict)
    except Exception as e:
        log.exception(e)
    return __response_handler(request, res, 'k8sr:list', to_redirect=True)
Ejemplo n.º 20
0
def list(request):
    user = osmutils.get_user(request)
    client = Client()
    result = {}
    result_client = client.nst_list(user.get_token())

    result['templates'] = result_client['data'] if result_client and result_client['error'] is False else []

    return __response_handler(request, result, 'nst_list.html')
Ejemplo n.º 21
0
def delete_project(request, project_id):
    user = osmutils.get_user(request)

    client = Client()
    result = client.project_delete(user.get_token(), project_id)
    if isinstance(result, dict) and 'error' in result and result['error']:
        return __response_handler(request, result['data'], url=None,
                                  status=result['data']['status'] if 'status' in result['data'] else 500)
    else:
        return __response_handler(request, {}, url=None, status=200)
Ejemplo n.º 22
0
def show(request, sdn_id=None):
    user = osmutils.get_user(request)
    project_id = user.project_id
    client = Client()
    result = client.sdn_get(user.get_token(), sdn_id)
    if isinstance(result, dict) and 'error' in result and result['error']:
        return render(request, 'error.html')
    return __response_handler(request, {
        "sdn": result['data'],
        "project_id": project_id
    })
Ejemplo n.º 23
0
def list(request):
    user = osmutils.get_user(request)
    project_id = user.project_id
    result = {'type': 'ns', 'project_id': project_id}
    raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
    if 'application/json' not in raw_content_types:
        return __response_handler(request, result, 'wim_list.html')
    client = Client()
    result_client = client.wim_list(user.get_token())
    result["datacenters"] = result_client[
        'data'] if result_client and result_client['error'] is False else []
    return __response_handler(request, result, 'wim_list.html')
Ejemplo n.º 24
0
def create(request):
    user = osmutils.get_user(request)
    client = Client()
    user_data = {
        "username": request.POST['username'],
        "password": request.POST['password'],
        "projects": request.POST.getlist('projects')
    }

    result = client.user_create(user.get_token(), user_data)

    return __response_handler(request, result, 'users:list', to_redirect=True)
Ejemplo n.º 25
0
def show(request, vim_id=None):
    user = osmutils.get_user(request)
    project_id = user.project_id
    client = Client()
    result = client.vim_get(user.get_token(), vim_id)
    print result
    if isinstance(result, dict) and 'error' in result and result['error']:
        return render(request, 'error.html')

    return __response_handler(request, {
        "datacenter": result['data'],
        "project_id": project_id
    }, 'vim_show.html')
Ejemplo n.º 26
0
def create_new_project(request):
    if request.method == 'POST':
        user = osmutils.get_user(request)
        client = Client()
        new_project_dict = request.POST.dict()
        keys = ["name", "domain_name"]
        project_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, new_project_dict.items()))
        result = client.project_create(user.get_token(), project_data)
        if isinstance(result, dict) and 'error' in result and result['error']:
            return __response_handler(request, result['data'], url=None,
                                      status=result['data']['status'] if 'status' in result['data'] else 500)
        else:
            return __response_handler(request, {}, url=None, status=200)
Ejemplo n.º 27
0
def delete(request, sdn_id=None):
    user = osmutils.get_user(request)
    project_id = user.project_id
    try:
        client = Client()
        del_res = client.sdn_delete(user.get_token(), sdn_id)
    except Exception as e:
        log.exception(e)
    return __response_handler(
        request,
        del_res,
        'sdns:list',
        to_redirect=True,
    )
Ejemplo n.º 28
0
def user_domains(request):
    user = osmutils.get_user(request)
    client = Client()
    result = client.get_domains(user.get_token())
    if result and result['error'] is False:
        domains = []
        if result['data'] and result['data']['user_domain_name']:
            domain_names = result['data']['user_domain_name'].split(',')
            domains.extend(x for x in domain_names if x not in domains)
        if result['data'] and result['data']['project_domain_name']:
            domain_names = result['data']['project_domain_name'].split(',')
            domains.extend(x for x in domain_names if x not in domains)

        return __response_handler(request, {'domains': domains})
    return __response_handler(request, {'domains': []})
Ejemplo n.º 29
0
def edit(request, template_id=None):
    user = osmutils.get_user(request)
    client = Client()
    if request.method == 'GET':
        page = 'nst_edit.html'
        result = client.nst_content(user.get_token(), template_id)
        if result['error']:
            return __response_handler(request, result, url=page, status=500)
        else:
            return __response_handler(request, {'template': {'template_id': str(template_id),  'data': result['data']}}, url=page, status=200)
    elif request.method == 'POST':
        result = client.nst_content_update(user.get_token(), template_id, request.POST.get('text'))
        if result['error'] == True:
            return __response_handler(request, result['data'], url=None, status=result['data']['status'] if 'status' in result['data'] else 500)
        else:
            return __response_handler(request, {}, url=None, status=200)
Ejemplo n.º 30
0
def ns_operations(request, instance_id=None, type=None):
    user = osmutils.get_user(request)
    project_id = user.project_id

    result = {'type': type, 'project_id': project_id, 'instance_id': instance_id}
    raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
    if 'application/json' not in raw_content_types:
        return __response_handler(request, result, 'instance_operations_list.html')
    client = Client()
    if type == 'ns':
        op_list = client.ns_op_list(user.get_token(), instance_id)
    elif type == 'nsi':
        op_list = client.nsi_op_list(user.get_token(), instance_id)
    result['operations'] = op_list['data'] if op_list and op_list['error'] is False else []

    return __response_handler(request, result, 'instance_operations_list.html')