// GetAllContainers returns a list of all containers func GetAllContainers(dbConn *sql.DB) ([]types.Container, error) { var rows *sql.Rows var err error queryStr := fmt.Sprintf("select c.id, c.name, c.clusterid, c.role, c.image, to_char(c.createdt, 'MM-DD-YYYY HH24:MI:SS'), p.id, p.name from project p, container c where c.projectid = p.id order by c.name") logit.Info.Println("admindb:GetAllContainers:" + queryStr) rows, err = dbConn.Query(queryStr) if err != nil { return nil, err } defer rows.Close() var clustername string containers := make([]types.Container, 0) for rows.Next() { container := types.Container{} if err = rows.Scan(&container.ID, &container.Name, &container.ClusterID, &container.Role, &container.Image, &container.CreateDate, &container.ProjectID, &container.ProjectName); err != nil { return nil, err } logit.Info.Println("cluster id is [" + container.ClusterID + "]") if container.ClusterID != "-1" { clustername, err = GetClusterName(dbConn, container.ClusterID) if err != nil { logit.Info.Println("admindb:GetAllContainers:error " + err.Error()) return nil, err } container.ClusterName = clustername } containers = append(containers, container) } if err = rows.Err(); err != nil { return nil, err } return containers, nil }
// GetContainerPgpool find the pgpool container in a cluster func GetContainerPgpool(dbConn *sql.DB, clusterid string) (types.Container, error) { //logit.Info.Println("admindb:GetContainerMaster:called") container := types.Container{} queryStr := fmt.Sprintf("select c.id, c.name, c.clusterid, c.role, c.image, to_char(c.createdt, 'MM-DD-YYYY HH24:MI:SS'), p.id, p.name from project p, container c where c.role = 'pgpool' and c.clusterid = %s and c.projectid = p.id", clusterid) logit.Info.Println("admindb:GetContainerPgpool:" + queryStr) err := dbConn.QueryRow(queryStr).Scan(&container.ID, &container.Name, &container.ClusterID, &container.Role, &container.Image, &container.CreateDate, &container.ProjectID, &container.ProjectName) switch { case err == sql.ErrNoRows: logit.Info.Println("admindb:GetContainerPgpool: no pgpool container with that clusterid " + clusterid) return container, err case err != nil: return container, err } if container.ClusterID != "-1" { var clustername string clustername, err = GetClusterName(dbConn, container.ClusterID) if err != nil { logit.Info.Println("admindb:GetContainerPgPool:error " + err.Error()) return container, err } container.ClusterName = clustername } return container, nil }
// GetAllContainersNotInCluster is used to fetch all nodes that are eligible to be added into a cluster func GetAllContainersNotInCluster(dbConn *sql.DB) ([]types.Container, error) { var rows *sql.Rows var err error queryStr := fmt.Sprintf("select c.id, c.name, c.clusterid, c.role, c.image, to_char(c.createdt, 'MM-DD-YYYY HH24:MI:SS'), p.id, p.name, l.name from project p, container c left join cluster l on c.clusterid = l.id where c.role != 'standalone' and c.clusterid = -1 and c.projectid = p.id order by c.name") logit.Info.Println("admindb:GetAllContainersNotInCluster:" + queryStr) rows, err = dbConn.Query(queryStr) if err != nil { return nil, err } defer rows.Close() containers := make([]types.Container, 0) for rows.Next() { container := types.Container{} if err = rows.Scan(&container.ID, &container.Name, &container.ClusterID, &container.Role, &container.Image, &container.CreateDate, &container.ProjectID, &container.ProjectName, &container.ClusterName); err != nil { return nil, err } container.ClusterName = container.ClusterID containers = append(containers, container) } if err = rows.Err(); err != nil { return nil, err } return containers, nil }