a_frame/utils/action_providers/RestAction.py
def execute_template(self, template):
        """
        We now have the global config (if any), the instance config, and the completed template from the input_form
        and can perform the desired request
        :param template: the completed template from the user or API
        :return Boolean based on execution outcome.
        """
        print("executing %s" % template)

        if not self.url.startswith(':') and not self.url.startswith('/'):
            self.url = "/" + self.url

        # set up debugging output
        handler = urllib2.HTTPHandler(debuglevel=1)
        opener = urllib2.build_opener(handler)
        urllib2.install_opener(opener)

        # ensure no CRLF was able to snick through
        template = template.replace('\r\n', '\n')

        # basic Rest Action allows the user to configure the full_url withouth host, protocol and url broken out
        if self.full_url == "":
            full_url = self.protocol + "://" + self.host + self.url
        else:
            full_url = self.full_url

        if self.request_type == "GET" and template != '':
            print(template)
            ct = self.unescape(template)
            print(ct)
            try:
                json_query_params = json.loads(ct)
                query_data = urllib.urlencode(json_query_params)

                if "?" in full_url:
                    full_url += "&"
                else:
                    full_url += "?"

                full_url += query_data
            except ValueError as ve:
                print("Could not parse template data for get request!")
                print(ve)
                pass

        print(full_url)

        request = urllib2.Request(full_url)

        print(self.header_list)

        try:
            if self.header_list != "":
                hl = json.loads(self.header_list)
                for h in hl:
                    if 'key' in h and 'value' in h:
                        print('Adding header: %s' % h['key'])
                        request.add_header(h['key'], h['value'])

        except ValueError:
            print('Could not load headers from string')

        if self.auth_type == "basic":
            print("using username: %s" % self.username)
            base64string = base64.b64encode("%s:%s" % (self.username, self.password))
            request.add_header("Authorization", "Basic %s" % base64string)

        elif self.auth_type == "bearer":
            request.add_header("Authorization", "Bearer %s" % self.password)

        elif self.auth_type == "keystone":
            if not self.__connect_to_keystone():
                return "Authentication error connecting to Keystone!"

            print("Connected to Keystone!")
            request.add_header("X-Auth-Token", self._auth_token)

        elif self.auth_type == "oauth2":
            if not self.__connect_to_oauth2():
                return "Authentication error!"

            print("OAuth2 authentication succeeded!")
            request.add_header("Authorization", str(self._auth_token))

        elif self.auth_type == "ruckus":
            if not self.__connect_to_ruckus():
                return "Authentication error!"

            print("Ruckus authentication succeeded!")
            request.add_header("Cookie", "JSESSIONID=" + str(self._auth_token))

        elif self.auth_type == "saltapi":
            if not self.__connect_to_saltapi():
                return "Authentication error!"

            print("Saltapi authentication succeeded!")
            request.add_header("X-Auth-Token", str(self._auth_token))
            print(self._auth_token)

        request.get_method = lambda: self.request_type

        if self.accepts_type != "":
            request.add_header("Accept", self.accepts_type)

        data = str(template + "\n\n")
        print("Request type: %s" % self.request_type)
        print("%s" % data)

        if self.request_type == "GET" or self.request_type == "DELETE":
            try:
                results_object = self.__perform_get(request)
                if type(results_object) is str:
                    results = results_object
                else:
                    results = results_object.read()

                content_type = results_object.info().getheader('Content-Type')
                print(content_type)
                if results != "":
                    return self.__format_results(results)
                else:
                    return "Successful REST Operation"

            except Exception as ex:
                print(str(ex))
                return "Error! %s" % str(ex)
        else:
            # this is a POST attempt
            try:
                request.add_header("Content-Type", self.content_type)
                request.add_header("Content-Length", len(data))

                result_object = self.__perform_post(request, data)

                if not hasattr(result_object, 'info'):
                    # this is an error string
                    return result_object

                content_type = result_object.info().getheader('Content-Type')
                print('Found content_type of %s' % content_type)

                # check if this is a binary file
                if not re.search('json|text|html|xml', content_type):
                    attachment = result_object.info().getheader('Content-Disposition')
                    print(attachment)
                    print(type(attachment))
                    if attachment is not None and 'filename=' in attachment:
                        print('GOT A FILENAME')
                        filename = attachment.split('filename=')[1]
                        print(filename)
                    else:
                        if 'zip' in content_type:
                            filename = 'aframe_archive.zip'
                        elif 'iso' in content_type:
                            filename = 'aframe_archive.iso'
                        else:
                            filename = 'aframe_archive'

                    # this is a binary response! We can't handle this inline, so let's cache the result
                    # and notify the caller
                    cache_key = str(uuid.uuid4())
                    result = dict()
                    res = result_object.read()
                    # print(res)
                    result['contents'] = res
                    result['display_inline'] = False
                    result['cache_key'] = cache_key

                    cache_object = dict()
                    cache_object['contents'] = res
                    cache_object['filename'] = filename
                    cache_object['content_type'] = content_type
                    cache.set(cache_key, cache_object, self.cache_timeout)
                    return result

                result = result_object.read()

                if result != "":
                    return self.__format_results(result)
                else:
                    return "Successful POST Operation"

            except HTTPError as he:
                return "Error! %s" % str(he)

        We now have the global config (if any), the instance config, and the completed template from the input_form
        and can perform the desired request
        :param template: the completed template from the user or API
        :return Boolean based on execution outcome.
        
