// GetProxy returns a given proxy object by container name
func GetProxy(dbConn *sql.DB, containername string) (types.Proxy, error) {
	var rows *sql.Rows
	proxy := types.Proxy{}
	var err error

	queryStr := fmt.Sprintf("select p.usename , p.passwd, p.port, p.host, p.databasename from proxy p, container c where p.containerid = c.id and c.name = '%s'", containername)

	logit.Info.Println("GetProxy:" + queryStr)
	rows, err = dbConn.Query(queryStr)
	if err != nil {
		return proxy, err
	}
	defer rows.Close()
	for rows.Next() {
		if err = rows.Scan(&proxy.Usename, &proxy.Passwd,
			&proxy.Port, &proxy.Host, &proxy.Database); err != nil {
			return proxy, err
		}
	}

	proxy.ContainerName = containername

	if err = rows.Err(); err != nil {
		return proxy, err
	}
	var unencrypted string
	unencrypted, err = sec.DecryptPassword(proxy.Passwd)
	if err != nil {
		return proxy, err
	}
	proxy.Passwd = unencrypted
	return proxy, nil
}
// GetProxyByContainerID returns a proxy object by container ID
func GetProxyByContainerID(dbConn *sql.DB, containerID string) (types.Proxy, error) {
	var rows *sql.Rows
	proxy := types.Proxy{}
	var err error

	queryStr := fmt.Sprintf("select p.usename, p.passwd, p.projectid, p.id,  p.containerid, c.name , p.port, p.host, p.databasename from proxy p, container c where p.containerid = c.id and c.id = %s", containerID)

	logit.Info.Println("GetProxyByContainerID:" + queryStr)
	rows, err = dbConn.Query(queryStr)
	if err != nil {
		return proxy, err
	}
	defer rows.Close()
	for rows.Next() {
		if err = rows.Scan(
			&proxy.Usename,
			&proxy.Passwd,
			&proxy.ProjectID,
			&proxy.ID, &proxy.ContainerID,
			&proxy.ContainerName, &proxy.Port, &proxy.Host, &proxy.Database); err != nil {
			return proxy, err
		}
	}
	if err = rows.Err(); err != nil {
		return proxy, err
	}
	var unencrypted string
	unencrypted, err = sec.DecryptPassword(proxy.Passwd)
	if err != nil {
		return proxy, err
	}
	proxy.Passwd = unencrypted

	//see if the container is up
	var resp cpmcontainerapi.StatusResponse
	resp, err = cpmcontainerapi.StatusClient(proxy.ContainerName)
	proxy.ContainerStatus = resp.Status
	if err != nil {
		return proxy, err
	}

	//test the remote host connectivity
	var hoststatus string
	hoststatus, err = util.FastPing(proxy.Port, proxy.Host)
	if err != nil {
		return proxy, err
	}
	if hoststatus == "OFFLINE" {
		proxy.Status = hoststatus
		return proxy, err
	}

	//test the database port on the remote host
	proxy.Status, err = GetDatabaseStatus(proxy, containerID)
	if err != nil {
		return proxy, err
	}

	return proxy, nil
}
// GetContainerUser returns a container user for a given container and user name
func GetContainerUser(dbConn *sql.DB, containername string, usename string) (types.ContainerUser, error) {
	var user types.ContainerUser
	var err error
	queryStr := fmt.Sprintf("select id, passwd, to_char(updatedt, 'MM-DD-YYYY HH24:MI:SS') from containeruser where usename = '%s' and containername = '%s'", usename, containername)
	logit.Info.Println("admindb:GetContainerUser:"******"GetContainerUser got a row back")
	}

	user.Rolname = usename
	user.Containername = containername

	var unencrypted string
	unencrypted, err = sec.DecryptPassword(user.Passwd)
	if err != nil {
		return user, err
	}
	user.Passwd = unencrypted
	return user, nil
}