This is a implementation of dim, which is built in python. But this time in go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

75 lines
2.1 KiB

package main
import (
"encoding/json"
"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
}
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 == "" {
res.AddMessage(LevelError, "empty name was provided")
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
}