We now have the global config (if any), the instance config, and the completed template from the input_form
and can perform the desired request
:param template: the completed template from the user or API
:return Boolean based on execution outcome.
We now have the global config ( if any ) the instance config and the completed template from the input_form and can perform the desired request : param template : the completed template from the user or API : return Boolean based on execution outcome .
def execute_template(self, template):
        print("executing %s" % template)
        if not self.url.startswith(':') and not self.url.startswith('/'):
            self.url = "/" + self.url
        handler = urllib2.HTTPHandler(debuglevel=1)
        opener = urllib2.build_opener(handler)
        urllib2.install_opener(opener)
        template = template.replace('\r\n', '\n')
        if self.full_url == "":
            full_url = self.protocol + "://" + self.host + self.url
        else:
            full_url = self.full_url
        if self.request_type == "GET" and template != '':
            print(template)
            ct = self.unescape(template)
            print(ct)
            try:
                json_query_params = json.loads(ct)
                query_data = urllib.urlencode(json_query_params)
                if "?" in full_url:
                    full_url += "&"
                else:
                    full_url += "?"
                full_url += query_data
            except ValueError as ve:
                print("Could not parse template data for get request!")
                print(ve)
                pass
        print(full_url)
        request = urllib2.Request(full_url)
        print(self.header_list)
        try:
            if self.header_list != "":
                hl = json.loads(self.header_list)
                for h in hl:
                    if 'key' in h and 'value' in h:
                        print('Adding header: %s' % h['key'])
                        request.add_header(h['key'], h['value'])
        except ValueError:
            print('Could not load headers from string')
        if self.auth_type == "basic":
            print("using username: %s" % self.username)
            base64string = base64.b64encode("%s:%s" % (self.username, self.password))
            request.add_header("Authorization", "Basic %s" % base64string)
        elif self.auth_type == "bearer":
            request.add_header("Authorization", "Bearer %s" % self.password)
        elif self.auth_type == "keystone":
            if not self.__connect_to_keystone():
                return "Authentication error connecting to Keystone!"
            print("Connected to Keystone!")
            request.add_header("X-Auth-Token", self._auth_token)
        elif self.auth_type == "oauth2":
            if not self.__connect_to_oauth2():
                return "Authentication error!"
            print("OAuth2 authentication succeeded!")
            request.add_header("Authorization", str(self._auth_token))
        elif self.auth_type == "ruckus":
            if not self.__connect_to_ruckus():
                return "Authentication error!"
            print("Ruckus authentication succeeded!")
            request.add_header("Cookie", "JSESSIONID=" + str(self._auth_token))
        elif self.auth_type == "saltapi":
            if not self.__connect_to_saltapi():
                return "Authentication error!"
            print("Saltapi authentication succeeded!")
            request.add_header("X-Auth-Token", str(self._auth_token))
            print(self._auth_token)
        request.get_method = lambda: self.request_type
        if self.accepts_type != "":
            request.add_header("Accept", self.accepts_type)
        data = str(template + "\n\n")
        print("Request type: %s" % self.request_type)
        print("%s" % data)
        if self.request_type == "GET" or self.request_type == "DELETE":
            try:
                results_object = self.__perform_get(request)
                if type(results_object) is str:
                    results = results_object
                else:
                    results = results_object.read()
                content_type = results_object.info().getheader('Content-Type')
                print(content_type)
                if results != "":
                    return self.__format_results(results)
                else:
                    return "Successful REST Operation"
            except Exception as ex:
                print(str(ex))
                return "Error! %s" % str(ex)
        else:
            try:
                request.add_header("Content-Type", self.content_type)
                request.add_header("Content-Length", len(data))
                result_object = self.__perform_post(request, data)
                if not hasattr(result_object, 'info'):
                    return result_object
                content_type = result_object.info().getheader('Content-Type')
                print('Found content_type of %s' % content_type)
                if not re.search('json|text|html|xml', content_type):
                    attachment = result_object.info().getheader('Content-Disposition')
                    print(attachment)
                    print(type(attachment))
                    if attachment is not None and 'filename=' in attachment:
                        print('GOT A FILENAME')
                        filename = attachment.split('filename=')[1]
                        print(filename)
                    else:
                        if 'zip' in content_type:
                            filename = 'aframe_archive.zip'
                        elif 'iso' in content_type:
                            filename = 'aframe_archive.iso'
                        else:
                            filename = 'aframe_archive'
                    cache_key = str(uuid.uuid4())
                    result = dict()
                    res = result_object.read()
                    result['contents'] = res
                    result['display_inline'] = False
                    result['cache_key'] = cache_key
                    cache_object = dict()
                    cache_object['contents'] = res
                    cache_object['filename'] = filename
                    cache_object['content_type'] = content_type
                    cache.set(cache_key, cache_object, self.cache_timeout)
                    return result
                result = result_object.read()
                if result != "":
                    return self.__format_results(result)
                else:
                    return "Successful POST Operation"
            except HTTPError as he:
                return "Error! %s" % str(he)
