2021-05-07 10:17:05 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-05-07 21:23:49 +02:00
|
|
|
"encoding/json"
|
2021-05-07 10:17:05 +02:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"dim/query"
|
|
|
|
"dim/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Layer3DomainSetOptions struct {
|
|
|
|
Attributes types.FieldMap `json:"attributes"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func layer3DomainSetAttr(c *Context, req Request, res *Response) error {
|
|
|
|
name := ""
|
|
|
|
attrs := types.FieldMap{}
|
|
|
|
if err := req.ParseAtLeast(2, &name, &attrs); err != nil {
|
|
|
|
res.AddMessage(LevelError, "could not parse options: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if name == "" {
|
|
|
|
res.AddMessage(LevelError, "empty name was provided")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if attrs.Size() == 0 {
|
|
|
|
res.AddMessage(LevelError, "no key/value pairs provided to update")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
setClause, args, err := query.FieldMapToUpdate(attrs, layer3DomainListMap)
|
|
|
|
if err != nil {
|
|
|
|
res.AddMessage(LevelError, "could not encode requested attributes: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
queryStr := fmt.Sprintf("update layer3domains l set %s where name = $%d", setClause, len(args)+1)
|
|
|
|
args = append(args, name) // don't forget to add the where clause parameter
|
|
|
|
if _, err := c.tx.Exec(queryStr, args...); err != nil {
|
|
|
|
res.AddMessage(LevelError, "could not set attributes")
|
|
|
|
c.Logf(LevelError, "could not set attributes on layer3domain '%s': %s - query: `%s` - args: `%#v`", name, err, queryStr, args)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-07 21:23:49 +02:00
|
|
|
|
|
|
|
func layer3DomainGetAttr(c *Context, req Request, res *Response) error {
|
|
|
|
name := ""
|
|
|
|
if err := req.ParseAtLeast(1, &name); err != nil {
|
|
|
|
res.AddMessage(LevelError, "could not parse name: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if name == "" {
|
2021-05-09 21:22:20 +02:00
|
|
|
res.AddMessage(LevelError, "empty name was provided")
|
2021-05-07 21:23:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result := json.RawMessage{}
|
|
|
|
selClause := query.FieldsToJSON("l", map[string]string{
|
|
|
|
"name": "name",
|
|
|
|
"modified_by": "l.modified_by",
|
|
|
|
"modified_at": "l.modified_at",
|
|
|
|
"created_by": "l.created_by",
|
|
|
|
"created_at": "l.created_at",
|
|
|
|
})
|
|
|
|
queryStr := fmt.Sprintf(`select %s from layer3domains l where name = $1`, selClause)
|
|
|
|
err := c.tx.QueryRow(queryStr, name).Scan(&result)
|
|
|
|
if err != nil {
|
|
|
|
res.AddMessage(LevelError, "could not return result")
|
|
|
|
return fmt.Errorf("could not get layer3domain '%s': %s - query: %s", name, err, queryStr)
|
|
|
|
}
|
|
|
|
res.Result = result
|
|
|
|
return nil
|
|
|
|
}
|