// AddContainerUser inserts a new database user for a given container and returns the new ID
func AddContainerUser(dbConn *sql.DB, s types.ContainerUser) (int, error) {

	//logit.Info.Println("AddContainerUser called")

	//encrypt the password...passwords at rest are encrypted
	encrypted, err := sec.EncryptPassword(s.Passwd)

	queryStr := fmt.Sprintf("insert into containeruser ( containername, usename, passwd, updatedt) values ( '%s', '%s', '%s',  now()) returning id",
		s.Containername,
		s.Rolname,
		encrypted)

	logit.Info.Println("AddContainerUser:" + queryStr)
	var theID int
	err = dbConn.QueryRow(queryStr).Scan(
		&theID)
	if err != nil {
		logit.Error.Println(err.Error())
		return theID, err
	}

	switch {
	case err != nil:
		logit.Error.Println(err.Error())
		return theID, err
	default:
	}

	return theID, nil
}
// ProxyUpdate updates a proxy node definition
func ProxyUpdate(w rest.ResponseWriter, r *rest.Request) {
	dbConn, err := util.GetConnection(CLUSTERADMIN_DB)
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), 400)
		return

	}
	defer dbConn.Close()

	req := ProxyRequest{}
	err = r.DecodeJsonPayload(&req)
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	//logit.Info.Println("ID=" + req.ID)
	//logit.Info.Println("Port" + req.Port)
	//logit.Info.Println("Host" + req.Host)
	//logit.Info.Println("Database" + req.Database)
	//logit.Info.Println("Usename" + req.Usename)
	//logit.Info.Println("Passwd" + req.Passwd)
	//encrypt the password...passwords at rest are encrypted
	var encrypted string
	encrypted, err = sec.EncryptPassword(req.Passwd)
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	queryStr := fmt.Sprintf("update proxy set ( port, host, usename, passwd, databasename, updatedt) = ( '%s', '%s', '%s', '%s', '%s', now()) where id = %s returning id",
		req.Port, req.Host, req.Usename, encrypted, req.Database, req.ID)

	logit.Info.Println("UpdateProxy:" + queryStr)
	var proxyid int
	err = dbConn.QueryRow(queryStr).Scan(&proxyid)
	switch {
	case err != nil:
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	default:
		logit.Info.Println("UpdateProxy: update " + strconv.Itoa(proxyid))
	}

	w.WriteHeader(http.StatusOK)
	status := types.SimpleStatus{}
	status.Status = "OK"
	w.WriteJson(&status)
}
// UpdateContainerUser updates a given container database user
func UpdateContainerUser(dbConn *sql.DB, user types.ContainerUser) error {
	logit.Info.Println("admindb:UpdateContainerUser encrypting password of " + user.Passwd)
	encrypted, err := sec.EncryptPassword(user.Passwd)
	queryStr := fmt.Sprintf("update containeruser set ( passwd, updatedt) = ('%s', now()) where usename = '%s' returning id", encrypted, user.Rolname)

	logit.Info.Println("[" + queryStr + "]")
	var userid int
	err = dbConn.QueryRow(queryStr).Scan(&userid)
	switch {
	case err != nil:
		return err
	default:
		logit.Info.Println("admindb:UpdateContainerUser:updated " + user.ID)
	}
	return nil

}
func insertProxy(request *ProxyRequest) error {

	/**
	//create user in the admin db
	dbuser := types.ContainerUser{}
	dbuser.Containername = request.ContainerName
	dbuser.Passwd = request.Passwd
	dbuser.Rolname = request.Usename
	*/

	dbConn, err := util.GetConnection(CLUSTERADMIN_DB)
	if err != nil {
		logit.Error.Println(err.Error())
		return err

	}
	defer dbConn.Close()

	/*
		containerUserID, err = admindb.AddContainerUser(dbConn, dbuser)
		if err != nil {
			logit.Error.Println(err.Error())
			return err
		}

		logit.Info.Printf("insertProxy: new ID %d\n ", containerUserID)
	*/

	var container types.Container
	container, err = admindb.GetContainerByName(dbConn, request.ContainerName)
	if err != nil {
		logit.Error.Println(err.Error())
		return err
	}

	proxy := types.Proxy{}
	proxy.ContainerID = container.ID
	proxy.Host = request.Host
	proxy.Database = request.Database
	proxy.ProjectID = request.ProjectID
	proxy.Port = request.Port
	proxy.Usename = request.Usename

	//encrypt the password...passwords at rest are encrypted
	var encrypted string
	encrypted, err = sec.EncryptPassword(request.Passwd)
	if err != nil {
		logit.Error.Println(err.Error())
		return err
	}

	proxy.Passwd = encrypted

	queryStr := fmt.Sprintf("insert into proxy ( containerid, projectid, port, host, usename, passwd, databasename, updatedt) values ( %s, %s, '%s', '%s', '%s', '%s', '%s', now()) returning id",
		proxy.ContainerID, proxy.ProjectID, proxy.Port, proxy.Host, proxy.Usename, proxy.Passwd, proxy.Database)

	logit.Info.Println("insertProxy:" + queryStr)
	var proxyid int
	err = dbConn.QueryRow(queryStr).Scan(&proxyid)
	switch {
	case err != nil:
		logit.Info.Println("insertProxy:" + err.Error())
		return err
	default:
		logit.Info.Println("insertProxy: inserted returned is " + strconv.Itoa(proxyid))
	}

	return err
}