def execute_template ( self , template ) : print ( "executing %s" % template ) if not self . url . startswith ( ':' ) and not self . url . startswith ( '/' ) : self . url = "/" + self . url handler = urllib2 . HTTPHandler ( debuglevel = 1 ) opener = urllib2 . build_opener ( handler ) urllib2 . install_opener ( opener ) template = template . replace ( '\r\n' , '\n' ) if self . full_url == "" : full_url = self . protocol + "://" + self . host + self . url else : full_url = self . full_url if self . request_type == "GET" and template != '' : print ( template ) ct = self . unescape ( template ) print ( ct ) try : json_query_params = json . loads ( ct ) query_data = urllib . urlencode ( json_query_params ) if "?" in full_url : full_url += "&" else : full_url += "?" full_url += query_data except ValueError as ve : print ( "Could not parse template data for get request!" ) print ( ve ) pass print ( full_url ) request = urllib2 . Request ( full_url ) print ( self . header_list ) try : if self . header_list != "" : hl = json . loads ( self . header_list ) for h in hl : if 'key' in h and 'value' in h : print ( 'Adding header: %s' % h [ 'key' ] ) request . add_header ( h [ 'key' ] , h [ 'value' ] ) except ValueError : print ( 'Could not load headers from string' ) if self . auth_type == "basic" : print ( "using username: %s" % self . username ) base64string = base64 . b64encode ( "%s:%s" % ( self . username , self . password ) ) request . add_header ( "Authorization" , "Basic %s" % base64string ) elif self . auth_type == "bearer" : request . add_header ( "Authorization" , "Bearer %s" % self . password ) elif self . auth_type == "keystone" : if not self . __connect_to_keystone ( ) : return "Authentication error connecting to Keystone!" print ( "Connected to Keystone!" ) request . add_header ( "X-Auth-Token" , self . _auth_token ) elif self . auth_type == "oauth2" : if not self . __connect_to_oauth2 ( ) : return "Authentication error!" print ( "OAuth2 authentication succeeded!" ) request . add_header ( "Authorization" , str ( self . _auth_token ) ) elif self . auth_type == "ruckus" : if not self . __connect_to_ruckus ( ) : return "Authentication error!" print ( "Ruckus authentication succeeded!" ) request . add_header ( "Cookie" , "JSESSIONID=" + str ( self . _auth_token ) ) elif self . auth_type == "saltapi" : if not self . __connect_to_saltapi ( ) : return "Authentication error!" print ( "Saltapi authentication succeeded!" ) request . add_header ( "X-Auth-Token" , str ( self . _auth_token ) ) print ( self . _auth_token ) request . get_method = lambda : self . request_type if self . accepts_type != "" : request . add_header ( "Accept" , self . accepts_type ) data = str ( template + "\n\n" ) print ( "Request type: %s" % self . request_type ) print ( "%s" % data ) if self . request_type == "GET" or self . request_type == "DELETE" : try : results_object = self . __perform_get ( request ) if type ( results_object ) is str : results = results_object else : results = results_object . read ( ) content_type = results_object . info ( ) . getheader ( 'Content-Type' ) print ( content_type ) if results != "" : return self . __format_results ( results ) else : return "Successful REST Operation" except Exception as ex : print ( str ( ex ) ) return "Error! %s" % str ( ex ) else : try : request . add_header ( "Content-Type" , self . content_type ) request . add_header ( "Content-Length" , len ( data ) ) result_object = self . __perform_post ( request , data ) if not hasattr ( result_object , 'info' ) : return result_object content_type = result_object . info ( ) . getheader ( 'Content-Type' ) print ( 'Found content_type of %s' % content_type ) if not re . search ( 'json|text|html|xml' , content_type ) : attachment = result_object . info ( ) . getheader ( 'Content-Disposition' ) print ( attachment ) print ( type ( attachment ) ) if attachment is not None and 'filename=' in attachment : print ( 'GOT A FILENAME' ) filename = attachment . split ( 'filename=' ) [ 1 ] print ( filename ) else : if 'zip' in content_type : filename = 'aframe_archive.zip' elif 'iso' in content_type : filename = 'aframe_archive.iso' else : filename = 'aframe_archive' cache_key = str ( uuid . uuid4 ( ) ) result = dict ( ) res = result_object . read ( ) result [ 'contents' ] = res result [ 'display_inline' ] = False result [ 'cache_key' ] = cache_key cache_object = dict ( ) cache_object [ 'contents' ] = res cache_object [ 'filename' ] = filename cache_object [ 'content_type' ] = content_type cache . set ( cache_key , cache_object , self . cache_timeout ) return result result = result_object . read ( ) if result != "" : return self . __format_results ( result ) else : return "Successful POST Operation" except HTTPError as he : return "Error! %s" % str ( he )
We now have the global config (if any), the instance config, and the completed template from the input_form
and can perform the desired request
:param template: the completed template from the user or API
:return Boolean based on execution outcome.
We now have the global config ( if any ) the instance config and the completed template from the input_form and can perform the desired request : param template : the completed template from the user or API : return Boolean based on execution outcome .
"""
        We now have the global config (if any), the instance config, and the completed template from the input_form
        and can perform the desired request
        :param template: the completed template from the user or API
        :return Boolean based on execution outcome.
        """ # set up debugging output # ensure no CRLF was able to snick through # basic Rest Action allows the user to configure the full_url withouth host, protocol and url broken out # this is a POST attempt # this is an error string # check if this is a binary file # this is a binary response! We can't handle this inline, so let's cache the result # and notify the caller # print(res)
self
template