Commit 82df5e7b authored by Cristiano Urban's avatar Cristiano Urban
Browse files

Fixed bug in 'getStorageId()' method + added 'insertStorage()' method.

parent b1752bcd
Loading
Loading
Loading
Loading
+70 −4
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ class DbConnector(object):
    Getters
    """

    ### Node

    def nodeExists(self, node):
        """Checks if a VOSpace node already exists. Returns a boolean."""
        if self.conn:
@@ -89,6 +91,8 @@ class DbConnector(object):
                vospacePathList.append(el["vos_path"])
            return vospacePathList

    ### Job

    def getJob(self, jobId):
        """Returns a JSON object containing job information, according to the job id."""
        if self.conn:
@@ -113,6 +117,8 @@ class DbConnector(object):
                out.close()
                return job
    
    ### Users
    
    def userExists(self, username):
        """Checks if a user already exists. Returns a boolean."""
        if self.conn:
@@ -135,8 +141,10 @@ class DbConnector(object):
            self.cursor.execute("SELECT user_name FROM users WHERE rap_id = %s;", (rapId,))
            return self.cursor.fetchall()[0]["user_name"]
  
    ### Storage
  
    def storageBasePathIsValid(self, path):
        """Checks if the base path of a physical path is valid. If true, returns tbe base path, else returns 'False'."""
        """Checks if the base path of a physical path is valid. If true, returns the base path, else returns 'False'."""
        if self.conn:
            self.cursor.execute("""
                SELECT base_path 
@@ -163,10 +171,16 @@ class DbConnector(object):
            return self.cursor.fetchall()
        
    def getStorageId(self, basePath):
        """Returns the storage id for a given storage base path"""
        """Returns the storage id for a given storage base path, if any. Otherwise it returns 'False'"""
        if self.conn:
            self.cursor.execute("SELECT storage_id FROM storage WHERE base_path = %s;", (basePath,))
            return self.cursor.fetchall()[0]["storage_id"]
            result = self.cursor.fetchall()
            if result:
                return result[0]["storage_id"]
            else:
                return False

    ### Location

    def getLocationId(self, destStorageId):
        """Returns the location id according to the storage id of the destination"""
@@ -179,6 +193,8 @@ class DbConnector(object):
    Setters
    """

    ### Job

    def insertJob(self, jobObj):
        """Inserts/updates a job object."""
        if self.conn:
@@ -239,6 +255,8 @@ class DbConnector(object):
                 jobId,))
            self.conn.commit()
            
    ### Node

    def insertNode(self, node):
        """Inserts a VOSpace node."""
        if self.conn:
@@ -350,3 +368,51 @@ class DbConnector(object):
                """,
                (value, nodeVOSPath,))
            self.conn.commit()
            
    ### Storage

    def insertStorage(self, storageType, basePath, hostname):
        if self.conn:
            if not self.getStorageId(basePath):
                self.cursor.execute("""
                    INSERT INTO storage(storage_type, 
                                        base_path, 
                                        hostname)
                    VALUES (%s, %s, %s)
                    RETURNING storage_id;
                    """,
                    (storageType,
                     basePath,
                     hostname,))

                storageSrcId = self.cursor.fetchall()[0]["storage_id"]
                
                if storageType == "cold" or storageType == "hot":
                    self.cursor.execute("""
                        SELECT storage_id
                        FROM storage
                        WHERE storage_type = 'local'
                        AND base_path = '/home'
                        AND hostname = 'localhost';
                        """)
                    storageDestId = self.cursor.fetchall()[0]["storage_id"]
                    locationType = "async"                                       
                else:
                    storageDestId = storageSrcId
                    locationType = "portal"
                                    
                self.cursor.execute("""
                    INSERT INTO location(location_type,
                                         storage_src_id,
                                         storage_dest_id)
                    VALUES (%s, %s, %s);
                    """,
                    (locationType,
                     storageSrcId,
                     storageDestId,))
                
                self.conn.commit()
                return True
            else:
                return False
                
 No newline at end of file