2023-01-04 05:36:56 +00:00
|
|
|
package upstream
|
2022-05-11 22:47:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"npm/internal/database"
|
|
|
|
"npm/internal/entity"
|
|
|
|
"npm/internal/errors"
|
|
|
|
"npm/internal/logger"
|
|
|
|
"npm/internal/model"
|
2023-02-24 07:19:07 +00:00
|
|
|
|
|
|
|
"github.com/rotisserie/eris"
|
2022-05-11 22:47:31 +00:00
|
|
|
)
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
// GetByID finds a Upstream by ID
|
2022-05-11 22:47:31 +00:00
|
|
|
func GetByID(id int) (Model, error) {
|
|
|
|
var m Model
|
|
|
|
err := m.LoadByID(id)
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
// create will create a Upstream from this model
|
|
|
|
func create(u *Model) (int, error) {
|
|
|
|
if u.ID != 0 {
|
2023-02-24 07:19:07 +00:00
|
|
|
return 0, eris.New("Cannot create upstream when model already has an ID")
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
u.Touch(true)
|
2022-05-11 22:47:31 +00:00
|
|
|
|
|
|
|
db := database.GetInstance()
|
|
|
|
// nolint: gosec
|
|
|
|
result, err := db.NamedExec(`INSERT INTO `+fmt.Sprintf("`%s`", tableName)+` (
|
|
|
|
created_on,
|
|
|
|
modified_on,
|
|
|
|
user_id,
|
|
|
|
name,
|
2023-01-04 05:36:56 +00:00
|
|
|
nginx_template_id,
|
|
|
|
ip_hash,
|
|
|
|
ntlm,
|
|
|
|
keepalive,
|
|
|
|
keepalive_requests,
|
|
|
|
keepalive_time,
|
|
|
|
keepalive_timeout,
|
|
|
|
advanced_config,
|
|
|
|
status,
|
|
|
|
error_message,
|
2022-05-11 22:47:31 +00:00
|
|
|
is_deleted
|
|
|
|
) VALUES (
|
|
|
|
:created_on,
|
|
|
|
:modified_on,
|
|
|
|
:user_id,
|
|
|
|
:name,
|
2023-01-04 05:36:56 +00:00
|
|
|
:nginx_template_id,
|
|
|
|
:ip_hash,
|
|
|
|
:ntlm,
|
|
|
|
:keepalive,
|
|
|
|
:keepalive_requests,
|
|
|
|
:keepalive_time,
|
|
|
|
:keepalive_timeout,
|
|
|
|
:advanced_config,
|
|
|
|
:status,
|
|
|
|
:error_message,
|
2022-05-11 22:47:31 +00:00
|
|
|
:is_deleted
|
2023-01-04 05:36:56 +00:00
|
|
|
)`, u)
|
2022-05-11 22:47:31 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
last, lastErr := result.LastInsertId()
|
|
|
|
if lastErr != nil {
|
|
|
|
return 0, lastErr
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
logger.Debug("Created Upstream: %+v", u)
|
|
|
|
|
2022-05-11 22:47:31 +00:00
|
|
|
return int(last), nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
// update will Update a Upstream from this model
|
|
|
|
func update(u *Model) error {
|
|
|
|
if u.ID == 0 {
|
2023-02-24 07:19:07 +00:00
|
|
|
return eris.New("Cannot update upstream when model doesn't have an ID")
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
u.Touch(false)
|
2022-05-11 22:47:31 +00:00
|
|
|
|
|
|
|
db := database.GetInstance()
|
|
|
|
// nolint: gosec
|
|
|
|
_, err := db.NamedExec(`UPDATE `+fmt.Sprintf("`%s`", tableName)+` SET
|
|
|
|
created_on = :created_on,
|
|
|
|
modified_on = :modified_on,
|
|
|
|
user_id = :user_id,
|
|
|
|
name = :name,
|
2023-01-04 05:36:56 +00:00
|
|
|
nginx_template_id = :nginx_template_id,
|
|
|
|
ip_hash = :ip_hash,
|
|
|
|
ntlm = :ntlm,
|
|
|
|
keepalive = :keepalive,
|
|
|
|
keepalive_requests = :keepalive_requests,
|
|
|
|
keepalive_time = :keepalive_time,
|
|
|
|
advanced_config = :advanced_config,
|
|
|
|
status = :status,
|
|
|
|
error_message = :error_message,
|
2022-05-11 22:47:31 +00:00
|
|
|
is_deleted = :is_deleted
|
2023-01-04 05:36:56 +00:00
|
|
|
WHERE id = :id`, u)
|
|
|
|
|
|
|
|
logger.Debug("Updated Upstream: %+v", u)
|
2022-05-11 22:47:31 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
// List will return a list of Upstreams
|
|
|
|
func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ListResponse, error) {
|
2022-05-11 22:47:31 +00:00
|
|
|
var result ListResponse
|
|
|
|
var exampleModel Model
|
|
|
|
|
|
|
|
defaultSort := model.Sort{
|
2023-01-04 05:36:56 +00:00
|
|
|
Field: "name",
|
2022-05-11 22:47:31 +00:00
|
|
|
Direction: "ASC",
|
|
|
|
}
|
|
|
|
|
|
|
|
db := database.GetInstance()
|
|
|
|
if db == nil {
|
|
|
|
return result, errors.ErrDatabaseUnavailable
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get count of items in this search
|
|
|
|
query, params := entity.ListQueryBuilder(exampleModel, tableName, &pageInfo, defaultSort, filters, getFilterMapFunctions(), true)
|
|
|
|
countRow := db.QueryRowx(query, params...)
|
|
|
|
var totalRows int
|
|
|
|
queryErr := countRow.Scan(&totalRows)
|
|
|
|
if queryErr != nil && queryErr != sql.ErrNoRows {
|
|
|
|
logger.Debug("%s -- %+v", query, params)
|
|
|
|
return result, queryErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get rows
|
2023-01-04 05:36:56 +00:00
|
|
|
items := make([]Model, 0)
|
2022-05-11 22:47:31 +00:00
|
|
|
query, params = entity.ListQueryBuilder(exampleModel, tableName, &pageInfo, defaultSort, filters, getFilterMapFunctions(), false)
|
|
|
|
err := db.Select(&items, query, params...)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug("%s -- %+v", query, params)
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
// Expand to get servers, at a minimum
|
|
|
|
for idx := range items {
|
|
|
|
// nolint: errcheck, gosec
|
|
|
|
items[idx].Expand(expand)
|
|
|
|
}
|
|
|
|
|
2022-05-11 22:47:31 +00:00
|
|
|
result = ListResponse{
|
|
|
|
Items: items,
|
|
|
|
Total: totalRows,
|
|
|
|
Limit: pageInfo.Limit,
|
|
|
|
Offset: pageInfo.Offset,
|
|
|
|
Sort: pageInfo.Sort,
|
|
|
|
Filter: filters,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|