예제 #1
0
    def post(self, uri, data=None, files=None):
        """
            Sends a POST request.

            @param uri: Uri of Service API.
            @param data: Requesting Data. Default: None

            @raise NetworkAPIClientError: Client failed to access the API.
        """
        try:

            request = requests.post(self._url(uri),
                                    data=json.dumps(data),
                                    files=files,
                                    auth=self._auth_basic(),
                                    headers=self._header())

            request.raise_for_status()

            try:
                return request.json()
            except Exception:
                return request

        except HTTPError:
            try:
                error = request.json()
                self.logger.error(error)
                err = error.get('detail', '')
            except:
                err = request
            raise NetworkAPIClientError(err)
        except Exception:
            try:
                error = request.json()
                self.logger.error(error)
                err = error.get('detail', '')
            except:
                err = request
            raise NetworkAPIClientError(err)
        finally:
            self.logger.info('URI: %s', uri)
            self.logger.info('Status Code: %s',
                             request.status_code if request else '')
            self.logger.info('X-Request-Id: %s',
                             request.headers.get('x-request-id'))
            self.logger.info('X-Request-Context: %s',
                             request.headers.get('x-request-context'))
    def get(self, uri):
        """
            Sends a GET request.

            @param uri: Uri of Service API.
            @param data: Requesting Data. Default: None

            @raise NetworkAPIClientError: Client failed to access the API.
        """
        request = None

        try:

            request = requests.get(self._url(uri),
                                   auth=self._auth_basic(),
                                   headers=self._header())

            request.raise_for_status()

            return request.json()

        except HTTPError:
            error = request.json()
            self.logger.error(error)
            raise NetworkAPIClientError(error.get('detail', ''))
        finally:
            self.logger.info('URI: %s', uri)
            if request:
                self.logger.info('Status Code: %s',
                                 request.status_code if request else '')
                self.logger.info('X-Request-Id: %s',
                                 request.headers.get('x-request-id'))
                self.logger.info('X-Request-Context: %s',
                                 request.headers.get('x-request-context'))
    def delete(self, uri, data=None):
        """
            Sends a DELETE request.

            @param uri: Uri of Service API.

            @raise NetworkAPIClientError: Client failed to access the API.
        """
        try:

            request = requests.delete(self._url(uri),
                                      data=json.dumps(data),
                                      auth=self._auth_basic(),
                                      headers=self._header())

            request.raise_for_status()

            return request.json()

        except HTTPError:
            error = request.json() if request else ""
            self.logger.error(error)
            raise NetworkAPIClientError(error.get('detail', ''))
        finally:
            if request:
                self.logger.info('URI: %s', uri)
                self.logger.info('Status Code: %s',
                                 request.status_code if request else '')
                self.logger.info('X-Request-Id: %s',
                                 request.headers.get('x-request-id'))
                self.logger.info('X-Request-Context: %s',
                                 request.headers.get('x-request-context'))
예제 #4
0
    def put(self, uri, data=None, verify=False):
        """
            Sends a PUT request.

            @param uri: Uri of Service API.
            @param data: Requesting Data. Default: None

            @raise NetworkAPIClientError: Client failed to access the API.
        """
        try:

            request = requests.put(self._url(uri),
                                   data=json.dumps(data),
                                   auth=self._auth_basic(),
                                   headers=self._header(),
                                   verify=verify)

            request.raise_for_status()

            return self._parse(request.text)

        except HTTPError:
            error = self._parse(request.text)
            self.logger.error(error)
            raise NetworkAPIClientError(error.get('detail', ''))
        finally:
            self.logger.info('URI: %s', uri)
            self.logger.info('Status Code: %s',
                             request.status_code if request else "")
            self.logger.info('X-Request-Id: %s',
                             request.headers.get('x-request-id'))
            self.logger.info('X-Request-Context: %s',
                             request.headers.get('x-request-context'))
    def get(self, uri):
        """
            Sends a GET request.

            @param uri: Uri of Service API.
            @param data: Requesting Data. Default: None

            @raise NetworkAPIClientError: Client failed to access the API.
        """
        try:

            request = requests.get(self._url(uri),
                                   auth=self._auth_basic(),
                                   headers=self._header())

            request.raise_for_status()

            return self._parse(request.text)

        except HTTPError:
            error = self._parse(request.text)
            raise NetworkAPIClientError(error.get('detail', ''))
    def delete(self, uri, data=None):
        """
            Sends a DELETE request.

            @param uri: Uri of Service API.

            @raise NetworkAPIClientError: Client failed to access the API.
        """
        try:

            request = requests.delete(self._url(uri),
                                      data=json.dumps(data),
                                      auth=self._auth_basic(),
                                      headers=self._header())

            request.raise_for_status()

            return self._parse(request.text)

        except HTTPError:
            error = self._parse(request.text)
            raise NetworkAPIClientError(error.get('detail', ''))
