示例#1
0
    def _unpack_response(self):
        details = None
        self.code = self.response.status_code
        if self.response.headers.get('content-type') == 'application/json':
            try:
                data = self.response.json()
            except ValueError:
                message = 'No valid message returned from SMC server'
            else:
                #status = data.get('status', None)
                message = data.get('message', None)
                details = data.get('details', None)
        else:  # it's not json
            if self.response.text:
                message = self.response.text
            else:
                message = "No message returned from SMC server"

        self.smcresult.code = self.code

        if details:
            details = unicode_to_bytes(' '.join(details)) \
                if isinstance(details, list) else unicode_to_bytes(details)
            # Some error messages from SMC include line breaks
            details = details.replace('\n', ' ').rstrip()

        if message:
            message = unicode_to_bytes(message)

        if message and details:
            self.smcresult.msg = '{} {}'.format(message, details)
        elif details:
            self.smcresult.msg = details
        else:
            self.smcresult.msg = message
示例#2
0
 def test_util_unicode_to_bytes(self):
     if PY3:
         result = unicode_to_bytes('teststring')
         self.assertIsInstance(result, str)
     else:
         result = unicode_to_bytes(u'teststring')
         self.assertIsInstance(result, str)
示例#3
0
    def _unpack_response(self):
        details = None
        self.code = self.response.status_code
        if self.response.headers.get("content-type") == "application/json":
            try:
                data = self.response.json()
            except ValueError:
                message = "No valid message returned from SMC server"
            else:
                message = data.get("message", None)
                details = data.get("details", None)
        else:  # it's not json
            if self.response.text:
                message = self.response.text
            else:
                message = "No message returned from SMC server"

        self.smcresult.code = self.code

        if details:
            details = (unicode_to_bytes(" ".join(details)) if isinstance(
                details, list) else unicode_to_bytes(details))
            # Some error messages from SMC include line breaks
            details = details.replace("\n", " ").rstrip()

        if message:
            message = unicode_to_bytes(message)

        if message and details:
            self.smcresult.msg = "{} {}".format(message, details)
        elif details:
            self.smcresult.msg = details
        else:
            self.smcresult.msg = message
示例#4
0
    def _unpack_response(self):
        details = None
        self.code = self.response.status_code
        if self.response.headers.get('content-type') == 'application/json':
            try:
                data = self.response.json()
            except ValueError:
                message = 'No valid message returned from SMC server'
            else:
                message = data.get('message', None)
                details = data.get('details', None)
        else:  # it's not json
            if self.response.text:
                message = self.response.text
            else:
                message = "No message returned from SMC server"

        self.smcresult.code = self.code

        if details:
            details = unicode_to_bytes(' '.join(details)) \
                if isinstance(details, list) else unicode_to_bytes(details)
            # Some error messages from SMC include line breaks
            details = details.replace('\n', ' ').rstrip()

        if message:
            message = unicode_to_bytes(message)

        if message and details:
            self.smcresult.msg = '{} {}'.format(message, details)
        elif details:
            self.smcresult.msg = details
        else:
            self.smcresult.msg = message
示例#5
0
def fetch_href_by_name(name,
                       filter_context=None,
                       exact_match=True,
                       domain=None):
    """
    Find the element based on name and optional filters. By default, the
    name provided uses the standard filter query. Additional filters can
    be used based on supported collections in the SMC API. This is generally
    not called directly, rather it is easier to use wrapper search utilities 
    in :py:mod:`smc.actions.search`
    
    :method: GET
    :param str name: element name, can use * as wildcard
    :param str filter_context: further filter request, i.e. 'host', 'group', 'single_fw',
                               network_elements, services, services_and_applications
    :param boolean exact_match: Do an exact match by name, note this still can return multiple entries
    :param str domain: specify domain in which to query
    :return: :py:class:`smc.api.web.SMCResult`
    """
    result = SMCRequest(params={'filter': name,
                                'filter_context':filter_context,
                                'exact_match': exact_match}).read()
    if result.json:
        if len(result.json) > 1:
            result.msg = "More than one search result found. Try using a filter "\
                            "based on element type"
        else:
            result.href = result.json[0].get('href')
    else:
        if not result.msg:
            result.msg = "No results found for: {}".format(unicode_to_bytes(name))
        result.json = []
    return result