// 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
}
// 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
}
Exemplo n.º 3
0
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
}