예제 #7
0
def edit_individ_perms_of_object(request, id_ugroup, id_type_obj, id_obj):

    try:
        lists = {}

        auth = AuthSession(request.session)
        client = auth.get_clientFactory()

        lists['id_ugroup'] = id_ugroup
        lists['id_type_obj'] = id_type_obj
        lists['id_obj'] = id_obj

        type_obj = client.create_api_object_type().get(
            [int(id_type_obj)])['ots'][0]['name']

        if type_obj == VIPREQUEST_OBJ_TYPE:
            obj_name = facade.get_vips_request(client, None,
                                               None, id_obj)['objs'][0]['name']

        elif type_obj == SERVERPOOL_OBJ_TYPE:
            obj_name = facade.get_server_pools(client, None,
                                               None, id_obj)['objs'][0]['identifier']

        elif type_obj == VLAN_OBJ_TYPE:
            obj_name = facade.get_vlans(client, None,
                                        None, id_obj)['objs'][0]['name']

        lists['object_name'] = obj_name

        if request.method == 'POST':

            lists['group_name'] = client.create_grupo_usuario(). \
                search(id_ugroup)['user_group']['nome']
            lists['object_type'] = client.create_api_object_type(). \
                get([id_type_obj])['ots'][0]['name']

            form_individ_group_perms = \
                IndividualPermsGroupUserEditForm(request.POST)
            lists['form_individ_perms_group_user'] = form_individ_group_perms

            if form_individ_group_perms.is_valid():
                id = form_individ_group_perms.cleaned_data['id']
                read = form_individ_group_perms.cleaned_data['read']
                write = form_individ_group_perms.cleaned_data['write']
                change_config = form_individ_group_perms.\
                    cleaned_data['change_config']
                delete = form_individ_group_perms.cleaned_data['delete']

                data = {
                    'id': id,
                    'read': read,
                    'write': write,
                    'change_config': change_config,
                    'delete': delete
                }

                client.create_api_object_group_permission().update([data])

                messages.add_message(request, messages.SUCCESS,
                                     object_group_perm_messages.get("success_edit"))

                return redirect('user-group.list-perms-obj', id_ugroup, id_type_obj)

        else:

            data_search = {
                'start_record': 0,
                'end_record': 25,
                'asorting_cols': [],
                'searchable_columns': [],
                'extends_search': [{
                    'user_group': id_ugroup,
                    'object_type': id_type_obj,
                    'object_value': id_obj

                }]
            }
            kind = ['basic']
            fields = ['id', 'read', 'write', 'change_config', 'delete',
                      'user_group__details', 'object_type__details']

            try:
                perms = client.create_api_object_group_permission()\
                    .search(search=data_search, kind=kind, fields=fields)['ogps'][0]
            except IndexError:
                raise NetworkAPIClientError(u'Permission not found')

            lists['group_name'] = perms['user_group']['name']
            lists['object_type'] = perms['object_type']['name']

            lists['form_individ_perms_group_user'] = \
                IndividualPermsGroupUserEditForm(initial=perms)

    except NetworkAPIClientError, e:
        logger.error(e)
        messages.add_message(request, messages.ERROR, e)
        return redirect('home')