Exemplo n.º 1
0
    def update_resource(self, resource_path, object_name, object_data):

        method = 'PUT'
        headers = {'Content-Type':'application/xml', 'Accept':'application/xml', 'Authorization' : self.auth}
        if self.headers:
            headers.update(self.headers)
        serializer = None
        if isinstance(object_data, str):
            request_body = object_data
        elif object_data:
            serializer = Serializer(plurals=self.plurals)
        
        if serializer != None:
            request_body = serializer.serialize(object_name, object_data, "xml")

        url_path = self.service_path + "/" + resource_path
    
        LOG.debug(_("Auth:%s url_path:%s requestbody: %s" % (self.auth,url_path, request_body)))
        
        try:

            connection = self._get_connection()

            connection.request(method, url_path, body=request_body, headers=headers)

            response = connection.getresponse()

            connection.close()

            resp_dict = self._get_response_dict(response, serializer)

            LOG.debug(_("Response: %s" % (resp_dict['body'])))

            response_status = resp_dict['status']

            if self.is_proxy:
                return response_status, resp_dict
            
            if str(response_status) == "401":
                LOG.error(_("Unable to login.Invalid credentials passed for host: %s." % (self.host)))
                raise q_exc.ServiceUnavailable()

            if not self._is_valid_response(response_status):
                LOG.error(_("Failed to update %s in %s, status: %s" % (url_path, self.uri , response_status)))
                raise q_exc.ServiceUnavailable()

            return response_status, resp_dict

        except (LookupError, ImportError) as e:
            exc_type, exc_value, exc_tb = sys.exc_info()
            LOG.error(_("Error while connecting to %s :  %s") % (self.uri, exc_type))
            raise q_exc.ServiceUnavailable()
Exemplo n.º 2
0
    def retrieve_resource(self, resource_path, parse_response=True):

        method = 'GET'
        headers = {'Content-Type':'application/xml', 'Accept':'application/xml', 'Authorization' : self.auth}
        if self.headers:
            headers.update(self.headers)
        
        url_path = self.service_path + "/" + resource_path

        LOG.debug(_("Request %s %s" %(method, url_path)))

        LOG.debug(_("Headers used for request %s" % (str(headers))))

        connection = self._get_connection()

        connection.request(method, url_path, headers=headers)
        
        LOG.debug(_("Plurals used for parsing %s" % (str(self.plurals))))
        
        serializer = None
        if parse_response:
            serializer = Serializer(plurals=self.plurals)
        
        LOG.debug(_("Retrieve resource_path: %s" % (url_path)))
        
        try:
        
            response = connection.getresponse()

            connection.close()

            resp_dict = self._get_response_dict(response, serializer)
            
            response_status = resp_dict['status']
            
            LOG.debug(_("Response: %s" % (resp_dict['body'])))
            
            if self.is_proxy:
                return response_status, resp_dict
            
            if str(response_status) == "401":
                LOG.error(_("Unable to login.Invalid credentials passed for host: %s." % (self.host)))
                raise q_exc.ServiceUnavailable()
            
        except (LookupError, ImportError) as e:
            exc_type, exc_value, exc_tb = sys.exc_info()
            LOG.error(_("Error while connecting to %s :  %s") % (self.uri, exc_type))
            raise q_exc.ServiceUnavailable()

        return resp_dict['status'], resp_dict
Exemplo n.º 3
0
    def remove_resource(self, resource_path, parse_response=True):
        
        method = 'DELETE'
        headers = {'Content-Type':'application/xml', 'Accept':'application/xml', 'Authorization' : self.auth}
        if self.headers:
            headers.update(self.headers)

        url_path = self.service_path + "/" + resource_path
        
        LOG.debug(_("Request %s %s" %(method, url_path)))

        LOG.debug(_("Headers used for request %s" % (str(headers))))

        connection = self._get_connection()

        connection.request(method, url_path, headers=headers)

        response = connection.getresponse()

        connection.close()

        serializer = None

        if parse_response:
            serializer = Serializer(plurals=self.plurals)
            

        resp_dict = self._get_response_dict(response, serializer)

        LOG.debug(_("Response: %s" % (resp_dict['body'])))

        response_status = resp_dict['status']

        LOG.debug(_("Response status %s" % (str(response_status))))
        
        if self.is_proxy:
            return response_status, resp_dict


        if str(response_status) == "401":
            LOG.error(_("Unable to login.Invalid credentials passed for host: %s." % (self.host)))
            raise q_exc.ServiceUnavailable()
            
        if not self._is_valid_response(response_status):
            LOG.error(_("Failed to remove %s in %s, status: %s" % (url_path, self.uri , response_status)))
            raise q_exc.ServiceUnavailable()

        return response_status, resp_dict
Exemplo n.º 4
0
    def __init__(self, uri, username, password, plurals=None, headers=None, is_proxy = False):
        
        if plurals:
            self.plurals = plurals
        else:
            self.plurals = {}
        self.is_proxy = is_proxy
        
        if not uri:
            LOG.debug(_("No uri passed. Cannot connect"))
            raise Exception("No uri passed. Cannot connect")
        
        self.headers={}
        if headers != None:
            self.headers = headers
           
        parts = urlparse(uri)
        self.uri = uri
        host_port_parts = parts.netloc.split(':')
        
        self.port = None
        
        if len(host_port_parts) > 1:
            self.host = host_port_parts[0]
            self.port = host_port_parts[1]
        else:
            self.host = host_port_parts[0]

        if type(self.host).__name__ == 'unicode':
            self.host = self.host.encode('ascii','ignore')
        
        if self.port and type(self.port).__name__ == 'unicode':
            self.port = self.port.encode('ascii','ignore')


        if parts.scheme.lower() == "http":   
            self.protocol = "http"
            if not self.port:

                self.port = 80     


        elif parts.scheme.lower() == "https":
            self.protocol = "https"
            if not self.port:
                self.port = 443

        else:
            LOG.error(_("scheme in uri is unrecognized:%s" % parts.scheme))
            raise q_exc.ServiceUnavailable()
            
        self.service_path = parts.path
        
        self.auth = None

        if username != None and password != None: 
            base64string = base64.encodestring("%s:%s" % (username, password))
            base64string = base64string[:-1]
            self.auth = 'Basic %s' % base64string
Exemplo n.º 5
0
    def _get_connection(self):

        if self.protocol == "http":
            connection = httplib.HTTPConnection(self.host, self.port)     
        elif self.protocol == "https":
            connection = httplib.HTTPSConnection(self.host, self.port)
        else:
            LOG.error(_("protocol unrecognized:%s" % self.protocol))
            raise q_exc.ServiceUnavailable()

        return connection
    def login_server(self):
        if self.session:
            yield
        else:
            ret = self.login()
            LOG.debug("SMC server LOGIN successfully.")

            if ret:
                try:
                    yield
                except Exception:
                    LOG.exception(_LE("exception while connect to server!"))
                    raise n_exc.ServiceUnavailable(resource='SMC server',
                                                   msg=_("OPERATION failed"))

                finally:
                    self.logout()

            else:
                raise n_exc.BadRequest(resource='SMC server',
                                       msg=_("LOGIN failed!"))
Exemplo n.º 7
0
 def diag_not_implemented(self, res, id, input):
     LOG.warning("Diagnostics not implemented on resource %ss." % res)
     raise exceptions.ServiceUnavailable()